Skip to content

fix(anthropic): preserve reported thinking token usage - #6721

Merged
lidge-jun merged 1 commit into
lidge-jun:devfrom
Ingwannu:ingw/fix-6719-thinking-usage
Oct 8, 2026
Merged

lidge-jun merged 1 commit into
lidge-jun:devfrom
Ingwannu:ingw/fix-6719-thinking-usage

Conversation

@Ingwannu

@Ingwannu Ingwannu commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Closes [Bug]: Anthropic thinking_tokens dropped from usage, so reasoning_tokens is always 0 on Chat/Responses #6719. Preserve Anthropic's reported usage.output_tokens_details.thinking_tokens as OcxUsage.reasoningOutputTokens, so translated Responses and Chat JSON/SSE replies expose the real reasoning_tokens breakdown.
  • Thinking is already a subset of output_tokens: output totals, cache accounting and spend totals are unchanged. No estimate is made from visible/summarized thinking. Missing, malformed, negative, nonfinite or above-total optional detail stays unreported internally without discarding valid totals.
  • The existing cumulative usage merge already carries the final message_delta detail; no stream/retry/authentication behavior is changed. Cover buffered, chunked SSE, terminal-without-message_stop, incomplete/error usage and both downstream encoders. Native Messages passthrough is unchanged.
  • Update the owning structure contract and canonical wire-format reference. The reporter's diagnosis/projection is credited in the commit.
  • Primary contract: Anthropic thinking usage describes the breakdown as part of output and final streaming usage.

Verification

Exact head e0f539ace1d5c3c5e054501b2eca3e0367470fe4, based on trusted dev bbab02ab270e30219dea97fea07471e776d65479; ubuntu, Linux x64, Bun 1.4.0 (34cbb9a40).

  • RED: the finalized new regression file on unchanged production source: 10 pass / 8 fail, with missing reasoningOutputTokens and zero downstream reasoning_tokens. An earlier fixture pass had the same failures; corrected the SSE options argument position and array-valued table cases before this final red control.
  • GREEN: bun scripts/test.ts --parallel=1 tests/adapters/anthropic/anthropic-thinking-usage.test.ts tests/adapters/anthropic/anthropic-error-stop-reason.test.ts tests/adapters/anthropic/anthropic-compatible-stream.test.ts tests/adapters/anthropic/anthropic-reasoning.test.ts tests/test-layout.test.ts tests/test-layout-tooling.test.ts tests/ci-workflows/file-size-ratchet.test.ts --timeout 20000 — 150 pass / 0 fail, 1035 assertions, 7 files. The new 18 cases include real adapter parsers and JSON/SSE Responses-to-Chat projections.
  • bun scripts/structure-ssot.ts — passed. git diff --check — passed. New test registered in both layout inventories; no baseline increase.
  • bun node_modules/typescript/bin/tsc --noEmit — incomplete: the cgroup's 180-second RuntimeMax stopped it; no typecheck success is claimed. No limit increase or retry.
  • All runtime/test commands used the absolute Bun binary inside serial systemd user cgroups: CPUQuota75%, MemoryHigh1280M/MemoryMax1536M, swap0, TasksMax64, IOWeight20, nice10, finite120/180s deadlines. Structure used smaller50%/384M/512M/32/60s caps. Effective properties verified before execution. Each execution had fresh private HOME/CODEX_HOME/OPENCODEX_HOME/TMPDIR and no inherited live credentials; existing dependency trees reused, no install/update.
  • Resource exception: the broad import-connected/full suite and docs production build were not run on this shared host. Typecheck, docs build and broader/platform coverage remain for exact-head CI/independent review. No real provider/account/native-client calls or runtime/service changes. Protected live config/catalog files unchanged. No security/privacy scan executed under the user's prohibition.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

No authentication, credential, provider destination or permission policy is changed. The unchecked box does not claim a scan/sign-off. This PR remains Draft because local typecheck is incomplete, docs build and broader validation are outstanding, and required exact-head CI/independent review must still complete. No merge/release is requested by this PR creation.

Map the upstream reasoning subset without changing inclusive output totals. Cover final cumulative SSE usage and buffered/streamed Chat and Responses projections.

Co-authored-by: daehwanahn <31888220+daehwanahn@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: acc06394-37d4-48bc-b250-9319545a7fa6
📥 Commits

Reviewing files that changed from the base of the PR and between bbab02a and e0f539a.

📒 Files selected for processing (6)
  • docs-site/src/content/docs/reference/proxy-formats.md
  • scripts/test-layout/layout.json
  • src/adapters/anthropic.ts
  • structure/providers-and-adapters.md
  • tests/adapters/anthropic/anthropic-thinking-usage.test.ts
  • tests/fixtures/test-layout-expected.json
 ______________________________________________________________________________________________________________________________________________________________________________________________________
< Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian Kernighan >
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Oct 8, 2026
@lidge-jun
lidge-jun marked this pull request as ready for review October 8, 2026 01:17
@lidge-jun
lidge-jun self-requested a review as a code owner October 8, 2026 01:17
@lidge-jun
lidge-jun merged commit 1a8377d into lidge-jun:dev Oct 8, 2026
33 checks passed
lidge-jun added a commit that referenced this pull request Oct 8, 2026
…atchet (#6733)

#6721 and #6724 each added one explicit registration and together took scripts/test-layout/layout.json to 2000 lines, over the 1999-line NEW_OVERSIZED ratchet, so file-size ratchet: repository fails on dev and on every PR's merge ref. Fold forty single-entry lines into four-per-line rows (the file's existing compact form). The parsed mapping is unchanged; the file drops to 1970 lines.
lidge-jun added a commit that referenced this pull request Oct 8, 2026
…nthropic inbound (#6732)

* fix(responses): relay undeclared passthrough tools for chat and anthropic

* test(layout): keep layout.json under the NEW_OVERSIZED cap after #6721

* test(responses): preserve CR framing coverage by inbound wire
@lidge-jun lidge-jun mentioned this pull request Oct 8, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants