fix: subtract cached_tokens from input_tokens in OpenAI→Claude usage conversion - #5850
fix: subtract cached_tokens from input_tokens in OpenAI→Claude usage conversion#5850sunhatSH wants to merge 2 commits into
Conversation
Walkthrough
ChangesToken accounting fix
Estimated code review effort: 1 (Trivial) | ~5 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.
🧹 Nitpick comments (1)
service/convert.go (1)
235-251: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueVerbose inline rationale comment.
The 16-line comment block is helpful context but noticeably longer than typical inline comments elsewhere in this file. Consider trimming to the essential rationale (why subtract, and the guard condition) and moving the provider-specific narrative to a commit message or doc if not already covered there.
🤖 Prompt for 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. In `@service/convert.go` around lines 235 - 251, Shorten the inline rationale comment in the conversion logic so it keeps only the essential reason for the cached-token subtraction and the guard condition. Update the comment near the prompt_tokens/cache_read_input_tokens handling in convert.go to be concise, and move the provider-specific examples and extended explanation out of the inline comment if needed.
🤖 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.
Nitpick comments:
In `@service/convert.go`:
- Around line 235-251: Shorten the inline rationale comment in the conversion
logic so it keeps only the essential reason for the cached-token subtraction and
the guard condition. Update the comment near the
prompt_tokens/cache_read_input_tokens handling in convert.go to be concise, and
move the provider-specific examples and extended explanation out of the inline
comment if needed.
5836ebe to
39ec3e5
Compare
|
Friendly ping for review. This fixes #4395 which has multiple users confirming the issue. The change is minimal (4 lines of logic + 4 lines of comment in a single function) and has been verified with GLM, DeepSeek, Qwen, and Claude models. |
Previously only CacheWriteTokens > 0 triggered the deduplication of cached tokens from input_tokens. Pure cache-read responses (CachedTokens > 0 but CacheWriteTokens == 0) bypassed the guard, causing input_tokens to double-count cached tokens when summed with cache_read_input_tokens. Now the condition also covers CachedTokens > 0 (pure cache-read), matching HsMirage's analysis in QuantumNous#4395. Fixes QuantumNous#4395
39ec3e5 to
f212f00
Compare
变更描述 / Description
功能概述
修复 OpenAI 兼容上游通过
/v1/messagesAnthropic 格式请求时,input_tokens未扣除缓存部分导致数值虚高的问题。修复覆盖两种场景:CacheWriteTokens==0且CachedTokens>0):原代码漏掉,本次修复补齐背景
OpenAI 兼容上游返回的
prompt_tokens是全量 token 数(包含已缓存的 token),prompt_tokens_details.cached_tokens标明缓存命中数。Anthropic 协议中input_tokens语义是不含缓存的纯输入,缓存单独放在cache_read_input_tokens。旧代码只在
CacheWriteTokens > 0时做减法,纯 cache-read 场景(第二次相同请求命中缓存)绕过了条件,导致input_tokens包含全量 +cache_read_input_tokens再次加上缓存 = 双倍统计,可能触发客户端提前自动压缩上下文。参考:HsMirage 在 #4395 的详细分析。
具体变更
文件:
service/relayconvert/internal/oai_chat/to_claude_messages_resp.gobuildClaudeUsageFromOpenAIUsage()函数:条件从仅CacheWriteTokens > 0扩展为CacheWriteTokens > 0 || CachedTokens > 0测试
新增
TestBuildClaudeUsageFromOpenAIPureCacheRead回归用例:总上下文 = 201 + 15104 = 15305(不双倍统计)
关联
Fixes #4395