LLM inference · agents · cost

The Cache Is the Bill

In a long agent session, most of what you pay for is the model re-reading what it has already seen. Prompt caching decides whether that costs cents or dollars. Here is how the cache works inside a frontier model, the rules the API plays by, and a simulation of a real working day on Claude Opus 5.5.

Every request resends everything read from cachewritten to cachegenerated

An agent session, one request at a time. Each request sends the whole conversation again. What the previous request already sent comes back from the cache; only the newest part is written; the reply is generated fresh and joins the prompt next time.
Contents
  1. The bill you don't see
  2. What is actually cached
  3. The rules the API plays by
  4. Anatomy of one request
  5. Every request re-reads everything
  6. When the cache goes cold
  7. Ping or pay
  8. Five minutes or an hour?
  9. Continue, or start fresh?
  10. Forks and parallel agents
  11. A day of agent work
  12. Field guide
  13. What this article can't tell you

01The bill you don't see

Every time a coding agent takes a step (reads a file, runs a test, answers you) it sends the model the entire conversation so far. Not a summary: all of it. The system prompt, every tool definition, every file it has read, every reply it has written. A session that has been running for an hour can easily hold half a million tokens, and the next tool call sends all half million again.

At the normal input price that would be ruinous. Claude Opus 5.5 charges $4 per million input tokens, so resending 500k tokens costs about $2 per step, and an agent takes hundreds of steps. What makes long sessions affordable is prompt caching: the provider keeps the work it already did on that prefix and charges a small fraction to reuse it. With the cache warm, the same step costs about 12 cents.

So in a long session, the number that decides your bill isn't the headline input price. It's the cache: whether it is warm, how long it stays warm, what it costs to write, and what breaks it. This article builds that picture from the inside out, then simulates a working day so you can see which habits cost money and which don't.

All prices are for Claude Opus 5.5 on the Claude API, in US dollars per million tokens, as documented at its launch in September 2026. Most figures let you change the inputs. Tokens are colour-coded the same way everywhere: cache read cache write uncached input output keep-alive.

02What is actually cached

A model handles a request in two phases (my book covers both in Chapter 7). In prefill it reads the whole prompt at once: every token passes through every layer, and at every layer each token leaves behind two vectors, a key and a value, that later tokens use to look back at it. In decode it generates the reply one token at a time, and each new token consults all of those stored keys and values. That store is the KV cache.

Within one request, the KV cache is simply how decoding works. Prompt caching is the provider deciding to keep it after the request ends. If the next request starts with exactly the same tokens, the provider can load the stored keys and values instead of running prefill over that prefix again. Only the new tokens at the end need computing.

Figure 1Prefill computes the KV cache; a cache hit loads it back
Request 1: prefill Request 2: same prefix + new tokens prompt tokens layer 1: K, V per tokenlayer 2: K, V per token…layer L: K, V per token keptfor TTL same prefix new loaded, not recomputedloaded…loaded
Prefill runs every prompt token through every layer and leaves a key and a value per token per layer. The provider keeps these for a while. A later request that starts with the same tokens loads them and only computes the new tokens at the end.

How big is that store? Anthropic doesn't publish Claude's architecture unknown, but open models give the order of magnitude. Llama 3.1 405B has 126 layers and keeps 8 key-value heads of 128 numbers each, at 2 bytes per number public. Per token that is:

2 (key, value) × 126 layers × 8 heads × 128 numbers × 2 bytes ≈ 0.5 MB per token 200,000 tokens ≈ 103 GB 1,000,000 tokens ≈ 516 GB

A single long conversation's cache can be bigger than the 80 GB of memory on one H100. Keeping it around is not free: it has to live somewhere, probably in a tier of cheaper memory or storage, and be moved back to the accelerators on a hit inferred. That explains the shape of the prices below. A write costs more than normal input, because you pay for prefill and for storing the result. A read costs much less, because prefill is skipped: for a model with N parameters it costs about 2N operations per token (Chapter 4), and a hit replaces that with moving bytes. And entries expire, because memory is finite inferred.

03The rules the API plays by

