Costs · November 27, 2026 · 7 min read

Turning the usage object into dollars: how Claude, OpenAI and Gemini count the tokens you pay for

Every API response tells you what it cost, but Claude, OpenAI and Gemini split cached, written and thinking tokens differently. Read the fields the wrong way and your per-run cost meter is off by 40 percent or more. Here's how to do the math on each.

Some links are affiliate links; we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Disclosure

If you want to know what an agent run actually cost, the answer is already sitting in every response. Each API hands back a usage block with token counts, and multiplying those by the price list gives you a per-turn dollar figure you can log, chart and cap. Most teams write that meter once, early on, and then never touch it again.

The problem is that Claude, OpenAI and Gemini don't agree on what "input tokens" means. One reports cached tokens on top of the input count, one reports them inside it, and one tucks thinking tokens into a field next to the output count instead of inside it. Write one cost function that treats all three the same and it'll be wrong on at least two of them. On a cached agent loop the miss can be several times the real bill, which is a lot more than a rounding error.

Everything below comes from the official docs and pricing pages as of November 27, 2026. Prices for the models used in the examples match our models page.

Claude: input_tokens is only the tail

Anthropic's prompt caching docs spell this out under "Tracking cache performance." The usage block has three input fields. cache_read_input_tokens is what came out of the cache. cache_creation_input_tokens is what got written to it on this request. And input_tokens is only what came after your last cache breakpoint, the part that was neither read nor written. The docs give the formula directly: total input equals cache reads plus cache creation plus input_tokens.

So on Claude you add. If you treat input_tokens as the whole prompt, a 40,000-token cached transcript plus a 300-token user message shows up in your logs as a 300-token request.

Writes get one more layer. When you mix five-minute and one-hour cache lifetimes, the response includes a cache_creation object with ephemeral_5m_input_tokens and ephemeral_1h_input_tokens, and the docs say cache_creation_input_tokens equals their sum. That split matters because the two are priced differently. Anthropic's pricing page lists a five-minute write at 1.25x base input, a one-hour write at 2x, and a cache hit at 0.1x on most models. Claude Sonnet 5.5 and Opus 5.5 are cheaper on hits at 0.05x, and Fable 5.2 is at 0.025x. For Sonnet 5.5 that works out to $2 per million for base input, $2.50 for five-minute writes, $4 for one-hour writes, $0.10 for hits and $10 for output.

Thinking is simpler. The extended thinking docs point you to usage.output_tokens_details.thinking_tokens, which reports how many of the billed output tokens were internal reasoning. Note the word "of." Thinking is already inside output_tokens, so don't add it again. Use the breakdown to see what your thinking budget is eating. One streaming detail: that breakdown only shows up on the final message_delta event, so a meter that reads usage off message_start alone will miss it.

OpenAI: input_tokens is everything, so subtract

OpenAI goes the other way. In the Responses API, input_tokens is the full prompt, and the cache numbers are broken out underneath it in input_tokens_details. The prompt caching guide walks through an example with 12,000 input tokens, 4,000 cached_tokens and 7,000 cache_write_tokens, and says the ordinary input is 12,000 minus 4,000 minus 7,000, or 1,000 tokens. On OpenAI you subtract.

Writes have their own split too. On models that support prompt_cache_options, cache_write_tokens_details breaks writes into ttl_30m_tokens and ttl_12h_tokens, which the guide says don't overlap and add up to cache_write_tokens. The 30-minute writes cost 1.25x the input price and the 12-hour writes cost 2.5x. The guide also warns not to count cache_write_tokens again after you've priced its two parts, and notes that cached_tokens counts billed reads only and leaves out anything charged as a write. Their worked example: at $1 input and $0.10 cached, that sample comes to $0.01515 for input.

Reasoning follows the same pattern as Claude's thinking. The reasoning guide says reasoning tokens aren't visible through the API but are billed as output tokens, and their count shows up as reasoning_tokens under output_tokens_details. In the guide's own sample, output_tokens is 1,186 and reasoning_tokens is 1,024. The reasoning is part of the 1,186, so bill output_tokens once and leave it there.

For prices, OpenAI's pricing page lists GPT-6.2 Sol at $2.00 input, $0.10 cached input, $2.50 for a 30-minute write, $5.00 for a 12-hour write and $10.00 output per million tokens on short context. GPT-6.2 Astra is $8.00 input, $0.40 cached, $10.00 and $20.00 for the two writes, and $40.00 output. Both have a separate, higher long-context column, so your meter needs to know which side of that line the request landed on. Our long-context price steps post covers where those lines sit.

Gemini: the prompt includes the cache, the output doesn't include thinking

Gemini mixes the two styles. In the generateContent UsageMetadata reference, promptTokenCount is described as the total effective prompt size, including cached content, and cachedContentTokenCount is the cached part. So the input side works like OpenAI: subtract the cached count from the prompt count to get the uncached tokens.

