Prompt cache hits in an agent session, how lifetime, write premium and pauses set the bill
An agent resends its whole history on every call, so prompt caching can cut the input bill sharply, but only while the cache is alive. Step through 24 calls of a coding-agent session, change the cache lifetime and prices, and see which calls hit, which pay to re-write the prefix and what the session costs.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Each row is one model call in a coding-agent session. gap_s is the time since the previous call started, prefix_tok is the previous call's whole prompt (the part that can come from the cache), new_tok is what was added since (tool results and the last turn) and out_tok is the model's output. Most gaps are 9 to 35 seconds, the rhythm of a tool loop. Three are long: before call 9 a person spends 7 minutes reviewing a diff, before call 17 they take 25 minutes, and before call 21 they come back after more than an hour.
The orange columns decide for each call whether the cache is still alive, how many tokens are read from it and how many are written to it, and what the call costs with and without caching. The chart plots every gap against the cache lifetime: points above the dashed line are misses. The bars show the money each call saves; a negative bar is a call that paid the write premium and got nothing back.
The algorithm
Prompt caching stores the processed prefix of a prompt so that the next request with the same prefix does not pay to process it again. On the Claude API the rules are documented precisely, and the simulation uses them.
1. Mark the breakpoint. Everything up to a cache_control breakpoint can be cached. With a moving breakpoint at the end of the conversation, each call reads the previous prompt and writes the newly added part.
2. Check the lifetime. The default cache lifetime is 5 minutes and it is refreshed at no cost every time the entry is used; the lifetime is measured from the start of the request. A 1-hour lifetime is available at a higher write price. A call hits when its gap is no longer than the lifetime: h = [gap ≤ TTL].
3. Price reads and writes. A 5-minute cache write costs 1.25 times the base input price, a 1-hour write 2 times, and a cache read 0.1 times (some newer models charge less for reads, which the read-price slider covers). On a hit the call reads its prefix and writes only the new tokens; on a miss it writes the whole prompt again:
cost = (read tokens × ρ + written tokens × ω) × input price + output tokens × output price
4. Compare with no caching, where every input token is billed at the base price.
5. Break-even. A write costs ω − 1 extra per token and each read saves 1 − ρ, so a write pays for itself after (ω − 1) / (1 − ρ) reads: 0.28 for the 5-minute cache and 1.11 for the 1-hour cache at the standard read price. In practice that means one read for the 5-minute cache and two for the 1-hour cache.
Worked example. At $3 per million input tokens and $15 per million output tokens, call 2 reads 6,200 cached tokens at $0.30 per million and writes 1,850 new ones at $3.75 per million; with its 220 output tokens it costs $0.0121 instead of $0.0275. Call 9 arrives after 7 minutes, too late for the 5-minute cache, so it writes all 20,660 tokens at the write price and costs $0.0842 instead of $0.0687. Over the session 20 of the 23 later calls hit, 78.1% of all input tokens come from the cache, and the bill falls from $2.16 to $0.83, a saving of 61.7%. The three misses spend $0.38 re-writing prefixes that had expired.
Things to try
- Switch the lifetime to 60 minutes. Calls 9 and 17 now hit (22 of 23) and 86.4% of input is read from the cache, yet the session costs $0.84, slightly more than the $0.83 of the 5-minute cache, because every write now costs twice the input price. A longer lifetime pays only when the pauses are long and the writes are few.
- Lower the read price to 0.05. With the 5-minute cache the session costs $0.75 and the saving rises to 65.4%.
- Edit call 17's gap to 240 seconds and watch its bar turn positive: the same work, done before the cache expires, is cheaper.
- Raise the input price to $15 and the output price to $75: the saving stays the same share of a larger bill, because input dominates in an agent loop.
- Paste the usage fields of your own session: gap, cached prefix, new input and output per call.
How it works in real systems
Cache hits need an exact prefix match. Anything that changes early in the prompt, such as a timestamp in the system prompt, reordered tools or an edited earlier message, breaks the match for everything after it. Keep stable content first (tools, system prompt, documents), put the conversation after it, and never rewrite history in place; summarise into a new message instead. Prompts shorter than the model's minimum cacheable length (512 to 4,096 tokens depending on the Claude model) are not cached at all, and no error is returned. The API allows up to four breakpoints, which lets a long system prompt and the growing conversation be cached separately.
Measure the hit rate from the usage block, not from guesses: Claude returns cache_read_input_tokens, cache_creation_input_tokens and input_tokens, and their sum is the total input. Langfuse records such usage types per generation and prices them from a model definition, and LangSmith does the same for its traces. OpenAI caches automatically, with its own minimum length, lifetime and pricing, so the same accounting applies with different numbers.
Caching is one of several levers on agent cost. The agent loop token cost simulation shows why input grows with every step, and context engineering covers trimming and summarising history. See also prompt caching, the KV cache that makes it possible on the server, and cost and latency budgets.