Repository navigation
fix(spend): record unpriced savings dollars as NULL, not a fabricated $0.00 - #46695
choksi2212 wants to merge 3 commits into
Conversation
… $0.00 Since the OpenRouter rows left the cost map (BerriAI#45665-BerriAI#45669), every openrouter/* deployment resolves no pricing in compute_savings_spend(): get_model_info() raises, the input-rate lookup collapses to 0.0, and the daily tables record compression_savings_spend = 0.00 while compression_saved_tokens keeps climbing (BerriAI#46677). Savings the request's pricing cannot resolve now report None and are recorded as NULL: - get_cost_per_unit(..., default_value=None) keeps a missing input rate distinguishable from an explicit 0.0 rate; zero saved tokens stay a priced 0.0 and a priced request with missing usage still reports 0.0 - SavingsSpend.compression/prompt_caching/gateway_injected_caching are float | None; autorouter keeps its plain float (an un-routed request is a true zero, unknown estimates are versioned separately) - the rollup queue merges None as no-dollars-from-this-row and never turns an unpriced row into a measured zero - the bulk upsert binds NULL and merges NULL-aware via COALESCE(col + EXCLUDED.col, col, EXCLUDED.col), so an unpriced request neither wipes a priced row nor zeroes an all-unpriced one - the three savings columns on the seven daily spend tables become nullable Float?; readers already coalesce (SUM ignores NULL, the daily activity endpoints apply or 0), so visible totals are unchanged Deployment-configured per-token prices keep pricing savings exactly as before, and billing is untouched (spend still comes from usage.cost). Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
|
Automatic reviews are disabled for this repository. |
|
|
…olumns The root schema.prisma changes in the previous commit must be mirrored byte-for-byte into litellm/proxy/schema.prisma and litellm-proxy-extras/litellm_proxy_extras/schema.prisma for the check-schema-sync CI job. Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
|
charan-rathore
left a comment
There was a problem hiding this comment.
Ran the four touched test files against both trees (pytest, unit suite). With main source under this branch tests, 15 assertions fail - all of them the new unpriced-savings cases (openrouter rows absent from the cost map, a missing model, and the zero-rate versus no-rate split all currently record a fabricated 0.0). On this branch all 370 tests pass. The None propagation through _priced_tokens keeps a genuine zero priced at 0.0 while unpriced savings reach the daily tables as NULL, and leaving autorouter as a plain float stays consistent with the recorded_estimated_autorouter_savings split. The three schema.prisma copies change in sync.
… fields - declare the two merge locals in _add_savings_dollars Final (LIT010) - move the writable-ok suppressions onto the TypedDict field lines they govern, matching the sibling spend fields (LIT012), which also removes the suppression-on-a-comment-line the gate counts as suppressing nothing (LIT013) Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
|
@charan-rathore — thanks again for the approval on Oct 15. Gentle check-in: is anything blocking the merge here, or would this need a second pair of eyes? The full matrix is green at c3f3bd6 (370 targeted + 2645 broad tests, failure set identical to pristine main, plus the real-PG18 e2e scenarios from the PR body). Happy to address anything else. |
|
Friendly merge-check 🙂 — this was approved by @charan-rathore on Oct 15 and the follow-ups are addressed, but it looks like it hasn't landed yet. Is there anything else needed from my side, or would it help if I rebased it against the current main? |
Problem
Since the OpenRouter rows were removed from the cost map (#45665–#45669), every
openrouter/*deployment records its savings dollars as$0.00:usage.cost(OpenRouter's reported total), so the bill doesn't depend on the cost map.compute_savings_spend()prices saved tokens throughget_model_info(), which raises "This model isn't mapped yet" foropenrouter/*(0 rows in the current cost map, verified), and the failure collapses to a flat0.0.The result is exactly what the issue describes:
compression_saved_tokenskeeps climbing inLiteLLM_DailyUserSpendwhilecompression_savings_spend/prompt_caching_savings_spendflatline at0, and since daily rows are priced at write time and never repriced, every unpriced request writes a row that claims a measured zero it never measured. A$0.00reads as "saved nothing"; the truth is "could not price".The same is true for any unmapped model, but the cost-map removals turned it from an edge case (a typo, a private deployment name) into the steady state for a whole provider.
Fix
Savings the request's pricing cannot resolve now report
None, and the write path records them as NULL — the "unpriced" marker — instead of a fabricated0.0:savings.py—get_cost_per_unit(..., default_value=None)keeps a missing input rate distinguishable from an explicit0.0rate (a free model is a price, not an unknown)._priced_tokens()keeps0saved tokens a priced zero. Prompt caching staysNoneonly when no pricing resolves; a priced request with missing/unusable usage keeps reporting0.0as before (that zero is about the usage, not the price).SavingsSpend—compression/prompt_caching/gateway_injected_cachingbecomefloat | None.autorouterkeeps its plainfloat: an un-routed request genuinely has zero auto-router savings, and unknown estimates are already kept apart byrecorded_estimated_autorouter_savings.Rollup queue —
_add_savings_dollars()merges an unpriced figure as "no dollars from this row":None + None = None,None + x = x. A row stays unpriced exactly while every contributing request was unpriced. Transactions that carry no savings keys at all keep their old zero semantics.Bulk upsert — unpriced figures bind as NULL, and the increment becomes NULL-aware:
so an unpriced request landing on a priced row contributes nothing instead of wiping it, a priced request landing on an all-unpriced row prices it, and two unpriced rows stay unpriced.
spendandautorouter_savings_spendkeep their plain increments.schema.prisma— the three savings columns on the seven daily spend tables become nullableFloat?(required for the raw-SQL NULL write;prisma db pushapplies it on startup). Readers need no changes:SUM()ignores NULL and the daily-activity endpoints already coalesce withor 0, so visible totals are exactly the priced dollars — the row just stops claiming a measurement it never made.Deployment-configured per-token prices (
model_infoon the deployment) still price savings exactly as before — that path was already independent of the cost map.Verification
Repro (fails on main, fixed here):
Negative controls — 14 new assertions fail on pristine main and pass on this branch:
_priced_tokensdistinguishes a0.0rate from no rateNone(main:0.0); missing-key keeps old zeroNone, NULL binds for the three figures, the COALESCE fragment is pinnedopenrouter/*payload with 5000 compression-saved tokens queuescompression_savings_spend=Nonewithcompression_saved_tokens=5000intact (main:0.0)Real PostgreSQL 18 (Debian, WSL) — drove the actual
build_bulk_upsert()statements against a liveLiteLLM_DailyTagSpend:SUM()over the tableSuites:
tests/unit/proxy/spend_tracking/test_savings.py132 pass,tests/unit/proxy/db/db_transaction_queue/test_daily_spend_update_queue.py19 pass (incl. the pre-existing same-key exact-dict test unchanged),tests/unit/proxy/db/test_daily_spend_bulk_upsert.py22 pass,tests/unit/proxy/db/test_db_spend_update_writer.py191 pass. The pre-existingtest_unknown_model_fails_open_to_zero/test_missing_model_fails_open_to_zeropins were updated to the new contract — the per-request estimator already distinguished unknown from zero (prompt_caching_savings_for_request(...) is None); this change stops the rollup from collapsing that distinction at the write. ruff check + ruff format clean on all touched source files; basedpyright: 0 diagnostics in the touched hunks.Notes for reviewers
usage.cost/x-litellm-response-cost.0.0rows written before this change are indistinguishable from measured zeros and are not repriced (same as today) — new rows stop adding to that ambiguity.db pushdrops the NOT NULL on those three columns.Closes #46677