Repository navigation
fix: collect usage across Anthropic and Responses SSE events - #23
Merged
xiaYuTian11 merged 2 commits intoSep 11, 2026
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Two moderate usage-handling issues remain unresolved.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Improves token-usage extraction across Anthropic and Responses SSE events while preserving cumulative streaming usage.
Changes:
- Supports nested usage payloads in SSE events.
- Preserves prior usage across stream chunks.
- Adds regression coverage for parsing, truncation, and invalid counts.
File summaries
| File | Summary |
|---|---|
tests/test_stream_usage.py |
Adds streaming usage regression tests. |
engine/transparent.py |
Preserves usage across stream chunks. |
engine/shield_defaults.py |
Extends usage extraction and merging; two moderate issues remain around zero-field fallback compatibility and retaining initial usage after stream truncation. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+466
to
+468
| - SSE:包括 message_start.message.usage 和 response.* 的 response.usage。 | ||
| 用量是累计计数:只更新本次出现的字段,不相加,也不把缺失字段清零。 | ||
| previous 可传入此前流片段的结果;文本截断不影响跨片段累计。 |
| updates[field] = value | ||
| break | ||
| # Preserve the existing total-only compatibility path. | ||
| if not updates and u.get("total_tokens") is not None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改动说明 / What does this PR do?
有些流式响应已经返回用量,面板却少记了输入 token,或完全没有记录。Anthropic 的输入用量可以放在
message_start.message.usage,而 Responses 完成事件使用response.usage;原来的解析只查看事件顶层usage。这次补齐了这些事件结构,并让流回调保留之前上报的字段。后续事件只含输出用量时,不再把输入清零;累计计数取最新值,避免重复相加。现有 Chat Completions、非流式 JSON 和 total-only 的兼容路径继续保留。
透传模式也会逐行累积 SSE 用量,因此长响应不会再把开头的输入计数挤出 64 KiB 尾部缓存。只保留用量和有上限的未完成行,不缓存整段响应;超长行跳过后仍能继续读取后续用量。
total_tokens与显式零值同时出现时,也保留原有的兼容换算。影响范围 / Scope
验证 / Validation
新增 8 项回归检查,覆盖 Anthropic 分阶段累计用量、Responses 终态事件、已有 JSON 格式、非法计数、零值与总计兼容,以及网络分片和文本留存截断。直接调用透传处理器验证超过 64 KiB 的响应、17 字节分片、超长行、CRLF 和末行无换行,并确认返回字节保持不变。
本地 macOS / Python 3.13:543 项单元测试通过;Python 编译检查、流式和出口代理冒烟、前端构建、Lint、中英文字典对齐及版本一致性检查均通过。使用模拟上游,未调用付费模型。
自检 / Checklist