fix: log info input_token (chat -> claude model ) - #2670
Conversation
…sing Claude's rendering logs, the two approaches handle input rendering differently.
WalkthroughThe change introduces Claude-specific conditional logic to the token calculation handler. A boolean flag identifies Anthropic channel types, and when price data is unused, cached tokens are subtracted from base tokens only for non-Claude channels. Anthropic channel types additionally receive semantic tagging in the output metadata. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧠 Learnings (3)📓 Common learnings📚 Learning: 2025-06-21T03:37:41.726ZApplied to files:
📚 Learning: 2025-08-05T17:14:17.246ZApplied to files:
🧬 Code graph analysis (1)relay/compatible_handler.go (2)
🔇 Additional comments (1)
✏️ Tip: You can disable this entire section by setting 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: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
relay/compatible_handler.go (1)
338-357: AWS Bedrock with Claude models should also skip cache token subtraction.The code correctly handles the semantic difference for direct Anthropic channels, but incompletely handles Anthropic-compatible implementations. AWS Bedrock supports Claude models (claude-3-sonnet, claude-3-opus, etc.) and uses Anthropic's API format, meaning its
input_tokensalready excludes cached tokens—just like direct Anthropic.Currently, the check
isClaudeUsageSemantic := relayInfo.ChannelType == constant.ChannelTypeAnthropiconly identifies direct Anthropic channels. When requests come through AWS Bedrock with Claude models, the code will incorrectly subtractdCacheTokensanddCachedCreationTokensfrombaseTokens(lines 346–350, 353–357), resulting in undercounting prompt tokens and incorrect quota deductions.Consider checking if the channel type is AWS and the model is a Claude variant, and apply the same no-subtraction logic as for Anthropic.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
relay/compatible_handler.go
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: 9Ninety
Repo: QuantumNous/new-api PR: 1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR `#1247`. PR `#1273` focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
📚 Learning: 2025-06-21T03:37:41.726Z
Learnt from: 9Ninety
Repo: QuantumNous/new-api PR: 1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR `#1247`. PR `#1273` focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
Applied to files:
relay/compatible_handler.go
📚 Learning: 2025-08-05T17:14:17.246Z
Learnt from: neotf
Repo: QuantumNous/new-api PR: 1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.
Applied to files:
relay/compatible_handler.go
🧬 Code graph analysis (1)
relay/compatible_handler.go (2)
constant/channel.go (1)
ChannelTypeAnthropic(18-18)types/price_data.go (1)
PriceData(11-27)
🔇 Additional comments (1)
relay/compatible_handler.go (1)
463-467: LGTM!The tagging correctly marks Claude usage in the consume log metadata, enabling differentiation in log analysis. The implementation is clean and aligns with the token calculation changes above.
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
for chat-based calls to the Claude model, tagging is required. U…sing Claude's rendering logs, the two approaches handle input rendering differently.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.