Claude Prompt Caching: Why Agent Loops Miss the 20-Block Lookback
Your agent starts a run with cache_read_input_tokens at 40K and climbing. Twelve tool calls later, reads drop to zero and cache_creation_input_tokens jumps to the full conversation length — on every single turn. Nothing in your prompt changed. No timestamp, no reordered tool, no model switch. The prefix is byte-identical. You just hit the 20-block lookback window, and it is the single most…
Claude Prompt Caching can fail when an agent is looping, due to a 20-block lookback window. Each turn appends 1 thinking block, 1 text block and N tool_use blocks. For an agent with 8 parallel tool calls, that's 18 blocks per round trip. Two turns make 36 blocks past the cached point, causing a silent miss.
The issue isn't with the prompt itself - cache lookup is limited to 20 blocks. If the previous block is more than 20 blocks away, the cache lookup fails and the API treats the request as cold. Chat apps don't see this because they don't have loops.
To fix this, place multiple breakpoints throughout the message list, spaced about 15 blocks apart. Use 4 breakpoints total across tools, system and messages, spending 1 on the system block and rotating the other 3. Each breakpoint acts as both a write and a read point, ensuring a prior entry is always found within 20 blocks.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.