Claude Prompt Caching: 6 Tricks That Cut Your API Bill

Breakpoints, TTL math, Batch stacking, and a two-session split. Measured on OpenRouter, $0.003 vs $0.022 per turn

·
·
Claude Prompt Caching: 6 Tricks That Cut Your API Bill
AuthorBy Adham Khaled
Read5 min
TopicInfra · Agents · Business
  • Automatic caching billed $0.022 per turn for 8,400 tokens that cost $0.003 once the breakpoint moved to the last stable block, with 8,427 tokens read on all six checks.
  • A 5-minute cache write costs 1.25x base input and repays after one read, while the 1-hour write costs 2x and needs two reads; a 370-second idle test killed the former ($0.02236 re-bill) and held the latter ($0.00298).
  • Batch halves every price field including cache reads (Opus $2.50 in and $0.25 a read, Sonnet $1 and $0.10), but interactive sessions cannot batch and max_tokens: 0 pre-warming is rejected inside a batch.
  • Swapping to Opus mid-thread on a 31,000-token prefix billed $0.197 with zero hits, while two pinned sessions with a plan file held sticky Sonnet at $0.064 a task against $0.068 for Sonnet plus GLM-5.3 and $0.123 for Sonnet plus Opus.
  • Opus 5 spends around 160 thinking tokens on a trivial prompt and returned empty shells nine times at a 128-token cap, so set effort per role up front and verify caching with cache_read_input_tokens above zero.

Automatic caching billed us $0.022 per turn for the same 8,400 tokens that cost $0.003 once we placed the cache marker by hand.

And as far as we know, Anthropic's automatic caching means one switch (the system decides what to save) but manual caching means you mark the exact caching point with cache_control.

So with the same model, the same afternoon, the six checks below decide whether the tokens you already paid for get re-read for pennies or re-billed at full price. And every one comes with code you can paste plus the meter reading that proves it worked.

Here is the whole game in four objects:

  • Your conversation history is the context window: everything the model must re-read before answering.
  • The saved copy of that history is the KV cache.
  • A reuse of the copy is a hit at roughly a tenth of the price.
  • A rebuild from scratch is a miss at full price plus a write fee.

So they're history, saved copy, hit, miss, and also the lunch (when you go away for a while) which is the enemy, because the copy expires after five minutes unless you pay to keep it longer.

My last piece showed why the bill grows the way it does. Models re-send history every turn, so cost climbs with the square of the session, and walking away can cost more than the afternoon you just worked (Coding Agent Cost: Why Sessions Get Expensive).

From the other side, the routing companion proved the same point. One mid-session model switch cost 5 to 11 times a warm turn (Model Routing Layers and the Prompt Cache Tax). This is the sequel that tells you what to type.

What follows is the breakpoint rule, the TTL (Time to Live) math, the prove-it meter, the Batch double-dip, the two-session split, and the thinking cap. Each trick has Python, cURL, a dollar figure from list prices or my own runs, and the exact usage field to read.

Where does the breakpoint go?

A breakpoint is a sticky note that instructs Anthropic to cache everything above this line. When you tell the Claude API to cache your prompt, it evaluates the exact sequence of digital bytes above the breakpoint. If a single character changes in the text you are trying to save, the system treats it as an entirely new document and charges you accordingly.

Anthropic's docs have an example where they put the breakpoint on a block holding a timestamp that changes regularly. Every request writes a new cache entry keyed on a new hash, so you pay the write fee every turn and read nothing, ever.

I ran that exact mistake against the fix on Sonnet 5 with an 8,400-token prefix. The trap billed $0.02243 a turn with zero tokens read.

Against the fix, the same bytes billed $0.00299 with 8,427 tokens read. Six checks each, and not one crossed over.

So the mechanical rule is to find the longest prefix that stays byte-identical across your requests, system instructions through stable brief, and put cache_control there. Then let the timestamp, the user message, and the run ID live below it, uncached and changing freely.

import anthropic

client = anthropic.Anthropic()

SYSTEM_PREFIX = open("system-brief.txt").read()  # stable bytes, no clocks

resp = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=512,
    system=[
        {"type": "text", "text": SYSTEM_PREFIX,
         "cache_control": {"type": "ephemeral"}},
    ],
    messages=[{"role": "user", "content": "summarize ticket-4821"}],
)
print(resp.usage.cache_read_input_tokens,
      resp.usage.cache_creation_input_tokens)
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 512,
    "system": [{"type": "text", "text": "YOUR STABLE PREFIX",
                "cache_control": {"type": "ephemeral"}}],
    "messages": [{"role": "user", "content": "summarize ticket-4821"}]
  }'

And here is the twist my runs forced into this piece. The fully automatic mode, one top-level cache_control field and no placement thinking, billed $0.02237 with zero reads across all six checks.

Automatic caching parks its breakpoint on the last block, which is your varying message, so it falls into the same trap with better marketing. It only helps chats that grow by appending, where each turn's new last block still finds the earlier write inside its 20-block lookback.

Four breakpoints, a 20-block lookback, and a per-model floor make up the whole rulebook. Opus 5 starts at 512 tokens, Sonnet 5 at 1,024, Haiku 4.5 at 4,096, per Anthropic's cache docs.

Below the floor the request runs full price with no error, so a short hot prefix is worth padding past the line. Also keep the prefix out of the rewrite-every-turn habit. A CLAUDE.md that injects git status or the clock is a miss engine wearing a config file as a disguise.

OpenAI delta: same rules, different buttons. Nothing needs placing, and the minimums idea and suffix discipline transfer.

Check it with the meter. cache_read_input_tokens above zero means the trick landed. Both fields at zero means something upstream varies that you have not found yet.

Buy the right TTL, not the longest one

TTL (Time to Live) is how long the saved copy lives, a parking meter for your prefix. Anthropic has two options for caching life. Per their pricing page, the five-minute cache charges 1.25 times the base input price when writing the cache. They also have a longer one-hour caching option, which charges double the base price.

Keep reading

Don't miss what's next in AI

Join 300,000+ engineers and researchers who get the signal, not the noise. Create a free account to read the rest of this story.

  • Full access to in-depth AI research breakdowns
  • Be the first to know what's trending before it hits mainstream
  • Daily curated papers, repos, and industry moves

Comments

avatar