From 7a0693e91feb926eafd97ee387b274f517aa5bcb Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:19:38 -0700 Subject: [PATCH] fix(proxy): check precedence when a team edit makes an unenforced model_max_budget entry take effect --- litellm/proxy/hooks/model_max_budget_limiter.py | 4 ++++ .../management_endpoints/team_endpoints.py | 4 +++- .../management_endpoints/test_team_endpoints.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/hooks/model_max_budget_limiter.py b/litellm/proxy/hooks/model_max_budget_limiter.py index b72636dfaae..11a47b2d424 100644 --- a/litellm/proxy/hooks/model_max_budget_limiter.py +++ b/litellm/proxy/hooks/model_max_budget_limiter.py @@ -743,6 +743,10 @@ def _window_reset_at(started_at: float | None, budget_duration: str | None, now: return datetime.fromtimestamp(ends_at, tz=timezone.utc).isoformat() +def is_enforced_budget_entry(raw_budget_config: object) -> bool: + return _usable_budget_config(raw_budget_config) is not None + + def _usable_budget_config(raw_budget_config: object) -> BudgetConfig | None: try: budget_config: Final = BudgetConfig.model_validate(raw_budget_config) diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index 2f5f39533d5..af0608629f1 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -144,6 +144,7 @@ from litellm.proxy.hooks.model_max_budget_limiter import ( build_model_max_budget_usage, entries_outranked_by, entry_names_model, + is_enforced_budget_entry, proxy_routed_models, resolve_model_budget, ) @@ -1499,7 +1500,8 @@ def _check_team_model_budget_update_authority( existing = _existing_model_cap(existing_model_max_budget.get(entry)) if existing is not None: _check_existing_model_cap_kept(entry=entry, cap_entry=entry, proposed=proposed, existing=existing) - continue + if is_enforced_budget_entry(existing) or not is_enforced_budget_entry(proposed): + continue governing = resolve_model_budget(model=entry, model_max_budget=existing_model_max_budget, team_id=data.team_id) if governing is not None and entry_names_model(entry=governing.budget_model, model=entry): _check_existing_model_cap_kept( diff --git a/tests/unit/proxy/management_endpoints/test_team_endpoints.py b/tests/unit/proxy/management_endpoints/test_team_endpoints.py index 5fdbbff01a9..d4465bef7a6 100644 --- a/tests/unit/proxy/management_endpoints/test_team_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_team_endpoints.py @@ -17746,6 +17746,10 @@ def test_team_admin_cannot_add_an_entry_that_takes_precedence_over_a_cap( {**_TEAM_OPUS_FAMILY_CAP, "opus-group": {"rpm_limit": 10}}, {**_TEAM_OPUS_FAMILY_CAP, "opus-group": {"rpm_limit": 20}}, ), + ( + {**_TEAM_OPUS_FAMILY_CAP, "claude-opus-4-8": {"rpm_limit": 10, "budget_duration": "1d"}}, + {**_TEAM_OPUS_FAMILY_CAP, "claude-opus-4-8": {"max_budget": 1000.0, "budget_duration": "1d"}}, + ), ( _TEAM_OPUS_FAMILY_CAP, {**_TEAM_OPUS_FAMILY_CAP, "gemini-3-pro*": {"max_budget": 1000.0, "budget_duration": "1d"}}, @@ -17761,6 +17765,7 @@ def test_team_admin_cannot_add_an_entry_that_takes_precedence_over_a_cap( "unchanged_looser_entry", "tightened_looser_entry", "uncapped_entry_edited", + "enforced_uncapped_entry_capped", "pattern_sharing_only_a_wildcard_route", ], ) @@ -17770,6 +17775,18 @@ def test_team_admin_can_keep_or_tighten_entries_and_add_ones_that_outrank_no_cap assert _check_team_admin_model_caps(existing=existing, requested=requested) is None +def test_team_admin_cannot_make_an_unenforced_entry_take_precedence_over_a_cap() -> None: + existing: Final = {**_TEAM_OPUS_FAMILY_CAP, "claude-opus-4-8": {"rpm_limit": 10}} + + with pytest.raises(HTTPException) as exc: + _check_team_admin_model_caps( + existing=existing, + requested={**existing, "claude-opus-4-8": {"max_budget": 1000.0, "budget_duration": "1d"}}, + ) + assert exc.value.status_code == 403 + assert "'claude-opus*'" in exc.value.detail["error"] + + @pytest.mark.parametrize("added_entry", _BEDROCK_ROUTE_ADDED_ENTRIES, ids=["model", "pattern"]) def test_team_admin_cannot_lift_a_bedrock_vendor_cap_through_a_bedrock_wildcard_route(added_entry: str) -> None: with pytest.raises(HTTPException) as exc: