fix(anthropic): preserve thinking token usage - #638
Conversation
📝 WalkthroughWalkthroughThe Anthropic provider parses ChangesAnthropic thinking-token accounting
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (5)
internal/providers/anthropic/anthropic.gointernal/providers/anthropic/anthropic_test.gointernal/providers/anthropic/types.gointernal/usage/cost.gointernal/usage/cost_test.go
Confidence Score: 5/5The 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.
What T-Rex did
Reviews (2): Last reviewed commit: "fix(anthropic): preserve streamed thinki..." | Re-trigger Greptile |
|
The streaming P1 reported against 8b5bf86 is addressed in 8eb0530: thinking tokens are retained during usage merges and emitted as |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
SantiagoDePolonia
left a comment
There was a problem hiding this comment.
LGTM, thank you!
|
Merged. Thank you again for your contribution! |
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:
completion_reasoning_tokensraw-usage field;Validation:
go test ./internal/providers/anthropic ./internal/usage -count=1go test ./cmd/... ./config/... ./ext/... ./internal/... ./run/... -count=1make lint(0 issues)make mod-checkgit diff --checkAI Generated (optional)
This PR was developed with Codex assistance. The issue scope, implementation, review follow-ups, tests, and validation results were reviewed before submission.