Skip to content

lmstudio: Fix context wheel by including token usage in streaming responses - #57861

Merged
bennetbo merged 4 commits into
zed-industries:mainfrom
GabrieleAncillai:lmstudio-fix-context-wheel-token-usage
Jul 10, 2026
Merged

bennetbo merged 4 commits into
zed-industries:mainfrom
GabrieleAncillai:lmstudio-fix-context-wheel-token-usage

Conversation

@GabrieleAncillai

Copy link
Copy Markdown
Contributor

LM Studio doesn't show the context token wheel (#53790) because token usage is
never reported in streaming responses.

Causes:

  1. stream_options was missing from the request. Without
    stream_options: { include_usage: true }, the LM Studio API omits usage
    from every streaming chunk entirely.

  2. The event mapper discarded usage data in the final chunk. OpenAI-compatible
    servers send the usage summary in a trailing chunk that has an empty choices
    array. The old guard treated that as an error, so even when usage was present
    it was thrown away before emitting a UsageUpdate event.

Fix:

  • Add StreamOptions { include_usage: bool } and stream_options to
    ChatCompletionRequest, and always set it to true for streaming requests.
  • Move usage handling in LmStudioEventMapper::map_event to run before the
    empty-choices guard, mirroring the OpenAI provider's approach.
  • Add four unit tests for map_event covering the fixed behavior.

Release Notes:

  • Fixed LM Studio not showing the context window usage wheel.
Screenshot_20260527_130449

…ponses

Add stream_options with include_usage: true to the ChatCompletionRequest so
LM Studio returns token usage in streaming responses. Previously, without
this field, the API never included usage data, so the context wheel had nothing
to display.

Also move usage handling in the event mapper to run before the empty-choices
guard. OpenAI-compatible servers send the final usage summary as a chunk with
an empty choices array, so the old guard was discarding usage data instead of
emitting a UsageUpdate event.

Fixes zed-industries#53790
…ponses

Add stream_options with include_usage: true to the ChatCompletionRequest so
LM Studio returns token usage in streaming responses. Previously, without
this field, the API never included usage data, so the context wheel had nothing
to display.

Also move usage handling in the event mapper to run before the empty-choices
guard. OpenAI-compatible servers send the final usage summary as a chunk with
an empty choices array, so the old guard was discarding usage data instead of
emitting a UsageUpdate event.

Fixes zed-industries#53790
@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @gancillai-sofka on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label May 27, 2026
@GabrieleAncillai

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @gancillai-sofka on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@GabrieleAncillai

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @gancillai-sofka on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@GabrieleAncillai

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 27, 2026
@cla-bot

cla-bot Bot commented May 27, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@smitbarmase smitbarmase added the area:ai/lmstudio lmstudio provider label May 29, 2026
@aliozinan

Copy link
Copy Markdown

This is a needed feature. When will it be available? Any updates?

@mlagiewka

Copy link
Copy Markdown

Hey, any updates here? This fix would come very handy

@MatunSh

MatunSh commented Jul 10, 2026

Copy link
Copy Markdown

YES! Please make it happen.
I believe that also causes auto-compaction to not work with lmstudio.

@bennetbo bennetbo added the area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features label Jul 10, 2026

@bennetbo bennetbo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@bennetbo
bennetbo added this pull request to the merge queue Jul 10, 2026
Merged via the queue into zed-industries:main with commit 04d03d1 Jul 10, 2026
34 checks passed
@gancillai-sofka

Copy link
Copy Markdown
Contributor

Thank you @bennetbo !!! 🙏🙌

@aliozinan

Copy link
Copy Markdown

thanks a lot!

jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…ponses (zed-industries#57861)

LM Studio doesn't show the context token wheel (zed-industries#53790) because token
usage is
never reported in streaming responses.

Causes:

1. `stream_options` was missing from the request. Without
`stream_options: { include_usage: true }`, the LM Studio API omits
`usage`
   from every streaming chunk entirely.

2. The event mapper discarded usage data in the final chunk.
OpenAI-compatible
servers send the usage summary in a trailing chunk that has an empty
`choices`
array. The old guard treated that as an error, so even when usage was
present
   it was thrown away before emitting a `UsageUpdate` event.

Fix:
- Add `StreamOptions { include_usage: bool }` and `stream_options` to
`ChatCompletionRequest`, and always set it to `true` for streaming
requests.
- Move usage handling in `LmStudioEventMapper::map_event` to run
*before* the
  empty-choices guard, mirroring the OpenAI provider's approach.
- Add four unit tests for `map_event` covering the fixed behavior.

Release Notes:

- Fixed LM Studio not showing the context window usage wheel. 

<img width="1184" height="1080" alt="Screenshot_20260527_130449"
src="https://github.com/user-attachments/assets/97eb8500-39dd-4824-aaf8-f0422b62119d"
/>

---------

Co-authored-by: Gabriele Ancillai <gabriele.ancillai@sofka.com.co>
Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
playdohface pushed a commit to playdohface/zed that referenced this pull request Aug 29, 2026
…ponses (zed-industries#57861)

LM Studio doesn't show the context token wheel (zed-industries#53790) because token
usage is
never reported in streaming responses.

Causes:

1. `stream_options` was missing from the request. Without
`stream_options: { include_usage: true }`, the LM Studio API omits
`usage`
   from every streaming chunk entirely.

2. The event mapper discarded usage data in the final chunk.
OpenAI-compatible
servers send the usage summary in a trailing chunk that has an empty
`choices`
array. The old guard treated that as an error, so even when usage was
present
   it was thrown away before emitting a `UsageUpdate` event.

Fix:
- Add `StreamOptions { include_usage: bool }` and `stream_options` to
`ChatCompletionRequest`, and always set it to `true` for streaming
requests.
- Move usage handling in `LmStudioEventMapper::map_event` to run
*before* the
  empty-choices guard, mirroring the OpenAI provider's approach.
- Add four unit tests for `map_event` covering the fixed behavior.

Release Notes:

- Fixed LM Studio not showing the context window usage wheel. 

<img width="1184" height="1080" alt="Screenshot_20260527_130449"
src="https://github.com/user-attachments/assets/97eb8500-39dd-4824-aaf8-f0422b62119d"
/>

---------

Co-authored-by: Gabriele Ancillai <gabriele.ancillai@sofka.com.co>
Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ai/lmstudio lmstudio provider area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants