LLM Kitchen
Explanations

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 . 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 : 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:

Step through

Reusing keys and values

Recorded ยท GPT-2 small
Key-value cache
For each step, which tokens go through the model and which are reused from the cache
StepTheยทcatยทsatยทonยทtheยทfloor
Step 1runrunrunrunrun

Step 1 picks ยทfloor. So far the model has run 5 token positions with the cache off, against 5 with it on. Either way it picks the same tokens.

050100ms per tokenwithout the cachewith the cacheNew token 1 to 500
Measured: 500 new tokens took 43.0 s without the cache and 10.0 s with it (CPU, 8 threads, float32). Both runs picked the same tokens.
The numbers
Milliseconds for each new token
New tokenWithout the cacheWith the cache
12324
22419
32319
42418
52419
62418
72718
82818
92818
102718
112819
122818
133919
143919
154018
163919
174018
184018
194019
204019
214019
224019
234119
244119
254019
264019
274119
284119
294719
304619
314718
324619
334718
344719
353719
363719
373719
383819
393919
403920
413819
423922
433819
443919
454219
464219
473820
483819
493719
503919
513820
523920
533819
543920
553919
563919
573920
584020
593919
603920
614419
624519
634420
644419
654420
664419
674419
684519
694420
704419
714420
724519
734419
744519
754520
764520
774720
784719
794719
804920
814820
824720
834820
844820
854720
864820
874820
884820
894819
904819
915019
924820
935120
945120
955120
965120
975120
985120
995119
1005119
1015119
1025120
1035119
1045219
1055220
1065219
1075220
1085320
1095319
1105319
1115419
1125320
1135419
1145520
1155620
1165420
1175419
1185520
1195420
1205420
1215519
1225520
1235419
1245520
1255720
1265720
1275820
1285720
1295720
1305720
1315720
1325720
1335820
1345720
1355820
1365720
1375920
1385820
1395820
1405920
1416020
1426020
1436020
1446020
1456020
1466020
1476020
1486120
1495920
1506120
1516020
1526020
1536120
1546020
1556120
1565920
1576220
1586320
1596320
1606220
1616320
1626320
1636320
1646320
1656220
1666320
1676520
1686420
1696620
1706619
1716620
1726520
1736720
1746620
1756719
1766620
1776720
1786820
1796820
1806620
1816820
1826820
1836721
1846721
1856720
1866720
1876820
1886720
1897620
1907420
1917420
1927420
1937420
1947520
1957419
1967620
1977420
1987520
1997520
2007520
2017620
2027620
2037520
2047520
2057820
2067820
2077820
2087620
2097820
2107820
2117820
2127820
2137820
2147820
2157820
2167820
2178020
2188320
2198020
2207820
2218520
2228121
2238220
2248020
2258120
2268120
2278120
2288120
2298220
2308320
2318320
2328120
2338219
2348320
2358220
2368120
2378421
2388420
2398320
2408320
2418320
2428521
2438420
2448320
2458420
2468420
2478421
2488620
2498420
2508720
2518820
2528320
2538920
2549020
2559020
2568820
2578920
2589220
2598920
2608720
2619020
2629120
2639120
2648920
2659120
2669220
2679020
2688920
2699220
2709320
2719220
2729220
2739320
2749420
2759320
2769120
2779421
2789320
2799321
2809120
2819320
2829620
2839620
2849221
2859620
2869720
2879821
2889521
28910320
2909920
2919620
2929520
2939721
2949720
2959720
2969520
2979821
2989921
2999820
3009520
3019821
30210020
3039820
3049720
3059820
3069920
3079820
3089720
3099920
31010020
3119920
3129721
3139920
31410020
31510020
3169821
31710220
31810420
31910620
32010220
32110320
32210320
32310420
32410320
32510420
32610520
32710520
32810220
32910320
33010520
33110420
33210220
33310621
33410720
33510620
33610420
33710720
33810920
33911220
34010620
34111020
34211120
34310820
34410720
34511120
34611221
34711120
34810720
34911020
35011120
35111020
35211020
35311121
35411220
35511020
35610920
35711120
35811120
35911120
36010820
36111120
36211220
36311021
36411020
36511221
36611320
36711221
36811020
36911621
37011320
37111321
37211120
37311320
37411320
37511420
37611020
37711420
37811420
37911321
38011020
38111920
38212121
38311721
38411520
38511721
38611821
38711721
38811520
38911820
39012121
39111921
39211720
39311920
39412120
39512021
39611821
39712420
39812420
39912521
40012321
40112220
40212420
40312620
40412020
40512320
40612420
40712321
40812020
40912220
41012321
41112320
41211721
41312221
41412520
41512320
41612020
41712520
41812521
41912520
42012120
42112321
42212420
42312521
42412221
42512320
42612520
42712620
42812220
42912521
43012521
43112720
43212520
43313120
43412721
43512620
43612320
43712721
43812821
43912821
44012420
44112720
44212921
44312820
44412321
44513221
44613220
44713321
44813220
44913721
45013321
45113120
45212820
45313120
45413320
45513121
45612920
45713220
45813321
45913220
46013020
46113420
46213620
46313721
46413620
46513520
46613720
46713820
46813421
46913720
47013821
47113821
47213421
47313920
47413920
47513821
47613421
47714020
47814020
47914021
48013721
48114020
48214121
48313821
48413620
48514021
48614120
48713921
48813721
48914120
49014021
49114121
49213721
49314320
49414321
49514220
49613921
49714320
49814221
49914221
50013921
Step 1 of 8 or use โ† โ†’ keys

