Skip to content

fix: skip estimated tokens for abnormal streams without usage (#4168)#6112

Draft
cat0825 wants to merge 1 commit into
QuantumNous:mainfrom
cat0825:codex/fix-4168-stream-no-estimate-billing
Draft

fix: skip estimated tokens for abnormal streams without usage (#4168)#6112
cat0825 wants to merge 1 commit into
QuantumNous:mainfrom
cat0825:codex/fix-4168-stream-no-estimate-billing

Conversation

@cat0825

@cat0825 cat0825 commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

当流式请求异常结束(client_gone/timeout 等)且上游未返回 usage 时,不再使用估算 prompt tokens 结算扣费。正常结束或上游提供 usage 的路径保持不变,避免误伤。

This PR was AI-assisted and manually reviewed before submission.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

docker run --rm -v "$PWD":/work -w /work -e GOMODCACHE=/gomod -e GOCACHE=/gocache golang:1.25.1 go test ./service -run 'TestCalculateTextQuotaSummary'
ok  	github.com/QuantumNous/new-api/service	0.021s

Summary by CodeRabbit

  • Bug Fixes
    • Prevented quota deductions when a streaming response ends abnormally and upstream usage data is unavailable.
    • Preserved estimated token usage for normally completed streams without upstream usage data.
    • Improved quota reporting to accurately reflect zero usage in applicable abnormal termination cases.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 81e5207a-9749-4a7e-b4ef-c9b6df464294

📥 Commits

Reviewing files that changed from the base of the PR and between ad900bb and b04b7f9.

📒 Files selected for processing (2)
  • service/text_quota.go
  • service/text_quota_test.go

Walkthrough

Streaming quota calculation now skips estimated prompt tokens when a stream ends abnormally without upstream usage. Billing metadata records the skipped deduction, while normal completed streams retain estimated usage. Tests cover both behaviors.

Changes

Streaming quota handling

Layer / File(s) Summary
Abnormal stream usage and validation
service/text_quota.go, service/text_quota_test.go
Abnormal streams without upstream usage use zero tokens and quota, append a skipped-billing reason, and are tested against normal completed streams that continue using estimates.

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

Possibly related issues

  • Issue 5235: The changes implement its objective of avoiding estimated usage and zeroing quota when streaming ends abnormally without upstream billing information.

Poem

I’m a bunny guarding tokens tonight,
No usage? Then charges take flight.
A broken stream gets zero to pay,
While normal streams keep estimates in play.
Hop, hop—billing’s clear and right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: skipping estimated tokens for abnormal streams without upstream usage.
Linked Issues check ✅ Passed The changes address #4168 by avoiding estimated prompt-token charging on abnormal stream endings when usage is missing.
Out of Scope Changes check ✅ Passed The diff stays focused on text quota billing logic and matching tests, with no obvious unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

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.

bug: stream 失败(client_gone/timeout)且 completion_tokens=0 时仍按 prompt_tokens 扣费

1 participant