Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test(proxy): cover guardrail tag budget edge cases

yassind6b13f938d82
diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py
index 6b8260e7f08..f480e096081 100644
--- a/tests/test_litellm/proxy/auth/test_auth_checks.py
+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py
@@ -51,6 +51,8 @@ from litellm.proxy.auth.auth_checks import (
get_key_object,
get_user_object,
invalidate_team_member_spend_state,
+ request_skips_budget_checks,
+ route_skips_budget_checks,
vector_store_access_check,
)
from litellm.caching.in_memory_cache import InMemoryCache
@@ -8447,3 +8449,15 @@ async def test_access_group_model_fallback_uses_the_injected_database(channel: s
llm_router=None, prisma_client=client,
) is True
reader.assert_awaited_once_with(where={"access_group_id": "group-a"})
+
+
+def test_route_skips_budget_checks_marks_only_spend_free_routes() -> None:
+ assert route_skips_budget_checks(route="/v1/models") is True
+ assert route_skips_budget_checks(route="/spend/logs") is True
+ assert route_skips_budget_checks(route="/health") is False
+ assert route_skips_budget_checks(route="/v1/chat/completions") is False
+
+
+def test_request_skips_budget_checks_extends_route_rule_with_zero_cost_models() -> None:
+ assert request_skips_budget_checks(route="/v1/models", model=None, llm_router=None) is True
+ assert request_skips_budget_checks(route="/v1/chat/completions", model=None, llm_router=None) is False
diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.py
index 5f1e4736373..4ac687625c2 100644
--- a/tests/test_litellm/proxy/test_common_request_processing.py
+++ b/tests/test_litellm/proxy/test_common_request_processing.py
@@ -673,6 +673,26 @@ class TestProxyBaseLLMRequestProcessing:
assert exc_info.value.code == "429"
assert "guardrail-tag" in exc_info.value.message
+ @pytest.mark.asyncio
+ async def test_enforce_guardrail_added_tag_budgets_still_checks_when_model_is_unparseable(self, monkeypatch):
+ from litellm.proxy.common_request_processing import _enforce_guardrail_added_tag_budgets
+
+ tag_budget_check = AsyncMock()
+ monkeypatch.setattr(litellm.proxy.common_request_processing, "tag_max_budget_check_for_tags", tag_budget_check)
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", MagicMock())
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_api_key_cache", MagicMock())
+
+ await _enforce_guardrail_added_tag_budgets(
+ data={"model": 5, "metadata": {"tags": ["guardrail-tag"]}},
+ tags_before_guardrails=frozenset(),
+ route="/v1/chat/completions",
+ llm_router=None,
+ user_api_key_dict=ProxyUserAPIKeyAuth(api_key="sk-test"),
+ proxy_logging_obj=MagicMock(spec=ProxyLogging),
+ )
+
+ tag_budget_check.assert_awaited_once()
+
@pytest.mark.asyncio
async def test_common_processing_pre_call_logic_arms_auto_router_compression_before_guardrails(
self, monkeypatch