Repository navigation
fix(streaming): preserve explicit zero usage - #37994
Closed
daleselaji-dev wants to merge 6 commits into
Closed
daleselaji-dev wants to merge 6 commits into
daleselaji-dev wants to merge 6 commits into
Conversation
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
devin-ai-integration
Bot
changed the base branch from
litellm_internal_staging
to
main
September 23, 2026 14:58
Contributor
|
Main already keeps an explicit zero usage count from the provider in streamed responses since #42323, so closing this. Reopen if something is missing |
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.
TLDR
Problem this solves:
How it solves it:
Problem
A client-disconnected streamed response with provider-reported zero usage can be recorded with positive estimated tokens and spend
Root Cause
Usage aggregation used truthiness, so explicit zero values were indistinguishable from missing provider usage
Solution
Track field presence outside the public usage aggregate and preserve zero values when providers supplied them. Keep Anthropic's synthetic message-start cursor eligible for text estimation
Changes
User Flow
Before: a disconnected streamed request with explicit zero usage can receive estimated token counts
After: the same response preserves the provider's explicit zero usage
Relevant issues
Fixes #37992
Testing
python -m pytest tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_cursor.py -q-> 41 passedruff checkon changed production file -> passedpython -m compileallon changed files -> passedgit diff --check-> passedCompatibility/Risk
This preserves existing estimation behavior when usage fields are absent, including Anthropic's synthetic cursor case. It changes only the handling of explicitly supplied zero prompt or completion usage
Notes for Reviewer
The regression uses a non-empty prompt and completion text so the previous truthiness fallback would fail with positive estimates. The internal presence state avoids changing the public aggregate schema
Screenshots / Proof of Fix
No live provider request was run because this local environment has no provider credentials. The focused regression tests exercise the same usage assembly boundary without network access
Type
🐛 Bug Fix
Caveats (if any)
Final Attestation