Part VII ยท Harnesses
Ch 26. Caching: reusing the work
Doing the same work twice
The chat in Ch 24 re-sent the whole conversation on every call, and the model read it all again. Even within one reply, it re-reads everything before each new token (Ch 11). Does it really redo all that work every time?
The chat in Ch 24 re-sent the whole conversation on every call: 163 tokens after the first exchange, and 633 by the time you asked your question. Inside each call, the token loop from Ch 11 does the same: to pick each new token, the model reads everything before it again. Does it really redo all that work every time?
It doesnโt have to. In attention (Ch 9), each token makes a key and a value that depend only on the text before it, so they never change once worked out. The model can store them and reuse them: this store is the KV cacheKV cache: The keys and values a model has already worked out for earlier tokens, stored so each new token only has to compute its own. Ch 26. Step through GPT-2 small finishing โThe cat sat on theโ with the cache off and on:
It doesnโt have to. Remember how attention works (Ch 9): every token makes a query, a key, and a value. Each token can only look at the tokens before it, so its key and value depend only on the text before it, and adding more text later doesnโt change them. The model can work them out once and keep them. This store is the KV cacheKV cache: The keys and values a model has already worked out for earlier tokens, stored so each new token only has to compute its own. Ch 26: with it, each new token only computes its own key and value, and its query is matched against the stored ones. Step through GPT-2 small finishing โThe cat sat on theโ with the cache off and on:
Without the cache, each new token took longer than the last, and 500 tokens took 43.0 seconds. With it, each took about the same time, about 20 milliseconds, and the run took 10.0 seconds. Both runs wrote exactly the same text.
Without the cache, every step runs the whole text through all 12 blocks again, so each step costs a little more than the last. Measured on a laptop, the time per new token grew from 23 to 139 milliseconds over 500 tokens, and the whole run took 43.0 seconds. With the cache, each token took about 20 milliseconds, and the run took 10.0 seconds. Both runs picked exactly the same 500 tokens; their scores differed by at most 0.01, from rounding when the same sums are added up in a different order.
Counting the work
Without the cache, step (counting from 0) runs all tokens through the model, so new tokens after a -token prompt take
token passes. For the 5-token prompt and 500 tokens here, thatโs 127,250, against 504 with the cache: 252 times as many. Yet the run took only 4.3 times as long. Running one token through the model is mostly waiting for its weights to arrive from memory, and many tokens in the same pass share that trip, so the extra tokens are cheap.[1] With the cache, each step still compares its query with every stored key, so the time per token crept up a little, from about 19 to 20 milliseconds.
The cache works with the guess-and-check trick from Ch 14 too: the text before the guesses already has its keys and values stored, so the big modelโs checking pass only computes them for the guesses.
The price is memory: GPT-2 small stores 18,432 numbers for every token, and for large models and long prompts the cache runs to gigabytes.[1]
The price is memory. GPT-2 small stores a key and a value in each of its 12 blocks for every token: 2 ร 12 ร 768 = 18,432 numbers per token. For large models and long prompts the cache runs to gigabytes, and keeping it small is a big part of serving them efficiently.[1][1] Some other kinds of models avoid it by keeping a fixed-size memory instead.
The cache for a bigger model
Qwen2.5-1.5B-Instruct, the model in the Harnesses chapters, has 28 blocks, and its attention has 12 heads of 128 numbers each. A key and a value for every head would be 2 ร 28 ร 12 ร 128 = 86,016 numbers per token. It stores far fewer, because its 12 heads share 2 sets of keys and values, an arrangement called grouped-query attention: 2 ร 28 ร 2 ร 128 = 14,336 numbers, six times fewer.[2] At 2 bytes each, thatโs about 29 KB per token, and about 0.94 GB for a full 32,768-token context.
Caching across calls
The cache for one reply is thrown away when the reply is done. But a chatโs next call starts with the same text as the last, plus a little more. So model providers keep the cache for the start of a prompt for a while, and reuse it when a new prompt starts the same way. This is prompt cachingprompt caching: A provider keeping the KV cache for the start of a prompt, so a later prompt that starts the same way is cheaper and faster. Ch 26, and it makes those tokens much cheaper.[2][3] Work that can wait is cheaper too: Anthropic charges half price for requests sent in a batch and answered later instead of straight away.[4]
The KV cache for one reply is thrown away when the reply is done. But a chatโs next call starts with exactly the same text, plus a little more. So model providers keep the cache for the start of a prompt for a while, from minutes to hours, and a later prompt that starts the same way, token for token, picks up from there. This is prompt cachingprompt caching: A provider keeping the KV cache for the start of a prompt, so a later prompt that starts the same way is cheaper and faster. Ch 26. Providers charge a tenth of the normal price or less for tokens read from the cache, and the reply starts sooner, because there is less to work through first.[2][3][3][4] Cached tokens still count toward the context window, though: caching changes what they cost, not how much room they take.[4][5] Work that can wait is cheaper too: Anthropicโs Batch API, which answers requests later instead of straight away, charges half price for both input and output tokens.[5][6]
The match has to be exact, from the very first token: change one early word and everything after it has to be worked out again. See what that means for the Ch 24 session:
The match has to be exact, from the very first token. Change one early word and every key and value after it is different, because each depends on all the text before it. So the cache only helps up to the first difference. See what that means for the Ch 24 session:
- Only appending reuses 2,342 of the 2,975 tokens sent (79%). Each call only pays full price for whatโs new.
- Putting the time at the top of the system prompt makes every prompt different from its first line, so nothing is reused.
- Editing the system prompt throws the cache away once; later calls build a new one.
- Dropping the oldest turn (Ch 24) sends fewer tokens, 2,189, but costs more than keeping them all: every drop changes the text right after the system prompt, so little else matches.
- Summarizing sends the fewest tokens and caches well between summaries, so it costs the least.
With cached tokens at a tenth of the price, a call costs about in full-price tokens. Only appending sends 2,975 tokens, 2,342 of them cached, so it costs about 633 + 234 = 867: 29% of the uncached price. Putting the time at the top costs all 2,975.
That is why harnesses keep the parts that donโt change at the front, in the same order every time: tool definitions, then the system prompt, then the conversation. Anything that changes, like the time or the new message, goes at the end, and the conversation only grows. The providersโ own guides give the same advice.[2][2][3]
The cache is not memory. It holds the modelโs working for text that is still in the prompt, so the model knows nothing more than it did (Ch 24), and it is deleted after a while without use. It doesnโt change the answer either: a cached prompt gets the same kind of reply as an uncached one.
Simplified: providers only cache prompts above a minimum length, from a few hundred to a few thousand tokens depending on the model, and charge a little extra to write the cache. This small session would be too short, so the view above ignores that limit.
Recipe card
Recap
- A tokenโs keys and values depend only on the text before it, so the model can store them: the KV cache.
- With the cache, each new token costs about the same; without it, each costs more than the last. The answer is the same either way.
- Providers keep the cache for the exact start of a prompt, so harnesses put what doesnโt change first and only ever append.
- Without the cache, tokens take passes; sharing keys and values across heads shrinks the cache.
Next question: so far one model has answered everything. Apps often have several, cheap and expensive. Who decides which one answers?
Sources
- Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
- OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
- Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
- Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.
Sources
- Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
- OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
- Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
- Anthropic (2026). Context windows. Claude Developer Platform documentation. Accessed October 25, 2026.
- Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.
Sources
- Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
- Qwen Team et al. (2024). Qwen2.5 Technical Report. arXiv.
- OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
- Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
- Anthropic (2026). Context windows. Claude Developer Platform documentation. Accessed October 25, 2026.
- Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.