Skip to content
mentoor

Topic 4 of 5 · 4 min read

Changes that silently break the cache

Goal: Find the change in a diff that invalidates the cache and propose a fix that keeps the prefix stable.

The cache breaks whenever any token before your breakpoint changes, so the bugs to look for are small edits near the top of a prompt: a timestamp, a reordered list, a per-user string, an edited message. They pass code review because the app still works.

Check yourself · 1 of 3

Which change to a shared system prompt is most likely to destroy cache hits?

Choose one answer

Why it matters

One innocent line can turn a 90 percent hit rate into zero. The cost is paid on every request until someone looks at the usage data.

The usual suspects

  1. Dynamic text in the system prompt. A date, time, request id, user name or feature-flag dump.
  2. Unstable ordering. Tools built from an unordered set or dictionary, or JSON serialised with a different key order. The content is the same but the bytes are not.
  3. Per-user data in the shared prefix. If the prefix includes the customer's name or profile, each customer gets a different cache entry, and shared reuse is gone.
  4. Changed tool definitions. Adding, removing or editing a tool changes the prefix from the tools onward. On Claude, the docs list which changes invalidate which cache levels.
  5. Edits to earlier history. Trimming old messages, rewriting a past turn or reformatting results changes everything after the edit.
  6. Short lifetime. A route used less often than the TTL, or traffic gaps at night, make the cache expire between calls.
  7. Switching models or settings. A different model, or other parameters the provider lists as cache-relevant, starts a new cache.

How to review for it

In a diff, ask for each changed line that feeds the prompt: does this text go before the breakpoint, and does it vary by call or by user? If yes, either move it after the breakpoint or remove it. Then check the hit rate after deploy.

Before and after

-SYSTEM = f"You are Acme support. Today is {date.today()}. Rules: {RULES}"
+SYSTEM = f"You are Acme support. Rules: {RULES}"
 
-messages = [{"role": "user", "content": question}]
+messages = [{"role": "user", "content": f"Today is {date.today()}.\n{question}"}]

The date moved out of the prefix, so the rules block is the same bytes on every call.

What goes wrong

  • Fixing the date but leaving a per-request id three lines lower in the same prefix.
  • Sorting tools in the code, then having another service send them in its own order.
  • Declaring the fix done without checking the hit rate in production.

Examples

The innocent refactor

-tools = [search_tool, calendar_tool, email_tool]
+tools = list({email_tool, search_tool, calendar_tool})

Building the list from a set gives an order that can differ between runs. The tool block is now different bytes on some calls, so the prefix often misses. Fix: use a list with a fixed order.

Per-user data in the prefix

System: You are Acme support. The customer is Maria Lopez, plan Pro. Rules: ...

Each customer creates a separate cache entry for the same rules. Move the customer line into the user turn and keep the rules identical for everyone.

Key points

  • Any changed token before a breakpoint invalidates the cache from that point on.
  • Timestamps, request ids and user names in a shared prefix are the most common cause.
  • Reordered tools or JSON keys break the cache even when the meaning is the same.
  • Editing or trimming earlier history changes every token after the edit.
  • Confirm every prompt-related change by checking the hit rate after the deploy.

Common mistakes

  • Approving a prompt change because the output looks the same, without checking cost.
  • Fixing one volatile line and missing a second one in the same prefix.
  • Building tool lists or JSON from unordered collections.
  • Blaming the provider for a low hit rate before checking the prompt for dynamic text.

Check yourself · 2 of 3

A diff changes tool setup from a fixed list to 'list(set(tools))'. Hit rate drops from 90 to 20 percent. What is the likely cause?

Choose one answer

Check yourself · 3 of 3

A team trims the oldest messages from long chats to save tokens, and cache reads vanish on those chats. Why?

Choose one answer

Practice items, not counted towards your mastery.

Start here