修复 Claude Messages 流式响应中 message_delta.usage.output_tokens 可能为零的问题。#6234
修复 Claude Messages 流式响应中 message_delta.usage.output_tokens 可能为零的问题。#6234CSCITech wants to merge 2 commits into
Conversation
|
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)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughClaude message-delta usage conversion now derives missing output tokens from completion tokens and patches ChangesClaude usage patching
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
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 `@relay/channel/claude/message_delta_usage_patch_test.go`:
- Around line 101-109: Add a regression case alongside the existing
buildMessageDeltaPatchUsage tests where both claudeResponse.Usage.OutputTokens
and claudeInfo.Usage.CompletionTokens are positive but different, and assert
that the returned usage preserves the upstream OutputTokens value rather than
applying the fallback.
🪄 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: c99e79c0-64c9-4dc6-911d-928e16161e98
📒 Files selected for processing (2)
relay/channel/claude/message_delta_usage_patch_test.goservice/relayconvert/internal/claude_messages/to_oai_chat_resp.go
Important
📝 变更描述 / Description
修复 Claude Messages 流式响应中
message_delta.usage.output_tokens可能为零的问题。当上游未在最终
message_delta中提供输出 token、但转换过程中已经累计得到CompletionTokens时,现在会使用累计值补全OutputTokens,并将其同步写回客户端收到的usage.output_tokens。补全过程仅在原值为零且累计值大于零时执行,不会覆盖上游已经返回的有效输出 token。这样可以确保日志、结算用量和客户端收到的最终 usage 保持一致。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
新增两个回归测试,分别覆盖:
CompletionTokens恢复缺失的OutputTokens。message_delta.usage.output_tokens。修复前,两个测试均能稳定复现问题: