Pay for the prefix once

Every request to a language model re-sends everything that came before it. A prefix cache lets the provider skip the part it has already read. This is how that works, what quietly breaks it, when it costs more than it saves, and how to lay out a prompt so that it mostly just works.

15 min read

request 1 · 3,950 tokens0 from cache · 3,950 paid in full

A chat with a language model is a series of separate requests. Each one carries the whole conversation so far: the instructions, the tool definitions, any documents you attached, every earlier message, and finally the new one. The model reads all of it from the top, every time. On the twentieth turn it is reading the system prompt for the twentieth time, and until recently you paid for that twentieth reading at the same rate as the first.

Prompt caching is the fix. If a request starts with exactly the same bytes as one the provider saw a few minutes ago, the work of reading those bytes can be reused instead of redone. You pay a little more to store it the first time and a lot less to reuse it afterwards. It is one of the few levers in this field that is both large, often most of the input bill, and almost entirely mechanical. There is no judgement involved. Either the front of your prompt matches or it does not.

The figures below run in your browser with no API calls. The token counts and prices are real as of 26 November 2026, but the prompts are toys, and the only thing each figure asks you to notice is where the boundary between “already paid for” and “paid for again” lands.

The forward pass remembers

In the previous essay I walked through what a transformer does with a prompt: every token becomes a vector, and at each layer every position computes a key and a value that later positions can attend to. Two facts about those keys and values make caching possible. First, they are expensive: computing them is most of the work of reading a prompt. Second, they are a function only of the token at that position and the tokens before it, because generation is causal and a position can never see the future.

Put those together and the keys and values for a prefix depend on nothing but the prefix. If two requests begin with the same hundred tokens, the first hundred positions of their key and value tensors are identical, down to the bit. So the provider can keep those tensors around for a short while and, when a matching request arrives, load them instead of recomputing them. That is the entire mechanism. A prompt cache is a store of key and value tensors, looked up by the exact bytes at the start of the prompt.

Figure 01What the cache stores
The
K
V
0
cat
K
V
1
sat
K
V
2
on
K
V
3
the
K
V
4
mat
K
V
5
because
K
V
6
it
K
V
7
was
K
V
8
warm
K
V
9
Reused
10 positions
Recomputed
0 positions

Green: keys and values identical to the last run, so they can be loaded instead of computed. Dark: positions at or after the first change. Note that “Insert a word” recomputes everything after it, even though the later words are the same.

Figure 01A sentence, one token per word, with the keys and values each position would hold. Edit the text and the positions that still match the cached run stay green. Everything from the first difference onwards goes dark, including later words that happen to be unchanged, because their position in the sequence has moved.

The figure shows the rule that everything else in this essay follows from. The match is taken from the front and stops at the first difference. Inserting a single word in the middle recomputes every position after it, even though the words are the same, because each position’s keys and values encode where it sits as well as what it is. There is no such thing as a partial hit in the middle of a prompt.

Key idea

A prompt cache is a lookup keyed by the exact bytes at the start of a request. Change one byte at position N and every position from N onwards is paid for again.

Where the boundary lands

A request is more than your message. By the time it reaches the model it has been rendered into one long sequence in a fixed order: the tool definitions first, then the system prompt, then the messages in the order they happened. That order is not something you choose, and it matters, because the cache boundary is found by walking that rendered sequence from the front.

This means the question to ask about each piece of a prompt is not “is this important?” but “how often does this change, and what sits after it?” A system prompt that never changes belongs early. A document the conversation is about belongs after it. The user’s newest message belongs last, because it changes every turn and anything placed after it would be thrown away every turn with it.

Figure 02Order decides
Drag to reorder, or use the arrows. “Edit” changes a block’s content.Cache holds the request as first built
  1. toolsTool definitions
  2. systemSystem prompt
  3. documentQ3 report (attached)
  4. historyEarlier turns
  5. user turnThis user message
Rendered request, front to back11,520 tokens
cache hit: identical to the previous request from the frontprocessed and paid for in full
From cache
11,520
Paid in full
0
Hit rate
100%

Try: send once, then edit only the user turn. Then drag the user turn above the report and send again. Then edit the system prompt. The boundary is always the first block that differs from what the cache remembers.

Figure 02A request as a stack of blocks, in the order the provider renders it. The cache remembers the last request you sent. Drag the blocks around, edit one, send, and watch how far the green hit extends. Putting the user turn above the attached report makes the report uncacheable, because it now sits after something that changes every time.

The pattern that falls out is simple to say and easy to break: stable things first, volatile things last. Most prompts that cache badly do so because something small and changeable, a date, a user’s name, a per-request identifier, was interpolated near the top where it poisons everything beneath it.

How providers expose it

Providers agree on the mechanism and differ on how much of it they show you. Anthropic’s API is explicit. You mark up to four points in the request with a cache_control field, and each one says “cache everything up to here”. By default an entry lives for five minutes from the start of the request that wrote or last read it. You can ask for an hour instead, at a higher write price, and entries with the longer lifetime have to come before entries with the shorter one. There is also a top-level option that puts a single breakpoint on the last block of the request for you, which is what you want for a plain multi-turn chat.

OpenAI’s API is automatic. There is nothing to mark. For any prompt of at least 1,024 tokens the service looks for the longest prefix it has recently seen and charges the matched part at a discounted rate, matching in steps of 128 tokens. You find out what happened from a cached_tokens field in the usage block. The discount on cached input has grown since the feature launched and varies by model, so I am not going to quote one number for it.

Both providers have a minimum. Below some size a prefix is not worth storing: the lookup and the storage cost more than the compute saved. On Anthropic’s side the minimum depends on the model, and as of 26 November 2026 it runs from 512 tokens on the current generation up to 4,096 on a few older ones. A prefix under the minimum fails silently. The request succeeds, the marker is accepted, and the usage block simply reports that nothing was cached.

Figure 03Explicit versus automatic
Opus 4.8 / Sonnet 4.6
Click a boundary to place a breakpoint, click again for a 1-hour one, again to remove1/4
served from cache on the next identical requestnot covered by a working breakpoint
  1. after system1,600 tokensttl 5mcached
Cached on repeat
1,600
Request
4,890 tokens
Figure 03Left: Anthropic’s explicit breakpoints. Click a boundary to mark it, click again to make it a one-hour entry, and shrink the system prompt until a breakpoint falls under the model’s minimum and quietly stops working. Right: OpenAI’s automatic matching, in 128-token steps above a 1,024-token floor. Figures from both providers’ documentation in November 2026.

The explicit design gives you something the automatic one cannot: several boundaries at once. Tool definitions that never change, a set of documents that changes daily and a conversation that changes every turn can each get their own breakpoint, so a new document invalidates the document cache without touching the tool cache in front of it. The automatic design gives you something the explicit one cannot: no way to forget to do it.

What breaks a hit

Having built a cache, the job becomes not breaking it, and the ways to break it are all the same way. Something earlier in the rendered prompt differs from last time. The usual suspects are dull. A timestamp or “today’s date” line in the system prompt. A request or session identifier. The user’s name. A JSON object serialised without sorted keys, so the same data comes out in a different order on a different server. A tool list that is built per user, or assembled from a set, so two users never share a prefix and one user does not share one with themselves. A feature flag that adds a paragraph to the system prompt for some requests and not others.

A few invalidators are less obvious because they are not in the text at all. Caches are scoped to a model, so switching models mid-conversation starts from nothing. Changing the thinking or effort settings between requests invalidates the conversation cache on every current Anthropic model. And a cache is scoped to the account and workspace it was written from, so traffic for the same prompt split across workspaces writes two entries and reads neither.

