Blog

Prompt caching in Claude Code: how it works, what it costs

Prompt caching makes re-sent context cheaper; context compression sends less of it. Cached tokens are still billed, still count against your plan and still fill the window. In a coding agent most of the bill is already cache reads, so the two stack, as long as compression does not break the cache.

98.6%
token cache hit rate
67.7%
of spend is still cache reads
72.3%
less on a 938-turn session through condense

How prompt caching works

The first time a prefix is seen it is billed as a cache write, a bit above normal input. Every later request with the same bytes is a discounted cache read. OpenAI caches long prompts automatically; see each provider’s pricing page for rates.

  • It is a prefix match. The cache helps only up to the first byte that differs.
  • It expires. About five minutes, refreshed on every read. Anthropic sells a one-hour tier at a higher write price.

What prompt caching saves, and what it does not

Across twelve coding sessions (18,333 assistant turns) almost nothing was raw input. Caching did its job, and the bill was still mostly cache reads.

Where the spend went
Twelve real coding sessions, by billing tier
  • cache read67.7%
  • cache write18.9%
  • output13.2%
  • uncached input0.2%
A discounted token re-read hundreds of times still adds up. From the anatomy of a token bill.
  • Still billed on every turn, just cheaper.
  • Still counts toward plan limits. See why Claude Code runs out of usage so fast.
  • Still fills the window, so caching does nothing to delay auto-compact.
  • Still distracts the model. Old logs are just as noisy from cache.

Why your prompt cache hit rate drops

Hit rate is cache_read_input_tokens over the sum of it, cache_creation_input_tokens and input_tokens. OpenAI reports cached tokens under prompt_tokens_details; /cost shows the Claude Code session total on an API key.

CauseWhat happens
Idle longer than the cache lifetimeThe whole prefix is written again on the next turn
Switching models with /modelCaches are per model, so the new model starts cold
Adding or removing MCP servers mid-sessionTool definitions sit near the top, so the prefix changes
/compact or auto-compactHistory is replaced by a summary that has never been cached
A tool that rewrites old messagesEverything after the first edited byte is a miss

What context compression does differently

Compression cuts the number of tokens, which goes after the 67.7% caching leaves behind and frees window space. But a compressor that rewrites history every turn turns cheap reads into writes. More in what is context compression.

Share of the bill removed
Same replays, priced cache-aware
condense
37.3%
Headroom
13.8%
kompress
1.8%
Tools advertising 80%+ token cuts landed at about 15% of tokens and at best 14% of the bill. From our quality evaluation.
Prompt cachingContext compression
What it reducesPrice per re-sent tokenNumber of re-sent tokens
SetupAutomatic in Claude Code and CodexA proxy, library or manual habits
Counts toward plan limitsYes, at the cached rateFewer tokens to count
Frees context windowNoYes
RiskMisses after idle or editsDropping information, breaking the cache

How the two levers multiply

Caching sets the price of a re-read; compression sets how much gets re-read. That only holds if the compressed prefix stays stable turn to turn. For a full bill, see Claude API pricing explained.

Tokens re-read on one turn
Illustrative, hypothetical session at turn 300
default
150k
compressed
60k
Same cache rate, 60% fewer tokens re-read. If the 60k changed every turn, it would be billed as cache writes instead.

Prompt caching vs compression: which saves more?

They are not alternatives. Do all three.

  1. 1
    Keep the cache warm

    Avoid long idle gaps, pick a model before you start, set up MCP servers before the session.

  2. 2
    Carry less by hand

    Shorter sessions and quieter tool output. See /compact vs /clear.

  3. 3
    Compress cache-aware

    condense trims new content with Helene before it is cached, and Adeline rewrites finished stretches once, then freezes them byte-identical. The system prompt, your messages and final answers are never rewritten.

SessionResult through condense
Under about 4k tokensWe lose money
Under about 20 turnsOverhead outruns the savings
Live run, about 179k tokens of context37.3% off the bill
938 turns, priced cache-aware72.3% under baseline

The saving grows with depth. If your sessions are short, caching alone is the right answer.

Frequently asked questions

Does Claude Code use prompt caching automatically?

Yes. Claude Code marks cache breakpoints for you, so the system prompt, tool definitions and history are cached with no setup.

Do cached tokens count against my Claude usage limit?

Yes. Cache reads are cheaper but still billed on the API and still count toward plan usage, which is why long sessions drain a subscription.

How long does the prompt cache last?

About five minutes, refreshed on every read. Anthropic also offers a one-hour tier at a higher write price.

What is a good prompt cache hit rate for a coding agent?

Very high: we measured 98.6% across twelve real coding sessions. If yours is much lower, look for idle gaps, model switches or edits to early history.

Does context compression break prompt caching?

It can. A tool that rewrites history every turn pays full write prices again; cache-aware compression trims new content before it is cached and rewrites old spans only once.

Is prompt caching or compression better for saving money?

Use both. Caching is already on; compression cuts the tokens the cache holds and re-reads every turn.

All posts