Skip to content

test(integration): assert the streamed usage.cost matches the spend row on every cost case - #46762

Merged
klu-berri merged 3 commits into
mainfrom
litellm_cost_suite_stream_usage_cost
Oct 15, 2026
Merged

klu-berri merged 3 commits into
mainfrom
litellm_cost_suite_stream_usage_cost

Conversation

@klu-berri

@klu-berri klu-berri commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

How it solves it:

  • assertions.py gains streamed_usage() (the last SSE frame carrying a usage) and assert_streamed_usage_cost()
  • assert_exact and assert_recount call it for every text/event-stream case, in both runners (test_cost_tracking.py and stream_parity)
  • Required on /v1/chat/completions, /v1/completions, /v1/responses when the client got a usage frame and spend is non-zero
  • Checked only when present elsewhere (/v1/messages, passthrough), skipped when the stream carried no usage frame at all
  • No new cases, 172 existing streamed cases now carry the check (169 chat with stream_options.include_usage, 1 completions, 2 responses)

User Flow

Before: an engineer breaks the streamed cost stamp and the cost suite stays green

  1. They make _stamp_streaming_usage_cost return early and run tests/integration/run.py cost
  2. Every streamed case passes, because only the SpendLogs row and the non-streamed header are checked
  3. A client streaming POST https://litellm-domain/v1/chat/completions with stream_options.include_usage gets a final chunk with no usage.cost and nobody notices until an issue is filed

After: the same mutation turns the streamed cases red

  1. They make the same change and run tests/integration/run.py cost
  2. Every streamed chat, completions and responses case that received a usage frame fails with streamed usage.cost None != spend 0.11
  3. The field the client reads is now pinned to the row the proxy bills

Relevant issues

Context for #43946, #43947, #46286, #46364, #46365

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • 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
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Test-only PR, so the proof is the suite against a mutated proxy. The suite boots a real proxy, Postgres, Redis and scripted upstream per worker (INTEGRATION_WORKERS=2 .venv/bin/python tests/integration/run.py cost --results <dir> <node ids>). Mutation: return at the top of _stamp_streaming_usage_cost (litellm/litellm_core_utils/streaming_chunk_builder_utils.py) and _stamp_responses_usage_cost (litellm/responses/streaming_iterator.py). Node ids for the mutation legs: gpt-6.1-sol-chat-parity, gpt-6.1-sol-responses-parity, text-completions-openai-stream-usage, gpt-5.3-codex-stream_no_usage

Before (46913be)

  1. Apply the mutation on main, run the four node ids
  2. Output, the suite cannot see the missing field:
PASSED test_cost_tracking.py::test_case_bills_expected_cost[gpt-5.3-codex-stream_no_usage]
PASSED stream_parity/test_basic.py::...[gpt-6.1-sol-chat-parity]
PASSED stream_parity/test_basic.py::...[gpt-6.1-sol-responses-parity]
PASSED stream_parity/test_basic.py::...[text-completions-openai-stream-usage]
4 passed in 24.55s

After (57b988d)

Mutated proxy

  1. Apply the same mutation, run the same four node ids
  2. Output:
FAILED stream_parity/test_basic.py::...[gpt-6.1-sol-responses-parity]
FAILED stream_parity/test_basic.py::...[gpt-6.1-sol-chat-parity]
FAILED test_cost_tracking.py::test_case_bills_expected_cost[gpt-5.3-codex-stream_no_usage]
FAILED stream_parity/test_basic.py::...[text-completions-openai-stream-usage]
E   AssertionError: gpt-6.1-sol-responses-parity-stream: streamed usage.cost None != spend 0.11
E   AssertionError: gpt-6.1-sol-chat-parity-stream: streamed usage.cost None != spend 0.11
E   AssertionError: gpt-5.3-codex-stream_no_usage: streamed usage.cost None != spend 0.000189
E   AssertionError: text-completions-openai-stream-usage: streamed usage.cost None != spend 1.882e-05
4 failed in 23.68s

Unmutated proxy, full cost group

  1. git checkout -- litellm/, run INTEGRATION_WORKERS=4 .venv/bin/python tests/integration/run.py cost --results /tmp/cost_full (782 tests)
  2. Output: 779 passed, 3 failed. The 3 fail identically on main at 46913be with the same node ids and are unrelated to streaming:
E   AssertionError: bedrock-converse-apac-bare-fallback: x-litellm-response-cost None != expected 0.01287
E   AssertionError: bedrock-invoke-haiku-json: x-litellm-response-cost None != expected 0.00425372
E   AssertionError: gpt-5.6-responses_reasoning-messages_bridged: output_cost 0.05656 != expected 0.06352
  1. Observed usage.cost per endpoint on the unmutated tip: chat 0.11 = spend, completions 1.882e-05 = spend, responses 0.11 = spend, /v1/messages and passthrough carry no cost field

Type

✅ Test

Caveats (if any)

Medium

  • /v1/messages streams carry no usage.cost by default, so those 10 cases are only checked when the field appears. Making them required is a product change, not a test change
  • 14 chat cases stream without stream_options.include_usage, so the client gets no usage frame and the check skips them

Low

Link to Devin session: https://app.devin.ai/sessions/54fde80e36ee41eebb876638a2516124
Open in Devin Desktop: https://app.devin.ai/desktop/session/54fde80e36ee41eebb876638a2516124?variant=devin
Requested by: @kerrylu-berri

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Automatic reviews are disabled for this repository.

@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@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.

@litellm-stampbot

Copy link
Copy Markdown

litellm-stampbot: risk low

Approved at f47e13f.

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Judge (gpt-6-luna): low, probability of high 0.00 (high at or above 0.90)

  • risk: low (probability of high 0.00)

Pushes do not re-run this check. Comment @litellm-stampbot again to re-check the new head.

@litellm-stampbot litellm-stampbot Bot added the risk:low Small, isolated, low-risk change label Oct 15, 2026

@kerry-berri kerry-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low risk by rule and by judgement at f47e13f

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@klu-berri
klu-berri enabled auto-merge (squash) October 15, 2026 21:19
@klu-berri
klu-berri merged commit 8e92a47 into main Oct 15, 2026
139 of 141 checks passed
@klu-berri
klu-berri deleted the litellm_cost_suite_stream_usage_cost branch October 15, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Small, isolated, low-risk change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants