Skip to content

[Bug]: Responses API cache-write tokens are dropped from usage metrics #6691

Open

Description

@GoGennady

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

For models served through the OpenAI Responses API, cache_write_tokens is always recorded as 0 in Metrics.token_usages, even when the provider reports cache writes.

normalize_usage() in openhands-sdk/openhands/sdk/llm/utils/telemetry.py handles two usage shapes. The Chat Completions branch reads prompt_tokens_details.cache_creation_tokens. The ResponseAPIUsage branch reads only input_tokens_details.cached_tokens and never sets cache_write_tokens.

Cache writes do arrive there in two ways:

  • OpenAI reports input_tokens_details.cache_write_tokens in Responses API usage for gpt-6 family models (observed in production usage, for example 12048 cache-write tokens on one gpt-6-luna call; feat(llm): report prompt-cache write tokens as cache_creation_tokens livekit/agents#7710 reads the same field).
  • LiteLLM's own chat-to-Responses bridge (LiteLLMCompletionResponsesConfig._transform_chat_completion_usage_to_responses_usage) emits the same key when the underlying Chat Completions usage has cache writes, for example an Anthropic model called through the Responses API.

LiteLLM's InputTokensDetails does not declare that field, but the model is extra="allow", so the value is kept in model_extra and is available to the SDK.

Expected Behavior

TokenUsage.cache_write_tokens reflects input_tokens_details.cache_write_tokens for Responses API calls, as it already does for Chat Completions calls.

Actual Behavior

cache_write_tokens is 0 for every Responses API call. Cache-write tokens show up only inside prompt_tokens, so anything that prices or reports cache writes separately (dashboards, budget checks, the gen_ai.usage.cache_creation_input_tokens span attribute) undercounts them.

Steps to Reproduce

from litellm.types.llms.openai import ResponseAPIUsage
from openhands.sdk.llm.utils.telemetry import normalize_usage

usage = ResponseAPIUsage.model_validate({
    "input_tokens": 1000,
    "output_tokens": 20,
    "total_tokens": 1020,
    "input_tokens_details": {"cached_tokens": 200, "cache_write_tokens": 600},
})
print(normalize_usage(usage))
# cache_write_tokens=0, expected 600

Acceptance Criteria

  • A Responses API usage block with input_tokens_details.cache_write_tokens records that value as cache_write_tokens.
  • A missing or null cache_write_tokens still records 0.

SDK Version

v1.60.2 (latest) and main 457e730

Version Confirmation

  • I have confirmed this bug exists on the LATEST version of OpenHands SDK

Python Version

3.13

Model Name (if applicable)

OpenAI gpt-6 family models on the Responses API; any model reached through LiteLLM's chat-to-Responses bridge that reports cache writes

Evidence and Additional Context

LiteLLM 1.105.1 (the version in uv.lock): _transform_chat_completion_usage_to_responses_usage() maps Chat Completions prompt_tokens_details.cache_creation_tokens to Responses input_tokens_details.cache_write_tokens.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Confirmed against main: the ResponseAPIUsage branch of normalize_usage() in openhands-sdk/openhands/sdk/llm/utils/telemetry.py never sets cache_write_tokens, while the Chat Completions branch reads prompt_tokens_details.cache_creation_tokens. Verified locally against the pinned LiteLLM 1.105.1 from uv.lock: InputTokensDetails is configured with extra="allow", so a cache_write_tokens value in input_tokens_details is retained and readable, and the reported reproduction is accurate. The existing test test_responses_api_shape_is_normalized covers only a usage block without the field and asserts the current 0 behavior.

Bounded scope: extend the existing Responses API branch of normalize_usage() to read input_tokens_details.cache_write_tokens using the same None/absence handling pattern already used for cached_tokens. Everything downstream (Telemetry._record_usage(), Metrics.add_token_usage(), and the gen_ai.usage.cache_creation_input_tokens span attribute) already consumes the normalized value and needs no change.

Non-goals: no changes to the Chat Completions branch or its model_fields_set/deleted-attribute handling; no pricing, cost-calculation, or dashboard changes; no new metrics fields; no changes to request serialization or LiteLLM pins.

Acceptance Criteria

  • normalize_usage() on a ResponseAPIUsage whose input_tokens_details carries cache_write_tokens returns a UsageSnapshot with cache_write_tokens equal to that value (e.g. the repro payload from this issue yields 600), and that value reaches Metrics.token_usages through Telemetry.on_response() with a ResponsesAPIResponse.
  • input_tokens_details missing the cache_write_tokens key, containing it as null, or input_tokens_details absent entirely still normalizes cache_write_tokens to 0 (the existing test_responses_api_shape_is_normalized assertion continues to pass).
  • The Chat Completions Usage branch of normalize_usage() behaves as before: a usage with prompt_tokens_details.cache_creation_tokens set still maps to cache_write_tokens, and a usage where LiteLLM deleted the None attribute still normalizes to 0.
  • With observability enabled, a Responses API response whose usage carries cache writes records gen_ai.usage.cache_creation_input_tokens equal to the reported cache-write count on the LLM call span.

Activity

  1. added
    priority:medium
    ready-for-dev
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions