Repository navigation
fix: preserve measured streaming chat token usage - #448
Merged
Merged
Conversation
devSejung
marked this pull request as ready for review
November 5, 2026 16:18
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.
Closes #447. Follow-up to #446.
What Problem This Solves
A successful OpenAI-compatible Chat Completions stream can return a provider token aggregate that the report records as zero. For example, a final usage-only event with total_tokens=42 was dropped when it did not include the input and output split. An empty trailing usage object could also erase a measurement received earlier in the stream.
Why This Change Was Made
Both stream implementations now use the shared usage updater to retain valid provider totals, preserve measured data when a weaker sample arrives, and distinguish absent or partial usage from a measured zero. The existing retained-session report consumes the provenance marker and retains authoritative billed cost separately from token coverage.
User Impact
The usage report keeps the measured total supplied by the model endpoint. A successful response without measured tokens still contributes one retained usage record and reports unknown tokens. A genuine zero is reported as zero. Historical tokens discarded before this change require a separate source to recover.
AI-assisted change.
Evidence
Validation ran in a task-owned Ubuntu 22.04 Docker container with Node 24.18.0 and pnpm 11.15.1. Commit 09eabc0 is based on 0a5f031. The full-index patch SHA-256 was 5907b65953aaad8755a2cd416cb5a949972a125b46283b1941bed3346b0509ae in both Linux and Windows.