Agents, Tool Use & MCP·Question 33 of 90Intermediate

Your agent's prompt cache hit rate is 20% when you expected 90%. Walk me through the debugging.

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Prompt caching works on stable prefixes. Any byte that changes at position N invalidates everything from N onward. In an agent loop the prefix should be system prompt, then tool definitions, then a strictly append-only message history. Anything mutating earlier content destroys the cache for the rest of the trajectory. So I go looking for what is mutating.

Full worked answer with diagram

Prompt caching works on stable prefixes. Any byte that changes at position N invalidates everything from N onward. In an agent loop the prefix should be system prompt, then tool definitions, then a strictly append-only message history. Anything mutating earlier content destroys the cache for the rest of the trajectory. So I go looking for what is mutating.

The usual culprits, roughly by frequency:

  1. A timestamp or session ID in the system prompt. "Current time: 14:32:07" at the top means a guaranteed miss on every call. Move volatile values to the end of context or into a tool result.
  2. Non-deterministic tool definition ordering. Serialising tools from a dict or set can reorder them between calls. Identical in meaning, different in bytes. Sort them.
  3. Compaction. The big structural one. The moment you summarise turns 1 to 20 and replace them, you have rewritten the prefix and everything after is a miss. Compaction is still worth it, but budget for a cache reset at each boundary, which argues for compacting rarely and in large chunks rather than trimming a little every turn.
  4. Clearing stale tool results in place. Same problem, sneakier, because it feels like a small edit. Editing turn 3's tool result invalidates turns 4 onward.
  5. Cache TTL expiry. Default cache lifetimes are short, often around five minutes, and longer retention is an opt-in that some providers charge extra for. An agent that waits twenty minutes on a human approval comes back to a cold cache unless you chose the longer TTL. If you have long human-in-the-loop pauses, that gap is your miss.
  6. Provider mechanics. Some providers cache automatically on prefix match; others require explicit cache breakpoints and enforce a minimum cacheable length. Below the minimum, or with breakpoints in the wrong place, you get nothing.

The fix that covers most of it: treat context as an append-only log, put everything volatile at the tail, and make serialization deterministic. Then verify with the cached-token counts the API returns per call rather than trusting the design.

Worth sketching. Draw the prefix as a line and mark where each culprit writes into it: everything downstream of the mark is a miss.

flowchart LR
    A["System prompt"] --> B["Tool definitions"]
    B --> C["Messages 1 to 20"]
    C --> D["New turn appended<br/>(the only safe write)"]
    E["Timestamp in the system prompt"] -->|"invalidates everything after"| A
    F["Compaction or an edited<br/>tool result at turn 3"] -->|"invalidates everything after"| C

What a good answer covers

  • Prompt caching works on stable prefixes
  • Any byte that changes at position N invalidates everything from N onward
  • In an agent loop the prefix should be system prompt, then tool definitions, then a strictly append-only…
  • Anything mutating earlier content destroys the cache for the rest of the trajectory
  • So I go looking for what is mutating

Key terms: stable prefixes · Non-deterministic tool definition ordering · Compaction · Cache TTL expiry · Provider mechanics

Interviewer follow-ups to expect

  1. You must inject the current time because the agent reasons about deadlines. Where does it go?

  2. How would you measure cost per successful task rather than cache hit rate?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep