Repository navigation
test(integration): assert the streamed usage.cost matches the spend row on every cost case - #46762
Conversation
…ow on every cost case
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Automatic reviews are disabled for this repository. |
|
@litellm-stampbot |
|
|
litellm-stampbot: risk lowApproved at Rules (
Judge (gpt-6-luna): low, probability of high 0.00 (high at or above 0.90)
Pushes do not re-run this check. Comment |
kerry-berri
left a comment
There was a problem hiding this comment.
Low risk by rule and by judgement at f47e13f
…eceived a usage frame
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
TLDR
Problem this solves:
usage.costthe client receivesusage.costdisagrees with the spend row"How it solves it:
assertions.pygainsstreamed_usage()(the last SSE frame carrying ausage) andassert_streamed_usage_cost()assert_exactandassert_recountcall it for everytext/event-streamcase, in both runners (test_cost_tracking.pyandstream_parity)/v1/chat/completions,/v1/completions,/v1/responseswhen the client got a usage frame and spend is non-zero/v1/messages, passthrough), skipped when the stream carried no usage frame at allstream_options.include_usage, 1 completions, 2 responses)User Flow
Before: an engineer breaks the streamed cost stamp and the cost suite stays green
_stamp_streaming_usage_costreturn early and runtests/integration/run.py coststream_options.include_usagegets a final chunk with nousage.costand nobody notices until an issue is filedAfter: the same mutation turns the streamed cases red
tests/integration/run.py coststreamed usage.cost None != spend 0.11Relevant issues
Context for #43946, #43947, #46286, #46364, #46365
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreDelays 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:returnat 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_usageBefore (46913be)
After (57b988d)
Mutated proxy
Unmutated proxy, full cost group
git checkout -- litellm/, runINTEGRATION_WORKERS=4 .venv/bin/python tests/integration/run.py cost --results /tmp/cost_full(782 tests)usage.costper endpoint on the unmutated tip: chat 0.11 = spend, completions 1.882e-05 = spend, responses 0.11 = spend,/v1/messagesand passthrough carry nocostfieldType
✅ Test
Caveats (if any)
Medium
/v1/messagesstreams carry nousage.costby default, so those 10 cases are only checked when the field appears. Making them required is a product change, not a test changestream_options.include_usage, so the client gets no usage frame and the check skips themLow
spend == 0skip the required check, the proxy omitscostat 0 today ([Bug]: Streamed usage.cost is omitted when the computed cost is 0, while the non-streamed call reports 0.0 #43946)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