Skip to content

fix(claude): avoid estimating input before message_delta - #7976

Open
zhu1025960209-web wants to merge 1 commit into
QuantumNous:mainfrom
zhu1025960209-web:fix/claude-interrupted-stream-input-usage
Open

zhu1025960209-web wants to merge 1 commit into
QuantumNous:mainfrom
zhu1025960209-web:fix/claude-interrupted-stream-input-usage

Conversation

@zhu1025960209-web

@zhu1025960209-web zhu1025960209-web commented Oct 22, 2026 •

Copy link
Copy Markdown

Agent

  • Tool: OpenAI Codex;代码与回归测试由 AI 辅助生成(AI-assisted)。
  • Tool version: Codex CLI 0.154.0-alpha.6.2。
  • Model (full id): gpt-6-astra。
  • Host (CLI / IDE / GitHub coding agent / other): Codex Desktop (Windows)。
  • Date (UTC): 2026-10-22。

Links

User request

  • Verbatim:

    用我账号提个pr上去

  • Later constraints or corrections from the user (quote, or none): 无;此前相关请求原文:

    再开个子代理看看newapi官方有没有修,有没有人提pr

Out of scope — refuse

  • Matched: no。
  • If yes, what was told to the user (stop here; do not open a PR): 不适用;只修改官方 Claude finalizer 的用量计算,不包含第三方适配、私有协议或透传改动。

Open gate — do not open unless all are satisfied

  • Out of scope (including pass-through-only forwarding): no。
  • Usage / configuration / integration (answered instead of opening): no。
  • Required issue facts present without invention: yes;对应事实在 Claude 流在 message_delta 前中断时仍将整请求估算记为输入用量 #7975。该 Issue 按 .agents/github/ISSUE.md 提交后,被机器人以“未使用标准模板”关闭,并非技术问题已解决;保留原报告,不重复提交。
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes,见下方 RED/GREEN 与回归结果。
  • Body is short and factual; no unfiltered AI-generated text: yes;已独立逐项审阅。
  • Open: yes。
  • If no, what was told to the user (stop here): 不适用;对应事实、独立审阅与 Ubuntu 完整后端测试均已核验。

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

Issue facts

  • Actual behavior: 见 Claude 流在 message_delta 前中断时仍将整请求估算记为输入用量 #7975:官方 main 7fb6e57a 的合成回归中,在 message_delta 前中断且输入/cache 无证据时,finalizer 把请求估算补成普通输入,并进入 canonical usage。
  • Impact: 未经上游输入与缓存分类证实的预估成为消费数量;不是只保留预扣估值。
  • Frequency: 本次实跑的部分文本、零帧、部分工具输出三类均出现 RED;不声明生产发生率。
  • Evidence that the problem is in new-api rather than the client or upstream: 直接执行官方 handler/finalizer,不调用客户端或供应商;只改一个输入赋值条件后相同断言转 GREEN。
  • Applicable types and their fields: 仅函数级 relay/billing 用量复现;代码所属 POST /v1/messages,未发送 HTTP;使用合成模型标识和 SSE 字符串;倍率/数据库不参与目标数量计算,前端与部署升级不适用。完整适用字段与未验证边界见 Claude 流在 message_delta 前中断时仍将整请求估算记为输入用量 #7975。

Change

缺失输入仅在 claudeInfo.Done 为 true 时沿用请求估算。未 Done 时不补普通输入;已有 input/cache、已收到输出的 fallback、工具输出开销及 Done 的原有兼容行为保留。finalize/canonical 从修正后的本地 usage 生成,不会在该路径重新补入请求估值。

Research

Duplicate / prior art

Docs and code

Alternatives considered

  • Option A: 在结算层将异常流整单清零,可能抹掉真实 input/cache/output 或固定价/工具收费。
  • Option B: 仅保护零帧,仍漏掉已收到部分内容的路径。
  • Why this approach: 在产生错误输入量的位置增加一个条件,保留其他用量与结算语义。

Files

Path Why
relay/channel/claude/relay-claude.go 限制缺失输入 fallback;一个条件与解释注释。
relay/channel/claude/relay_claude_test.go 扩展既有表与工具调用回归,断言最终 usage 及 canonical。

Behavior

  • Before: 合成部分文本/零帧分别补入 1200 input;部分工具输出补入 100 input;canonical 同样带入。
  • After: 三类缺输入且未 Done 的普通输入均为 0;部分文本/工具输出的 canonical 输入为 0,零帧保持非 nil 全零 usage 且没有可用 canonical usage。文本输出仍为 2,工具输出计数保留;已报告 input/cache 与 Done 缺 usage 兼容不变。
  • Explicit non-goals / leftover work: Done 仍在任意 message_delta 后为 true,不代表 transport 成功;wire billing snapshot、图片计数、定价、固定价/工具附加费及历史账务不改,不声称所有断流免费。

Verification

  • Commands and results:
    • 在官方基线仅添加测试后:GOWORK=off go test ./relay/channel/claude -run 'TestClaudeStreamFinalUsageEstimatesOnlyUncountedUsage|TestClaudeStreamFallbackCountsInterruptedToolUse' -count=1 -v:目标三类 RED,错误输入为 1200/1200/100;一个生产条件修改后同命令 GREEN,9 个表用例及工具用例通过。
    • GOWORK=off go test ./relay/channel/claude -count=1:全包通过;提交前再次复核 3.660s。
    • GOWORK=off go test ./service -run 'TestCalculateTextQuotaSummary|TestUsageFromOpenAIBillingUsage|TestUsageBillingPathForLog|TestClaudeCacheOnlyUsageRemainsBillable|TestPostTextConsumeQuotaLogsMissingUsageAsStructuredContent' -count=1 -v:22 项顶层测试通过。
    • 在 relaykit、tokenkit 分别执行 GOWORK=off go test ./... -count=1:通过;三个模块分别 go build ./...、go vet ./...:退出 0。
    • Windows 根模块 GOWORK=off go test ./... -count=1:44 包通过、3 包失败;controller 的 SQLite 临时文件清理锁、service affinity 计数串扰、HTTP/2 socket 错误文本断言。官方 HEAD overlay 复现前两类,HTTP/2 单独重测通过;不声称 Windows 全 root 通过。
    • Ubuntu backend CI:精确检出本补丁 51986f9371d9c5c4c63578c8d34e09798c74e6d4,Go 1.26.10 linux/amd64;root、relaykit、tokenkit 分别执行 GOWORK=off go test ./... -count=1 全部通过。验证 workflow 位于 fork 独立分支,不在本 PR diff 中。
    • gofmt -l 两文件无输出,git diff --check 退出 0。
  • Manual steps and observed result: 合成测试源码直接喂入 Claude JSON 事件并停止于 message_delta 前;未调用真实供应商。
  • UI: 不涉及 UI,无截图。
  • Tests added or updated, or why none: 集中扩展现有文件,含零帧、全零首包+部分输出、已有输入、cache read/write/split、Done 缺 usage、工具输出及 canonical。
  • Databases / providers / platforms exercised: Windows amd64 / Go 1.27.1,Ubuntu Linux amd64 / Go 1.26.10;目标回归无 DB/供应商。既有 service 回归使用本地合成 SQLite/miniredis fixture,不是生产结算。
  • Not verified: 真实 HTTP scanner/client abort、真实供应商断流、生产发生率/金额、MySQL/PostgreSQL、macOS。不将 ignored frontend embed placeholder 的 backend build 称为前端构建。

Risks

  • Failure modes: 未 Done 且缺失输入时不估输入,可能少计未报告的真实上游输入;这是拒绝把完整请求估值当作输入消费证据的取舍。
  • Billing / quota / auth impact: 仅改变这一分支的输入数量来源;不修改价格、退款、鉴权或数据库。已报告用量与 wire snapshot 保留。
  • Follow-ups: 真实 scanner/client-abort 传输级验证不在本次函数级回归内;保留上述 Done 与 wire usage 边界。

Scope check

  • Single focused change: yes。
  • Secrets included: no;仅合成 fixture,无客户记录。
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no。

Summary by CodeRabbit

  • Bug Fixes
    • Usage reporting no longer fills in estimated prompt tokens for incomplete or interrupted Claude responses when input usage is missing.
    • For completed responses, estimated prompt tokens can still fill in missing input usage. Completion-token fallback behavior is unchanged.

AI-assisted change with synthetic regression fixtures. Preserve reported usage, cache categories, output fallback, and existing message_delta completion semantics.
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d24a079e-e46e-4670-a58a-f0f9af373da5
📥 Commits

Reviewing files that changed from the base of the PR and between 7fb6e57 and 51986f9.

📒 Files selected for processing: 2
  • relay/channel/claude/relay-claude.go
  • relay/channel/claude/relay_claude_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The Claude stream finalizer now fills missing input usage with estimated prompt tokens only after the stream is done. Tests cover cache usage, interrupted streams, and zero-frame aborts.

Changes

Claude stream usage

Layer / File(s) Summary
Gate estimated input fallback
relay/channel/claude/relay-claude.go, relay/channel/claude/relay_claude_test.go
The finalizer uses estimated prompt tokens for missing input usage only after stream completion. Tests check cache usage, recorded and canonical usage, interrupted streams, and zero-frame aborts.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • QuantumNous/new-api issue 7975 reports the same pre-completion fallback behavior addressed by this change.

Suggested reviewers: calcium-ion

Merge Risk: ⚪ Minimal · up to 51986

The change is ready to merge after normal checks; no unresolved billing issue was established.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main Claude billing change: delaying input-token estimation until message_delta.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

A rabbit checks the stream’s last sign,
Prompt tokens wait until the end.
Cache counts settle into line,
Interrupted totals do not pretend.
Then off I hop, with tests in hand.

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

1 participant