Skip to content

fix(proxy): reconcile budget reservation to recovered stream cost on failure - #35752

Closed
Hamjaster wants to merge 1 commit into
BerriAI:mainfrom
Hamjaster:fix-recovered-stream-cost-reservation
Closed

Hamjaster wants to merge 1 commit into
BerriAI:mainfrom
Hamjaster:fix-recovered-stream-cost-reservation

Conversation

@Hamjaster

Copy link
Copy Markdown
Contributor

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_hook releases the pre-call reservation at hook entry, which reconciles every reserved counter to zero, and recovered_response_cost is only computed much further down, right before update_database

So 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_budget

How 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_object check that already guarded the value passed to update_database, so the failure spend log still records the same number it did before

Relevant issues

Fixes #35568

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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 proof

The 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_hook computes recovered_response_cost before 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 untouched

Two existing tests asserted the old unconditional release at hook entry; they now assert a reconcile with actual_cost 0.0, which is the same refund. Added test_async_post_call_failure_hook_reconciles_reservation_to_recovered_cost, which is the one that fails if the reservation goes back to being refunded

QA runbook

  1. Configure a priced streaming model and a key with a low max_budget
  2. Start a stream and let several billed chunks arrive
  3. Force a mid-stream provider read error so combined_usage_object and response_cost are both available
  4. Check the failure row in the spend logs records the recovered positive cost, as it did before
  5. Inspect the matching live spend:key:* counter. Before this change the pre-call reservation was refunded and the counter lost that cost; now it holds the recovered amount

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

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-commit is green

…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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR aligns live budget counters with partial streaming costs already recovered and persisted by failure tracking.

  • Computes the guarded recovered response cost before handling the budget reservation.
  • Reconciles an active reservation to recovered cost instead of unconditionally refunding it.
  • Updates existing mocks and adds coverage for positive partial-stream cost reconciliation.

Confidence Score: 5/5

The 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.

Important Files Changed

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

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Hamjaster:fix-recovered-stream-cost-reservation (2a8514b) with litellm_internal_staging (956d517)

Open in CodSpeed

@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:38
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 15:18
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 15:18
@github-actions

Copy link
Copy Markdown
Contributor

#35568 was fixed by commit f4d1f45 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Recovered partial stream cost is released from live budget counters

3 participants