lmstudio: Fix context wheel by including token usage in streaming responses - #57861
Conversation
…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
…ub.com/GabrieleAncillai/zed into lmstudio-fix-context-wheel-token-usage
|
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 check |
|
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'. |
|
The cla-bot has been summoned, and re-checked this pull request! |
|
@cla-bot check |
|
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'. |
|
The cla-bot has been summoned, and re-checked this pull request! |
|
@cla-bot check |
|
The cla-bot has been summoned, and re-checked this pull request! |
|
This is a needed feature. When will it be available? Any updates? |
|
Hey, any updates here? This fix would come very handy |
|
YES! Please make it happen. |
|
Thank you @bennetbo !!! 🙏🙌 |
|
thanks a lot! |
…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>
…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>
LM Studio doesn't show the context token wheel (#53790) because token usage is
never reported in streaming responses.
Causes:
stream_optionswas missing from the request. Withoutstream_options: { include_usage: true }, the LM Studio API omitsusagefrom every streaming chunk entirely.
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
choicesarray. The old guard treated that as an error, so even when usage was present
it was thrown away before emitting a
UsageUpdateevent.Fix:
StreamOptions { include_usage: bool }andstream_optionstoChatCompletionRequest, and always set it totruefor streaming requests.LmStudioEventMapper::map_eventto run before theempty-choices guard, mirroring the OpenAI provider's approach.
map_eventcovering the fixed behavior.Release Notes: