deep · 6 min
Prompt caching
Reusing the provider's work on a repeated prompt prefix. Cheaper and faster.
updated 29 Sept 2026
Here are two bills for the same 100 requests to the same model: $4.00 and $0.45. Same prompts, same answers. What changed?
The requests come from a support bot. Every one starts with the same 20,000 tokens: tool definitions, a long system prompt, a product manual. Then the user's question, maybe 50 tokens. On Claude Sonnet 5 (as of 2026-09, $2 per million input tokens) that 20,000-token start costs $0.04 every time we send it. A hundred times, $4.
The second bill used prompt caching. Let's see what it's doing.
What gets kept
Before the model writes anything, it reads the whole prompt (the prefill stage, see prefill and decode). Along the way it works out keys and values for every token, the KV cache. Normally that work gets thrown away when the request ends. So the next request, with the same 20,000 tokens up front, computes all of it again, and we pay for all of it again.
Prompt caching keeps it. The provider stores the keys and values for that shared start. When a later request begins with exactly the same tokens, the model loads them instead of recomputing, and only processes what's new: the 50-token question.
So the second bill looks like this. Writing the 20,000 tokens into the cache the first time costs a bit extra, $0.05. Every read after that is a tenth of the normal price, $0.004. One write and 99 reads come to about $0.45.
It's easy to get the wrong idea here. What's stored is the model's working state for the prompt. Not the text, and not the answer. The model still writes a fresh reply every time, and you still pay for every output token. Send the same prompt twice and you can still get two different answers. (Reusing whole answers for similar questions is a different idea, semantic caching.)
Only an exact start counts
The cache is matched from the first token forward. A request can reuse it up to the first token that differs, and nothing after that.
So order matters a lot. On Claude, the request is read as one sequence: tools, then system prompt, then messages. On OpenAI it's the whole rendered context, including settings like the output schema or reasoning effort. Change something near the front and everything after it misses.
The things that quietly break it are small:
- A timestamp at the top of the system prompt, especially one precise to the second. Every request now starts differently.
- JSON that doesn't serialize the same way twice. Lots of libraries don't promise key order, so the "same" tool schema comes out different.
- Adding or removing tools mid-conversation. Tools sit at the very front, so this throws away everything.
- Editing earlier turns, including summarizing them.
The fix is the same each time. What never changes goes first, what changes goes last. Treat the conversation as append-only. If something needs updating, like the current time, put it as a note in the next message instead of editing the system prompt.
How the two big providers differ
The idea is the same; the knobs aren't. As of 2026-09:
| Claude | OpenAI (GPT-5.6 and later) | |
|---|---|---|
| Turned on by | cache_control, once at the top or on up to 4 blocks | On by default |
| Read price | 0.1x input on most models | 0.1x input |
| Write price | 1.25x for 5 minutes, 2x for 1 hour | 1.25x |
| Lifetime | 5 minutes or 1 hour, refreshed on each hit | At least 30 minutes after last use |
| Minimum prefix | 512 to 4,096 tokens, by model | 1,024 tokens |
| Hits count toward rate limits? | No | Yes |
On Claude with the 5-minute cache, one write plus one read already beats sending the prefix twice uncached. With the 1-hour cache it takes two reads. And on Claude, hits not counting toward the rate limit means a high hit rate lets us push much more input per minute (more in rate limits).
Does it make answers faster?
Partly. Reading cached state is faster than computing it, so the wait for the first token shrinks. When Anthropic launched caching in 2024, it measured time to first token on three workloads:
The longer the cached part, the bigger the win. But caching only skips the reading. Writing the answer still happens token by token, exactly as before. A long cached prompt with a short answer gets much faster. A short prompt with a long answer barely changes.
Why agents care most
An agent in an agent loop resends its whole growing context on every step, and each step's output is usually a short tool call. One agent team measured about 100 input tokens for every output token. Nearly all of that input is a start the model has already seen. That's why teams building agents treat cache hit rate as a top metric. The Claude Code team alerts on it and treats a drop like an outage.
Their design follows straight from the exact-start rule. The prompt goes from most shared to least: fixed instructions and tools, then per-project files, then per-session state, then the conversation. "Plan mode" is a tool the model calls, not a swap of the tool list. Tools that aren't needed yet are sent as small stubs instead of being added later. Compaction runs with the same system prompt and tools as the main conversation, so the summarize call reuses the cache. A separate "summarize this" call with its own prompt would share nothing and pay full price for the whole history.
Small traps
It fails quietly. On Claude, a prefix shorter than the model's minimum
just isn't cached, with no error. So check the usage fields on every
response (cache_read_input_tokens and cache_creation_input_tokens on
Claude, cached_tokens on OpenAI). Both zero means nothing was cached.
Parallel requests don't help each other at first. On Claude a cache entry only exists once the first response starts, so ten requests fired at once on a cold cache all pay full price. Send one, wait for it to begin, then send the rest.
The 5-minute clock starts at the request. A reply that takes 4 minutes to stream leaves about a minute for the follow-up.
Hit rates also depend on the provider's design. In late 2025 one developer sent the same request twice in a row and got a hit about half the time on OpenAI's automatic caching, and every time with Anthropic's explicit breakpoints. OpenAI has changed its routing since, so measure your own.
And caches are private between organizations, but inside our app all our
customers share one. On OpenAI, a customer could send guessed prompts and
watch for hits to learn what someone else sent. A separate
prompt_cache_key per customer helps prevent that.
The cheaper model that costs more
Back to bills. Say we're 100,000 tokens into a conversation on Opus and the user asks something easy. Switch to the cheaper Haiku for that one question? The Claude Code team found it costs more than letting Opus answer, because the cache belongs to one model. Haiku has to read all 100,000 tokens from scratch. The same goes for a fallback that sends a long request somewhere else. Their answer was to hand the easy job to a subagent with a short summary. (The prices in this article will move. OpenAI's cache writes used to be free, for one. Check the docs before you design around a number.)
Further reading
- Prompt caching, Anthropic docs. Everything about breakpoints, lifetimes, minimums and what breaks the cache on Claude. The "what invalidates" table is worth bookmarking.
- Prompt caching, OpenAI docs. On-by-default caching, routing, and a neat worked example of when padding a short prefix pays off.
- Prompt caching with Claude, Anthropic, 2024. The launch post, with the latency and cost table.
- Lessons from building Claude Code: Prompt caching is everything, Thariq Shihipar (Anthropic), 2026. A real agent designed around the cache. Read this one if you build agents.
- Context Engineering for AI Agents: Lessons from Building Manus, Yichao Ji (Manus), 2025. The 100-to-1 ratio and the everyday mistakes that wreck hit rates.
- Prompt caching: 10x cheaper LLM tokens, but how?, Sam Rose (ngrok), 2025. A visual walk from attention to the cached keys and values, if you want to see what's actually stored.