Skip to content

test(main): pin what a streamed response costs, end to end - #37812

Merged
yuneng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_streamed_response_cost_path
Aug 22, 2026
Merged

test(main): pin what a streamed response costs, end to end#37812
yuneng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_streamed_response_cost_path

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Streamed chunks reach a spend row through stream_chunk_builder
  • No test in test_main.py asserted a dollar figure on that path
  • Zeroing the reported usage there leaves all 86 green

How it solves it:

  • Three cases that assert the number, not that it is positive
  • Streamed and whole responses must bill the same usage the same

User Flow

No end-user behavior changes. A developer streaming a completion keeps seeing the
same spend on https://litellm-domain/ui/?page=logs, and a change that would zero
it now fails before it ships.

Relevant issues

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Shared setup: the chunk builder is rewritten to discard the token counts the
provider reported in its usage chunk, one field at a time, and the mapped test
file is run against it.

in litellm/litellm_core_utils/streaming_chunk_builder_utils.py
    prompt_tokens = usage_chunk.get("prompt_tokens", 0) or 0     ->  prompt_tokens = 0
    completion_tokens = usage_chunk.get("completion_tokens", 0) or 0  ->  completion_tokens = 0

uv run pytest tests/test_litellm/test_main.py -q

Before (ff02d5c)

  1. uv run pytest tests/test_litellm/test_main.py -q
86 passed, 11 warnings
  1. Both rewrites, against the whole file:
SURVIVED  reported prompt tokens dropped
SURVIVED  reported completion tokens dropped

kill rate: 0/2
  1. So a build that bills every streamed request at $0 ships green

After (34930e8)

  1. uv run pytest tests/test_litellm/test_main.py -q
90 passed, 11 warnings in 1.13s
  1. The same two rewrites:
KILLED    reported prompt tokens dropped
KILLED    reported completion tokens dropped

kill rate: 2/2

Type

✅ Test

Caveats (if any)

  • The expected figure is recomputed from model_cost, not hard-coded
  • A non-zero guard runs first, so all-zero prices cannot satisfy it
  • The no-usage case asserts the relationship, so a tokenizer bump is fine
  • Text streams only; tool calls and reasoning tokens stay unpinned

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This test-only PR adds end-to-end regression coverage for streamed completion cost accounting.

  • Verifies provider-reported prompt and completion token usage survives stream reconstruction.
  • Confirms streamed and non-streamed responses with identical usage receive the same cost.
  • Covers tokenizer-based billing when a stream contains no provider-reported usage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
tests/test_litellm/test_main.py Adds local streamed-response cost tests without changing production behavior; no eligible follow-up finding remains.

Reviews (2): Last reviewed commit: "test(main): pin what a streamed response..." | Re-trigger Greptile

@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Rebuilding a streamed response and pricing it is the path a spend row comes
from, and nothing asserted it end to end. Reversing either half of the usage
the provider reported left the file green.

Three cases: the rebuilt response bills the usage the last chunk carried,
streaming and not streaming bill the same usage the same, and a stream that
reported no usage is still billed rather than dropped.

The cost is asserted against the catalog prices the run itself reads, with a
non-zero guard in front of it so an all-zeros lookup cannot satisfy it
vacuously. Pinning the dollar figure as a literal would have made a routine
gpt-4o price update fail a test about usage reconstruction.
@yuneng-berri
yuneng-berri force-pushed the litellm_streamed_response_cost_path branch from 12321c8 to 34930e8 Compare August 21, 2026 10:58
@yuneng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@yuneng-berri
yuneng-berri merged commit b416bda into litellm_internal_staging Aug 22, 2026
69 checks passed
@yuneng-berri
yuneng-berri deleted the litellm_streamed_response_cost_path branch August 22, 2026 03:15
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.

3 participants