Skip to content

[Bug] 流式 message_start 的 input_tokens 恒为 0,Claude Code /context 虚高 50 倍(上游 #437) #1

Open

Description

@xiarunjie

@xrj-dsh-agent 请修复这个 bug。

上游 issue: aimod-cc#437(完整复现细节、抓包与数据表证据可用 gh api repos/aimod-cc/Agent2API/issues/437 读取,本仓库是其 fork,代码同源)

现象

  • Claude Code /context 显示 2.6M / 1M(100%),而网关 requests 表实际 prompt_tokens 只有 5 万左右,虚高约 50 倍;用量统计里该模型输入累计也异常(只有 18)。
  • 直接抓 /v1/messages SSE 流:usage 只出现在 message_start(input_tokens 恒为 0)与 message_delta(只有 output_tokens)两帧。

本仓库已确认的代码位置

  • desktop-tauri/src-tauri/server/src/server/core/protocol/anthropic.rs 约 681 行:return json!({ "input_tokens": 0, "output_tokens": 0 }); —— 流式 message_start 的 usage 生成处。
  • 约 692 行附近有非流式的 input 计算(含缓存扣减),可参考其逻辑。

修复要求

  1. 流式 message_start 帧应携带真实 input_tokens(与该请求最终计费的 prompt_tokens 一致;若流式下 input 在首帧时不可得,研究本仓库流式管线里 token 统计的实际来源后再定方案,不要拍脑袋在 message_start 填错误值);
  2. 不改变非流式行为;
  3. 附上验证方式(构造流式请求检查 SSE 帧);
  4. 正常走 PR 流程,PR 描述里附 SSE 帧修改前后的对照。

Activity

  1. xiarunjie commented on Nov 3, 2026

    @xiarunjie
    OwnerAuthor

    @xrj-dsh-agent 模型通道已修复,请开始处理这个 bug。注意:仓库 main 已同步到 v3.3.0-beta.6(3239d12f),请基于该版本的代码工作。

  2. xiarunjie commented on Nov 3, 2026

    @xiarunjie
    OwnerAuthor

    @xrj-dsh-agent 评估与执行模型已配置完毕,请开始修复。

  3. xiarunjie commented on Nov 3, 2026

    @xiarunjie
    OwnerAuthor

    @xrj-dsh-agent 模型已就绪,开始修复。

  4. xiarunjie commented on Nov 3, 2026

    @xiarunjie
    OwnerAuthor

    @xrj-dsh-agent 开始修复。

  5. xrj-dsh-agent commented on Nov 3, 2026

    @xrj-dsh-agent
    Collaborator

    开始修复流式 message_start 的 input_tokens 恒为 0 问题,让首帧携带真实输入 token。

  6. xrj-dsh-agent commented on Nov 3, 2026

    @xrj-dsh-agent
    Collaborator

    On it: 我会基于 main(3239d12)排查流式 message_start 的 input_tokens 来源,修复后附 SSE 前后对照提 PR。

  7. xrj-dsh-agent commented on Nov 3, 2026

    @xrj-dsh-agent
    Collaborator

    This is by an automated agent, not a human.

    PR #3 is ready for review: #3

    The streaming message_start frame now sends a request-body estimate of input_tokens instead of 0 (the exact value only becomes available in the upstream's final usage), while message_delta still reports the billed value. The PR description includes the before/after SSE frame comparison. The fork has no CI checks and the change exceeds the auto-merge size limit, so it needs a manual review and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions