Sub topic 11
Finding and fixing the changes that break your cache
This chapter assumes you know how exact prefix matching works, how to read the three usage counters (tokens written, read, and uncached), and how to compute a hit ratio. Here you will use those tools to find out why a cache that should hit does not.
The symptom
A broken cache looks the same every time. The hit ratio stays near zero, and the "written" counter is high on every request instead of only the first. The provider keeps storing a prefix that nobody reads, because each request's prefix differs from the last one.
Prefix matching means the first changed token ends the reusable part. So the question to ask is always: where does the first difference between two consecutive prompts sit?
Common causes
| Cause | What changes | Fix |
|---|---|---|
| Timestamp or date at the top | The first line differs on every call | Move it after the static text |
| Request ID, user name, or session ID early in the prompt | The prefix differs per user | Put per-request values last |
| Tools reordered or edited | Tool definitions usually come before the system prompt and messages | Sort tools in a fixed order and version them deliberately |
| Edited or summarized history | An old turn changes, so every later turn is a miss | Append new turns only |
| Non-deterministic serialization | The same data produces different text, such as unordered dictionary keys or varying whitespace | Serialize with a fixed key order |
| Prompt below the minimum length | The prefix is too short to be cached at all | Check the model's published minimum |
| Entry expired | Requests arrive further apart than the cache lifetime | Send requests more often, or accept the miss |
Find the first difference
Do not guess. Log the exact prompt text for two consecutive requests that should have shared a prefix, then locate where they diverge.
def first_difference(a, b, context=40):
for i, (x, y) in enumerate(zip(a, b)):
if x != y:
start = max(0, i - context)
return i, a[start:i + context], b[start:i + context]
if len(a) != len(b):
return min(len(a), len(b)), "", ""
return None
prompt_1 = "Policy v1.\nCurrent time: 10:00:01\nQuestion: refunds?"
prompt_2 = "Policy v1.\nCurrent time: 10:00:09\nQuestion: refunds?"
print(first_difference(prompt_1, prompt_2))The output points at character 28, inside the timestamp. Everything after that point is unreusable, even though the question is identical.
Compare the serialized request body, not your template. A template can look stable while the rendered text still varies, for example when a dictionary is printed in a different order.
A diagnostic flow
Check one thing at a time
After each fix, send the same request twice and read the counters. The first call should show tokens written. The second should show tokens read. If the second call still reports a write, the prefix is still changing, and you should diff again.
def check_cache(first_usage, second_usage):
wrote = first_usage["written"] > 0
read = second_usage["read"] > 0
return "ok" if wrote and read else "still missing"
print(check_cache({"written": 4000, "read": 0}, {"written": 0, "read": 4000}))
print(check_cache({"written": 4000, "read": 0}, {"written": 4000, "read": 0}))Keep it fixed
Add a test that renders your prompt twice with different per-request values and asserts that the static part is byte-identical. Log the hit ratio per request and alert when it drops. A sudden fall usually follows a deploy that touched the prompt.
Exercise
Your hit ratio was 0.95 until yesterday and is now 0.0, and every request reports a large "written" count. A deploy changed the prompt template to start with "Request ID: {id}". Why did the cache stop hitting, and what is the fix?
Show answer
The request ID differs on every call and now sits at the start, so the first token of the prompt changes and no prefix can match. Move the request ID after the static instructions, at the end of the prompt, and confirm with two identical requests that the second shows tokens read.