Figure 04Break the cache
Click any character to change it. Click it again to put it back.≈ 4 characters per token
tools[{"name":"get_weather","description":"Current weather for a city"},{"name":"search_handbook","description":"Search the staff handbook"}]
systemYou are the assistant for Northwind Travel. Answer in British English. Be brief. Never invent prices or dates.
documentHandbook §4: Refunds requested within 14 days of booking are automatic. After 14 days a refund needs manager approval. Vouchers never expire.
historyUser: Can I get a refund on my booking? Assistant: Yes. Within 14 days of booking it is automatic.
user turnUser: It has been 16 days.
Still a hit
128 tokens
Paid again
0 of 128
Boundary
none

Green: identical from the front, served from cache. Grey: at or after the first change, processed again. A date in the system prompt costs almost the whole prompt every request. Changing only the user turn costs almost nothing.

Figure 04The cache remembers this exact prompt. Click any character to change it and the boundary jumps to that character; everything after it is processed again. The presets reproduce the classic mistakes. Stamping a date on the system prompt re-pays nearly the whole prompt. Changing only the user turn re-pays a handful of tokens.

The game is unfair on purpose. A one-character change near the top costs the same as rewriting the whole prompt, and there is no warning when it happens. The costliest caching failures I have seen were regressions: caching worked when it was set up, then months later someone added a helpful “current date” line to the system prompt, every request started missing, and nothing announced it. Requests kept succeeding. The bill just went up.

What it costs

Caching is not free, and this is the part worth doing the arithmetic on. On Anthropic’s API, as of 26 November 2026, writing a cache entry costs 1.25 times the normal input price for a five-minute entry and 2 times for a one-hour entry. Reading one costs a small fraction of the input price: historically a tenth, and less on the newest models. Claude Opus 5.5 reads at $0.20 per million tokens against $4 for uncached input, a twentieth; Claude Sonnet 5.5 reads at $0.10 against $2; Claude Fable 5.2 reads at $0.20 against $8, a fortieth.

Follow that through for a prefix that is written once and read once. With the five-minute lifetime you pay 1.25 plus roughly 0.1 of the uncached price instead of 2, so the second request already wins. With the one-hour lifetime you pay 2 plus 0.2 instead of 2, so you need a third request before you are ahead. Caching costs you money in exactly three situations: a prefix that is written and never read back, because the session was a single request; a prefix whose entry lapses between requests, because the gap was longer than the lifetime; and a breakpoint placed after content that is unique to each request, which pays the write premium on bytes that nothing will ever match.

Figure 05Two bills
Without caching
$0.490
With caching
$0.084
Saved
83%
Input $4.00/M · cache read $0.200/M · write 1.25× input. Prices as of 26 Nov 2026.
Input tokens billed per turn, by rate
cache read, $0.200/Mcache write, $5.00/Muncached, $4.00/M

Each turn reads everything the previous request wrote and writes only the new tail. A write costs 1.25× an uncached read of the same tokens, so the second request is where the prefix starts paying for itself.

Figure 05One session, billed twice: once without caching and once with a breakpoint on the last block of each request. Drag the gap between requests past the lifetime and watch caching start to lose. Set the session to a single turn and it loses straight away. Prices from the Claude developer platform in November 2026.

Time

The lifetime has two details that catch people out. The clock starts at the beginning of the request that writes or refreshes the entry, not when its response finishes. A reply that takes four minutes to generate leaves about one minute for the next request to arrive before a five-minute entry lapses. And a read refreshes the clock at no extra cost, which means steady traffic keeps a five-minute entry alive indefinitely. If requests sharing a prefix always arrive less than five minutes apart, the one-hour lifetime buys you nothing except the doubled write price.

The one-hour lifetime earns its keep in the gap between five minutes and an hour: a user who replies after their coffee, an agent that goes away to do a long side task, a generation that itself runs past five minutes. Beyond an hour nothing helps and you start again. There is one more trick for the middle range. A request with max_tokens set to zero runs the prefill, refreshes the entry and returns nothing, billed as a cheap cache read. On models whose reads are very cheap, sending one of those just before a five-minute entry would lapse can beat paying double for the one-hour write.

