test: add reasoning token and adjustGoogleCandidateTokens tests - #1658
Conversation
WalkthroughAdds unit tests for token-usage logic and exports a new helper Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts (beta)
No actionable comments were generated in the recent review. 🎉 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.
Pull request overview
Fixes undercounted totalTokens for the Anthropic provider by ensuring reasoning_output_tokens are included in the total token calculation, aligning billing/analytics/rate-limit inputs with Anthropic’s “extended thinking” usage reporting.
Changes:
- Update Anthropic
extractTokenUsageto computetotalTokensaspromptTokens + completionTokens + reasoningTokens. - Add Vitest coverage for Anthropic token extraction (with/without reasoning tokens, cache tokens, missing usage, zero reasoning tokens).
- Add unit tests for
adjustGoogleCandidateTokensand basic OpenAI usage parsing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| apps/gateway/src/chat/tools/extract-token-usage.ts | Fixes Anthropic totalTokens calculation to include reasoning_output_tokens. |
| apps/gateway/src/chat/tools/extract-token-usage.spec.ts | Adds regression tests covering Anthropic reasoning tokens and related token parsing helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add tests to extract-token-usage.spec.ts verifying: - Anthropic totalTokens does not double-count reasoning tokens (output_tokens already includes reasoning_output_tokens) - Anthropic with absent/zero reasoning tokens - Anthropic cache tokens combined with reasoning tokens - adjustGoogleCandidateTokens deduplication logic Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
9f9c2c9 to
21881ee
Compare
Summary
extract-token-usage.spec.tscovering reasoning token handling andadjustGoogleCandidateTokenstotalTokensis correctly calculated aspromptTokens + completionTokens— Anthropic'soutput_tokensalready includesreasoning_output_tokens, so no separate addition is neededadjustGoogleCandidateTokenshelper for deduplicating thoughts from candidatesContext
Investigation confirmed that Anthropic's API already includes reasoning tokens within
output_tokens, so the existingtotalTokenscalculation is correct. These tests document and guard that behavior.Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit