From 34433f8cef84cbc3be47178fbb6c9b6ff2409950 Mon Sep 17 00:00:00 2001 From: RichardoMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Tue, 22 Sep 2026 20:07:40 +0800 Subject: [PATCH 1/6] fix(proxy): write end_user_max_budget back onto the per-request token Fixes #29142. --- litellm/proxy/auth/user_api_key_auth.py | 2 + .../auth/test_custom_auth_end_user_budget.py | 37 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 2231706b282..f11d391267e 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -1255,6 +1255,8 @@ def update_valid_token_with_end_user_params(valid_token: UserAPIKeyAuth, end_use valid_token.end_user_rpm_limit = end_user_params["end_user_rpm_limit"] if end_user_params.get("end_user_tpd_limit") is not None: valid_token.end_user_tpd_limit = end_user_params["end_user_tpd_limit"] + if end_user_params.get("end_user_max_budget") is not None: + valid_token.end_user_max_budget = end_user_params["end_user_max_budget"] if end_user_params.get("allowed_model_region") is not None: valid_token.allowed_model_region = end_user_params["allowed_model_region"] if end_user_params.get("end_user_model_max_budget") is not None: diff --git a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py index 0c3487255f6..8140711f8ed 100644 --- a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py +++ b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py @@ -36,7 +36,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id(): assert getattr(result, "end_user_id", None) is None mock_common.assert_not_awaited() - # With opt-in flag: still not from the helper — the centralized gate + # With opt-in flag: still not from the helper 鈥?the centralized gate # in the wrapper handles it. with ( patch( @@ -403,3 +403,38 @@ def test_end_user_budget_tpd_limit_reaches_the_token(): assert result.end_user_rpm_limit == 5 assert result.end_user_tpd_limit == 750000 + +def test_end_user_max_budget_reaches_the_token(): + # Regression for #29142: a DB-derived end_user max_budget must land on the + # per-request token, exactly like end_user_model_max_budget already does. + # Without the write-back, a token reused from the user_api_key_cache keeps + # the previous end user's budget and rejects valid requests. + from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params + + end_user_params = {"end_user_id": "user_1"} + _apply_budget_limits_to_end_user_params( + end_user_params=end_user_params, + budget_info=LiteLLM_BudgetTable(max_budget=20.0), + end_user_id="user_1", + ) + result = update_valid_token_with_end_user_params(UserAPIKeyAuth(token="test_token"), end_user_params) + + assert result.end_user_max_budget == 20.0 + + +def test_end_user_max_budget_is_not_leaked_from_a_previous_end_user(): + # #29142 cache-poisoning shape: a token reused across end users carries the + # earlier end user's smaller budget; the DB lookup for the current end user + # must overwrite it so the request is not rejected against the wrong budget. + from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params + + reused_token = UserAPIKeyAuth(token="test_token", end_user_max_budget=2.0) + end_user_params = {"end_user_id": "user_2"} + _apply_budget_limits_to_end_user_params( + end_user_params=end_user_params, + budget_info=LiteLLM_BudgetTable(max_budget=20.0), + end_user_id="user_2", + ) + result = update_valid_token_with_end_user_params(reused_token, end_user_params) + + assert result.end_user_max_budget == 20.0 \ No newline at end of file From ab76747e1d727cad372951a0375bb30da4aa41bd Mon Sep 17 00:00:00 2001 From: RichardoMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Wed, 23 Sep 2026 12:26:31 +0800 Subject: [PATCH 2/6] fix(proxy): keep the key default end-user budget out of the write-back The new end_user_max_budget write-back also fired on the no-end-user-row branch, where the budget is the key's end_user_budget_id fallback rather than this end user's own budget. That loosened a tighter cap a custom auth callable had set, breaking test_custom_auth_cap_stays_below_the_key_default_end_user_budget. Drop the key so only the existing fill-only assignment applies it. --- litellm/proxy/auth/user_api_key_auth.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index f11d391267e..b7b6737ba28 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -4402,6 +4402,12 @@ async def _lookup_end_user_and_apply_budget( budget_info=default_budget, end_user_id=valid_token.end_user_id or "", ) + # This branch has no end-user row, so the budget here is the key's + # ``end_user_budget_id`` (or the proxy-wide default) - a fallback, not + # this end user's own budget. It must only fill a cap the custom auth + # callable left unset, never loosen one it set, so keep it out of the + # write-back and let the fill-only assignment below apply it. + end_user_params.pop("end_user_max_budget", None) valid_token = update_valid_token_with_end_user_params( valid_token=valid_token, end_user_params=end_user_params ) From 24d4752bf21f5a33fe87559314873071adec98e2 Mon Sep 17 00:00:00 2001 From: RichardoMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Wed, 23 Sep 2026 12:26:47 +0800 Subject: [PATCH 3/6] test: restore the mangled em dash and the trailing newline The earlier push round-tripped this file through a cp936 decode, turning the em dash on line 39 into mojibake, and dropped the final newline. --- tests/unit/proxy/auth/test_custom_auth_end_user_budget.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py index 8140711f8ed..8b29ba28d86 100644 --- a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py +++ b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py @@ -36,7 +36,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id(): assert getattr(result, "end_user_id", None) is None mock_common.assert_not_awaited() - # With opt-in flag: still not from the helper 鈥?the centralized gate + # With opt-in flag: still not from the helper — the centralized gate # in the wrapper handles it. with ( patch( @@ -437,4 +437,4 @@ def test_end_user_max_budget_is_not_leaked_from_a_previous_end_user(): ) result = update_valid_token_with_end_user_params(reused_token, end_user_params) - assert result.end_user_max_budget == 20.0 \ No newline at end of file + assert result.end_user_max_budget == 20.0 From cb126e36156b00854292e97491b8f91396ce3f2b Mon Sep 17 00:00:00 2001 From: RichardoMrMu <947676438@qq.com> Date: Tue, 29 Sep 2026 08:27:31 +0000 Subject: [PATCH 4/6] test: remove narrative comments flagged in greptile review Drops the two setup-narration comment blocks in the end-user budget regression tests per the repo AGENTS.md comment policy. Comments only; no assertions or litellm logic changed. Addresses the two unresolved greptile-apps review threads on PR #42466. --- tests/unit/proxy/auth/test_custom_auth_end_user_budget.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py index 8b29ba28d86..499df1cd2b4 100644 --- a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py +++ b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py @@ -405,10 +405,6 @@ def test_end_user_budget_tpd_limit_reaches_the_token(): assert result.end_user_tpd_limit == 750000 def test_end_user_max_budget_reaches_the_token(): - # Regression for #29142: a DB-derived end_user max_budget must land on the - # per-request token, exactly like end_user_model_max_budget already does. - # Without the write-back, a token reused from the user_api_key_cache keeps - # the previous end user's budget and rejects valid requests. from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params end_user_params = {"end_user_id": "user_1"} @@ -423,9 +419,6 @@ def test_end_user_max_budget_reaches_the_token(): def test_end_user_max_budget_is_not_leaked_from_a_previous_end_user(): - # #29142 cache-poisoning shape: a token reused across end users carries the - # earlier end user's smaller budget; the DB lookup for the current end user - # must overwrite it so the request is not rejected against the wrong budget. from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params reused_token = UserAPIKeyAuth(token="test_token", end_user_max_budget=2.0) From ea6bb3b326e5153240744ab847e55fe1caa23534 Mon Sep 17 00:00:00 2001 From: RichardoMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:48:13 +0800 Subject: [PATCH 5/6] style: satisfy ruff isort/format for the end-user budget writeback test --- .../auth/test_custom_auth_end_user_budget.py | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py index 499df1cd2b4..201f7132377 100644 --- a/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py +++ b/tests/unit/proxy/auth/test_custom_auth_end_user_budget.py @@ -1,15 +1,17 @@ -import pytest from unittest.mock import AsyncMock, patch + +import pytest + import litellm -from litellm.proxy.auth.user_api_key_auth import ( - _run_post_custom_auth_checks, - update_valid_token_with_end_user_params, -) from litellm.proxy._types import ( LiteLLM_BudgetTable, LiteLLM_EndUserTable, UserAPIKeyAuth, ) +from litellm.proxy.auth.user_api_key_auth import ( + _run_post_custom_auth_checks, + update_valid_token_with_end_user_params, +) @pytest.mark.asyncio @@ -21,9 +23,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id(): valid_token = UserAPIKeyAuth(token="test_token") # Default: common_checks should NOT be called inside the helper - with patch( - "litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock - ) as mock_common: + with patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock) as mock_common: mock_common.return_value = True result = await _run_post_custom_auth_checks( valid_token=valid_token, @@ -39,9 +39,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id(): # With opt-in flag: still not from the helper — the centralized gate # in the wrapper handles it. with ( - patch( - "litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock - ) as mock_common, + patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock) as mock_common, patch( "litellm.proxy.proxy_server.general_settings", {"custom_auth_run_common_checks": True}, @@ -64,15 +62,11 @@ async def test_custom_auth_run_post_custom_auth_checks_with_end_user_budget_exce valid_token = UserAPIKeyAuth( token="test_token", end_user_id="test_user", - end_user_model_max_budget={ - "gpt-4": {"budget_limit": 10.0, "time_period": "1d"} - }, + end_user_model_max_budget={"gpt-4": {"budget_limit": 10.0, "time_period": "1d"}}, ) request_data = {"model": "gpt-4"} - with patch( - "litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock - ): + with patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock): with patch( "litellm.proxy.proxy_server.model_max_budget_limiter.is_end_user_within_model_budget", new_callable=AsyncMock, @@ -106,9 +100,7 @@ async def test_custom_auth_enforces_end_user_budget_when_common_checks_skipped() litellm_budget_table=LiteLLM_BudgetTable(max_budget=1.0), ) - async def mock_get_current_spend( - counter_key, fallback_spend, max_budget=None, **kwargs - ): + async def mock_get_current_spend(counter_key, fallback_spend, max_budget=None, **kwargs): if counter_key == "spend:end_user:customer-1": return 5.0 return fallback_spend @@ -404,6 +396,7 @@ def test_end_user_budget_tpd_limit_reaches_the_token(): assert result.end_user_rpm_limit == 5 assert result.end_user_tpd_limit == 750000 + def test_end_user_max_budget_reaches_the_token(): from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params From 64a9e82d9417155bd84877669ae94e8d5c3ce7bf Mon Sep 17 00:00:00 2001 From: RichardoMu <44485717+RichardoMrMu@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:04:36 +0800 Subject: [PATCH 6/6] style: record why the end-user budget write-back mutates the token The repo's type-discipline gate (LIT011) counts the new `valid_token.end_user_max_budget = ...` store as an unexplained in-place mutation of a parameter, one over the merge-base ceiling. Mark it with the sanctioned `# rebind-ok:` reason, matching the sibling limit write-backs. Verified with scripts/check_type_discipline.py on this file: LIT011 83 vs merge-base 82 before the marker, 82 vs 82 after; LIT013 still 0. --- litellm/proxy/auth/user_api_key_auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index b7b6737ba28..de7db502e4c 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -1255,8 +1255,8 @@ def update_valid_token_with_end_user_params(valid_token: UserAPIKeyAuth, end_use valid_token.end_user_rpm_limit = end_user_params["end_user_rpm_limit"] if end_user_params.get("end_user_tpd_limit") is not None: valid_token.end_user_tpd_limit = end_user_params["end_user_tpd_limit"] - if end_user_params.get("end_user_max_budget") is not None: - valid_token.end_user_max_budget = end_user_params["end_user_max_budget"] + if cast(dict[str, object], end_user_params).get("end_user_max_budget") is not None: # cast-ok: pin dict param types + valid_token.end_user_max_budget = end_user_params["end_user_max_budget"] # rebind-ok: sibling limit write-back if end_user_params.get("allowed_model_region") is not None: valid_token.allowed_model_region = end_user_params["allowed_model_region"] if end_user_params.get("end_user_model_max_budget") is not None: