jcode icon indicating copy to clipboard operation
jcode copied to clipboard

OpenAI-compatible Chat streams do not request token usage

Open apierron opened this issue 4 months ago • 1 comments

Problem

OpenAI-compatible Chat requests use streaming, but do not request the terminal usage chunk:

{
  "stream": true
}

Many compatible endpoints only include token counts in a streamed response when the request also contains:

{
  "stream_options": { "include_usage": true }
}

Jcode's existing OpenRouter/OpenAI-compatible stream parser already reads usage.prompt_tokens, usage.completion_tokens, cached-token fields, and emits StreamEvent::TokenUsage. Because the request does not ask for usage, named/direct compatible profiles can finish successfully without reporting tokens for context and cost accounting.

Minimal reproduction

  1. Point a named OpenAI-compatible profile at a mock Chat Completions endpoint.
  2. Send one streamed request and inspect the captured JSON body.
  3. Return a normal terminal SSE usage chunk from the mock server.

Actual: The request contains "stream": true but no stream_options.include_usage, so conforming endpoints may omit the terminal usage chunk.

Expected: OpenAI-compatible Chat streams explicitly request usage, allowing the existing parser to emit token counts.

Acceptance criteria

  • Direct/named OpenAI-compatible Chat requests include "stream_options":{"include_usage":true}.
  • A regression test inspects the serialized request body.
  • The existing stream parser continues to emit prompt, completion, cache-read, and cache-creation token fields when supplied.
  • Existing extra_body merge/override behavior remains explicit and tested.
  • No change is required to native OpenAI Responses requests or non-streaming providers.

apierron avatar Jul 21 '26 18:07 apierron

I traced the missing totals to compatible Chat Completions streams not requesting the terminal usage chunk. PR #536 adds stream_options.include_usage: true for direct and named compatible routes, preserves explicit extra_body overrides, and leaves native OpenRouter requests unchanged.

The direct-route, override, named-profile regressions, and the combined TUI selfdev build pass.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Jul 21 '26 19:07 1jehuang