Sub topic 3
Why Only an Identical Prefix Can Hit
This chapter assumes you can call an LLM API, know what tokens and context windows are, and can read short Python snippets. It also assumes you know that a provider stores reusable attention state (the key/value cache) from earlier prompts. Here you will see why that stored state can only be reused when the start of a new prompt matches the start of an old one exactly.
The working model
A model reads a prompt as a sequence of tokens, left to right. The attention state stored for each token depends on every token before it. So the saved state for token 500 is only valid if tokens 1 to 499 are the same as when it was computed.
This gives one rule: a cache hit covers the longest run of leading tokens that is identical to a cached prompt. Everything after the first difference must be processed again, even if the later text is the same.
The match is also exact. It is not semantic, so a reworded instruction that means the same thing counts as a different prompt. A single changed character, an extra space, or a different line ending counts as a difference.
A toy model
The function below treats a prompt as a list of pieces and counts how many leading pieces two prompts share. Real providers compare tokens (often in blocks), but the behavior is the same.
def shared_prefix_len(a, b):
n = 0
for x, y in zip(a, b):
if x != y:
break
n += 1
return n
first = ["SYSTEM", "RULES", "DOC", "Q1"]
second = ["SYSTEM", "RULES", "DOC", "Q2"]
changed = ["SYSTEM", "RULES v2", "DOC", "Q2"]
print(shared_prefix_len(first, second)) # 3 -> three leading pieces are reusable
print(shared_prefix_len(first, changed)) # 1 -> one early edit invalidates everything after itCompare the two results:
firstandseconddiffer only in the last piece, so three of four pieces can be reused.firstandchangeddiffer at position 2.DOCis identical in both prompts, but it comes after the edit, so it cannot be reused. OnlySYSTEMis.
What this means in practice
The position of a change matters more than its size. A one-word edit near the top costs more than a paragraph replaced at the end.
| Change | Where it happens | Reusable part |
|---|---|---|
| New user question | End of prompt | Almost all of it |
| Updated policy text | Middle of prompt | Everything before the policy |
| Timestamp at the top | Start of prompt | Nothing |
| Tools reordered | Before the system prompt or messages | Nothing, or very little |
Some details that follow from the model:
- Order counts. Swapping two identical-length sections changes the prefix at the first swapped position.
- Hidden differences count. Trailing whitespace, a different JSON key order, or a regenerated ID all break the match.
- A match must reach a boundary the provider supports. Some providers compare in fixed-size blocks, or only at breakpoints you set. Later chapters cover those rules.
Reasoning about your own prompts
To predict hits, write your prompt as an ordered list and ask, for two consecutive requests, "where is the first difference?" Everything before that point can hit. Everything from that point on cannot.
Then ask which parts differ between requests. Anything that varies per request belongs after everything that does not. The next chapters build on this when they cover prompt layout.
Exercise
A prompt is built as [SYSTEM, TOOLS, DOC, HISTORY, QUESTION]. On the second request, the HISTORY is identical but the DOC was edited by one word. Which pieces can be reused from the cache, and why?
Show answer
Only SYSTEM and TOOLS can be reused. The DOC edit is the first difference, and the attention state for HISTORY and QUESTION depends on all earlier tokens, including the edited word. HISTORY is identical text, but it follows the change, so it must be processed again.
Exercise
Using sharedprefixlen, what does sharedprefixlen(["A", "B", "C"], ["B", "C"]) return, and what does this show about removing the first piece of a prompt?
Show answer
It returns 0. The first pieces, "A" and "B", differ, so no leading piece matches. Removing or shifting the first piece destroys the whole prefix, even though "B" and "C" still appear in both prompts.