Repository navigation
Description
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_tokensin Responses API usage for gpt-6 family models (observed in production usage, for example 12048 cache-write tokens on onegpt-6-lunacall; 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 600Acceptance Criteria
- A Responses API usage block with
input_tokens_details.cache_write_tokensrecords that value ascache_write_tokens. - A missing or null
cache_write_tokensstill 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 aResponseAPIUsagewhoseinput_tokens_detailscarriescache_write_tokensreturns aUsageSnapshotwithcache_write_tokensequal to that value (e.g. the repro payload from this issue yields 600), and that value reachesMetrics.token_usagesthroughTelemetry.on_response()with aResponsesAPIResponse. -
input_tokens_detailsmissing thecache_write_tokenskey, containing it asnull, orinput_tokens_detailsabsent entirely still normalizescache_write_tokensto 0 (the existingtest_responses_api_shape_is_normalizedassertion continues to pass). - The Chat Completions
Usagebranch ofnormalize_usage()behaves as before: a usage withprompt_tokens_details.cache_creation_tokensset still maps tocache_write_tokens, and a usage where LiteLLM deleted theNoneattribute still normalizes to 0. - With observability enabled, a Responses API response whose usage carries cache writes records
gen_ai.usage.cache_creation_input_tokensequal to the reported cache-write count on the LLM call span.
Activity
docs(verify-sdk): S01-S10 replayed live on 9bf835c