AI summary
With Claude prompt cachingA Claude API feature that keeps the part of your request that never changes stored for a few minutes. The next request that starts with the same text pays much less for that part., the first request pays extra and later ones save. A quiet site may never get one. When to keep it, use a 1-hour cache or turn it off.
- On a quiet site, the cache pays off if about one visitor in three asks a follow-up question.
- With a visit or more per hour, use the 1-hour cache. With fewer visits, keep the 5-minute cache if people ask follow-ups and turn caching off if they don't.
Everyone says prompt cachingA Claude API feature that keeps the part of your request that never changes stored for a few minutes. The next request that starts with the same text pays much less for that part. saves money. On a busy app, sure. The chat on my site calls the Claude API and doesn’t get many visitors, and that made me wonder whether the cache was saving me anything or costing me extra.
The first request pays, the later ones save
Every question to the chat sends the same long context to the model: the instructions and the text it answers from. Prompt cachingA Claude API feature that keeps the part of your request that never changes stored for a few minutes. The next request that starts with the same text pays much less for that part. lets the API keep that context for a few minutes, so the next request doesn’t pay full price for it.
The first request writes your context to the cache and costs 25% more than a normal request. Requests after it read from the cache and pay a tenth of the price or less. The default cache lives for five minutes, and every read resets that timer.
So caching is a bet that someone else will show up within five minutes. A busy site always wins that bet. A quiet site can lose it over and over.
A follow-up question saves the bet
This is the part I didn’t expect. You don’t need more visitors for the cache to pay. You need visitors who ask a second question, because it arrives inside those five minutes.
For example, say a normal request costs 100. Writing the same request to the cache costs 125. If nobody comes back, you paid 25 for nothing. If a second question arrives within five minutes, it reads from the cache for 10, so the two questions cost 135 instead of 200.
One follow-up saves 90, more than three times the 25 you paid extra. That’s why about one visitor in three asking a follow-up is enough. The cache comes out ahead even when every visit starts with an empty cache.
How to see which case you’re in
Every response tells you what happened to the cache. This small example asks two questions against the same context. For each one, it prints how many tokenThe unit a model reads and bills text in. A token is a short piece of text, often part of a word.s were written to the cache and how many were read from it:
import { readFile } from "node:fs/promises";
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
// Needs at least 512 tokens on Haiku 5.5, or it won't be cached.
const context = await readFile("context.md", "utf8");
async function ask(question: string) {
const message = await client.messages.create({
model: "claude-haiku-5-5",
max_tokens: 1024,
system: [
{ type: "text", text: context, cache_control: { type: "ephemeral" } },
],
messages: [{ role: "user", content: question }],
});
const { cache_creation_input_tokens, cache_read_input_tokens } = message.usage;
console.log({ written: cache_creation_input_tokens, read: cache_read_input_tokens });
}
await ask("What does this document cover?"); // expected: written > 0, read 0
await ask("Who is it written for?"); // expected: written 0, read > 0The comments show what you should see, not output I copied from a run: the first question writes the cache and the second one reads it. The exact counts depend on how long your context is.
If your real traffic keeps looking like that first question, with lots of writes and few reads, you’re losing the bet.
My chat is on the 5-minute cache and logs these two counts for every question. In my tests on the live chat, later requests came back with a read count above zero, so the cache works. What I don’t know yet is how many real visitors ask that second question. Once the chat has real traffic, the logs will tell me.
The rules below come from a simple calculation, not from measurements. It assumes visitors arrive at random and counts only money. Real traffic goes quiet at night and comes in bursts, and a read from the cache may also answer faster, which I haven’t measured. Treat the rules as a starting point.
With my chat’s context on Claude HaikuThe smallest and cheapest family of Claude models, the one my chat runs on., even a visit that starts with an empty cache costs about a cent. Cache writes nobody reads hurt much more on a pricier model.
5-minute, 1-hour or no cache
- Fewer than one visit an hour, and people ask one question: turn caching off, because nobody earns back the extra you paid to write it.
- Fewer than one visit an hour, but people ask follow-ups: keep the default 5-minute cache.
- A visit or more per hour: use the 1-hour cache. Its write costs double, but it survives most gaps between visitors, so you pay for that write far less often.
cache_control is a field you add to one part of the prompt. In the example above, that part is the system text. The field tells the API to cache everything from the start of the prompt to the end of that part. The 1-hour cache is one extra field inside cache_control, as Anthropic’s docs show:
cache_control: { type: "ephemeral", ttl: "1h" }You might be tempted to ping the API every few minutes to keep the 5-minute cache alive. By my math, on HaikuThe smallest and cheapest family of Claude models, the one my chat runs on. the 1-hour cache is cheaper than that at any traffic level.
