← Back to Blog

Why Prompt Caching Only Keeps the Beginning

Why Prompt Caching Only Keeps the Beginning
In this article

If you call language models through an API, prompt caching is the biggest discount on your bill. Send the same long system prompt again within a few minutes and the provider charges a fraction of the price for it: a tenth on most Claude and OpenAI models. In When the Fact Stops Being True I made an agent’s instructions three and a half times longer and its bill went down 41 %, because the longer instructions crossed the minimum size for caching. The same content with a block that changed every step moved to the front cost 5.2 times more.

That second number is the rule everybody runs into: only an identical beginning is reused. Change one token near the top and everything after it is read and billed again, even if it is the same text you sent a minute ago. It looks arbitrary. A retrieved document is the same document whether it comes first or fifth, so why can’t its cached copy be reused wherever it lands? I read what the research says and then measured it on an open model.

The short version

  • Every API caches only an identical prefix, and changing one token invalidates everything after it.
  • It isn't arbitrary. Moving a cached passage to another position can be corrected exactly. What can't be corrected is that every cached token already summarises the text it read before it, and a passage cached on its own never read what now sits in front of it.
  • That costs accuracy, by an amount that depends on the task. In my tests: nothing measurable when the passage doesn't need what comes before it, a quarter to two fifths of the score on questions that combine passages, and most of it on few-shot prompts.
  • For API users the rule stays: what never changes goes first; timestamps, IDs and the question go last.

Every API caches the prefix, and only the prefix

I checked the current documentation of seven providers: all of them reuse only an identical prefix. Anthropic, OpenAI, Google Gemini, AWS Bedrock, DeepSeek, Mistral and xAI differ in the details (the minimum size, how long an entry lives, whether writing to the cache costs extra), but none of them reuses anything that is not at the start.

The way they do it explains my 5.2x. The prompt is cut into blocks, and each block is stored under a hash of its own content plus the hash of the block before it. vLLM, the open-source serving engine, documents exactly this, and Anthropic describes its hash as cumulative. Change one token in block 2 and block 2 gets a new hash; block 3 includes that hash, so it misses too, and so does everything after it.

Request 1 instructionsh1 toolsh2 = H(h1, …) documenth3 = H(h2, …) chat so farh4 = H(h3, …) question Request 2: one date changed in the tools block instructionsh1 ✓ hit tools*h2' ✗ documenth3' ✗ chat so farh4' ✗ question same text, new hash: everything after the first change is read and billed again
Each block's key includes the key of the block before it, so one changed token invalidates every block after it, however identical their text.

Conclusion of this part: the chain of hashes is why order matters. But the chain is an engineering choice, and engineering choices can be changed. The real question is whether a cached block that now sits after different text would still be correct, and for that we have to look at what is actually cached.

What is cached, and the two things that break when you move it

What is cached is not your text, but the notes the model took while reading it. As a model reads a prompt, it computes for every token, at every layer, two vectors: a key, which later tokens use to decide whether this token is relevant to them, and a value, the information they take from it if it is. Together they are the KV cache, and reusing them is what prompt caching saves. Two things go into those notes besides the token itself.

The first is the token’s position, and it can be corrected exactly. Most open models mark position with RoPE: each key is split into pairs of numbers, and each pair is turned like a clock hand by an angle that grows with the token’s position, some pairs fast, some slow. To move a cached key from position 0 to position 1,000 you just turn every pair further, by the angle 1,000 positions would have added. On the model I used, encoding a passage directly at position 4,000 and encoding it at 0 and turning it gives the same keys, down to floating-point noise.

The second is what the token read, and that cannot. A token’s notes are computed by attending to every token before it: “Paris” after “The capital of France is” carries something of that sentence. A passage cached on its own never read the text that now sits in front of it, so its notes describe a reading that never happened in this prompt. No turning adds that; the only exact fix is to recompute the notes, which is the work caching was meant to save.

