Repository navigation
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: 4 · PR risk: 0/10 Reviews |
Price replies with missing or malformed usage from locally counted tokens so key max_budget still applies.
cursor
Bot
force-pushed
the
cursor/fix-missing-malformed-usage-budget-3bbf
branch
from
November 6, 2026 23:23
5757855 to
83531b0
Compare
…lback Treat empty or null token counts as unreported, include tools and reasoning in the local count, and keep charging completion tokens when the prompt counter raises.
…malformed-usage-budget-3bbf # Conflicts: # tests/unit/litellm_core_utils/test_litellm_logging.py
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
When an OpenAI-compatible upstream omits
usageor returns it malformed, LiteLLM priced the reply at $0 (or 500'd on malformed values), so a key'smax_budgetnever closed on that traffic. This marks missing or unparseable usage the same way streams already mark unreported usage, so cost tracking prices the reply from locally counted tokens while the response body still reports 0/0.Details
TLDR
Problem this solves:
usageis priced at $0max_budgetnever closes on that traffic"oops",null) 500s the client even though the upstream already billedHow it solves it:
Intentional product change: a reply with no usable usage is now charged an estimate instead of $0. The JSON body still reports 0 prompt and 0 completion tokens
User Flow
Before: a key with a hard budget keeps serving an upstream that leaves usage out or mangles it, and every call is free or 500s
usage, or returns{"prompt_tokens":"oops","completion_tokens":null}max_budget: 0.0005x-litellm-response-cost: 0.0, GET https://litellm-domain/key/info shows spend 0.0, and a malformed-usage call returns 500Invalid response objectAfter: the same omitted or malformed calls are charged an estimate, so the budget closes, and malformed usage no longer 500s
max_budget: 0.0005x-litellm-response-cost, then the key gets 422 budget exceeded, GET https://litellm-domain/key/info shows the spend, and a malformed-usage call also returns 200 with a non-zero costRelevant issues
Fixes #50124
This is the non-streamed side of the stream path that already estimates tokens when usage is missing. Open PR #50442 covers omitted usage only. This change also covers malformed usage so that case no longer 500s
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
Verification below was automated through the running proxy HTTP API; no manual UI verification was performed.
Before ran on current upstream main (709b27e) and After on this PR's tip (0c3f3f1), with the same command, config, client and Redis, each on a fresh Postgres 17 database. The proxy applied its own migrations at boot.
The proxy ran two workers (two
Started server processlines, two worker processes) against one local Redis 7.4 used by bothrouter_settingsandgeneral_settings.coordination_redis, flushed before each run. That way the key's spend and budget check go through the shared counters.The upstream is a small local OpenAI-compatible stub. For model
stub-nousageit answers with a normal chat completion and nousagekey. Forstub-malformedit sends"usage": {"prompt_tokens": "oops", "completion_tokens": null}. No real provider was called, see Caveats.Proxy command, config.yaml, the stub and the client
Env var name:
DATABASE_URL.sk-local-testis a throwaway master key for this local run.python stub.py # on :9100 python litellm/proxy/proxy_cli.py --config config.yaml --port 4000 --num_workers 2 --detailed_debug --reload --use_v2_migration_resolver python client.py client.logBefore (709b27e)
Key budget
max_budget: 0.0005returns 200no-usagereturn 200 withx-litellm-response-cost: 0.0and usage{"completion_tokens": 0, "prompt_tokens": 0, "total_tokens": 0}Malformed usage
bad-usagereturns 500After (0c3f3f1)
Key budget
max_budget: 0.0005returns 200x-litellm-response-cost: 0.0038, and the body still says{"completion_tokens": 0, "prompt_tokens": 0, "total_tokens": 0}The stub's reply is long enough that one call costs more than the whole budget, so the key closes after the first call.
Malformed usage
bad-usagereturns 200 withx-litellm-response-cost: 0.0038, and the body usage is still 0/0Tests
8 passedat 0c3f3f1:test_missing_usage_still_returns_a_responsetest_malformed_usage_still_returns_a_responsetest_reported_usage_is_kepttest_a_reply_without_usage_is_priced_from_locally_counted_tokenstest_a_reply_with_malformed_usage_is_priced_from_locally_counted_tokenstest_a_reply_with_empty_usage_is_priced_from_locally_counted_tokenstest_a_reply_with_null_token_counts_is_priced_from_locally_counted_tokenstest_a_reply_whose_provider_reports_zero_tokens_stays_at_zeroWith the base versions of the two source files swapped in, the cost tests price the reply at 0.0 and malformed usage fails before cost tracking runs. The last test checks that a provider's explicit 0/0 still prices at $0. Both test files passed in full locally (903 tests).
Type
🐛 Bug Fix
Caveats (if any)
Medium
Low
Final Attestation