Skip to content

test(integration): wait for the team member Redis counter to settle between budget calls - #47599

Closed
mateo-berri wants to merge 1 commit into
mainfrom
litellm_team_member_reservation_settle
Closed

mateo-berri wants to merge 1 commit into
mainfrom
litellm_team_member_reservation_settle

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Closed without merging. The integration-accounting reds this PR targeted came from the double charge that #47510 fixed, where one 0.06 chat billed 0.12. With that charge gone, the unchanged test passes on main, so the extra Redis waits here change nothing

The window the demo below shows is still real, since a request sent right after a 200 chat can get a 422 until the reservation is released. It is tracked separately and not fixed here

/stupid

Original

TLDR

Problem this solves:

  • integration-accounting on main goes red now and then with a 422
  • The test's DB spend wait does not cover the Redis counter release

How it solves it:

  • Wait for the member's Redis counter to read the settled spend
  • Add the same wait to two sibling tests with the same race

User Flow

Before: a contributor watching main CI sees integration-accounting go red on a test nobody touched

  1. A commit merges to main and CircleCI runs integration-accounting
  2. The test sends a chat as a team member with a 1.0 budget, waits for the DB spend, then calls GET /team/{team_id}/members/me
  3. That call sometimes answers 422 Budget has been exceeded! TeamMember=<user>:<team> Current cost: 1.000000000000003, Max budget: 1.0, so the job goes red

After: the same job passes on every run

  1. A commit merges to main and CircleCI runs integration-accounting
  2. The test sends the same chat, waits for the DB spend and for the member's counter to read the settled 0.12, then calls GET /team/{team_id}/members/me
  3. That call answers 200 with the member's budget table

Pre-Submission checklist

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

  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

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

QA finding: before and after both pass, so this PR does not fix anything on main. Both legs ran through the integration harness's accounting lane (python tests/integration/run.py accounting <file>, 2 xdist workers, each with its own gateway and peer gateway sharing one Postgres and one Redis) against the harness's priced fake upstream, so no provider $$$ applies. Logs and the demo rows are in this gist

Case Before (2c6b280) After (4097bf2)
Test file, 3 lane runs 3 of 3 pass 3 of 3 pass

Before (2c6b280)

  1. Run the merge base's unchanged tests/integration/spend/test_team_member_spend.py on the accounting lane three times
  2. Every run passes the whole file, the lowering cell included
[gw0] [ 92%] PASSED tests/integration/spend/test_team_member_spend.py::test_member_update_lowering_the_budget_below_spend_blocks_the_next_call
[gw1] [  7%] PASSED tests/integration/spend/test_team_member_spend.py::test_member_over_budget_is_blocked_when_redis_counter_reads_stale_low
[gw1] [ 85%] PASSED tests/integration/spend/test_team_member_spend.py::test_bulk_update_restores_team_default_when_last_member_limit_is_cleared
================== 12 passed, 2 skipped in 150.85s (0:02:30) ===================
================== 12 passed, 2 skipped in 311.56s (0:05:11) ===================
================== 12 passed, 2 skipped in 371.66s (0:06:11) ===================
  1. The four main reds (pipelines 91403, 91411, 91419 and 91432) all ran before fix(proxy): share the billing logger across callback registries #47510. In job 2265376 every team member spend test failed, most reading 0.12 spend for one 0.06 chat. Main's integration-accounting jobs after fix(proxy): share the billing logger across callback registries #47510 (2265397, 2265434 and 2265458) pass the whole file

After (4097bf2)

  1. Run this branch's tests/integration/spend/test_team_member_spend.py on the accounting lane three times
  2. Every run passes the whole file, the same as before (logs)
================== 12 passed, 2 skipped in 293.05s (0:04:53) ===================
================== 12 passed, 2 skipped in 253.46s (0:04:13) ===================
================== 12 passed, 2 skipped in 295.61s (0:04:55) ===================

Type

✅ Test

Caveats (if any)

High

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

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@litereview guide

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@litereview

litereview Bot commented Oct 20, 2026

Copy link
Copy Markdown

📝 guide

Open preview

Scope: full · Full PR review.

On main, the integration-accounting job sometimes goes red. A team member's next call answers 422 Budget has been exceeded ... Current cost: 1.000000000000003, Max budget: 1.0 even though the member has spent only about 0.12.

Walkthrough

  1. Member gets a 1.0 budget: the test sets max_budget_in_team: 1, and each chat costs 0.06. Same both ways.
  2. First chat: the caller gets a 200, but the member's Redis counter still holds a hold for the whole 1.0. I read "1" right after the 200 in 4 of 4 local tries. Same both ways.
  3. Database catches up: the test waits for the database to show 0.06. That wait was already there and doesn't cover the Redis release. Same both ways.
  4. Member counter: breaks here today, because the test moves on while the counter can still read 1.0. Fixed here after the PR: a new helper polls the counter until it reads the settled 0.06 or 0.12.
  5. Next call: today GET /team/{team_id}/members/me answers 422. Once the counter settles, it answers 200. I reproduced both locally.
  6. CI job: today it goes red now and then; the PR cites four main pipelines. After the PR, the three changed tests passed in one local run of the accounting lane.

What changes for the caller: nothing. No proxy code changes. The test now waits for the member's Redis counter in the flaky test and in the two sibling tests that send a second request on the same member.

Tests added: no new tests. Three existing integration tests in tests/integration/spend/test_team_member_spend.py get the new wait.

Black box coverage: Not needed. One thing to decide: a back-to-back call on a tight member budget is refused for a few seconds after a 200 (4 of 4 locally). The new waits step around this, and no test pins it down. If it counts as a product bug, tests/integration/AGENTS.md asks for a pytest.skip("BUG: ...") test.

Checks and execution details
  • Passed · Changed integration tests via accounting lane (optional): Ran python tests/integration/run.py accounting with INTEGRATION_WORKERS=2 against local Postgres 15 and Redis. Selected the three changed tests in tests/integration/spend/test_team_member_spend.py. Result: 3 passed in 68.58s. This was one run, so it does not prove the race is gone.
  • Passed · Race reproduction probe (throwaway test, deleted afterwards) (optional): Set a member budget of 1.0 and sent one 200 chat. Right after it, the member's Redis counter read "1", and both GET /team/{team_id}/members/me and a second chat answered 422 Budget has been exceeded! ... Current cost: 1.0, Max budget: 1.0, in 4 of 4 tries each. About 3 s later the counter read 0.06 and the same calls answered 200.
  • Passed · Preview template wiring (required): 12 radios (2 x 6 stops), exactly one checked, every for= matches an id, no em dashes, no scripts or buttons.

View review · Reviewed 4097bf2f0d66

@mateo-berri

Copy link
Copy Markdown
Contributor Author

Closing since the CI reds came from the double charge #47510 fixed, and the unchanged test now passes 3 of 3 on main

@mateo-berri
mateo-berri deleted the litellm_team_member_reservation_settle branch October 20, 2026 18:08
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.

1 participant