OpenAI and Anthropic count cached tokens in opposite ways
Send the same 2,048-token prompt to OpenAI and to Anthropic, with most of it served from the prompt cache, and look at the usage objects that come back: OpenAI prompt_tokens = 2048 (prompt_tokens_details.cached_
Send the same 2,048-token prompt to OpenAI and to Anthropic, with most of it served from the prompt cache, and look at the usage objects that come back:
OpenAI prompt_tokens = 2048 (prompt_tokens_details.cached_tokens = 1792)
Anthropic input_tokens = 256 (cache_read_input_tokens = 1792)
Same prompt, same cache hit, and the headline input numbers differ by 8x. Neither provider is wrong. They just put the cached tokens in different places:
-
OpenAI counts cached tokens inside
prompt_tokens.cached_tokenstells you how many of them were a cache hit. -
Anthropic counts cached tokens outside
input_tokens.input_tokensis only what came after the last cache breakpoint. Cache reads and cache writes are separate fields that you add on.
If your dashboard or cost script reads one field per provider and calls it "input tokens", it will be wrong for at least one of them. With Anthropic you will undercount usage, sometimes badly. With OpenAI you will overcharge yourself if you price every prompt token at the full input rate.
This post shows how each provider reports cache usage, a small function that turns both into the same shape, and the mistakes I see most often.
How Anthropic reports it
Anthropic's usage object has three input fields:
usage.input_tokens # tokens after the last cache breakpoint
usage.cache_read_input_tokens # tokens served from the cache
usage.cache_creation_input_tokens # tokens written to the cache on this call
The Anthropic docs give the total as the sum of all three:
total input = input_tokens + cache_read_input_tokens + cache_creation_input_tokens
Each part has its own price. Per the prompt caching docs, cache writes cost 1.25x the base input price with the default 5-minute lifetime and 2x with the 1-hour lifetime. Cache reads cost 0.1x on most models, and some newer models go lower. So a call that writes a large prompt to the cache costs more than the same call without caching. The payoff only comes on later reads.
There is also a cache_creation object that splits the write count by lifetime (ephemeral_5m_input_tokens and ephemeral_1h_input_tokens). If you use both lifetimes, you need that split to price writes correctly.
How OpenAI reports it
On Chat Completions, OpenAI's usage looks like this:
usage.prompt_tokens # all input tokens, cached or not
usage.prompt_tokens_details.cached_tokens # how many of those were a cache hit
usage.completion_tokens
usage.total_tokens # prompt_tokens + completion_tokens
prompt_tokens is already the full input count, so the fresh, full-price part is prompt_tokens - cached_tokens. The Responses API uses different names (input_tokens and input_tokens_details.cached_tokens) but the same idea: cached tokens sit inside the input count.
Recent versions of the Python SDK (I checked openai 3.60.0) also have a cache_write_tokens field in prompt_tokens_details. How it is priced depends on the model, so check OpenAI's pricing page for the models you use before you put it into your formula.
One shape for both
The fix is to stop passing raw usage objects around and convert them once, at the edge, into one shape with the same meaning for every provider:
from dataclasses import dataclass
from openai.types import CompletionUsage
from anthropic.types import Usage
@dataclass
class Tokens:
fresh_input: int # billed at the normal input price
cache_read: int # billed at the cache-hit price
cache_write: int # billed at the cache-write price
output: int
@property
def total_input(self) -> int:
return self.fresh_input + self.cache_read + self.cache_write
def from_openai(u: CompletionUsage) -> Tokens:
d = u.prompt_tokens_details
read = (d.cached_tokens or 0) if d else 0
return Tokens(u.prompt_tokens - read, read, 0, u.completion_tokens)
def from_anthropic(u: Usage) -> Tokens:
return Tokens(
u.input_tokens,
u.cache_read_input_tokens or 0,
u.cache_creation_input_tokens or 0,
u.output_tokens,
)
Run both example usage objects through it, built with the real SDK types:
oa = CompletionUsage(prompt_tokens=2048, completion_tokens=300, total_tokens=2348,
prompt_tokens_details={"cached_tokens": 1792})
an = Usage(input_tokens=256, cache_read_input_tokens=1792,
cache_creation_input_tokens=0, output_tokens=300)
print(from_openai(oa))
print(from_anthropic(an))
Tokens(fresh_input=256, cache_read=1792, cache_write=0, output=300)
Tokens(fresh_input=256, cache_read=1792, cache_write=0, output=300)
Now the two calls look the same, because they were the same. Cost becomes one function over one shape:
def cost(t: Tokens, price: dict) -> float:
# price holds dollars per million tokens, taken from your own rate card
return (
t.fresh_input * price["input"]
+ t.cache_read * price["cache_read"]
+ t.cache_write * price["cache_write"]
+ t.output * price["output"]
) / 1_000_000
Keep the prices out of the code and in config. They change more often than the field names do.
Mistakes I keep seeing
Reading input_tokens from Anthropic and calling it the input. On a heavily cached agent this can hide most of the input. A long system prompt plus tool definitions is often thousands of cached tokens, while input_tokens shows only the new user turn.
Subtracting cached tokens from Anthropic's input_tokens. This is the reverse mistake, usually made by someone who learned the OpenAI shape first. The cached tokens were never in there, so you get a small or negative number.
Pricing all of OpenAI's prompt_tokens at the full rate. Cached tokens are discounted, so you overestimate cost. That sounds harmless until you compare models or decide whether caching is worth it.
Ignoring cache writes. On Anthropic a write costs more than a normal input token. An agent that keeps changing the start of its prompt (a timestamp in the system prompt is the classic cause) writes the cache on every call and never reads it. Usage looks normal and the bill goes up. Track the ratio of cache_read to cache_write over a run. If writes dominate, your cache is not working.
Dropping usage on streams. With OpenAI Chat Completions streaming, you only get a usage chunk if you pass stream_options={"include_usage": True}, and the SDK notes that an interrupted stream may never send it. With Anthropic streaming, usage arrives in message_start and is updated in message_delta, and the counts in message_delta are cumulative, so take the last value, don't add them up.
Summing total_tokens across providers. OpenAI's total_tokens includes cached input. Anthropic has no total_tokens field at all, so whatever you computed for it depends on which fields you added. Totals from mixed providers are only comparable after normalization.
A checklist
- Store the raw usage object for every model call, exactly as returned. You will want fields you did not think of today.
- Convert to one internal shape (fresh input, cache read, cache write, output) in one place, with a test per provider.
- Price each part separately, with prices in config.
- Watch the cache read to write ratio per agent, not only total tokens.
- Turn on usage for streams, and flag calls whose usage never arrived instead of counting them as zero.
Why I looked into this
I build Uncoil, an open-source tracing tool for AI agents. It records every model call in an agent run with its token usage, and the cache fields were the part that broke cost totals most often when people mixed providers in one agent. If you want to see your own numbers per run and per step, the cost dashboard shows the setup. Whatever you use, check which way your provider counts cached tokens before you trust a total.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.