Skip to content

流式 Chat Completions 缺少 usage chunk,客户端无法显示 token 用量 #19

@Zker67

Description

@Zker67

问题

/v1/chat/completions 的非流式响应会返回 usage,但流式响应在请求包含 stream_options: {"include_usage": true} 时没有发送最终 usage chunk。

这会导致部分兼容 OpenAI 的客户端无法显示上传 token、下载 token 和总 token 数量,尤其是在默认使用流式输出的客户端里更明显。

复现方式

请求示例:

{
  "model": "auto",
  "stream": true,
  "stream_options": {"include_usage": true},
  "messages": [{"role": "user", "content": "Hello"}]
}

当前现象:只返回内容 chunk 和 finish_reason: "stop" 的 chunk,没有 choices: [] 且包含 usage 的最终 chunk。

期望行为

当请求设置 stream_options.include_usage=true 时:

  1. 普通内容 chunk 保持原有 delta 输出,并带 usage: null
  2. 结束前额外输出一个 choices: [] 的 chunk。
  3. 该最终 chunk 包含 prompt_tokenscompletion_tokenstotal_tokens
  4. token 统计逻辑应复用现有非流式响应里的计数函数,保证普通文本、Grok Console 和 Grok app-chat 流式路径行为一致。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions