fix: map Chat usage onto Responses usage counters - #115
Merged
Conversation
The Responses adapter forwarded the upstream Chat usage object unchanged for non-streamed requests and emitted no usage at all for streamed ones. Clients therefore received the wrong field names for /v1/responses and Codex-style UIs displayed zeroed or missing token counts. Chat usage is now mapped onto the Responses usage shape for both paths, carrying over prompt and completion totals, cached prompt tokens and reasoning tokens, while falling back to zeroed counters when upstream omits usage.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e0b8d66575
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #115 +/- ##
==========================================
+ Coverage 94.77% 94.83% +0.05%
==========================================
Files 21 21
Lines 4251 4300 +49
Branches 1238 1257 +19
==========================================
+ Hits 4029 4078 +49
Misses 222 222
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Two fallback paths in the Chat to Responses usage mapping produced incorrect counters: - When prompt_tokens was absent, only prompt_cache_miss_tokens was used as input_tokens, so cached tokens could exceed the reported input total. The split counters are now summed in that case. - The fallback total added cache creation tokens on top of prompt_tokens, double-counting them because prompt_tokens already includes its cached and created subsets. The fallback is now input plus output.
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.
Problem
/v1/responsesreturned token counts that clients could not read:prompt_tokens/completion_tokenswhere the Responses API contract requiresinput_tokens/output_tokens.response.completedemitted nousageat all.Codex and other Responses-style clients read the
input_tokens/output_tokensfields, so they displayed zeroed or missing token counts even though upstream reported real values.Fix
Added a shared
mapChatUsageToResponseshelper inlib/server/proxy/responses.tsand applied it to both paths:prompt_tokens→input_tokens,completion_tokens→output_tokensinput_tokens_details.cached_tokens(readsprompt_tokens_details.cached_tokens,input_tokens_details.cached_tokens,cache_read_input_tokens, orprompt_cache_hit_tokens)output_tokens_details.reasoning_tokenstotal_tokenspreserved, with a computed fallback when upstream omits itStreaming now captures the aggregated usage from the final upstream Chat chunk and includes it in the downstream
response.completedevent.Verification
bun run lintbun run format:checkbun run typecheckbun run test:coverage— 247 tests passing, 94.01% statements (threshold 90%)bun run buildAdded three regression tests in
tests/server/responses-memory.test.tscovering non-streamed mapping, streamedresponse.completedusage, and the missing-usage fallback.