Step 1 of 8

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 t (counting from 0) runs all p+t tokens through the model, so n new tokens after a p-token prompt take

โˆ‘t=0nโˆ’1(p+t)=np+n(nโˆ’1)2

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 , 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 . 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:

Try it

What the provider can reuse

Recorded ยท Qwen2.5-1.5B-Instruct

The Ch 24 session: six turns of chat, then you ask a question. Each row is the prompt the harness sends, drawn to scale. The shaded start of each row matches an earlier prompt exactly, so it can be read from the cache.

What the harness does
  1. Turn 1 0 of 163 cached
  2. Turn 2 163 of 258 cached
  3. Turn 3 258 of 343 cached
  4. Turn 4 343 of 437 cached
  5. Turn 5 437 of 525 cached
  6. Turn 6 525 of 616 cached
  7. Your question 616 of 633 cached
Tokens sent
2,975
Read from the cache
2,342 (79%)
Processed from scratch
633
Input cost, in full-price tokens
867

Simplified: cached tokens are priced at 10% of normal, about what providers charge, and the budget for dropping or summarizing is a small 400 tokens so it comes into play. Real providers also charge a little extra to write the cache and only cache prompts above a minimum length.

  • 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 new+0.1ร—cached 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, n tokens take np+n(nโˆ’1)/2 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

  1. Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
  2. OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
  3. Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
  4. Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.

Sources

  1. Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
  2. OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
  3. Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
  4. Anthropic (2026). Context windows. Claude Developer Platform documentation. Accessed October 25, 2026.
  5. Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.

Sources

  1. Reiner Pope et al. (2022). Efficiently Scaling Transformer Inference. arXiv preprint (MLSys 2023).
  2. Qwen Team et al. (2024). Qwen2.5 Technical Report. arXiv.
  3. OpenAI (2026). Prompt caching. OpenAI API documentation. Accessed October 23, 2026.
  4. Anthropic (2026). Prompt caching. Claude Developer Platform documentation. Accessed October 23, 2026.
  5. Anthropic (2026). Context windows. Claude Developer Platform documentation. Accessed October 25, 2026.
  6. Anthropic (2026). Pricing. Claude Developer Platform documentation. Accessed October 25, 2026.