The output side is where it gets you. candidatesTokenCount is the tokens across the generated response, and thoughtsTokenCount is reported separately. The reference defines totalTokenCount as prompt plus tool-use prompt plus thoughts plus response candidates, which tells you thoughts are not inside the candidates number. Google's thinking docs say that when thinking is on, response pricing is the sum of output tokens and thinking tokens, and that you pay for the full thoughts even though the API only returns a summary. So on Gemini you add thoughts to the output count yourself, or every thinking-heavy turn looks cheaper than it was.

There's also toolUsePromptTokenCount. The pricing page notes on its Gemini 4 Neon and Hydrogen entries that every token the model processes is billed, including thinking tokens and internal tool system instructions and function declarations when using grounding. Keep that field in the meter even though it isn't part of promptTokenCount. If you're on the newer Interactions API, the token counting docs list the equivalent fields as total_input_tokens, total_output_tokens, total_thought_tokens, total_cached_tokens, total_tool_use_tokens and total_tokens.

Gemini 4 Hydrogen is $1.50 input, $7.50 output (the pricing table labels it "including thinking tokens") and $0.075 for cached input per million on the standard tier. Gemini 4 Neon is $3.00, $15.00 and $0.15. Both are implicit caching only, so there's no separate write charge to track.

One agent turn, three meters

Here's a turn you'll see all day in a tool loop. About 40,000 tokens of transcript and tool definitions are already cached, 2,000 tokens of new tool results get cached on this turn, the fresh user text is 300 tokens, and the model writes 1,200 tokens, 900 of which are reasoning.

On Claude Sonnet 5.5 with a five-minute write, the usage block would show cache_read_input_tokens 40,000, cache_creation_input_tokens 2,000, input_tokens 300 and output_tokens 1,200. Priced out: 40,000 hits at $0.10 per million is $0.004, 2,000 writes at $2.50 is $0.005, 300 input at $2 is $0.0006 and 1,200 output at $10 is $0.012. Total, $0.0216. A meter that only reads input_tokens and output_tokens says $0.0126, which misses about 42 percent of the cost.

On GPT-6.2 Sol with a 30-minute write, the same turn shows input_tokens 42,300, cached_tokens 40,000, cache_write_tokens 2,000, output_tokens 1,200 and reasoning_tokens 900. Subtract to get 300 ordinary tokens, and it comes to the same $0.0216. Now make the opposite mistake and charge all 42,300 input tokens at the full $2 rate. You get $0.0966, about four and a half times the real number. Add reasoning_tokens on top of output as well and it climbs to $0.1056.

On Gemini 4 Hydrogen, the usage would show promptTokenCount 42,300, cachedContentTokenCount 40,000, candidatesTokenCount 300 and thoughtsTokenCount 900. That's 2,300 uncached tokens at $1.50 per million ($0.00345), 40,000 cached at $0.075 ($0.003), and 1,200 output tokens at $7.50 ($0.009), for $0.01545. Bill only the 300 candidate tokens and output drops from $0.009 to $0.00225, so the turn looks like it cost $0.0087.

That's one turn and three different ways to get it wrong: two meters read low, one reads way high. No single shared cost function gets all three right.

Responses that look failed still cost money

Read usage even when a turn gives you nothing back. OpenAI's reasoning guide says that when a response hits max_output_tokens, you get status incomplete, and this can happen before any visible output, so you can pay for input and reasoning with nothing to show for it. Google's thinking docs say the same about Gemini: if the model hits the limit while reasoning, you get truncated or empty output and still pay for the thinking tokens it generated. Anthropic notes that thinking tokens count toward max_tokens. Skip incomplete or truncated turns in your logging and the cost chart runs low, usually by dropping the pricey ones.

Building the meter

Give each provider its own small cost function. Price lookups should be keyed by model and, where it applies, by context tier, service tier and region, since Claude's inference_geo US-only routing adds a 1.1x multiplier to every token category on Claude 4.6 and later (see our data residency post). Store the raw usage JSON next to the computed dollars, so when a price changes or you find a bug you can recompute history instead of guessing. Once a week, hold the meter up against the provider's billing dashboard. If they disagree, the dashboard is the one sending you invoices, so the meter is what needs fixing.

Once you trust the meter, the other cost levers get measurable too. Budget caps need a correct running total, and you can't tell whether prompt caching is paying off if cache reads are hiding in a field you never read. For a broader look at running LLM systems in production, including cost tracking, AI Engineering by Chip Huyen is a solid read, though it predates the specific API fields above.

Newsletter

The daily agent toolkit.

New tools, MCP servers and frameworks, in one short email every morning. Free, no spam. Sign up and get the free AI agent cost cheat sheet (PDF).