Skip to content

fix(streaming): preserve explicit zero usage - #37994

Closed
daleselaji-dev wants to merge 6 commits into
BerriAI:mainfrom
daleselaji-dev:litellm_streaming_zero_usage_37992
Closed

daleselaji-dev wants to merge 6 commits into
BerriAI:mainfrom
daleselaji-dev:litellm_streaming_zero_usage_37992

Conversation

@daleselaji-dev

@daleselaji-dev daleselaji-dev commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Streaming providers can report zero billable tokens on a disconnected response
  • The proxy replaces explicit zero usage with local token estimates

How it solves it:

  • Preserve whether prompt and completion usage fields were provided
  • Fall back to token counting only when a field is absent
  • Preserve Anthropic's existing single-token cursor fallback behavior

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

  • Track prompt/completion usage presence during stream aggregation
  • Preserve explicit zero usage without adding new public usage fields
  • Add a regression test for explicit zero prompt and completion usage

User Flow

Before: a disconnected streamed request with explicit zero usage can receive estimated token counts

  1. A client sends a streaming chat completion and disconnects
  2. The provider reports zero prompt and completion usage in the partial response
  3. The proxy estimates tokens locally and records positive usage or spend

After: the same response preserves the provider's explicit zero usage

  1. A client sends the same streaming chat completion and disconnects
  2. The provider reports zero prompt and completion usage in the partial response
  3. The proxy keeps both values at zero and does not bill estimates as provider usage

Relevant issues

Fixes #37992

Testing

  • Baseline: focused streaming usage tests 29 passed
  • Final: 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 passed
  • ruff check on changed production file -> passed
  • python -m compileall on changed files -> passed
  • git diff --check -> passed
  • Full basedpyright -> not verified; the sparse checkout reports existing repository diagnostics
  • Live provider billing flow -> not run; no provider credentials or billable service were used
  • Full proxy/integration suite -> not run locally; CI is running the repository matrix

Compatibility/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)

  • Full basedpyright and integration coverage remain environment/CI dependent
  • Live provider billing was not invoked

Final Attestation

  • The focused regression checks the explicit-zero edge case
  • Existing Anthropic cursor fallback tests pass
  • The change preserves fallback estimation for missing usage fields

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.87500% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...itellm_core_utils/streaming_chunk_builder_utils.py 96.87% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing daleselaji-dev:litellm_streaming_zero_usage_37992 (0eacc03) with litellm_internal_staging (98c5233)

Open in CodSpeed

@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:44
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 14:58
@mateo-berri

Copy link
Copy Markdown
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

@mateo-berri mateo-berri closed this Nov 9, 2026
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.

[Bug]: Client-disconnect partial billing treats explicit zero usage as missing and charges estimated tokens

4 participants