Whatever happens inside, what you're billed for follows a small set of documented rules public. They're worth knowing exactly, because every cost effect in this article follows from them.

  1. It is a prefix match. The prompt is laid out as tools, then system prompt, then messages. A cached entry is keyed by the exact tokens up to a point. Change one byte anywhere and everything after that byte is a miss.
  2. Entries exist only at breakpoints. A request marks up to 4 positions with cache_control, or turns on automatic caching, which puts the mark at the end of the conversation and moves it forward every turn. The cache stores the prefix up to each mark. Nothing in between is separately reusable.
  3. Lookup walks back a limited distance. Each breakpoint searches backwards at most 20 content blocks for an existing entry. A turn that appends more than that (long sequences of tool calls, many images) can miss an entry that is still alive.
  4. Entries live 5 minutes, or 1 hour if you ask. Every read resets the timer, for free. The timer runs from the start of the request that last touched the entry, so a 4-minute generation leaves about 1 minute before a 5-minute entry expires.
  5. Each input token is billed once, at one of three prices. The response's usage reports cache_read_input_tokens, cache_creation_input_tokens (writes) and input_tokens (the uncached remainder after the last breakpoint). The three add up to the prompt size. A write replaces the input price; it doesn't add to it.
  6. Caches are scoped. They are separate per model and, on the Claude API, per workspace. Switching models mid-session starts from zero.
  7. A new entry is readable once its request starts streaming. Several requests fired in parallel with the same new prefix all pay to write it.
  8. Short prompts don't cache. The minimum on the Opus 5 family is 512 tokens; below it, nothing is stored.
Claude Opus 5.5$ per million tokens× inputSource
Input (uncached)$4.001×public
Cache write, 5-minute lifetime$5.001.25×inferred from the documented 1.25× multiplier
Cache write, 1-hour lifetime$8.002×inferred from the documented 2× multiplier
Cache read$0.200.05×public
Output (includes thinking)$20.005×public

Note the read price. Most Claude models charge 0.1× input for a cache read; Opus 5.5 lists $0.20 against a $4 input price, which is 0.05×. That halves the cost of re-reading, and it doubles the ratio between a cold write and a warm read. Keep that ratio in mind: on a 5-minute cache, a cold write costs 25 times what a warm read of the same tokens costs; on a 1-hour cache, 40 times. Most of this article is consequences of those two numbers.

Figure 2One edit invalidates everything after it still cachededitedwritten again
A 48k-token session prompt about to send its next request. Click any block to edit it, or pick a common change. Everything from the edit onwards has to be written again at the write price. The effect of switching models and changing tools is documented. Where an effort change lands depends on the model: the docs say it always invalidates the message history, and on some models the system prompt too.

04Anatomy of one request

With the rules in place, one agent request splits cleanly into three parts:

The previous reply is billed three times over its life: once as output when it is generated, once as a write when it comes back as input, and as a read on every request after that. That isn't double billing; the token does three different jobs.

Figure 3Where the tokens are, and where the money goes
Tokens
Dollars
One request on Opus 5.5. At 200k tokens of context, 99% of the tokens and about two-thirds of the dollars are cache reads. Shrink the context to 20k and output becomes the biggest cost. Switch to a cold cache to see the same request cost about 16 times as much.

Two regimes show up. In a small context, output dominates: the reply and its thinking, at $20 per million, cost more than everything read. In a large context, re-reading dominates, even at five cents on the dollar, because there's so much of it. Agent sessions spend most of their life in the second regime.

05Every request re-reads everything

Because each request re-reads the whole prefix, the cost of a request grows in a straight line with the context. And because the context itself grows with every request, the total cost of a session grows with the square of its length:

cost of request n ≈ read × (base + n·g) + write × g + output × o total after n ≈ read × g × n² / 2 + terms that grow only linearly

Here g is how much the context grows per request (new input plus the previous reply) and o is the output per request. With a 25k-token start, 2.6k tokens of growth per request and a warm cache, 100 requests cost about $5.71. Doubling to 200 requests roughly triples it, to $16.48. Without caching, those 200 requests would cost $231.

Figure 4The next request, and the whole session warmcold, 5-mincold, 1-hourno caching
Top: the cost of one more request (2k new tokens, 600 output) at each context size. Hover or tap for values. Bottom: the running total of one continuous session. Caching doesn't remove the quadratic growth, it divides it: a warm re-read costs one-twentieth of uncached input.

The top chart shows the most important fact in this article: a cold request is more expensive than no caching at all. When the cache is cold, the harness still marks a breakpoint, so the whole prefix is billed as a write at 1.25× or 2× the input price. At 200k tokens the next request costs $0.06 warm, $0.82 with no caching, $1.02 cold on a 5-minute cache and $1.63 cold on a 1-hour cache. Caching is a bet that you'll come back soon. When you lose it, you pay a premium.

