Skip to content

fix: log info input_token (chat -> claude model ) - #2670

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/chat2claude-log
Jan 15, 2026
Merged

fix: log info input_token (chat -> claude model )#2670
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/chat2claude-log

Conversation

@seefs001

@seefs001 seefs001 commented Jan 15, 2026

Copy link
Copy Markdown
Collaborator

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

  • Bug Fixes
    • Improved accuracy of cached token deduction in pricing calculations, ensuring correct quota usage tracking.
    • Enhanced token calculation logic to properly handle different API provider token requirements.
    • Refined token tracking precision for more accurate billing and usage monitoring.

✏️ Tip: You can customize this high-level summary in your review settings.

…sing Claude's rendering logs, the two approaches handle input rendering differently.
@coderabbitai

coderabbitai Bot commented Jan 15, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The 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

Cohort / File(s) Summary
Claude-aware token calculation
relay/compatible_handler.go
Adds isClaudeUsageSemantic flag to detect Anthropic channel types. Modifies cached token subtraction logic to only deduct from baseTokens when not Claude-semantic. Introduces tagging logic that sets other["claude"] and other["usage_semantic"] for Anthropic channels.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A hop through the tokens, a skip through the cache,
Claude now gets special treatment—no need for a dash!
When Anthropic calls, the logic now knows,
To tag and to deduct in semantic flows. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: log info input_token (chat -> claude model)' directly relates to the main change: fixing input token logging for Claude chat calls by introducing Claude-aware token handling logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings


📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5cb9ac and 1d8a11b.

📒 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.


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 and usage tips.

@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: 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_tokens already excludes cached tokens—just like direct Anthropic.

Currently, the check isClaudeUsageSemantic := relayInfo.ChannelType == constant.ChannelTypeAnthropic only identifies direct Anthropic channels. When requests come through AWS Bedrock with Claude models, the code will incorrectly subtract dCacheTokens and dCachedCreationTokens from baseTokens (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

📥 Commits

Reviewing files that changed from the base of the PR and between e5cb9ac and 1d8a11b.

📒 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.

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.

2 participants