Repository navigation
Conversation
…failure The failure hook recovers partial usage and cost from a stream that died after delivering chunks, and writes that cost to the spend log. The live budget counters never saw it: the hook reconciled the pre-call reservation to zero at entry, well before recovered_response_cost was computed further down. So the shared spend counters gave back the whole reservation and picked up nothing in its place, and the recovered cost only landed once the async DB writes flushed and a later counter repair noticed. In that window a key can keep serving past its max_budget. Compute the recovered cost first and reconcile the reservation to it instead of releasing it. Nothing recovered still means a full refund, so non-stream failures and streams that died before any chunk behave exactly as before.
|
|
Greptile SummaryThe PR aligns live budget counters with partial streaming costs already recovered and persisted by failure tracking.
Confidence Score: 5/5The PR appears safe to merge because failure-path live counters now retain recovered partial streaming spend while ordinary failures still receive a full refund. The reconciliation helper preserves zero-cost release semantics, applies positive recovered cost consistently across reservation entries, and is guarded against repeated finalization; the tests cover both zero-cost and recovered-cost paths.
|
| Filename | Overview |
|---|---|
| litellm/proxy/hooks/proxy_track_cost_callback.py | Replaces unconditional failure-path reservation release with idempotent reconciliation to the same guarded partial cost used by spend logging; no actionable regression was found. |
| tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py | Updates prior release expectations without weakening refund coverage and adds focused coverage for positive recovered-cost reconciliation. |
Reviews (1): Last reviewed commit: "fix(proxy): reconcile budget reservation..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
TLDR
Problem this solves:
The failure hook already recovers partial usage and cost from a stream that died after delivering chunks, and it writes that cost to the spend log. The live budget counters never see it.
async_post_call_failure_hookreleases the pre-call reservation at hook entry, which reconciles every reserved counter to zero, andrecovered_response_costis only computed much further down, right beforeupdate_databaseSo the shared counters give the whole reservation back and pick up nothing in its place. The recovered cost lands only once the asynchronous DB writes flush and a later counter repair notices, and in that window a key can keep serving past its
max_budgetHow it solves it:
Compute the recovered cost first and reconcile the reservation to it instead of releasing it. Nothing recovered still means a full refund, so plain failures and streams that died before any chunk behave exactly as they did
The computation moved up but did not change; it is the same
combined_usage_objectcheck that already guarded the value passed toupdate_database, so the failure spend log still records the same number it did beforeRelevant issues
Fixes #35568
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
I could not give you the live-proxy curl proof this repo prefers. Reproducing it needs a priced streaming model, a key with a low
max_budget, and a forced mid-stream provider read error so the recovered cost is non-zero, and I have no provider key or database to point a real proxy at. Saying that straight rather than dressing a test run up as proofThe QA runbook below is what I would run if I had a keyed proxy, and the regression test covers the counter path in the meantime
Type
🐛 Bug Fix
Changes
async_post_call_failure_hookcomputesrecovered_response_costbefore touching the reservation and reconciles to that value rather than calling the release helper. The exception message now says reconcile instead of release. The invalidate-on-failure fallback is untouchedTwo existing tests asserted the old unconditional release at hook entry; they now assert a reconcile with
actual_cost0.0, which is the same refund. Addedtest_async_post_call_failure_hook_reconciles_reservation_to_recovered_cost, which is the one that fails if the reservation goes back to being refundedQA runbook
max_budgetcombined_usage_objectandresponse_costare both availablespend:key:*counter. Before this change the pre-call reservation was refunded and the counter lost that cost; now it holds the recovered amountFinal Attestation
An AI coding agent was used while preparing this change. I reviewed the diff, mutation-checked the new test by reverting the reconcile back to 0.0 and confirming it fails, and
make pre-commitis green