06When the cache goes cold

The cache goes cold for two kinds of reasons. The first is time: the gap from the start of one request to the start of the next exceeds the lifetime (5 minutes by default). In an agent loop that happens when you step away, when you read a long diff before replying, or when a single step (a long test run, a slow tool) takes longer than the lifetime.

The second is a change to the prefix. The docs list the usual suspects public:

Either way the price of a cold request is the same: every token of the prefix that isn't still cached is written again. On Opus 5.5 that is 25 times a warm read on the 5-minute cache. A cold request on a 300k-token session costs about $1.44 more than the warm one would have. That's the price of a coffee each time you take a coffee break.

07Ping or pay

If a read resets the timer, you can keep a cache alive on purpose: while you're away, resend the last request shortly before the entry would expire. The API supports this directly public. A request with max_tokens: 0 runs prefill, generates nothing, and bills only the cache read. That is a keep-alive ping.

Each ping costs one read of the context. Letting the cache expire costs one extra write of the context, minus the read you'd have paid anyway. The context size cancels out of the comparison:

pings worth sending = write price ÷ read price − 1 5-minute cache: 5.00 ÷ 0.20 − 1 = 24 pings one every 4.5 min → about 1 h 48 min 1-hour cache: 8.00 ÷ 0.20 − 1 = 39 pings one every 54 min → about 35 hours

So on Opus 5.5, if you'll be back within about an hour and three-quarters, keeping the 5-minute cache alive is cheaper than letting it go cold, whatever the size of the session. Beyond that, let it expire. The break-even is this generous because the read is so cheap: at the usual 0.1× read price it would be 11.5 pings, under an hour.

Figure 5What a pause costs
The extra cost caused by stepping away, compared with not stepping away. "Let it expire" is free until the lifetime runs out, then jumps to a full cold rewrite. Keep-alive climbs one small step per ping. The break-even point doesn't move when you change the context size; only the height of the curves does.

Two cautions. First, a keep-alive has to resend the identical last request, with the same model, tools, thinking and effort settings, and with streaming off (the API rejects max_tokens: 0 with streaming). That is a job for the harness, not for you. Second, typing "ping" into the agent is not a keep-alive. It adds a new user message, the model answers it, the answer and its thinking are billed as output at $20 per million, and both join the context forever inferred. It does keep the cache warm, but at several times the price of a real keep-alive.

08Five minutes or an hour?

The 1-hour lifetime looks like the obvious fix for gaps: pay a bit more and the cache survives your meetings. The catch is what you pay more on. The 1-hour write price applies to every write, and in an agent session every token is written once, as it first joins the context. On Opus 5.5 that's $3 more per million tokens written: a session that grows to 600k tokens pays about $1.80 extra, whether or not it ever has a gap.

The docs give a simple guide, by the start-to-start gap between requests that share the prefix public:

Gaps are…UseWhy
under 5 minutes5-minute cacheevery request refreshes it; the 1-hour cache buys nothing but the 2× write
5 to 60 minutes1-hour cache, or keep-alivethe only range where the 1-hour write can pay off
over an hourneitherkeep alive on a schedule, or accept the cold miss

For models with a cheap read, the docs add that keep-alive on the 5-minute cache usually beats the 1-hour cache, and Opus 5.5's read is cheap. The simulator in section 11 bears that out on most working patterns: the 1-hour cache wins only when medium gaps are frequent and nothing sends keep-alives.

09Continue, or start fresh?

A big context is a tax on every request. At 400k tokens, each request pays 8 cents just to re-read the session before doing anything new. A fresh session that starts with the system prompt and a few thousand tokens of hand-off notes re-reads for less than a cent. Starting fresh costs one write of the notes, plus the system prompt if that has gone cold too.

Figure 6Carry on in the big session, or start a fresh one
The cost of the remaining work, in requests, if you continue a large warm session or start fresh with hand-off notes (system prompt 25k, 2k new tokens and 600 output per request). In dollars, starting fresh pays off almost immediately at any large context size.

So why not restart constantly? Because the dollars aren't the whole cost. A fresh session has lost everything the old one knew but the notes didn't capture. It will re-read files and re-run commands to rediscover it, which costs tokens this chart doesn't count, and it may simply do worse work. The practical rule is to restart at natural boundaries: when a task is done, when the context is full of material that no longer matters, and above all after a long break. After a long break the cache is cold anyway: continuing a 400k-token session means rewriting all 400k at the write price, $2.00, while a fresh session costs a few cents. Resuming a big session after lunch is the single most expensive moment to continue it.

