Skip to content

fix(llm): request stream usage by default with extra_body override - #822

Merged
lizhengfeng101 merged 2 commits into
alibaba:mainfrom
Fanzzzd:fix/streaming-usage-accounting
Sep 10, 2026
Merged

lizhengfeng101 merged 2 commits into
alibaba:mainfrom
Fanzzzd:fix/streaming-usage-accounting

Conversation

@Fanzzzd

@Fanzzzd Fanzzzd commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Description

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 every streamed review silently loses token/cost accounting (resp.Usage stays nil and the session totals under-report).

This PR sets stream_options: {"include_usage": true} by default when streaming is enabled, while keeping the existing extra_body passthrough fully in control:

extra_body.stream_options wire behavior
absent {"include_usage": true} (new default)
an object sent as-is, replaces the default
null field omitted entirely — escape hatch for gateways that reject stream_options (some older vLLM/Azure builds)

Non-streaming requests never carry the field regardless of config, mirroring how the stream key itself is handled (#647): providers reject stream_options unless stream is true.

Behavioral change: streamed requests now include stream_options.include_usage: true by default. Servers that implement stream_options respond with one extra final chunk carrying usage; servers that reject the field can be accommodated with stream_options: null in extra_body. No change for non-streaming requests.

Split out of #806 (which forced include_usage unconditionally, with no escape hatch) so it can be reviewed independently of the replay design discussion there.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • make test passes locally
  • New HTTP-boundary tests assert the actual request body: TestOpenAIClient_StreamingRequestsUsageByDefault (default present + usage captured from the final chunk) and TestOpenAIClient_StreamOptionsConfigOverrides (explicit object replaces default / explicit null suppresses the field / non-streaming never sends it)

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) — extra_body.stream itself is currently undocumented; happy to add a Streaming section to the configuration docs (en/zh/ja/ru) in a follow-up if you'd like
  • I have signed the CLA

Related Issues

Related to #806 (split out of it).

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).
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

Comment thread internal/llm/client.go
// 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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could further check whether include_usage is set here to prevent silent disabling of this option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@lizhengfeng101
lizhengfeng101 merged commit 8a300f8 into alibaba:main Sep 10, 2026
1 check passed
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants