计费统计脚本按流式返回累加 token,跑了一周发现用量一直是 0,同一个模型用非流式请求 usage 完全正常,chunk 里也确实找不到 usage 字段。
这类报错背后常见的原因有几处,逐个排除就行。流式默认不返回 usage,必须显式带 stream_options.include_usage;总量出现在最后一条 choices 为空的 chunk 里,容易被跳过;部分兼容网关没实现这个参数,加了也拿不到;客户端读到 finish_reason 就 break,最后一帧根本没读。
请求里显式打开 include_usage
curl -N https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"stream":true,"stream_options":{"include_usage":true}}'
再往下把带 usage 的帧单独过滤出来
curl -N https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"stream":true,"stream_options":{"include_usage":true}}' | grep -o '"usage":{[^}]*}' | tail -1
然后确认末尾还有帧可以读
curl -N https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"stream":true,"stream_options":{"include_usage":true}}' | tail -3
剩下的就是用非流式结果交叉核对口径
curl -s https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}' | jq '.usage'
别只看报错消失,验证要多做一步。最后一条 chunk 里 usage 的 total_tokens 与同输入的非流式请求一致,统计表当天数据不再为 0,连续两天金额与后台账单差异在百分之二以内。
补一句。这一帧的 choices 是空数组,很多客户端直接 continue 掉了,统计为 0 往往不是接口问题,而是最后一帧被自己的循环丢掉。这类问题最好一次改到位,反复小改反而容易把原因搅混。
