Dev.to AI πŸ€– Ai πŸ‘ 2 πŸ“– 7 min read

Claude Prompt Cache Expires in 5 Minutes: How to Stop Paying 25x After a Break

You're 210,000 tokens deep in a Claude Opus 5.5 agent session. You go make coffee. Eight minutes later you type "ok, now fix the failing test", and the turn that would have been a $0.04 cache read gets billed as a $1.05

You're 210,000 tokens deep in a Claude Opus 5.5 agent session. You go make coffee. Eight minutes later you type "ok, now fix the failing test", and the turn that would have been a $0.04 cache read gets billed as a $1.05 cache write. You just paid 25 times the price for being away more than 5 minutes.

That example comes from Requesty's analysis of 30 days of coding-agent traffic (Claude Code, OpenCode, Codex, Pi, Kilo), and it happened on 81% of Opus 5.5 pauses of 5 to 30 minutes in their data. Take the same break on GPT-6.1 Sol and, in their words, the cache "still has 22 minutes left on its clock."

It comes down to one number. Claude prompt caching has a 5-minute TTL by default, and OpenAI's current models get 30 minutes. I'll go through the cost math from both providers' docs and then four fixes: the Claude Code TTL setting, a split 1-hour TTL, max_tokens: 0 keep-alive pings, and not breaking your own cache.

The 23% headline is inflated, but the 5-to-30-minute gap holds up

Requesty's title says Claude's 5-minute timer is 23% of your coding bill. On Hacker News Edwin from Anthropic pointed out that the 23% counts breaks over 30 minutes too, and there both caches are gone anyway (7.8% Claude vs 8.5% GPT).

He's right, so leave those out. Breaks of 5 to 30 minutes, the kind where you read a diff, wait on CI or answer Slack, cost 15.2% of the Claude bill vs 0.8% on GPT. The 5-to-10-minute slice by itself costs Claude users 8.0% of their bill, which is more than every break over an hour put together.

That's a 19x difference on the exact behaviour a coding agent produces. Calling it a footgun is fair.

Edwin also pointed out that not everyone is on the 5-minute TTL. Two caveats before you change anything:

  • This is API billing. Claude Code on a subscription already uses a 1-hour TTL for the main conversation while you're within plan usage (docs). If you use an API key, a cloud provider, or you've run past your plan into usage credits, you're on 5 minutes.
  • Subagents get 5 minutes even on a subscription.

Claude vs GPT prompt caching cost, side by side

The multipliers are now identical on both sides, so the TTL is what you're comparing.

Claude Opus 5.5 / Sonnet 5.5 GPT-6.1 Sol
Input price per MTok $4 (Opus) / $2 (Sonnet) $2
Cache read 0.05x input 0.05x input
Cache write, default TTL 1.25x 1.25x
Default TTL 5 minutes 30 minutes (minimum)
Longer option 1 hour at 2.0x write 12 hours at 2.5x write
Read refreshes the timer Yes Yes

Sources: Anthropic prompt caching docs, OpenAI prompt caching docs. OpenAI only started charging for cache writes with GPT-5.6. Before that, writes were free.

The Opus math: 210K tokens Γ— $0.20/MTok read = $0.042. The same 210K Γ— $5/MTok write = $1.05. A miss costs 25x a hit.

Anthropic's docs also say the 5 minutes run "from the start of the request that writes or reads the cache entry, not from the end of its response." If a response takes 4 minutes to stream, you have about 1 minute left to send the next turn. A cold cargo test that runs longer than 5 minutes kills the cache while you're still sitting at your desk.

What a coffee break costs per developer

From Requesty's per-pause numbers for 5 to 30 minute breaks:

Model Miss rate Median prompt Expected cost per pause
Opus 5.5 81.0% 207K $0.86
Sonnet 5.5 81.2% 140K $0.28
GPT-6.1 Sol 2.3% 142K $0.008

At 4 breaks a day, that's about $69 a month per Opus developer, or $1,380 a month for a 20-person team. One HN commenter said their team saves "about $1,000 per week" with an Opus cache warmer at the router level.

Fix 1: Set promptCacheTtl in Claude Code

If you run Claude Code with an API key or through Bedrock or Vertex, add this to settings.json (needs v2.1.242 or later):

{
  "promptCacheTtl": "1h",
  "subagentPromptCacheTtl": "5m"
}

The env var equivalents are CLAUDE_CODE_PROMPT_CACHE_TTL and CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL. To check what you're actually getting, run claude -p "hello" --output-format json and look at usage.cache_creation for ephemeral_1h_input_tokens vs ephemeral_5m_input_tokens. /usage also shows a Prompt cache (main) line, and on newer versions it names the likely cause of the last miss.

