Measuring Real Token Costs for Claude and GPT APIs With Caching
Bottom line: The sticker price per million tokens is the least useful number in your cost model — cache write premiums, cache expiry, and retry amplification routinely move real per-request cost by 2-4x in either direction, and the only way to know which way is to log the usage block on every response.
I spent a month running the same workload against Anthropic’s Messages API and OpenAI’s Responses API: a long system prompt, a tool schema, and a growing conversation history. I logged every response’s usage object, priced it against the published tables, and compared that to what I’d have predicted from the headline input/output rates. The gap was large enough that I rebuilt my cost estimator around cache hit rates rather than token counts. Here is what actually happened.
The pricing tables are three tables, not one
Both providers charge different rates for tokens that write a cache entry, tokens that read from it, and tokens that miss entirely. Anthropic publishes this openly. For Sonnet 5.5 the numbers are $2/MTok base input, $2.50/MTok for a 5-minute cache write, $4/MTok for a 1-hour write, and $0.10/MTok for hits and refreshes. Output is $10/MTok regardless.
That means a cache write costs 25% more than an uncached input token, and a cache hit costs 95% less. The break-even is trivially low: two requests sharing a prefix and the caching pays for itself. The problem is the failure mode, not the math.
| Line item | Sonnet 5.5 | Opus 5.5 | Haiku 5.5 (≤100k) |
|---|---|---|---|
| Base input | $2.00 | $4.00 | $0.10 |
| Output | $10.00 | $20.00 | $0.50 |
| 5m cache write | $2.50 | $5.00 | $0.125 |
| 1h cache write | $4.00 | $8.00 | $0.20 |
| Cache hit / refresh | $0.10 | $0.20 | $0.01 |
All figures USD per million tokens, from the published pricing tables. Note the Haiku row: prompts over 100,000 tokens jump to $0.50 input and $2.50 output, a 5x step change that is easy to trip with a large retrieved context.
What the usage block actually tells you
Anthropic returns cache_creation_input_tokens, cache_read_input_tokens, and input_tokens as separate fields. input_tokens is only the uncached remainder — it is not the prompt size. If you compute cost as input_tokens * rate, you will undercount badly on cache-heavy traffic and overcount on cold traffic.
A minimal logging wrapper:
1 | import anthropic, json, time |
Run that for a day and you get the distribution you actually need: what fraction of input tokens were reads, what fraction were writes, and how many requests were fully cold. In my workload the read fraction settled around 70-85% for multi-turn sessions and collapsed to near zero whenever I changed the system prompt or tool schema.
The 5-minute window is shorter than it looks
Anthropic’s cache lifetime is measured from the start of the request that writes or reads the entry, not from the end of the response. The docs are explicit: if a response takes four minutes to stream, a follow-up request reusing that prefix must start within about one minute of the response completing.
This is the single biggest source of surprise cost. An agent that thinks for a while, calls a slow tool, then resumes will silently miss the cache and pay full input price plus a fresh write. On a 40k-token prefix at Sonnet 5.5 rates, that is $0.10 for the write instead of $0.004 for the read — a 25x difference on that request.
The 1-hour cache exists for this. It costs $4/MTok to write versus $2.50, so 60% more on the write, but it survives tool latency, human-in-the-loop pauses, and rate-limit backoff. For any agent with a tool call in the loop, the 1-hour tier was cheaper in my measurements despite the higher write price.
Retries are the hidden multiplier
Both SDKs retry on 429 and 5xx by default. A retried request that already wrote a cache entry may write it again, and a retried request that hit a cache may hit it again — you pay for both. I saw retry overhead between 3% and 11% of total spend depending on how aggressively I was running parallel requests against rate limits.
Instrument it rather than guessing:
1 | # Count retries by status code from your own logs |
If the second number is more than a couple of percent of your total cache writes, your concurrency is too high for your tier and you are paying a tax to discover that. Backing off to a lower parallelism cut my retry overhead to under 2% and reduced wall-clock time, because the retries were not making progress anyway.
Where the two providers diverge
OpenAI’s caching is automatic and does not expose a write premium in the same way — cached input is billed at a discount and there is no separate charge for populating the cache. That sounds strictly better, and for pure read-heavy traffic it is. The catch is control: you cannot pin a breakpoint, so you cannot guarantee which prefix gets cached, and you cannot choose a longer lifetime. Anthropic’s explicit cache_control breakpoints let you cache exactly the tool schema and system prompt and nothing else, which matters when your message history contains per-user data you do not want held in a cache entry.
My practical split after a month: OpenAI for workloads where the prompt is stable and I want zero cache management; Anthropic with explicit breakpoints and a 1-hour tier for agent loops where I need the cache to survive tool latency. Mixing them per-endpoint rather than picking one vendor was worth roughly 20-30% on the bill, though that range is workload-specific and I would not generalize it.
Pitfalls and how to diagnose them
Cache misses you did not expect. Symptom: cache_read_input_tokens is zero on requests you believed shared a prefix. Cause is almost always a changed prefix — a timestamp in the system prompt, a reordered tool list, or a per-request user ID injected at the top. Diagnose by hashing the exact prefix bytes before the breakpoint and logging the hash alongside the usage block. If the hash changes, the cache cannot hit.
Counting input_tokens as prompt size. Symptom: your cost estimate is off by a factor that grows with conversation length. Fix the estimator to sum all three input fields.
Expired entries during tool calls. Symptom: cost spikes correlated with slow tools. Log the gap between response completion and next request start; anything approaching five minutes on the 5-minute tier is a miss waiting to happen.
Retry amplification. Symptom: cache writes exceed the number of logical requests. Count writes per request ID.
The Haiku threshold. Symptom: a sudden 5x jump on a model you thought was cheap. Check whether your retrieved context crossed 100,000 tokens.
Output tokens dominating. Symptom: caching optimizes input and the bill does not move. Output is $10/MTok on Sonnet 5.5 and is never cached. If output is more than about a fifth of your token volume, caching is not your lever — prompt design is.
FAQ
Does a cache hit also refresh the entry’s lifetime?
Yes. Anthropic’s docs state the cache is refreshed for no additional cost each time the cached content is used, and the lifetime restarts from the beginning of that request. A steady stream of requests under five minutes apart keeps the entry alive indefinitely without paying write rates again.
Should I use automatic caching or explicit breakpoints?
Automatic caching puts a single cache_control field at the top level and moves the breakpoint forward as the conversation grows. It is the right default for multi-turn chat. Explicit breakpoints on individual blocks are worth the extra code when you want the tool schema and system prompt cached but not the message history, which is common when history contains user-specific data.
Why did my cost go up after I enabled caching?
Almost always because your hit rate is low. Every cold request now pays the write premium instead of base input. If more than roughly a third of your requests are cold, caching is a net loss on that traffic. Measure the read fraction before and after.
Is the 1-hour cache worth 60% more on writes?
For agent loops with tool calls, in my measurements yes — the alternative is paying full input price plus a fresh write on every resumed turn. For a tight chat loop where requests are seconds apart, the 5-minute tier is strictly cheaper.
How do I estimate cost before running anything?
Multiply each token class by its own rate and sum: uncached input at base, cache writes at the write rate, cache reads at the hit rate, output at the output rate. Any estimator that uses a single input rate will be wrong by a factor that depends on your hit rate, which is the number you do not know until you measure.
Does the cache survive a model version change?
No. Cache entries are tied to the model. Pinning a version and only bumping it deliberately keeps your hit rate stable; floating to a moving alias means every rollout silently resets your cache and shows up as a cost spike you did not schedule.











