Skip to content

fix(anthropic): preserve thinking token usage - #638

Merged
SantiagoDePolonia merged 2 commits into
ENTERPILOT:mainfrom
hafzism:agent/anthropic-thinking-token-usage
Aug 4, 2026
Merged

fix(anthropic): preserve thinking token usage#638
SantiagoDePolonia merged 2 commits into
ENTERPILOT:mainfrom
hafzism:agent/anthropic-thinking-token-usage

Conversation

@hafzism

@hafzism hafzism commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #629.

Anthropic returns extended-thinking usage under usage.output_tokens_details.thinking_tokens, but the provider's typed response discarded that nested object during JSON decoding. As a result, thinking-token counts never reached persisted usage metadata.

This change:

  • decodes Anthropic's nested thinking-token count;
  • preserves it as the existing normalized completion_reasoning_tokens raw-usage field;
  • retains thinking-token usage across streaming usage merges and emits standard nested details for both Chat Completions and Responses streams;
  • treats the count as a breakdown already included in output tokens, avoiding duplicate charges and false unmapped-token caveats;
  • adds table-driven regression coverage for zero and positive counts, streaming propagation, and base/distinct reasoning rates.

Validation:

  • go test ./internal/providers/anthropic ./internal/usage -count=1
  • go test ./cmd/... ./config/... ./ext/... ./internal/... ./run/... -count=1
  • make lint (0 issues)
  • make mod-check
  • git diff --check

AI Generated (optional)

This PR was developed with Codex assistance. The issue scope, implementation, review follow-ups, tests, and validation results were reviewed before submission.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Anthropic provider parses output_tokens_details.thinking_tokens, stores positive values as completion_reasoning_tokens, exposes them in usage payloads, and includes them in Anthropic cost calculation.

Changes

Anthropic thinking-token accounting

Layer / File(s) Summary
Capture thinking tokens
internal/providers/anthropic/types.go, internal/providers/anthropic/anthropic.go, internal/providers/anthropic/anthropic_test.go
Usage parsing reads thinking_tokens. Raw usage records positive values as completion_reasoning_tokens. Tests validate parsing, omission of zero values, and usage merging.
Emit usage details
internal/providers/anthropic/chat_stream.go, internal/providers/anthropic/responses.go
Chat Completions and Responses API payloads use the shared helper for cache and thinking-token details.
Price thinking tokens
internal/usage/cost.go, internal/usage/cost_test.go
Anthropic cost mapping recognizes reasoning output tokens. Tests cover base output pricing and a configured reasoning-output rate.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: santiagodepolonia

Poem

I’m a rabbit; thinking tokens now appear,
Raw usage keeps each count clear.
Payload details follow the trail,
Cost rules map the thought detail.
Hop, hop—the tests confirm!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes satisfy issue #629 by decoding, preserving, and cost-accounting Anthropic thinking tokens without duplicate charges.
Out of Scope Changes check ✅ Passed All changes support issue #629 and related regression coverage; no unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely describes preserving Anthropic thinking-token usage, which is the main change.
Description check ✅ Passed The description explains the problem, implementation, tests, validation, and linked issue in the required sections.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@hafzism
hafzism marked this pull request as ready for review August 3, 2026 11:58

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/providers/anthropic/anthropic_test.go`:
- Around line 2236-2263: The Anthropic thinking-token tests are not table-driven
and do not cover zero-value normalization or pricing variants. In
internal/providers/anthropic/anthropic_test.go:2236-2263, refactor
TestConvertFromAnthropicResponse_WithThinkingTokens into table-driven cases
asserting zero thinking_tokens omits completion_reasoning_tokens and positive
values preserve it. In internal/usage/cost_test.go:96-112, add table-driven
cases for absent reasoning rates and distinct ReasoningOutputPerMtok values,
asserting base-output adjustment prevents duplicate billing.

In `@internal/providers/anthropic/anthropic.go`:
- Around line 352-354: Update mergeAnthropicUsage to copy nonzero
src.OutputTokensDetails.ThinkingTokens into
dst.OutputTokensDetails.ThinkingTokens and set merged = true, ensuring
buildAnthropicRawUsage can emit completion_reasoning_tokens for streaming usage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 668d5aa3-e0eb-4341-b88b-4ea379ba5265

📥 Commits

Reviewing files that changed from the base of the PR and between 9d54827 and 8b5bf86.

📒 Files selected for processing (5)
  • internal/providers/anthropic/anthropic.go
  • internal/providers/anthropic/anthropic_test.go
  • internal/providers/anthropic/types.go
  • internal/usage/cost.go
  • internal/usage/cost_test.go

Comment thread internal/providers/anthropic/anthropic_test.go
Comment thread internal/providers/anthropic/anthropic.go
@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

The PR is safe to merge; no blocking failure remains.

Focused execution covered normal Anthropic usage conversion, both streaming response formats, and included-token reasoning pricing without identifying a defect.

T-Rex T-Rex Logs

What T-Rex did

    • Authored and ran a focused Go validation covering normal Anthropic response conversion, simulated Chat Completions streaming, simulated Responses streaming, and granular cost calculation.
    • Compared results to the parent commit 8b5bf86 and found the focused stream assertion failed because final chat usage lacked completion_tokens_details.reasoning_tokens.
    • After updating to PR 8eb0530, all focused assertions passed, including normal conversion retaining completion_reasoning_tokens, both stream formats emitted the expected reasoning-token detail, and the 20,000 thinking tokens cost matched the base and alternative pricing rates.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (2): Last reviewed commit: "fix(anthropic): preserve streamed thinki..." | Re-trigger Greptile

@hafzism

hafzism commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

The streaming P1 reported against 8b5bf86 is addressed in 8eb0530: thinking tokens are retained during usage merges and emitted as completion_tokens_details.reasoning_tokens for Chat Completions and output_tokens_details.reasoning_tokens for Responses. The expanded focused tests, full unit suite, and repository-wide lint pass.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@SantiagoDePolonia
SantiagoDePolonia self-requested a review August 4, 2026 13:41

@SantiagoDePolonia SantiagoDePolonia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thank you!

@SantiagoDePolonia
SantiagoDePolonia merged commit 36d38f5 into ENTERPILOT:main Aug 4, 2026
15 checks passed
@SantiagoDePolonia

Copy link
Copy Markdown
Contributor

Merged. Thank you again for your contribution!

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.

Issue: Extended thinking tokens (Anthropic) not captured in usage.raw_data

3 participants