[PR #5450] [Engine] Serve: an asked include_usage also gets OpenAI's separate usage chunk #5430

Open
opened 2026-11-07 16:35:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5450
Author: @wornet-che
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: usage-chunk


📝 Commits (1)

  • 9078cab Serve: a stream asked for include_usage also gets OpenAI separate usage chunk. openai_chunks merged usage into the finish chunk, and a gateway that counts streamed tokens only from the empty-choices chunk of the spec (OpenAI's contract, served that way by llama.cpp) fell back to its character estimate - a tool-call turn logged 0 tokens. The merged usage stays: the web app and openai_collect read it, and openai_collect now skips the choice-less chunk. Test: serve.test_server passes (373 at 9f7d977d, 359 at 0.2.10), the mock stream carries the chunk only when asked for, and two live hosts plus a counting gateway now log exact counts.

📊 Changes

1 file changed (+9 additions, -0 deletions)

View changed files

📝 serve/server.py (+9 -0)

📄 Description

Problem and result: with stream_options.include_usage asked, a stream still never
sends the final usage chunk of its own - the usage only rides on the finish chunk,
whose choices stay filled. Clients that follow OpenAI's contract and llama.cpp's own
behavior, reading usage from the choice-less chunk, see none and fall back to
estimating (the issue linked below measures what that cost us). After the change the
stream ends with the choice-less usage chunk when the request asked for it; a stream
that did not ask, the web app's path and the non-stream path keep their current shape.

Related discussion: https://github.com/Niko1221/Strata/issues/5448

Approach: openai_chunks' final branch already builds the usage onto the finish chunk;
when - and only when - the request asked include_usage, it now yields one further
chunk after it: choices [] with that same usage block. openai_collect, which reuses
the same generator for the non-stream path, skips choice-less chunks: it reads
choices[0] everywhere and would break on a non-stream request that carries
stream_options. The merged usage on the finish chunk stays, because the web app and
openai_collect read it there. serve/server.py only, +8 lines.

Validation:
Passed: python -m unittest serve.test_server at two revisions: 359 tests OK at
ffa80d73 (0.2.10), 373 tests OK at 9f7d977d (current main era), Linux x86_64.
Passed: mock stream check at both revisions (serve/server.py --engine mock):
include_usage asked gives exactly one choice-less usage chunk, last before
[DONE]; not asked gives none; a non-stream request with
stream_options.include_usage answers 200 with usage; a plain non-stream request
is unchanged.
Passed: live serve on two hosts (strata engine, Qwen3.8-Flash-Next IQ3_S):
a stream with include_usage ends with the spec chunk carrying the real
prompt/completion/cached counts; a plain stream carries no choice-less chunk;
non-stream usage intact. Through our gateway, which counts only the spec chunk:
rows flip from estimated (a tool-call turn at 0 completion tokens; 204 of 310
rows in a 24 h window) to exact counts - every served row reports usage, median
1010 completion tokens over the first 17.
Failed: none observed.
Not run: no engine file is touched, so no C++, CUDA, HIP, SYCL or Metal build was
run; the Anthropic /v1/messages path was not exercised; the web app's browser
paths were not clicked through.

Limits: a client that asks for include_usage now sees the usage twice - once on the
finish chunk (Strata's existing behavior, kept for the web app's reader) and once in
the spec chunk. If the finish chunk's usage should go away later, its readers
(openai_collect and the web app) have to move with it; that is beyond this change.

AI assistance: implemented with an agent; the diff and every quoted output were
reviewed line by line by me before submitting.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5450 **Author:** [@wornet-che](https://github.com/wornet-che) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `usage-chunk` --- ### 📝 Commits (1) - [`9078cab`](https://github.com/Niko1221/Strata/commit/9078cab66bd3760209d424e07f70b92fc2448596) Serve: a stream asked for include_usage also gets OpenAI separate usage chunk. openai_chunks merged usage into the finish chunk, and a gateway that counts streamed tokens only from the empty-choices chunk of the spec (OpenAI's contract, served that way by llama.cpp) fell back to its character estimate - a tool-call turn logged 0 tokens. The merged usage stays: the web app and openai_collect read it, and openai_collect now skips the choice-less chunk. Test: serve.test_server passes (373 at 9f7d977d, 359 at 0.2.10), the mock stream carries the chunk only when asked for, and two live hosts plus a counting gateway now log exact counts. ### 📊 Changes **1 file changed** (+9 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+9 -0) </details> ### 📄 Description Problem and result: with stream_options.include_usage asked, a stream still never sends the final usage chunk of its own - the usage only rides on the finish chunk, whose choices stay filled. Clients that follow OpenAI's contract and llama.cpp's own behavior, reading usage from the choice-less chunk, see none and fall back to estimating (the issue linked below measures what that cost us). After the change the stream ends with the choice-less usage chunk when the request asked for it; a stream that did not ask, the web app's path and the non-stream path keep their current shape. Related discussion: https://github.com/Niko1221/Strata/issues/5448 Approach: openai_chunks' final branch already builds the usage onto the finish chunk; when - and only when - the request asked include_usage, it now yields one further chunk after it: choices [] with that same usage block. openai_collect, which reuses the same generator for the non-stream path, skips choice-less chunks: it reads choices[0] everywhere and would break on a non-stream request that carries stream_options. The merged usage on the finish chunk stays, because the web app and openai_collect read it there. serve/server.py only, +8 lines. Validation: Passed: python -m unittest serve.test_server at two revisions: 359 tests OK at ffa80d73 (0.2.10), 373 tests OK at 9f7d977d (current main era), Linux x86_64. Passed: mock stream check at both revisions (serve/server.py --engine mock): include_usage asked gives exactly one choice-less usage chunk, last before [DONE]; not asked gives none; a non-stream request with stream_options.include_usage answers 200 with usage; a plain non-stream request is unchanged. Passed: live serve on two hosts (strata engine, Qwen3.8-Flash-Next IQ3_S): a stream with include_usage ends with the spec chunk carrying the real prompt/completion/cached counts; a plain stream carries no choice-less chunk; non-stream usage intact. Through our gateway, which counts only the spec chunk: rows flip from estimated (a tool-call turn at 0 completion tokens; 204 of 310 rows in a 24 h window) to exact counts - every served row reports usage, median 1010 completion tokens over the first 17. Failed: none observed. Not run: no engine file is touched, so no C++, CUDA, HIP, SYCL or Metal build was run; the Anthropic /v1/messages path was not exercised; the web app's browser paths were not clicked through. Limits: a client that asks for include_usage now sees the usage twice - once on the finish chunk (Strata's existing behavior, kept for the web app's reader) and once in the spec chunk. If the finish chunk's usage should go away later, its readers (openai_collect and the web app) have to move with it; that is beyond this change. AI assistance: implemented with an agent; the diff and every quoted output were reviewed line by line by me before submitting. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#5430
No description provided.