Skip to content

fix(openai): report Responses prompt-cache tokens (TypeScript)#2782

Open
didhd wants to merge 1 commit into
strands-agents:mainfrom
didhd:fix/ts-responses-cached-tokens
Open

fix(openai): report Responses prompt-cache tokens (TypeScript)#2782
didhd wants to merge 1 commit into
strands-agents:mainfrom
didhd:fix/ts-responses-cached-tokens

Conversation

@didhd

@didhd didhd commented Jun 15, 2026

Copy link
Copy Markdown

Description

The TypeScript OpenAIModel Responses adapter dropped OpenAI prompt-cache usage. mapResponsesEventToSDK built finalUsage from input_tokens / output_tokens / total_tokens only, so usage.input_tokens_details.cached_tokens was never surfaced as cacheReadInputTokens. Cache reads were therefore invisible in AgentResult usage metrics and OTel spans (gen_ai.usage.cache_read_input_tokens) for the Responses path, even when the API was returning cached_tokens.

This restores parity with the other adapters — models/bedrock.ts, models/anthropic.ts, and models/vercel.ts already plumb cacheReadInputTokens/cacheWriteInputTokens. The Usage type (models/streaming.ts) already declares both fields and the accumulator already sums them, so only the Responses adapter needed updating.

It is the TypeScript counterpart of the Python issue/PR (#2407 / #2663). The write side already works (a caller can pass prompt_cache_key / prompt_cache_retention via params, which formatResponsesRequest spreads into the request body); this PR makes the resulting cache reads observable.

Changes:

  • Extract mapResponsesUsage() and call it from both the response.completed and response.incomplete branches (dedupes the existing mapping).
  • Set cacheReadInputTokens only when cached_tokens > 0, matching the optional-field behavior of the other adapters.
  • Add unit tests for a cache hit and the no-cache (cached_tokens: 0) case.

Related Issues

Fixes #2781

Documentation PR

No docs change needed — this aligns the Responses adapter with existing usage/telemetry behavior already documented for the other model providers.

Type of Change

Bug fix

Testing

strands-ts: npm run build (tsc, no errors), npm run lint (0 errors), npm test (full unit-node suite: 124 files / 3396 tests pass, no type errors). The two new tests in src/models/openai/__tests__/responses.test.ts cover the cache-hit and no-cache paths.

  • I ran the TypeScript equivalents (npm run build, npm run lint, npm test)

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

The TypeScript OpenAIModel Responses adapter dropped OpenAI prompt-cache
usage: the stream-state usage mapping only read input_tokens /
output_tokens / total_tokens, so usage.input_tokens_details.cached_tokens
was never surfaced as cacheReadInputTokens. Cache reads were therefore
invisible in AgentResult usage metrics and OTel spans
(gen_ai.usage.cache_read_input_tokens) for the Responses path, even when
the API was returning cached_tokens.

This is the TypeScript counterpart of strands-agents#2407 / strands-agents#2663 (Python) and restores
parity with the Bedrock, Anthropic, and Vercel adapters, which already
plumb cacheReadInputTokens. The Usage type and accumulator already
support the field; only the Responses adapter was not filling it.

- Extract mapResponsesUsage() and use it from both the response.completed
  and response.incomplete branches (dedupes the mapping).
- Set cacheReadInputTokens only when cached_tokens > 0, matching the
  other adapters' optional-field behavior.
- Add unit tests for a cache hit and the no-cache (cached_tokens 0) case.

Fixes strands-agents#2781

Signed-off-by: Sanghwa Na <sanghwa.me@gmail.com>
@github-actions github-actions Bot added size/s bug Something isn't working area-model Related to models or model providers area-otel Open-telemetry related typescript Pull requests that update typescript code labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-model Related to models or model providers area-otel Open-telemetry related bug Something isn't working size/s typescript Pull requests that update typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] OpenAIModel (Responses, TypeScript) drops prompt cache tokens (cached_tokens not plumbed to cacheReadInputTokens)

1 participant