Repository navigation
fix(cost): price a non-streamed reply without usage from counted tokens - #50442
Open
zuraiz-anjum wants to merge 2 commits into
Open
zuraiz-anjum wants to merge 2 commits into
zuraiz-anjum wants to merge 2 commits into
Conversation
Contributor
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
3 of 6 tasks
This branch has not been deployed
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.
Handwritten 1-3 sentence description
[insert handwritten description here]
Details
TLDR
Problem this solves:
usageis priced at $0How it solves it:
UnreportedUsagemarkerIntentional product change: a reply with no usage is now charged an estimate instead of $0. The response body still shows the same 0/0 usage it shows today
User Flow
Before: a key with a hard budget keeps serving an upstream that leaves usage out, and every call is free
usagefrom repliesmax_budget: 0.0005x-litellm-response-cost: 0.0, and GET https://litellm-domain/key/info shows spend 0.0After: the same calls are charged an estimate, so the budget closes
usagefrom repliesmax_budget: 0.0005x-litellm-response-cost, then the key gets 400 budget exceeded, and GET https://litellm-domain/key/info shows the spendRelevant issues
Fixes #50124
This is the non-streamed side of #44230, which restored the token estimate for streams that send no usage
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
No major provider leaves usage out of a normal reply, so the upstream here is a small OpenAI-compatible stub that answers every chat completion with a fixed reply and no
usagekey, streamed or not. With--usageit sends a 12/9 usage block, which is the control. The proxy ran with one worker and no Redis, since the change is in how a single reply is priced and not in shared counters. The budget case used a throwaway PostgresShared setup, the stub, the config and the requests
The response cost case ran without a database and with the same config minus
general_settings. Env var names areLITELLM_MASTER_KEY,LITELLM_LOCAL_MODEL_COST_MAP=Trueand, for the budget case only,DATABASE_URLBefore (2a4f8b1)
Response cost
x-litellm-response-cost: 0.0and usage{"completion_tokens": 0, "prompt_tokens": 0, "total_tokens": 0}"stream": trueandinclude_usagethe last chunk is{"id":"chatcmpl-stub-1794006257060714784","created":1794006257,"model":"no-usage-model","object":"chat.completion.chunk","choices":[],"usage":{"completion_tokens":9,"prompt_tokens":12,"total_tokens":21,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":9},"cost":0.000099}}Key budget
max_budget: 0.0005answers the 20 calls with200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200/key/infoshows spend 0.0, max_budget 0.0005After (fa9664a)
Response cost
x-litellm-response-cost: 9.9e-05, and the body still says{"completion_tokens": 0, "prompt_tokens": 0, "total_tokens": 0}stub-model returned no token usage, so it is priced from locally counted tokens (12 prompt, 9 completion)"stream": trueandinclude_usagethe last chunk is{"id":"chatcmpl-stub-1794006326184869702","created":1794006326,"model":"no-usage-model","object":"chat.completion.chunk","choices":[],"usage":{"completion_tokens":9,"prompt_tokens":12,"total_tokens":21,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":9},"cost":0.000099}}, so the stream path is unchangedx-litellm-response-cost: 9.9e-05and no warningKey budget
max_budget: 0.0005answers the 20 calls with200 200 200 200 200 200 422 422 422 422 422 422 422 422 422 422 422 422 422 422/key/infoshows spend 0.0005939999999999999, max_budget 0.0005Tests
uv run pytest tests/unit/litellm_core_utils/test_litellm_logging.py -k "without_usage or reports_zero_tokens or omits_usage"gives3 failed, 1 passed, 773 deselected, 18 warnings in 30.30sat the merge base with only the tests added and4 passed, 773 deselected, 18 warnings in 23.29sat the tip. Three fail at the merge base because the reply is priced at 0.0, the fourth checks that a provider's explicit 0/0 still prices at $0Type
🐛 Bug Fix
Caveats (if any)
Medium
Low
response.usage == litellm.Usage()is now False for these replies, since pydantic compares the classFinal Attestation