[GH-ISSUE #5448] [Engine] serve: an asked include_usage never gets the usage chunk of its own; spec-following clients count 0 tokens #3256

Open
opened 2026-11-07 14:39:12 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @wornet-che on GitHub (Nov 7, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/5448

Problem: a streamed completion asked for stream_options.include_usage never gets
OpenAI's final usage chunk of its own. The usage rides on the finish chunk, whose
choices stay filled (delta {}, finish_reason stop). The contract's last chunk carries
empty choices with the usage block, and llama.cpp's server answers that way. A client
that reads usage from that chunk - the shape the OpenAI docs and llama.cpp clients
expect - sees no usage at all and falls back to estimating.

Environment: Strata 0.2.10 (commit ffa80d73, engine 0.1.56) and current main
(9f7d977d), Linux x86_64. The serving engines run strata with
Qwen3.8-Flash-Next IQ3_S, 262k context. The repro needs no GPU and no model - it
uses the repository's own mock engine.

Reproduce (no GPU, about ten seconds):

  cd Strata        # a plain, unmodified checkout
  python serve/server.py --engine mock --port 8096 &
  curl -sN localhost:8096/v1/chat/completions -H 'Content-Type: application/json' \
    -d '{"model":"m","messages":[{"role":"user","content":"hi"}],"stream":true,"stream_options":{"include_usage":true}}' | grep '^data:' | tail -2

Expected: the second-to-last event is a chunk with "choices": [] carrying the usage
block - OpenAI's streaming contract, as llama.cpp serves it.

Actual: no choice-less chunk is ever sent. Observed tail (ffa80d73, mock engine,
shortened):

  data: {... "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], "usage": {"prompt_tokens": ...
  data: [DONE]

Effect on us, measured on two serving hosts: a gateway that counts completion tokens
only from the spec chunk (ours injects include_usage towards the host
and drops the chunk again for clients that did not ask) accounts every stream from
its character estimate instead. Tool-call-only turns - most of an agent's turns -
carry no content characters, and the estimate gives 0. In a 24 h window, 204 of 310
completion rows for this model logged completion_tokens 0 while the engine's own
DONE lines reported roughly 100 to 1300 generated tokens each; the per-model speed
column fell to about 2 tok/s against a real 40-45 tok/s decode. With a local eight-
line patch that adds the spec chunk, the same gateway logs exact prompt and completion
counts on every row and its usage-reported flag flips.

Reproduction status: reproduced locally - mock engine at two revisions, live serve
on two hosts.

Originally created by @wornet-che on GitHub (Nov 7, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/5448 Problem: a streamed completion asked for stream_options.include_usage never gets OpenAI's final usage chunk of its own. The usage rides on the finish chunk, whose choices stay filled (delta {}, finish_reason stop). The contract's last chunk carries empty choices with the usage block, and llama.cpp's server answers that way. A client that reads usage from that chunk - the shape the OpenAI docs and llama.cpp clients expect - sees no usage at all and falls back to estimating. Environment: Strata 0.2.10 (commit ffa80d73, engine 0.1.56) and current main (9f7d977d), Linux x86_64. The serving engines run strata with Qwen3.8-Flash-Next IQ3_S, 262k context. The repro needs no GPU and no model - it uses the repository's own mock engine. Reproduce (no GPU, about ten seconds): cd Strata # a plain, unmodified checkout python serve/server.py --engine mock --port 8096 & curl -sN localhost:8096/v1/chat/completions -H 'Content-Type: application/json' \ -d '{"model":"m","messages":[{"role":"user","content":"hi"}],"stream":true,"stream_options":{"include_usage":true}}' | grep '^data:' | tail -2 Expected: the second-to-last event is a chunk with "choices": [] carrying the usage block - OpenAI's streaming contract, as llama.cpp serves it. Actual: no choice-less chunk is ever sent. Observed tail (ffa80d73, mock engine, shortened): data: {... "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], "usage": {"prompt_tokens": ... data: [DONE] Effect on us, measured on two serving hosts: a gateway that counts completion tokens only from the spec chunk (ours injects include_usage towards the host and drops the chunk again for clients that did not ask) accounts every stream from its character estimate instead. Tool-call-only turns - most of an agent's turns - carry no content characters, and the estimate gives 0. In a 24 h window, 204 of 310 completion rows for this model logged completion_tokens 0 while the engine's own DONE lines reported roughly 100 to 1300 generated tokens each; the per-model speed column fell to about 2 tok/s against a real 40-45 tok/s decode. With a local eight- line patch that adds the spec chunk, the same gateway logs exact prompt and completion counts on every row and its usage-reported flag flips. Reproduction status: reproduced locally - mock engine at two revisions, live serve on two hosts.
Author
Owner

@wornet-che commented on GitHub (Nov 7, 2026):

PR #5450 carries exactly our tested workaround, with the measurements from this issue.

<!-- gh-comment-id:6701553351 --> @wornet-che commented on GitHub (Nov 7, 2026): PR #5450 carries exactly our tested workaround, with the measurements from this issue.
Author
Owner

@bahooo22 commented on GitHub (Nov 7, 2026):

Confirmed on this fork's serve (engine 0.1.56, Windows 11, RTX 3060 Ti 8 GiB, qwen35moe KAT
IQ4_XS, stream=true, stream_options.include_usage=true): 10 SSE data: frames — the first 9
carry choices=[{..., finish_reason: null}] and no usage, the last carries finish_reason=length
WITH the usage block. There is no final frame with choices=[] and usage, i.e. the shape the
OpenAI docs and llama.cpp clients read; a client that takes usage only from that empty-choices
frame sees none and estimates.

Repro: any streamed request with {"stream": true, "stream_options": {"include_usage": true}};
inspect the last data: frame — choices stays filled.

<!-- gh-comment-id:6705132607 --> @bahooo22 commented on GitHub (Nov 7, 2026): Confirmed on this fork's serve (engine 0.1.56, Windows 11, RTX 3060 Ti 8 GiB, qwen35moe KAT IQ4_XS, `stream=true`, `stream_options.include_usage=true`): 10 SSE `data:` frames — the first 9 carry `choices=[{..., finish_reason: null}]` and no usage, the last carries `finish_reason=length` WITH the usage block. There is no final frame with `choices=[]` and usage, i.e. the shape the OpenAI docs and llama.cpp clients read; a client that takes usage only from that empty-choices frame sees none and estimates. Repro: any streamed request with `{"stream": true, "stream_options": {"include_usage": true}}`; inspect the last `data:` frame — `choices` stays filled.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3256
No description provided.