Topic 1 of 5 · 4 min read
How prompt caching reuses a prefix
Goal: Explain when a request hits the cache and compute the cost of a cache write versus a cache read from a price table.
A prompt cache stores the computed state of the start of your prompt, so a later request that begins with exactly the same tokens skips that work and costs less. The match is on the prefix only: one changed token ends the match at that point.
Check yourself · 1 of 3
A request starts with the same 5,000 tokens as a cached request, then adds a new question. What happens to the 5,000 shared tokens?
Why it matters
Chat apps, coding agents and document assistants send the same long block again and again: tool definitions, a system prompt, a policy manual, a codebase summary. Without caching you pay full input price and wait for full processing on every call. With it, repeated prefix tokens are billed at a fraction and the first token arrives sooner.
How it works
- Exact prefix match. The provider hashes the prompt from the start. Tools, system prompt and messages are read in that order. Everything up to the first difference can be reused. Everything after it is processed fresh.
- Write, then read. The first request that stores a prefix is a cache write. Later requests that match are cache reads. On Claude a 5-minute write costs 1.25 times the base input price, a 1-hour write costs 2 times, and a read costs 0.1 times (see the pricing reference). OpenAI caches automatically on supported models and reports cached tokens in usage. Gemini offers implicit caching and explicit caches you create and pay storage for.
- Time to live (TTL). A cache entry expires if unused. On Claude the default is 5 minutes, refreshed each time it is read, with a 1-hour option at a higher write price. Check each provider's current figures in its docs.
- Minimum length. Short prompts are not cached. The minimum token count differs by provider and model, so read the docs for the model you use.
Worked arithmetic
Take a 20,000-token prefix on a model priced at 3 dollars per million input tokens, with a 3.75 write price and a 0.30 read price:
- No caching: 20,000 x 3 / 1,000,000 = 0.060 dollars per call.
- First call, cache write: 20,000 x 3.75 / 1,000,000 = 0.075 dollars.
- Every later call within the TTL, cache read: 20,000 x 0.30 / 1,000,000 = 0.006 dollars.
The write costs 0.015 more than uncached. One read saves 0.054. So caching pays back after a single reuse.
Before and after
Weak: "Caching saves 90 percent, so I will turn it on everywhere." Better: "This route reuses a 20,000-token prefix about 40 times an hour, well inside the TTL, so reads dominate. That other route is called once a day, so each call would pay the write price and never read."
What goes wrong
- Expecting a hit on a prompt that differs by one token near the start.
- Caching a prompt that is used less often than the TTL, so you pay writes and never get reads.
- Assuming the numbers are the same on every provider and model.
Examples
Write versus read cost
Price table for one model, per million input tokens: base 3.00, 5-minute cache write 3.75, cache read 0.30.
| Request | Prefix tokens | Billed as | Cost |
|---|---|---|---|
| 1 | 20,000 | cache write | 0.075 |
| 2 | 20,000 | cache read | 0.006 |
| 3 | 20,000 | cache read | 0.006 |
Three calls cost 0.087 dollars against 0.180 uncached, a saving of about 52 percent. At 20 calls the saving passes 85 percent.
Same idea, three providers
Claude: you mark breakpoints and read cache_read_input_tokens. OpenAI: caching is automatic on supported models and usage reports cached tokens. Gemini: implicit caching is on by default, and you can create an explicit cache for a large document and pay for the time it is stored. The prefix rule is the same in all three.
Key points
- A cache hit needs the new request to start with exactly the same tokens as a stored prefix.
- A cache write costs more than normal input, and a cache read costs far less, so savings come from reuse.
- Entries expire after a time to live, so a route called less often than that pays writes without reads.
- Minimum cacheable length, TTL and prices differ by provider and model, so read the current docs and price table.
Common mistakes
- Thinking the cache matches on meaning. It matches on exact tokens, so a reworded instruction is a miss.
- Turning caching on for a rarely called route, where every call is a write.
- Counting the first call's cost as savings. The first call is the most expensive one.
- Copying prices and limits from a blog post instead of the current price table.
Check yourself · 2 of 3
A 20,000-token prefix costs 3.00 per million tokens uncached, 3.75 as a cache write and 0.30 as a cache read. What does one cache read of that prefix cost?
Check yourself · 3 of 3
An internal tool sends one long-prefix request about once a day. The cache lifetime is minutes. Is caching likely to save money?
Practice items, not counted towards your mastery.
Start here
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
Breakpoints, cache lifetimes, what invalidates the cache and the usage fields that report reads and writes.
- Prompt caching (OpenAI API docs) (opens in a new tab)DocsFree
How automatic prefix caching works on OpenAI models, the cache key and the cached-token usage field.
- Context caching (Gemini API docs) (opens in a new tab)DocsFree
Implicit and explicit caching on Gemini, with storage-time and token-count details.
More for this topic
- Claude API pricing (opens in a new tab)DocsFree
The price table with base input, cache write and cache read rates per model, for cost arithmetic.