Position: a rotation each key is turned by an angle set by its position another pair of numbers, faster position 0 position 1,000 moving a cached key = turning it further exact, and nearly free What it read: not a rotation each token's notes summarise the words before it The capital of France is Paris in context: "Paris" looked back at the words before it Paris cached on its own: nothing before it to look at no turning adds what was never read only recomputing fixes it
Position is a rotation, so a cached key can be moved anywhere exactly. What a token read before it is baked into its notes, and no rotation puts it there.

Both effects are visible inside the model. The next figure compares, layer by layer, the notes of passages cached separately with the ones a full computation of the same prompt produces. At the first layer nothing has been read yet, so once the position is fixed the cached notes are exact. From the second layer on, every layer has read the one below, and the cached notes drift.

0.00.10.20.30.405101520253035layerdistance to full prefillkeys, position not fixedkeys, position fixedvalues (what the token read)
Fixing the position makes the first layer exact and leaves the rest drifting from what the model computes in context. The values are the same with or without the position fix, because rotation only touches keys. Without the fix the keys are far off, and yet, as the experiments below show, accuracy was the same either way.

Conclusion of this part: the prefix is the only reuse in which every cached note is exactly what the model would compute. Everything else is an approximation, and the question becomes how much accuracy it costs.

What research has tried

Several methods reuse cached passages out of position and patch the damage, all of them approximately.

  • CacheBlend (EuroSys 2025) turns the keys and recomputes the ~15 % of tokens whose notes drift most; it reports the first token arriving 2.2 to 3.3 times sooner than computing everything. It is the one method available in open-source serving, through LMCache.
  • EPIC (ICML 2025) recomputes only the first few tokens of each passage, which hog attention when a passage is cached as if it started the prompt.
  • APE (ICLR 2025) puts a shared prefix in front of every passage and rescales attention. It reports that reusing cached passages without any fix loses 49 % of the accuracy on few-shot tasks, prompts that teach the task with worked examples.
  • Block-Attention (ICLR 2025) and KVLink retrain the model so that passages cached separately work together. KVLink’s starting point is the clearest picture of the problem: on Llama-3.1-8B, answering Natural Questions from ten retrieved documents, the share of correct answers drops from 71.3 % to 28.9 % when the documents are cached separately and pasted.

Conclusion of this part: none of this is offered by an API, because the output is no longer exactly what you would get without the cache.

The experiment

I wanted to see the gap on one model myself, and to separate the part position explains from the part reading explains. I used Qwen2.5-3B-Instruct, a small open model that runs on two of Kaggle’s free GPUs, and wrote the reuse from scratch on Hugging Face Transformers. Before any run I checked that it is exact where it should be: a cached passage reused in its original position gives exactly the same output as computing it. The code, the tests and the raw results are on GitHub.

Three experiments, each with one question:

QuestionTest
AHow much does pasting cached passages cost, and what recovers it?Questions over ~10 Wikipedia passages, each cached separately
B1Does it matter what a passage was moved behind?The same passage placed first, after the passage it depends on, or after unrelated ones
B2What about examples in a few-shot prompt?16 or 32 worked examples, each cached separately

A: fixing the position recovers nothing

Question: when passages are cached separately and pasted, how much is lost, and which fix brings it back? The test uses 200 questions from HotpotQA and 200 from 2WikiMultihopQA, two benchmarks of “multi-hop” questions whose answer needs facts from two of about ten Wikipedia passages (around 5,500 tokens of context). The score is F1, the word overlap between the model’s answer and the correct one, from 0 to 1.

Answer: pasting cost 0.20 F1 on HotpotQA (0.50 → 0.30) and 0.10 on 2WikiMultihopQA (0.39 → 0.29), and fixing the position did not change it. The model gave the same answer as the full computation on only 36 % and 20 % of the questions.