Figure 06Lifetimes
Click the timeline to add a requestan entry lives 5 minutes from the start of the request that touched it
0 min306090
write: nothing to readhit: read, and the clock restartslapsed: written againentry alive
Hits
5
Writes
2
Of which lapsed
1

Switch the coffee break to the 1-hour lifetime and the request at minute 26 becomes a hit, but every write now costs 2× instead of 1.25×. Whether that trade is worth it is the question of the next figure.

Figure 06One cache entry on a ninety-minute clock. Dark dots write it, green dots read it and restart its clock, hollow dots arrive after it lapsed and pay to write it again. Click the timeline to add requests, drag Now, or play it through. Switch the coffee-break preset to the one-hour lifetime and the lapse disappears.

Patterns that work

For a chat app the recipe is short. Freeze the system prompt. Put documents after it and the conversation after them. Put the breakpoint on the last block of the newest turn, or let the automatic option do it, and every request reads the entire previous conversation and writes only the new exchange. Earlier breakpoints stay valid, so hits accumulate as the conversation grows. If you need to tell the model something mid-conversation, append it as a message rather than editing the system prompt at the top; on Anthropic’s current models there is a system-role message for exactly this, and it leaves the cached prefix intact.

For an agent the same recipe applies with more at stake, because an agent loop makes many requests a minute over a prefix that is mostly tool definitions. Serialise the tools deterministically and never change the set mid-loop; if the agent needs modes, give it a tool that records the mode instead of swapping the tool list. Keep the loop on one model; spawn a cheaper subagent for side work rather than switching. Put one explicit breakpoint on the end of the static system prefix, so the expensive part has a guaranteed read point whatever happens later, and let the automatic breakpoint follow the growing conversation. If a turn can run past five minutes, or the human in the loop wanders off, use the one-hour lifetime or a keep-alive.

Parallelism is the counterintuitive one. An entry becomes readable only once the first response that wrote it has started streaming. Fire ten workers at the same prefix in the same instant and all ten write it, and none reads it. If the shared prefix is large, make one request first, then fan out. The same arithmetic argues for fewer workers over a byte-identical prefix rather than many workers each with a slightly different one.

Figure 07An agent loop
Input tokens per request. Click a bar for its usage block.
123456789101112
cache_read_input_tokenscache_creation_input_tokens
// request 12
"usage": {
  "input_tokens": 0,
  "cache_creation_input_tokens": 700,
  "cache_read_input_tokens": 13_200
}
Hit rate
88%
Input bill
$0.091
Uncached would be
$0.482

Priced at Claude Opus 5.5 rates as of 26 Nov 2026 ($4.00/M input, $0.200/M read, writes at 1.25×). A healthy loop reads nearly everything and writes one tool call’s worth per turn. Turn on the timestamp and watch the bars go dark without a single error being raised.

Figure 07Twelve turns of an agent calling tools, as the bill sees them. Each toggle is a real mistake. A timestamp in the system prompt leaves only the tools cached. Shuffled tools cache nothing. A twenty-minute pause lapses a five-minute entry, unless the lifetime is an hour. Three workers fired at once all pay to write. Click a bar to see the usage block the API would have returned.

The last habit is the one I would keep if I could only keep one. Read the usage block. Every response says how many input tokens were read from cache, how many were written, and how many were processed at full price. If the read count is zero on the second of two requests that should share a prefix, something in the prefix is changing, and the fix is to log two consecutive request bodies and diff them. Check it after every change to how the prompt is assembled, not just when caching is first switched on. Nothing else will tell you.

Key idea

Stable things first, volatile things last, one breakpoint at the end of what is shared, and a standing check that the cache read count is not zero. That is most of prompt caching.

Written by

Dan Farr — a contract product engineer in UK, building product front-ends and whole applications in React, Next.js and TypeScript.

Available for contract work and fixed-scope projects. See what I do.