Skip to content

fix: map Chat usage onto Responses usage counters - #115

Merged
orangeboyChen merged 2 commits into
mainfrom
codex/fix-responses-usage-mapping
Sep 9, 2026
Merged

fix: map Chat usage onto Responses usage counters#115
orangeboyChen merged 2 commits into
mainfrom
codex/fix-responses-usage-mapping

Conversation

@orangeboyChen

Copy link
Copy Markdown
Owner

Problem

/v1/responses returned token counts that clients could not read:

  • Non-streamed: the upstream Chat usage object was forwarded verbatim, so callers received prompt_tokens / completion_tokens where the Responses API contract requires input_tokens / output_tokens.
  • Streamed: response.completed emitted no usage at all.

Codex and other Responses-style clients read the input_tokens / output_tokens fields, so they displayed zeroed or missing token counts even though upstream reported real values.

Fix

Added a shared mapChatUsageToResponses helper in lib/server/proxy/responses.ts and applied it to both paths:

  • prompt_tokensinput_tokens, completion_tokensoutput_tokens
  • cached prompt tokens → input_tokens_details.cached_tokens (reads prompt_tokens_details.cached_tokens, input_tokens_details.cached_tokens, cache_read_input_tokens, or prompt_cache_hit_tokens)
  • reasoning tokens → output_tokens_details.reasoning_tokens
  • total_tokens preserved, with a computed fallback when upstream omits it
  • zeroed counters when upstream omits usage entirely

Streaming now captures the aggregated usage from the final upstream Chat chunk and includes it in the downstream response.completed event.

Verification

  • bun run lint
  • bun run format:check
  • bun run typecheck
  • bun run test:coverage — 247 tests passing, 94.01% statements (threshold 90%)
  • bun run build

Added three regression tests in tests/server/responses-memory.test.ts covering non-streamed mapping, streamed response.completed usage, and the missing-usage fallback.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread lib/server/proxy/responses.ts Outdated
Comment thread lib/server/proxy/responses.ts Outdated
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.83%. Comparing base (7845b54) to head (8330d0a).
⚠️ Report is 4 commits behind head on main.
✅ All tests successful. No failed tests found.

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              
Flag Coverage Δ
unittests 94.83% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.
@orangeboyChen
orangeboyChen enabled auto-merge (squash) September 9, 2026 06:03
@orangeboyChen
orangeboyChen merged commit d7e6d06 into main Sep 9, 2026
6 of 7 checks passed
@orangeboyChen
orangeboyChen deleted the codex/fix-responses-usage-mapping branch September 9, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant