Repository navigation
fix(llm): request stream usage by default with extra_body override - #822
Merged
lizhengfeng101 merged 2 commits intoSep 10, 2026
Merged
Conversation
Streaming requests currently receive token usage only when the provider volunteers it: OpenAI-compatible servers omit usage from streams unless stream_options.include_usage is set, so streamed reviews silently lose token and cost accounting. Set stream_options.include_usage=true by default when streaming. The existing extra_body passthrough stays in control: an explicit stream_options object replaces the default, an explicit null suppresses the field entirely for gateways that reject stream_options, and non-streaming requests never carry the field (mirroring the alibaba#647 treatment of the "stream" key).
Contributor
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). |
8 of 14 tasks
| // An explicit stream_options in extra_body replaces the default. | ||
| // An explicit null suppresses the field entirely, for gateways | ||
| // that reject stream_options. | ||
| opts = append(opts, openaiopt.WithJSONSet("stream_options", streamOptions)) |
Contributor
There was a problem hiding this comment.
Maybe you could further check whether include_usage is set here to prevent silent disabling of this option.
Contributor
Author
There was a problem hiding this comment.
Good catch — an object configuring only an unrelated stream option (e.g. vLLM's continuous_usage_stats) would indeed silently drop usage accounting. Fixed in 9d026d7: include_usage: true is now merged into any explicit stream_options object unless the config spells out include_usage itself; the explicit-null suppression escape hatch is unchanged. Covered by a new case in TestOpenAIClient_StreamOptionsConfigOverrides.
…ted options An explicit stream_options object in extra_body used to replace the include_usage default wholesale, so configuring an unrelated stream option silently disabled usage accounting. include_usage now stays requested unless the config spells it out (or suppresses stream_options with an explicit null).
stay-foolish-forever
approved these changes
Aug 11, 2026
Githab-capibara
added a commit
to Githab-capibara/open-code-review
that referenced
this pull request
Sep 17, 2026
…libaba#822) * fix(llm): request stream usage by default with extra_body override Streaming requests currently receive token usage only when the provider volunteers it: OpenAI-compatible servers omit usage from streams unless stream_options.include_usage is set, so streamed reviews silently lose token and cost accounting. Set stream_options.include_usage=true by default when streaming. The existing extra_body passthrough stays in control: an explicit stream_options object replaces the default, an explicit null suppresses the field entirely for gateways that reject stream_options, and non-streaming requests never carry the field (mirroring the alibaba#647 treatment of the "stream" key). * fix(llm): keep include_usage on when stream_options configures unrelated options An explicit stream_options object in extra_body used to replace the include_usage default wholesale, so configuring an unrelated stream option silently disabled usage accounting. include_usage now stays requested unless the config spells it out (or suppresses stream_options with an explicit null).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Streaming requests currently receive token usage only when the provider volunteers it. OpenAI-compatible servers omit usage from streams unless
stream_options.include_usageis set, so every streamed review silently loses token/cost accounting (resp.Usagestays nil and the session totals under-report).This PR sets
stream_options: {"include_usage": true}by default when streaming is enabled, while keeping the existingextra_bodypassthrough fully in control:extra_body.stream_options{"include_usage": true}(new default)nullstream_options(some older vLLM/Azure builds)Non-streaming requests never carry the field regardless of config, mirroring how the
streamkey itself is handled (#647): providers rejectstream_optionsunlessstreamis true.Behavioral change: streamed requests now include
stream_options.include_usage: trueby default. Servers that implement stream_options respond with one extra final chunk carrying usage; servers that reject the field can be accommodated withstream_options: nullinextra_body. No change for non-streaming requests.Split out of #806 (which forced
include_usageunconditionally, with no escape hatch) so it can be reviewed independently of the replay design discussion there.Type of Change
How Has This Been Tested?
make testpasses locallyTestOpenAIClient_StreamingRequestsUsageByDefault(default present + usage captured from the final chunk) andTestOpenAIClient_StreamOptionsConfigOverrides(explicit object replaces default / explicit null suppresses the field / non-streaming never sends it)Checklist
go fmt,go vet)extra_body.streamitself is currently undocumented; happy to add a Streaming section to the configuration docs (en/zh/ja/ru) in a follow-up if you'd likeRelated Issues
Related to #806 (split out of it).