feat(service): 优化Claude缓存创建token处理逻辑 - #6355
Conversation
- 在billing_usage.go中添加对CacheCreationInputTokens的fallback处理, 当原有缓存token为0时使用新的输入token字段 - 在tiered_settle.go中添加对Claude缓存创建token的非零值检查, 避免无效的零值参与计算
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughClaude cache-creation token extraction now has an additional fallback, and tiered settlement only initializes corresponding cache fields for positive values. ChangesClaude cache billing
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
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.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@service/billing_usage.go`:
- Around line 132-141: The Claude cache fallback logic must preserve the
original presence of the 5m and 1h split values before populating fallbacks.
Update the surrounding Claude usage handling to record those original
zero/nonzero states, then use them when assigning CacheCreationInputTokens so
both originally absent splits receive the intended 1h fallback without
misattributing it to 5m. Add coverage for both split values initially being
zero.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 02c5f75e-33b6-4cd7-bf65-5e2c836ed6fe
📒 Files selected for processing (2)
service/billing_usage.goservice/tiered_settle.go
移除重复的条件判断逻辑,将CacheCreationInputTokens的回退赋值集中在5分钟缓存处理中, 并添加注释说明Claude标准缓存TL为5分钟。简化了代码结构,避免了不必要的条件检查。
51fdfc5 to
2b6f1df
Compare
Important
📝 变更描述 / Description
现象
Tiered billing(动态计费)模式下,使用 Claude 上游返回的 billing_usage 时,cache_creation_input_tokens(缓存写入 Token)在上游返回了总量但未拆分 5m/1h 的情况下,被完全漏计,值为0。总费用未包含缓存写入 Token 的费用。
问题根源
上游数据特征: Claude API 返回了 cache_creation_input_tokens: n(总量),但 cache_creation 子对象为 null,没有 ephemeral_5m_input_tokens / ephemeral_1h_input_tokens 拆分字段。
两处级联 Bug:
① service/billing_usage.go — 数据映射层
cacheCreation5m := claudeUsage.GetCacheCreation5mTokens() // 0(CacheCreation==nil)
if cacheCreation5m == 0 {
cacheCreation5m = claudeUsage.ClaudeCacheCreation5mTokens // 0(也被设为0)
}
// cacheCreation5m = 0,总量 n 丢掉了
GetCacheCreation5mTokens() 返回 0(CacheCreation 为 nil),回退到 ClaudeCacheCreation5mTokens 也是 0(因为 relay 代码同样从 GetCacheCreation5mTokens() 赋值),导致 cacheCreation5m = 0,没有继续兜底到 CacheCreationInputTokens 总量。
② service/tiered_settle.go — 计费参数构建层
cc5m := float64(usage.PromptTokensDetails.CacheCreationTokensTotal()) // = n ✅
if usage.UsageSemantic == "anthropic" {
cc5m = float64(usage.ClaudeCacheCreation5mTokens) // = 0 🔴
}
cc5m 先通过 CacheCreationTokensTotal() 正确初始化为 n,然后被 ClaudeCacheCreation5mTokens(0)无条件覆盖,导致表达式计算时 cc=0。
解决方案
修复 ① — 数据映射层添加兜底(service/billing_usage.go)
当拆分字段和旧字段均为 0 时,兜底到 CacheCreationInputTokens 总量:
if cacheCreation5m == 0 && claudeUsage.CacheCreationInputTokens > 0 {
cacheCreation5m = claudeUsage.CacheCreationInputTokens
}
if cacheCreation1h == 0 && claudeUsage.CacheCreationInputTokens > 0 && cacheCreation5m == 0 {
cacheCreation1h = claudeUsage.CacheCreationInputTokens
}
1h 的兜底加 cacheCreation5m == 0 条件,防止总量被两边重复使用。
修复 ② — 计费参数层加防御检查(service/tiered_settle.go)
只当有真实拆分值时才覆盖,否则保留 CacheCreationTokensTotal() 的初始值:
if usage.ClaudeCacheCreation1hTokens > 0 {
cc1h = float64(usage.ClaudeCacheCreation1hTokens)
}
if usage.ClaudeCacheCreation5mTokens > 0 {
cc5m = float64(usage.ClaudeCacheCreation5mTokens)
}
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
已在生产环境验证。

修复前:
修复后:

Summary by CodeRabbit
Summary by CodeRabbit