Repository navigation
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
bd9d1be to
942f482
Compare
|
Updated and rebased this pull request; @greptile-apps, please review the latest changes and flag any remaining concerns. |
There was a problem hiding this comment.
Pull request overview
This PR backports a fix to the router budget limiter so Redis increment pipeline flushes are awaited before sync reads, preventing stale Redis values from overwriting in-memory spend. It also initializes the same queue lock in the proxy model-budget limiter subclass and adds regression tests covering the race and failure requeue behavior
Changes:
- Await Redis increment pipeline flushes (with a locked queue snapshot) before performing Redis sync reads
- Add a per-instance lock for the Redis increment queue, including in
_PROXY_VirtualKeyModelMaxBudgetLimiter - Add regression tests for awaiting the pipeline, requeueing on failures, and preserving new increments during a failed flush
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| litellm/router_strategy/budget_limiter.py | Adds a lock-protected increment queue snapshot and awaits Redis pipeline before sync reads |
| litellm/proxy/hooks/model_max_budget_limiter.py | Initializes the inherited Redis increment queue lock in the subclass initializer |
| tests/test_litellm/router_strategy/test_budget_limiter.py | Adds regression tests for the sync race and requeue semantics on Redis pipeline failure |
| tests/proxy_unit_tests/test_unit_test_max_model_budget_limiter.py | Adds a regression test ensuring the subclass initializes the lock used by the inherited increment path |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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 |
72d95d8 to
6498d2f
Compare
|
Rebased onto latest staging and addressed the Copilot and Veria review notes in 6498d2f93b. @greptile-apps please re-review |
6498d2f to
5bfcc99
Compare
|
Rebased onto latest litellm_internal_staging so lint can load .github/actions/detect-changes. |
|
Follow-up: extracted Redis copy-after-flush so C901 stays within the strict budget. |
|
Addressed the empty-flush budget sync bug so later Redis increments are not dropped. |
fb82c4f to
45a7bf9
Compare
|
Rebased onto main and verified focused tests locally. @greptileai @veria-ai please review the current tip for regressions bugbot run |
|
Resolved the CI compatibility findings and pushed the updated tip. @greptileai @veria-ai please review these latest changes bugbot run |
|
bugbot run |
|
@greptileai @veria-ai Please review tip 7e82f1f. The duplicate test file is removed, and 89 focused tests plus make check pass |
|
bugbot run Please review tip 7e82f1f. The Redis tests now live in the mapped hot-path file, with behavior unchanged |
|
@veria-ai Please review tip 7e82f1f. Veria skipped this test-only follow-up; all required CI and patch coverage now pass |
|
@greptileai @veria-ai Please review tip 2ec5918. Failed Redis increments now coalesce by key; 94 focused budget tests and make check pass |
|
bugbot run Please review tip 2ec5918. Redis outage retries now retain exact spend with one queued operation per key |
|
@greptileai @veria-ai Please review tip 1a40105. Private-state assertions are removed; 94 focused budget tests and make check pass |
|
bugbot run Please review tip 1a40105. This test-only follow-up removes detached-batch assertions while retaining spend and recovery coverage |
|
Current-tip CI and patch coverage pass; Greptile is 5/5, Bugbot and Veria are clear. @mateo-berri, please review the Redis synchronization fix |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d3b1ddc. Configure here.
…copy of #32618) (#43125) * fix(router): await budget redis pipeline before sync reads * refactor(router): remove superseded Redis flush helper * fix(router): preserve concurrent spend during Redis synchronization * fix(router): type per-key spend totals without loop Final bindings * fix(router): log Redis failures before cancellable cleanup * fix(router): finalize Redis batches before propagating cancellation * test(router): reproduce cancellation while Redis cleanup is blocked * test: align budget hotpath checks with awaited Redis flush * fix(budgets): finish Redis flush after cancellation while queued * test(budgets): consolidate Redis regressions in mapped tests * fix(budgets): coalesce failed Redis increments by key * test(budgets): assert spend behavior instead of batch state * perf(router): sum pending budget spend by key once per sync --------- Co-authored-by: Emerson Gomes <emerson.gomes@thalesgroup.com> Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
TLDR
Problem this solves:
How it solves it:
A flush task owns the detached batch through the Redis write and its cleanup. Cancellation waits for that task to finish, including while the flush waits for its lock and after repeated cancellation, so an applied batch is not replayed. Failed writes coalesce retained increments by spend key, keep the latest TTL, and skip the Redis read. Synchronization merges spend queued during Redis I/O under the queue lock, and the flush lock serializes overlapping syncs. The virtual-key model budget limiter initializes the inherited queue state
User Flow
Before: a team with a tight provider budget keeps getting completions after spend should have blocked them, and a slow Redis flush plus a logging timeout can let an authenticated client keep spending past the budget
budget_limitof 0.00001 for1don the proxy routerAfter: spend stays at the recorded value once logging finishes, later requests are blocked, and a cancelled Redis flush does not drop the spend that already happened
budget_limitof 0.00001 for1dRelevant issues
Fixes #32614
Refs #20886
Backports #20888 onto
mainLinear ticket
Resolves LIT-8390
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<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 more@greptileaito re-request a review after pushing changes)Delays 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
Both revisions used real AWS Bedrock requests and Redis 7.4.4 in a local container. The proxy configuration was the same on each revision
Before (238f434)
Start the main proxy on port 4300 and clear the isolated Redis database
docker exec litellm-pr32618-redis redis-cli FLUSHDBSend the same request twice
Observe
HTTP 200, thenHTTP 200. Both responses containpongalthough the first request cost $0.0000152, above the $0.00001 budgetAfter (e817a974cd36)
Start the PR proxy on port 4301 with the same configuration and clear the isolated Redis database, then restart the proxy so its in-memory budget state is empty too
docker exec litellm-pr32618-redis redis-cli FLUSHDBSend the request twice two seconds apart, wait five more seconds, then send it a third time
Observe the first request return 200, the second return 200 while spend logging is still in flight, and the third return 429 with
Exceeded budget for provider bedrock: 1.52e-05 >= 1e-05. Redis held0.0000152spend, above the configured0.00001budgetType
🐛 Bug Fix
✅ Test
Caveats (if any)
Low
Final Attestation
Note
Medium Risk
Changes async spend accounting on the request logging hot path and multi-instance Redis sync; mistakes could mis-enforce budgets, but behavior is narrowly scoped and heavily tested.
Overview
Fixes router and proxy budget spend getting out of sync with Redis, which could let traffic continue after a cap was exceeded.
Router budget limiting no longer fires Redis increment pipelines in a detached task. Spend flushes are serialized with queue/flush locks, awaited before periodic sync reads Redis, and use
asyncio.shieldso a cancelled callback still finishes an in-flight write. Failed pipelines re-queue detached increments (coalesced per spend key); successful writes are not replayed on cancel. When copying Redis into memory, pending queue increments are added so a read cannot wipe live in-memory spend.Virtual-key model max budgets initialize the same Redis increment queue locks/state (they skip the parent
__init__), so keyed model budgets use the same safe flush path.Tests cover ordering (pipeline before
batch_get), failure/requeue, cancellation, and concurrent increments during Redis I/O.Reviewed by Cursor Bugbot for commit d3b1ddc. Bugbot is set up for automated code reviews on this repo. Configure here.