Leave subagents on 5 minutes. They're short and they rarely sit idle.

Fix 2: Use the 1-hour cache TTL on tools and system only

Turning on 1h for everything from turn 1 is the obvious move, and in Requesty's replay it's a bad one. Total savings come out at 1.4% to 9.7% depending on the model, but "87% to 89% of sessions pay more", because most conversations finish in under 5 minutes and now pay 2.0x on every write instead of 1.25x.

Also, don't switch a warm 5-minute cache to 1h mid-session on the Anthropic API or Vertex. In their live tests that request "read 0 tokens and rewrote the entire prompt at 2.0x." Bedrock behaved differently in 4 of 4 runs, but that isn't documented.

What I'd use instead is a split TTL. Tools and system prompt rarely change, so they go on 1h. The conversation goes on 5m:

def with_cache_marker(messages):
    # Put a 5-minute breakpoint on the last block of the last message.
    *head, last = messages
    content = last["content"]
    if isinstance(content, str):
        content = [{"type": "text", "text": content}]
    *blocks, final = content
    final = {**final, "cache_control": {"type": "ephemeral"}}
    return [*head, {**last, "content": [*blocks, final]}]

resp = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=4096,
    tools=[*tools[:-1], {**tools[-1], "cache_control": {"type": "ephemeral", "ttl": "1h"}}],
    system=[{"type": "text", "text": SYSTEM_PROMPT,
             "cache_control": {"type": "ephemeral", "ttl": "1h"}}],
    messages=with_cache_marker(messages),
)

Anthropic's docs say "a 1-hour cache entry must appear before any 5-minute cache entries," and Requesty reports an HTTP 400 if you get it backwards. Since the prefix order is always tools β†’ system β†’ messages, the layout above satisfies that rule automatically.

Fix 3: Keep the cache warm with max_tokens: 0 pings

Requesty ranks this fix first: 8% to 9% net savings on the Claude coding bill. After about 4.5 minutes idle, re-send the exact last request with max_tokens: 0:

def keep_alive(client, last_request: dict):
    # Keep model, system, tools, messages, thinking, and effort identical.
    body = {**last_request, "max_tokens": 0}
    body.pop("stream", None)  # stream: true is rejected with max_tokens: 0
    return client.messages.create(**body)

You pay a cache read and get zero output tokens. Anthropic's docs describe the same max_tokens: 0 request as a way to pre-warm the cache, so this is supported behaviour. One thing that doesn't work: in Requesty's tests, count_tokens did not refresh the cache.

A ping pays off when the chance the developer comes back is higher than read / (write - read). On Opus or Sonnet 5.5 that's 0.20 / (5 βˆ’ 0.20) β‰ˆ 4.2%. Requesty measured return odds of 2.7% on turns 1 to 5 and 19.5% on turns 21 to 40. So send no pings on shallow threads and 2 to 6 on deep ones.

Limits: max_tokens: 0 is rejected with thinking.type: "enabled", structured outputs, forced tool_choice, and Message Batches. Your harness also has to keep the last request body around.

Fix 4: Stop invalidating your own cache

None of this matters if you blow the prefix away yourself. From the invalidation table:

  • Changing tool definitions invalidates everything, including system and messages.
  • Toggling web search, citations or speed: "fast" invalidates system and messages.
  • Changing tool_choice or adding images invalidates messages. Changing thinking parameters invalidates messages too, and on some models tools and system as well.
  • Hits need "100 percent identical prompt segments", so a timestamp in your system prompt means you never hit.

In Claude Code, /model switches start the next request with no cache at all. Editing a project or user CLAUDE.md mid-session is fine because it's read at startup. Run /compact before you walk away: compacting after the cache has expired means the summarization pass reprocesses the whole history as uncached input.

Audit your Claude bill in five minutes

Look in your request logs for continuing turns that start more than 300 seconds after the previous one, where cache_read_input_tokens drops to near zero and cache_creation_input_tokens spikes. Each one of those is a coffee break you paid for. If they add up to more than a few percent of input spend, start with Fix 3.

My view: Anthropic should print the TTL next to the per-token prices, and a 1-hour write should be able to extend a warm 5-minute entry. Until then, a 5-minute default on a product built for agents that wait on compilers is a cost you have to engineer around yourself.

What share of your Claude bill is cache writes? And if you've switched to the 1-hour TTL, did it save money, or did you find what one HN commenter found, that "it's basically a wash"?

Sources

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.