A Waste Worth Fixing
Every message you send to Claude or GPT triggers a full prefill: the model re-encodes the system prompt, tool definitions, conversation history, and new input from scratch. In a 20-turn conversation, the first turn is re-encoded 20 times. Prompt Cache exists to kill this redundancy.
How It Works: Prefix Matching
Mark a breakpoint in your request. The backend stores the encoding from start to breakpoint. On the next request, if the prefix is byte-identical, the cached prefix is reused and only the new suffix is computed. Similar is not enough—*identical* is the rule. One changed character invalidates everything after it.
The Savings Are Real
- Cached tokens cost roughly 10% of fresh-token price.
- First write costs ~25% extra; subsequent hits save ~90%.
- Default TTL: 5 minutes, auto-renewed on each hit.
- Minimum cacheable length: 1024 tokens (older models) or 4096 tokens (newer models).
- Lazy loading: group tools by scenario; load only what's needed so the prefix stays shorter and more stable.
- Cache-safe forking: when compressing, summarizing, or branching a conversation, the fork must share the same prefix—same model, same system prompt, same tool definitions—or it rebuilds from zero.
- Any change anywhere in the prefix invalidates everything after it.
- Use messages, not instructions, for dynamic content.
- Don't swap tools or models mid-session.
- Monitor hit rate like uptime.
- Forks must share the parent's prefix.
Example: a 100k-token conversation on Claude Sonnet runs about $0.30/turn without caching, $0.375 first turn with caching, then $0.03/turn. Over 10 turns, input cost drops by roughly 90%, and TTFT (time-to-first-token) improves noticeably because less has to be recomputed.
Anthropic monitors cache hit rate as production infrastructure—on par with uptime. Drop in hit rate triggers a formal incident response.
The Four-Layer Desk
Order matters because of prefix matching. Anthropic's recommended stack:
1. System instructions + tool definitions — fixed, shared across sessions. 2. Project docs (e.g., CLAUDE.md) — shared within a project. 3. Session context — valid only in this conversation. 4. Chat messages — grows turn by turn.
Rule of thumb: *the less often something changes, the earlier it goes.*
Three Cache-Breaking Traps
Trap 1 — Embedding time in the system prompt. A Today is {datetime.now()} field changes every second and breaks the prefix. Fix: keep system instructions frozen and inject updates into the conversation via <system-reminder> tags.
Trap 2 — Unordered tool containers. Python dict or set iteration order can vary between requests. Use ordered list.
Trap 3 — Mutating tool schemas. Adding one parameter or changing a field type invalidates the entire prefix chain, since tool definitions live at layer 1.
Counter-Intuitive: Don't Switch Models
Caches are model-bound. Switching to a smaller model mid-session wipes all accumulated cache, and rebuilding it often costs more than just letting the large model answer the easy question.
Claude Code's solution: keep the main conversation on one model. For lightweight work, dispatch sub-tasks with their own isolated context and cache. Write a concise handoff briefing, let the sub-task run, and return only the result—like giving an intern a separate machine instead of swapping them into your seat.
Plan Mode Without Breaking Cache
Intuition says: enter plan mode → remove execution tools; exit plan mode → add them back. Anthropic does the opposite. The tool set stays frozen, and two extra tools (enter_plan_mode, exit_plan_mode) handle transitions. The "no execution" constraint is delivered as a system message inside the conversation flow, not as a system-prompt edit. Bonus: the model can self-trigger plan mode when it senses a complex task.
Lazy Loading and Cache-Safe Forking
A Note for Relay Services
Cache is account-isolated. Pooling accounts across requests tanks the hit rate. Auto-rotating accounts every couple of messages has the same effect.
The One Rule Behind Everything
All ten lessons reduce to one constraint: cache equals prefix matching.
---
*Based on the 515b759 commit of the easy-learn-ai project, which offers an interactive, animation-driven, 15-chapter walkthrough of Prompt Cache design—inspired by Anthropic engineers' talks on Claude Code's caching architecture.*