Skip to content

fix: collect usage across Anthropic and Responses SSE events - #23

Merged
xiaYuTian11 merged 2 commits into
xiaYuTian11:masterfrom
Shy7777:fix/streaming-usage-accounting
Sep 11, 2026
Merged

xiaYuTian11 merged 2 commits into
xiaYuTian11:masterfrom
Shy7777:fix/streaming-usage-accounting

Conversation

@Shy7777

@Shy7777 Shy7777 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

改动说明 / 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

  • 未提交真实密钥、内网地址或个人数据
  • 未新增对外网络请求
  • 提交信息符合 Conventional Commits
  • 遵循项目 AGPL-3.0 开源许可

Copilot AI lite review requested due to automatic review settings September 11, 2026 10:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 thread engine/shield_defaults.py
Comment on lines +466 to +468
- SSE:包括 message_start.message.usage 和 response.* 的 response.usage。
用量是累计计数:只更新本次出现的字段,不相加,也不把缺失字段清零。
previous 可传入此前流片段的结果;文本截断不影响跨片段累计。
Comment thread engine/shield_defaults.py Outdated
updates[field] = value
break
# Preserve the existing total-only compatibility path.
if not updates and u.get("total_tokens") is not None:
@xiaYuTian11
xiaYuTian11 merged commit e2e3855 into xiaYuTian11:master Sep 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants