fix: correct token usage conversion between Claude and OpenAI formats - #2744
fix: correct token usage conversion between Claude and OpenAI formats#2744zer0kara wants to merge 5006 commits into
Conversation
chore: update openapi files
chore: update the relay openapi file
- Introduced new OpenAI text models in `common/model.go`. - Added `IsOpenAITextModel` function to check for OpenAI text models. - Refactored token estimation methods across various channels to use estimated prompt tokens instead of direct prompt token counts. - Updated related functions and structures to accommodate the new token estimation approach, enhancing overall token management.
fix: try resolve the high concurrency issue to a single host
…ken-counter feat: refactor token estimation logic
…king-level feat: gemini 3 thinking level gemini-3-pro-preview-high
Increase token name length limit from 30 to 50
- Add model to Claude ModelList - Add model ratio (0.5, $1/1M input tokens) - Add completion ratio support (5x, $5/1M output tokens) - Add cache read ratio (0.1, $0.10/1M tokens) - Add cache write ratio (1.25, $1.25/1M tokens) Model specs: - Context window: 200K tokens - Max output: 64K tokens - Release date: October 1, 2025
fix(adaptor): fix reasoning suffix not processing in vertex adapter
…haiku-4-5 feat: add claude-haiku-4-5-20251001 model support
Increase token name length limit from 30 to 50
fix: Set default to unsupported value for gpt-5 model series requests
fix(go.mod): 更新modernc.org/sqlite依赖项版本
…tible interface; others require conversion.
…f6a98269c242dda05da3ea6d7bc
…3e207853d06cc695ca7903388c9
…f73324da8aebf6a98269c242dda05da3ea6d7bc Revert "fix: video content api Priority use url field"
…t-field fix: openAI function to gemini function field adjusted to whitelist mode
…config feat: TLS_INSECURE_SKIP_VERIFY env
fix: the login method cannot be displayed under the aff link.
…tput-tokens fix: codex Unsupported parameter: max_output_tokens
…s-code feat: customizable automatic retry status codes
…l-index fix: calls to multiple tools in gemini all return index=0
fix: replace Alibaba's Claude-compatible url with new url
…ion-info feat: log shows request conversion
feat(qwen): support qwen image sync image model config
…b852bff73ff4ecd52d73a447981
fix request pass-through aws channels can't test
WalkthroughUpdates token usage calculations in Claude relay and service conversion code to properly account for cached tokens. Specifically, Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
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
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)
service/convert.go (1)
322-326: Prevent negative InputTokens when cached tokens exceed prompt tokens.These subtractions can go negative if upstream usage is inconsistent (e.g., cached tokens reported but prompt tokens missing/underreported), which can break downstream billing or assertions. Clamp to zero before assigning.
🐛 Proposed fix (apply to all four occurrences)
- InputTokens: oaiUsage.PromptTokens - oaiUsage.PromptTokensDetails.CachedTokens, + InputTokens: promptTokensExcludingCache(oaiUsage.PromptTokens, oaiUsage.PromptTokensDetails.CachedTokens),- InputTokens: oaiUsage.PromptTokens - oaiUsage.PromptTokensDetails.CachedTokens, + InputTokens: promptTokensExcludingCache(oaiUsage.PromptTokens, oaiUsage.PromptTokensDetails.CachedTokens),- InputTokens: oaiUsage.PromptTokens - oaiUsage.PromptTokensDetails.CachedTokens, + InputTokens: promptTokensExcludingCache(oaiUsage.PromptTokens, oaiUsage.PromptTokensDetails.CachedTokens),- InputTokens: openAIResponse.PromptTokens - openAIResponse.PromptTokensDetails.CachedTokens, + InputTokens: promptTokensExcludingCache(openAIResponse.PromptTokens, openAIResponse.PromptTokensDetails.CachedTokens),+func promptTokensExcludingCache(promptTokens, cachedTokens int) int { + if cachedTokens < 0 { + cachedTokens = 0 + } + if promptTokens < cachedTokens { + return 0 + } + return promptTokens - cachedTokens +}Also applies to: 350-354, 479-483, 535-538

问题描述
Claude API 的
input_tokens不包含缓存命中的 tokens,而 OpenAI API 的prompt_tokens包含缓存 tokens。当前代码直接将两者等同,导致格式转换时 usage 数据错误。
修复内容
PromptTokens = InputTokens + CacheReadInputTokensInputTokens = PromptTokens - CachedTokens相关PR
#2477
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.