Your coding agent probably resends the same few thousand tokens on every turn: tool definitions, a system prompt, project rules. Prompt caching lets the provider reuse that work instead of recomputing it, but only if the start of your prompt stays byte-for-byte identical between calls.
What prompt caching actually does
When a request comes in, the provider checks whether the beginning of your prompt matches something it processed recently. If it does, that part is served from cache: it's faster and billed at a reduced rate. Everything after the first difference is processed normally.
The key word is prefix. OpenAI's docs say cache reuse requires the entire rendered prefix to match. Anthropic's docs say a cache hit needs "100 percent identical prompt segments" up to the cached point. A cache is not a fuzzy similarity search. One changed character early in the prompt means everything after it is a miss.
The prefix is built in a fixed order. Anthropic spells it out: tools, then system, then messages. So on Claude a change to your tool list invalidates everything after it, even if your system prompt and conversation are untouched.
How each provider turns it on
OpenAI: the docs say caching is enabled by default for supported models. There is a minimum prompt length before anything is cached (1,024 tokens on current models; check the guide for the model you use). You can see hits in usage.input_tokens_details.cached_tokens.
Anthropic (Claude API): you opt in with cache_control, either as one top-level field (automatic caching) or as explicit breakpoints on content blocks, up to 4 per request. The default cache lifetime is 5 minutes, refreshed each time it's used, with a 1-hour option. Writing to the cache costs more than normal input and reading from it costs much less; the exact multipliers and the minimum cacheable length vary by model, so read the pricing table for yours. Hits show up as cache_read_input_tokens, and writes as cache_creation_input_tokens.
If you use an agent or IDE tool rather than the API directly, it makes these choices for you. You still control the inputs it puts at the front: rules files, custom instructions and which tools are enabled.
The things that quietly break the prefix
Common cache misses come from content that looks static but isn't:
- Timestamps in the system prompt. "Current time: 10:42:07" at the top changes every call.
- Unstable ordering. Tools or files serialized from a dictionary or a directory listing in a different order each run.
- Per-user or per-request IDs injected near the top "for context".
- Toggling tools mid-session. Enabling one extra tool rewrites the very first section of the prefix.
- Editing an early message in the history instead of appending a new one.
Anthropic's docs also list settings whose changes invalidate parts of the cache, such as tool definitions, tool_choice, and whether images are present. It's worth a skim before you tune.
Before and after
Here's a typical pattern that defeats the cache:
# Before: dynamic data first, so the prefix changes on every call system = f"""Today is {datetime.now().isoformat()}. User: {user.id}, repo: {repo.name} You are a code review assistant. {LONG_STYLE_GUIDE} # ~3,000 tokens, never changes {TOOL_INSTRUCTIONS} # ~1,500 tokens, never changes """ messages = [{"role": "user", "content": diff}] The style guide and tool instructions never change, but they sit after a timestamp, so they're never part of a matching prefix.
# After: stable content first, volatile content last system = f"""You are a code review assistant. {LONG_STYLE_GUIDE} {TOOL_INSTRUCTIONS} """ messages = [{ "role": "user", "content": f"Date: {date.today()}\nRepo: {repo.name}\n\n{diff}", }] Same information, different order. Now the long stable block can be reused across every review. Both OpenAI's guide and Anthropic's guide recommend this layout: static content at the beginning, variable content at the end. Two smaller details help too: using the date instead of a full timestamp, and moving the user ID out entirely, since the model didn't need it.
Check that it's working
Don't assume; read the usage numbers. Send the same request twice within a minute and compare:
- On OpenAI,
cached_tokensshould be non-zero on the second call (once you're over the minimum length). - On Claude, the first call should show
cache_creation_input_tokensand the secondcache_read_input_tokens.
If both cache fields stay at zero, either the prompt is under the minimum length for that model, or something early in the prefix is changing. Diff the two raw requests and look at the first difference. That's your culprit.
Log these fields per request in your agent loop. A sudden drop in cache reads after a deploy usually means someone added something dynamic near the top.
Takeaways
- Caching matches on an exact prefix, not on similar content.
- Order matters (on Claude: tools, then system, then messages). Put stable content first and volatile content last.
- Watch for timestamps, IDs, unstable ordering and tool toggles near the top.
- Verify with the usage fields (
cached_tokens,cache_read_input_tokens) rather than guessing. - Minimum lengths, lifetimes and prices differ by model, so check the official docs for yours.
Official references: OpenAI prompt caching guide and Anthropic prompt caching docs.
What's the sneakiest thing you've found breaking your cache hit rate?
Written with AI assistance and reviewed before publishing.