Topic 3 of 5 · 5 min read
How prompt caching cuts agent cost
Goal: Explain from a usage log why cache hits dropped in a session and name the change that restored them.
Prompt caching lets the provider reuse the unchanged start of your request at a fraction of the normal input price, so keep the start of every request identical and put anything that changes at the end. Agents send the same system prompt, tool definitions, memory file and early conversation on every turn. Without caching you pay full price to resend them each time. With caching the repeated part is read from cache, which is cheaper and faster.
Check yourself · 1 of 3
For prompt caching to work, which part of a request must stay identical between turns?
Why it matters
In a long agent session, most input tokens are repeats. A good cache hit rate is the single biggest lever you do not have to think about, as long as you do not break it. Claude's API, OpenAI's API and others all cache by prefix: the cache matches only if the beginning of the request is byte-for-byte the same.
How it works
- Order matters. A request is built as tools, then system prompt, then memory files, then the conversation. Everything before the first change can be a cache hit. Everything after it is processed again.
- Writes cost a little extra, reads cost much less. The first request writes the cache. Later requests read it. See the vendor's pricing page for the exact multipliers.
- Caches expire. They live for minutes, not hours, and are refreshed each time they are read. A long pause, such as lunch, can mean the next turn pays full price again.
- Usage fields show it. API responses report cache creation and cache read tokens next to normal input tokens. Read those to see what happened.
What breaks the cache
- Editing your memory file (
AGENTS.md,CLAUDE.md) in the middle of a session. - Adding, removing or reordering tools or MCP servers, since tool definitions sit at the start.
- Putting a timestamp or random id near the top of the system prompt.
- Switching model, which uses a separate cache.
Before and after
turn 1 input=48,000 cache_write=48,000 cache_read=0
turn 2 input=1,200 cache_write=0 cache_read=48,000
turn 3 input=1,100 cache_write=0 cache_read=49,200
turn 4 input=51,500 cache_write=51,500 cache_read=0 <- hit rate collapsed
Turn 4 re-wrote the whole prefix. The log of that turn shows a new MCP server was enabled just before it, which changed the tool list at the very front. Disabling it for the session, or enabling servers only before the first turn, restores reads on turn 5.
What goes wrong
- Assuming caching is always on and never checking the usage fields.
- Tweaking the memory file during the session and paying a full rewrite each time.
- Putting changing data, like the current time, first instead of last.
Examples
Reading a drop in cache hits
turn 5 input=900 cache_read=51,500 cache_write=0
turn 6 input=900 cache_read=51,500 cache_write=0
turn 7 input=53,100 cache_read=0 cache_write=53,100
Turn 7 paid for a full rewrite. Nothing in the conversation changed, but the user had edited CLAUDE.md between turns 6 and 7, which sits near the front of the prefix. The fix is to make memory edits at the start of the next session, and to keep the file out of mid-session changes.
Key points
- Caches match on an identical prefix, so stable content goes first and changing content goes last.
- Cache reads cost a fraction of normal input, and cache writes cost slightly more, so reuse pays after the first turn.
- Changing the tool list, the memory file, the model or the top of the system prompt invalidates the cached prefix.
- Caches expire after a short idle time, so a long pause can make the next turn full price.
- Check the cache creation and cache read fields in usage output to confirm hits instead of assuming them.
Common mistakes
- Editing the memory file or toggling tools in the middle of a session and not noticing the cache rewrite.
- Placing a timestamp or session id at the top of the prompt, which makes every request a new prefix.
- Never reading the usage fields, so a collapsed hit rate goes unseen for weeks.
Check yourself · 2 of 3
A usage log shows cache reads of about 50,000 tokens per turn, then one turn with a full cache write. The user had just connected a new tool server. Most likely cause?
Check yourself · 3 of 3
Which habit best protects cache hits in a long session?
Practice items, not counted towards your mastery.
Start here
- Manage costs effectively (Claude Code docs) (opens in a new tab)DocsFree
Vendor guidance on checking usage, trimming context, choosing models and setting team spend limits.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How cache prefixes work, what invalidates them, and which usage fields report cache reads and writes.
- Monitoring usage (Claude Code docs) (opens in a new tab)DocsFree
The metrics and events an agent exports so a team can see tokens and cost per user and per model.
More for this topic
- Prompt caching (OpenAI API docs) (opens in a new tab)DocsFree
A second vendor's take: automatic prefix caching, so the same stable-prefix rule applies outside Claude.