HotpotQApasted, position fixed0.30 · 0 %EPIC-lite (16)0.35 · 2 %shared prefix0.34 · 0 %CacheBlend-lite 5 %0.37 · 5 %CacheBlend-lite 15 %0.41 · 15 %full prefill0.502WikiMultihopQApasted, position fixed0.29 · 0 %EPIC-lite (16)0.28 · 3 %shared prefix0.28 · 0 %CacheBlend-lite 5 %0.31 · 5 %CacheBlend-lite 15 %0.37 · 15 %full prefill0.39bar = F1 · label = F1 and share of the context recomputed
Fixing the position recovers nothing; patching the first tokens of each passage (EPIC-lite) or a shared prefix (APE-lite) barely helps. Recomputing the 15 % of tokens whose notes drift most (CacheBlend-lite) recovers about half of the gap on HotpotQA and four fifths on 2WikiMultihopQA.

What helps is recomputing tokens, chosen by how far their notes drifted: with 15 % of the context recomputed, F1 comes back to 0.41 and 0.37. That is a real saving over computing everything, but it is not free, and it does not close the gap.

The obvious objection is that a 3B model is weak, and its scores are indeed low. But the comparison is paired: same model, same questions, same tokens, only the cache differs. The drop is the cost of the reuse, and the published results on larger models point the same way.

B1: what a passage sits behind matters a little

Question: does a cached passage lose more when what now precedes it is text it should have read? The passage with the answer is cached once, then placed first, after the passage it depends on (the one that names who or what the question is about), or after unrelated passages.

Answer: a little, and only as a trend. Placed first, it lost nothing. After the passage it depends on, or a related one, it lost 0.05 to 0.07 F1; unrelated passages in between changed nothing. The difference is not large enough to survive the correction for the number of comparisons I made, so I read it as the direction the mechanism predicts, not as a result.

B2: few-shot examples are where it breaks

Question: what happens when the cached pieces are the worked examples of a few-shot prompt? The test is TREC, classifying questions into six types (“is this asking for a number, a place, a person…”), with 16 or 32 examples in the prompt, each cached on its own, in three different orders.

Answer: accuracy collapses, from 0.80 to 0.36 with 16 examples and from 0.86 to 0.24 with 32, and the model stops answering in the format the examples teach. What brings it back is giving every example the same start: caching each one after the task instruction keeps 0.75 and 0.73.

0.000.250.500.751.0016 demos0.800.360.470.700.7532 demos0.860.240.300.650.73full prefillpasted, position fixedCacheBlend-lite 15 %shared positions (APE-style)instruction before each demoaccuracy
Pasting separately cached examples loses most of the accuracy, more with more examples; CacheBlend-lite, which helped on questions, recovers little. Caching each example after the same task instruction keeps most of it.

Few-shot examples are written to be read as a sequence: each one says “here is another like the last”. An example cached alone never saw the others, and loses exactly that.

I tried to go beyond the prefix

With the cause in hand, I tried six ideas for reusing more than the prefix. Under the criteria I had fixed in advance, only one gave a usable gain, and only inside a boundary. All of it is one model, Qwen2.5-3B-Instruct, in half precision (fp16) and always taking the most likely token. The full tables and the caveats are in the repo’s results.

The idea that held is reusing the old reading when a block in the middle changes. It is the real API case: instructions, a header with the date and a request ID, documents, question. The header changes on every call, and today everything behind it is recomputed. I kept the documents’ notes as the previous call had computed them, when they read the old header, and recomputed only the new header.

When the question asks for the value that changed, the reuse gets it right. It gave the new value in all 199 cases where the full computation gives it, and never the old one. A header that has nothing to do with the question changed nothing either (F1 0.450 against 0.451), but a date and an ID barely matter to those questions anyway.

The bigger and the earlier the edit, the more answers move. I replaced a block of prose of about 30, 200 or 800 tokens, on 84 questions. Placed before all the documents, the answer stayed the same text as the full computation in 86 % of the questions at 30 tokens and in 79 % at 800. Placed in the middle of them, in 98 % and 87 %. The F1 differences are small, the largest −0.044 [−0.093, −0.001] at 800 tokens in front, but with 84 questions I cannot call any of them equivalent. Part of that movement is not the cache: sending the entire old prompt, old block included, kept the same text in 79 % to 95 % of the questions across the six cases, so the replaced text itself matters to these answers.

