[BUG] Usage cache-token fields mix two incompatible conventions, over-counting cost on OpenAI/Gemini/LiteLLM cache hits

Author: niklas-palmCreated Jul 29, 2026Updated Sep 22, 2026
Labelsbugarea-otelarea-modeltypescript

Summary

Usage.cacheReadInputTokens is populated from provider fields that do not mean the same thing, and nothing on the type says which convention applies. For Bedrock Converse and the Anthropic API, cache tokens are disjoint from inputTokens — the true prompt is inputTokens + cacheRead + cacheWrite. For OpenAI (Chat Completions and Responses), Gemini, and LiteLLM, the cache counts are a subset of the input field — the same tokens are reported twice, once in the total and once broken out.

Both are mapped onto one optional field and _accumulate_usage sums them blindly, so any consumer doing the obvious thing — total = input + output + cacheRead + cacheWrite, or cost = Σ tokens × rate — over-counts every OpenAI-style cache hit and charges those tokens twice, at the input rate and the cache rate. On agentic workloads, where most of the prompt is a cache hit, that is a multiple rather than a rounding error. The same code path is correct for Bedrock, which is what makes it hard to spot.

Reproduction

Live capture through Agent on openai.gpt-5.6-luna via Bedrock Mantle, us-east-1 (same prompt twice, second call is a cache hit):

call 1 accumulated: {"inputTokens": 6459, "outputTokens": 5, "totalTokens": 6464}
call 2 accumulated: {"inputTokens": 12936, "outputTokens": 10, "totalTokens": 12946, "cacheReadInputTokens": 6457}

sum(4 counters) = 19403  vs  accumulated totalTokens = 12946   → over-counts by 6457 (+50%)

The provider's own usage for call 2 was {"input_tokens": 6452, "output_tokens": 5, "total_tokens": 6457, "input_tokens_details": {"cached_tokens": 6450, "cache_write_tokens": 0}} — 6452 + 5 == 6457, so cached_tokens is inside input_tokens.

Contrast Bedrock Converse on eu.anthropic.claude-haiku-4-5, eu-north-1, same shape of workload:

call 1 {"inputTokens": 10, "outputTokens": 4, "totalTokens": 5862, "cacheReadInputTokens": 0, "cacheWriteInputTokens": 5848}
call 2 {"inputTokens": 10, "outputTokens": 4, "totalTokens": 5862, "cacheReadInputTokens": 5848, "cacheWriteInputTokens": 0}

10 + 4 + 5848 == 5862. Here the cache counters are additional tokens and totalTokens includes them. No single interpretation of cacheReadInputTokens is correct for both providers.

Per-provider findings

Provider Convention Issue
Bedrock Converse disjoint ✅ [captured] correct today; the de facto reference
Anthropic direct disjoint [documented] totalTokens is recomputed as input + output, excluding cache writes — under-reports a cached run. Anthropic's docs state total_input_tokens = cache_read + cache_creation + input_tokens
OpenAI Chat / Responses subset [captured via Mantle] cache counts are inside inputTokens, so summing over-counts. cache_write_tokens is dropped (it exists first-party from GPT-5.6, billed at 1.25× input)
Gemini subset [captured] cached_content_token_count is inside prompt_token_count. Separately, outputTokens was inferred as total - prompt, which silently counted tool_use_prompt_token_count (an input type) as output
LiteLLM subset [verified in litellm source] folds cache into prompt_tokens for every backend including Anthropic (prompt_tokens += cache_creation_input_tokens + cache_read_input_tokens), so the existing "only LiteLLM over Anthropic supports cache write tokens" comment is inverted
Vercel AI SDK either reads inputTokens.total (inclusive) when the AI SDK also provides inputTokens.noCache, which is exactly the disjoint value
TS Chat Completions — drops prompt_tokens_details entirely, so cache reads are invisible (Python's does read it — a py/ts parity gap)
Reasoning tokens — completion_tokens_details.reasoning_tokens / thoughts_token_count are billed but never surfaced

Gemini's own field docs confirm total_token_count is "the sum of prompt_token_count, candidates_token_count, tool_use_prompt_token_count, and thoughts_token_count" — verified live: 23293 + 1 + 0 + 80 == 23374.

Blast radius beyond cost reporting

Usage is not only a metric. Anything derived from inputTokens or totalTokens inherits the ambiguity:

  • limits.total_tokens is an agent-loop stopping condition (_check_limits), so the convention decides when a run halts.
  • latest_context_size / projected_context_size (public API via AgentResult.context_size) are context-window quantities, not cost quantities — cached tokens occupy the window regardless.
  • Proactive compression gates on that same estimate (_estimate_input_tokens → BeforeModelCallEvent.projected_input_tokens → ConversationManager). Under a subset-convention provider a mostly-cached 100k prompt reads as ~5k, so compaction never fires and the run hits a hard context-window error instead.
  • Multi-agent aggregation (Graph/Swarm._accumulate_metrics) sums only the three required counters, dropping the cache counts while still adding their totalTokens — the total then reports tokens no counter accounts for.
  • OTel (telemetry/tracer.py) emits gen_ai.usage.input_tokens, which the GenAI semconv defines as including cached tokens ("This value SHOULD include all types of input tokens, including cached tokens"), with the cache attributes as breakdowns of it. So the SDK's field and the emitted attribute genuinely should differ, which needs stating rather than leaving to chance.

Proposed fix

  1. Write the contract down on Usage. Whichever convention is chosen, the doc comment must say whether the cache counters are also in inputTokens, and whether totalTokens includes them. This is the cheapest half and the half that stops the next adapter repeating it.
  2. Normalize to disjoint, i.e. inputTokens + outputTokens + cacheReadInputTokens + cacheWriteInputTokens == totalTokens. Bedrock Converse is already disjoint, so the flagship provider's numbers do not move; cost becomes a plain weighted sum, which is the point. Route every adapter through one shared helper taking a single input_includes_cache flag, so a new provider makes one decision instead of re-deriving the arithmetic.
  3. Keep OTel conformant separately — add the cache counters back when emitting gen_ai.usage.input_tokens, per semconv.
  4. Fix the context-window consumers to use the full prompt, not inputTokens.
  5. Surface reasoning tokens as a field documented as a subset of outputTokens (so it must not be added to a total).
  6. Test the invariant, not field presence. A test asserting only cacheReadInputTokens == 1920 passes under both conventions and proves nothing — that is how this got in.

This changes the value of inputTokens for OpenAI/Gemini/LiteLLM users and of totalTokens for Anthropic-direct users. It is a behaviour change on metrics fields rather than an API break, but it deserves a release note: anyone charting those fields will see a step.

Environment

harness-sdk@main. Captures on Bedrock Converse (eu-north-1), Bedrock Mantle Responses (us-east-1), and Gemini (AI Studio) were taken directly against the live APIs. Anthropic-direct and first-party OpenAI are not wire-captured — those rows are from vendor docs and the installed SDK type definitions.

I have a fix ready for both SDKs and will open a PR referencing this issue.

Source: strands-agents/harness-sdk