fix(openai): report Responses prompt-cache tokens (TypeScript)#2782
Open
didhd wants to merge 1 commit into
Open
fix(openai): report Responses prompt-cache tokens (TypeScript)#2782didhd wants to merge 1 commit into
didhd wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The TypeScript
OpenAIModelResponses adapter dropped OpenAI prompt-cache usage.mapResponsesEventToSDKbuiltfinalUsagefrominput_tokens/output_tokens/total_tokensonly, sousage.input_tokens_details.cached_tokenswas never surfaced ascacheReadInputTokens. Cache reads were therefore invisible inAgentResultusage metrics and OTel spans (gen_ai.usage.cache_read_input_tokens) for the Responses path, even when the API was returningcached_tokens.This restores parity with the other adapters —
models/bedrock.ts,models/anthropic.ts, andmodels/vercel.tsalready plumbcacheReadInputTokens/cacheWriteInputTokens. TheUsagetype (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_retentionviaparams, whichformatResponsesRequestspreads into the request body); this PR makes the resulting cache reads observable.Changes:
mapResponsesUsage()and call it from both theresponse.completedandresponse.incompletebranches (dedupes the existing mapping).cacheReadInputTokensonly whencached_tokens > 0, matching the optional-field behavior of the other adapters.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 insrc/models/openai/__tests__/responses.test.tscover the cache-hit and no-cache paths.npm run build,npm run lint,npm test)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.