It is much faster, in research code. Rebuilding the context took 3.2 to 4.2 seconds in full, and 0.05 to 0.44 reusing the old reading. Today’s exact prefix reuse took about 2 seconds when the edit is in the middle, and as long as the full computation when it is in front. These are times of my code on a T4 GPU, not a serving number.

It breaks when later text depends on the block. The block was a line that defines a code name the documents use, or that says which of the two currencies on an invoice counts. With the old reading reused, the answer that went with the old line came out in 180 of 189 cases. Recomputing the 15 % of tokens that drift most, the fix that helped in experiment A, still left 158.

It repeated on 300 fresh cases, and in one of the two I found where the old answer lives. The old answer came out in 97 of 97 cases for the code name and in 60 of 64 for the currency. In the currency case, recomputing only the sentences that hold the two amounts, about 2 % of the reused tokens, left the old answer in 1 of 64. Recomputing the same number of tokens elsewhere left 60 of 64: the old choice lives in the notes of those sentences. In the code-name case the same targeted repair removed only 6 of 97, so there I did not find it. And when the edit removes the old value from the text, a name replaced by another, the old name came out in 0 of 98.

The reuse itself is already published; what I add is the boundary. PIE corrects the positions of the cached keys after an edit in the middle of a code file, and PatchKV repairs the cache behind an edited block of the prompt. My part is where reusing the cache as it is stops being safe on this model, and that in one case the old answer sat in about 2 % of the tokens.

The other five ideas:

What I triedWhat happened
Recomputing the upper layers of every token, instead of some tokens at every layerAt about the same cost in token-layer work (not measured time), F1 0.286 against 0.414 for recomputing 15 % of the tokens
Storing one correction per pair of passages and combining themAdded up, F1 0.001 against 0.507 for the full computation. Averaged, the cache got closer to the true one, but F1 did not clearly move
Caching each passage after a generic filler paragraph, so that it has read somethingDid not show a benefit: +0.019 F1 [−0.023, +0.060]
Letting each token attend only to its strongest links, with a proof of which rows an edit leaves untouched1–3 % of the work behind the edit survives
Rebuilding the non-attention part of each layer (the MLP) from a few neuronsIn a simplified version of the idea, F1 fell from 0.547 to about 0.07

Conclusion of this part: in these tests, what decided was what each cached piece had read, and whether that was still what sat in front of it.

What this means if you call models through an API

The prefix rule is not a limitation the providers forgot to remove. It is the only reuse that gives you the same output; everything beyond it trades accuracy for speed, by an amount that depends on how much each piece needs to know what came before it.

So the practical rules stay what the documentation says, and now they have a reason:

  • What never changes goes first: instructions, tool definitions, reference documents. Timestamps, request IDs, the user’s question and anything that changes per call go last. One changing block in front, as in my agent, invalidates everything behind it.
  • Freeze the tool definitions and serialise them the same way every time. Some languages order JSON keys randomly, which breaks the cache without changing a word.
  • Append, don’t rewrite. Editing or summarising earlier turns resets the reuse from that point on.
  • Check that you are above the minimum (4,096 tokens on Claude Haiku 4.5, 1,024 on OpenAI’s current models) in the usage fields (cache_read_input_tokens, cached_tokens). Below it there is no error, just no discount.

If you serve your own models, reuse beyond the prefix exists today, CacheBlend in LMCache. Measure it on your own task before turning it on: in this experiment the same method recovered four fifths of the gap on one dataset and little of it on few-shot classification.


Code, validation tests, raw results and the analysis: github.com/JaviMaligno/kv-cache-beyond-prefix. One model (Qwen2.5-3B-Instruct), contexts up to about 6,000 tokens, simplified re-implementations of EPIC, APE and CacheBlend; the cost is reported as the share of tokens recomputed, not as a measured speed-up.