流式请求中 client_gone 导致 New API 计费为 0,但上游已正常扣费且客户端收到完整响应
Author: jixishiCreated Aug 28, 2026Updated Sep 17, 2026
Labelsbugneeds reproduction / insufficient info
Deployment source
Repository release / official image (self-hosted)
Your current newapi version
v1.0.0-rc.25
Submission Checks
- Non-duplicate issue: I have searched existing Issues and confirmed there are no similar issues.
- Read this first: I have fully read the section above, reviewed the docs at https://docs.newapi.ai/ and the project README, and asked AI first, confirming this is not a usage, configuration, or integration question.
- Supported version: I have provided an exact version, commit, or image tag (not
latestorunknown) and confirmed that the issue reproduces on an unmodified, supported version from this repository. - Not a third-party service: I confirm that this issue is not exclusive to a third-party hosting site, relay, API service, or fork that has not been verified against the unmodified repository. Third-party instance issues must be reported to their operator.
- Issue attribution: I have provided evidence that distinguishes the client, new-api, and upstream layers. For relay issues, I compared equivalent redacted requests sent directly upstream and through new-api, confirming that new-api introduces or changes the error rather than merely forwarding the upstream error unchanged.
- Channel and protocol boundary: I confirm that the issue is not caused by a Coding Plan service, reverse-engineered channel, third-party API wrapper, Codex reverse-proxy endpoint, or behavior specific to the Codex API. If first observed through such an interface, I have reproduced it using a standard API protocol supported by this repository.
- Maintainer time: I understand that maintainers have limited time and issues that do not follow this template may be ignored or closed directly.
Issue Description
当流式请求(stream: true)在首字已成功返回给客户端后,因网络波动、客户端超时或用户主动取消等原因导致连接断开时,New API 记录 client_gone 并立即取消上游请求(终止向上游 Sub2API 的读取)。由于上游响应被中断,New API 无法获取最终的 usage 信息,便将本次请求标记为失败,退还预扣配额,因此消费记录中显示 费用为 $0,Tokens 为 0。
但上游 Sub2API 已经完整处理了该请求,生成了完整响应,并已按实际用量计费(可从 Sub2API 后台账单核对)。客户端在断开前通常已收到大部分甚至全部内容,导致用户认为服务已正常提供,而 New API 却显示未计费,造成账目不一致。
影响范围
- 财务对账困难:用户未在 New API 内扣费,但上游 Sub2API 实打实花了钱,需要人工逐一核对。
- 影响流式长回复场景,在网络不稳定的环境下触发频率较高。
- 从截图可见,多条请求 New API 显示
$0,而客户端显示了正数的 token 和费用。
发生频率
[请填写:例如"在响应长度超过500 tokens的流式请求中,约5%~10%出现此情况"]
证明问题出在 New API 层的证据
- 直接上游对比:将相同请求直接发往 Sub2API(绕过 New API),使用同样的客户端,Sub2API 在完整处理后记录用量并扣费,其后台账单可查。
- 经 New API 后:同样条件下,New API 日志出现
client_gone,上游被取消,消费记录显示$0,relay 日志中无usage信息。 - 客户端侧证据:截图显示客户端成功显示了每条请求的 token 数和费用,而 New API 列表却显示同样时间点的多条请求费用为
$0。
Steps to Reproduce
启动 New API v1.0.0-rc.25,配置 Sub2API 为上游渠道。
使用Codex客户端并使用gpt-5.6-sol模型进行开发到有较大上下文时new api后台概率出现0 token计费
同时查看sub2api可见正常扣费
Expected Result
当客户端在首字已返回后New API 不应立即取消上游请求,而应继续读取直到上游流正常结束,获取 usage 后按实际用量扣费;或设置合理超时(如 30 秒)后仍无 usage 再退款。这样消费记录才能与上游 Sub2API 账单保持一致。
Related Screenshots
Source: QuantumNous/new-api