Compaction, which many harnesses do automatically as the context fills up, is a restart built into the harness. One request reads everything and writes a summary as output; the session then continues from the system prompt plus the summary. It costs one full read and a long output, and resets the per-request tax.

10Forks and parallel agents

Caches belong to prefixes, not to sessions. Fork a session into two branches and they share every cache entry up to the fork point. Whenever either branch reads a shared entry, it refreshes it for both. The newer entries each branch writes after the fork are its own, and only its own requests keep them alive.

Take a session on a 1-hour cache that has finished its second turn. The latest entry, C2, covers everything up to your second message. You fork it. One minute later, branch SS1 sends U3: it reads C2 and writes C3a. At T+50, branch SS2 sends U4: it reads C2, which resets C2's timer to T+110, and writes C3b. At T+75, SS1 sends U5. Its own entry C3a expired at T+61, but C2 is still alive because SS2 read it. So SS1 reads C2 and rewrites only the part after the fork. Without SS2, C2 would have expired at T+61 too, and SS1 would have rewritten everything.

Figure 7A forked session on a 1-hour cache
Each bar is a cache entry's lifetime; dots are writes (amber) and reads (green), and every read extends the bar by an hour. Drag the time cursor, and switch the fork off to see SS1's request at T+75 go fully cold. Sizes are illustrative: 60k tokens up to the fork, 2k per turn after it.

Two more rules matter when you run several agents at once. Parallel requests don't share a write in progress: an entry becomes readable only once its request starts streaming, so ten sub-agents launched at the same instant over the same new prefix all pay the write. Launch one, wait for its first token, then launch the rest. And sub-agents with a different system prompt or tool set share nothing with the parent, because their prompts diverge at the first byte. Where the sub-agent can reuse the parent's exact prefix and only append to it, it inherits the parent's cache.

11A day of agent work

Everything so far, put together. The simulator below plays out a working day: blocks of work, during which the agent sends a request every few seconds, separated by time away. It prices each request on Opus 5.5 according to the rules above, for up to five strategies at once:

Figure 8The day simulator
The day, in order (edit minutes, remove or add blocks):
Context size over the day for:
Session settings
Shaded bands are time away. Defaults: a 25k-token system prompt and tools, a request every 45 seconds while working, 2k new tokens and 600 output tokens per request, keep-alive for at most 2 hours, a fresh session after breaks of an hour or more, with 8k tokens of hand-off notes. The table's "per request" column divides the day's total by the requests sent.

With the defaults, the "meetings in between" day shows every effect at once:

Switch to Resume next morning, and keep-alive capped at 2 hours becomes a waste: it pays for 26 pings and then goes cold anyway. Switch to Stop and go, with short breaks only, and both alternatives beat the default: keep-alive comes in 30% cheaper, the 1-hour cache 24% cheaper. Change the settings to match how you actually work; the ranking changes more than you might expect.

12Field guide

What to do with all this, day to day:

  1. Keep the prefix still. Don't switch models or change effort in the middle of a session, don't let tools come and go, and don't edit earlier messages. Each of these rewrites the session at the write price. If you need a different model for a side task, use a sub-agent and leave the main loop alone.
  2. Know your gaps. Under 5 minutes, nothing needed. From 5 minutes to about 1 h 45, a keep-alive is cheaper than a cold miss on Opus 5.5, if your harness can send one. Longer than that, let it expire.
  3. Treat a cold resume of a big session as a decision. It rewrites the whole context at more than the uncached price. After a long break, a fresh session with good hand-off notes is usually cheaper and often no worse.
  4. Remember that a big context is a per-request tax. Start fresh at task boundaries. Compaction helps, and so do notes that let a new session skip rediscovery.
  5. Choose the 1-hour cache deliberately. It's worth it when 5-to-60-minute gaps are frequent and keep-alive isn't available. Otherwise it's a 60% surcharge on every write.
  6. Launch parallel agents in a staggered way, over byte-identical shared prefixes, so they read one entry instead of each writing their own.
  7. Measure. The usage fields are the only ground truth. A healthy session reads nearly everything and writes only what each request added. A request whose writes are close to the whole context has gone cold, and you now know why.

13What this article can't tell you

Sources