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]

fix(rate-limit): fail closed on unrecognized OVER_LIMIT descriptor

Krrish Dholakiaeba0cdf3f5e0
diff --git a/litellm/proxy/hooks/dynamic_rate_limiter_v3.py b/litellm/proxy/hooks/dynamic_rate_limiter_v3.py
index d827cf3067c..2cbb5272845 100644
--- a/litellm/proxy/hooks/dynamic_rate_limiter_v3.py
+++ b/litellm/proxy/hooks/dynamic_rate_limiter_v3.py
@@ -528,6 +528,37 @@ class _PROXY_DynamicRateLimitHandlerV3(CustomLogger):
},
)
+ # Fail-closed guard: overall_code says OVER_LIMIT but no status
+ # matched a descriptor key we know how to translate into a 429.
+ # Refuse the request rather than silently fall through and let an
+ # over-limit request proceed to the model. Without this, a future
+ # caller wiring an unfamiliar descriptor into enforced_descriptors
+ # would silently bypass the rate limit.
+ offending = next(
+ (s for s in atomic_response["statuses"] if s["code"] == "OVER_LIMIT"),
+ None,
+ )
+ verbose_proxy_logger.error(
+ f"Dynamic rate limiter: OVER_LIMIT response with unknown "
+ f"descriptor_key(s) — refusing request. response={atomic_response}"
+ )
+ raise HTTPException(
+ status_code=429,
+ detail={
+ "error": "Rate limit exceeded",
+ "descriptor_key": (
+ offending["descriptor_key"] if offending else "unknown"
+ ),
+ "rate_limit_type": (
+ str(offending["rate_limit_type"]) if offending else "unknown"
+ ),
+ },
+ headers={
+ "retry-after": str(self.v3_limiter.window_size),
+ "x-litellm-priority": priority or "default",
+ },
+ )
+
# If priority is NOT enforced (saturation below threshold) but
# priority_descriptors exist, increment them for tracking only — no
# check, no rollback. This matches the prior tracking semantics.
diff --git a/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py b/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py
index b8ced4e6615..ceea5de7991 100644
--- a/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py
+++ b/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py
@@ -412,3 +412,78 @@ async def test_batch_zero_token_consumes_rpm_only():
batch_usage=zero_batch,
)
assert exc.value.status_code == 429
+
+
+async def test_dynamic_rate_limiter_v3_fails_closed_on_unknown_descriptor():
+ """
+ Fail-closed guard: when atomic_check_and_increment_by_n returns
+ overall_code=OVER_LIMIT but with a descriptor_key the dispatcher does
+ not recognize, the dynamic limiter must raise 429 rather than silently
+ fall through.
+
+ Reproduces by patching atomic_check_and_increment_by_n to return an
+ OVER_LIMIT response carrying an unknown descriptor_key.
+ """
+ from fastapi import HTTPException
+
+ os.environ["LITELLM_LICENSE"] = "test-license-key"
+ litellm.priority_reservation = {"high": 0.9, "low": 0.1}
+
+ dual_cache = DualCache()
+ handler = DynamicRateLimitHandler(internal_usage_cache=dual_cache)
+
+ model = "fail-closed-model"
+ llm_router = Router(
+ model_list=[
+ {
+ "model_name": model,
+ "litellm_params": {
+ "model": "gpt-3.5-turbo",
+ "api_key": "test-key",
+ "api_base": "test-base",
+ "tpm": 1000,
+ },
+ }
+ ]
+ )
+ handler.update_variables(llm_router=llm_router)
+
+ async def fake_atomic(*args, **kwargs):
+ return {
+ "overall_code": "OVER_LIMIT",
+ "statuses": [
+ {
+ "code": "OVER_LIMIT",
+ "current_limit": 100,
+ "limit_remaining": 0,
+ "rate_limit_type": "tokens",
+ "descriptor_key": "future_unrecognized_descriptor",
+ }
+ ],
+ }
+
+ handler.v3_limiter.atomic_check_and_increment_by_n = fake_atomic
+
+ from litellm.types.router import ModelGroupInfo
+
+ user = UserAPIKeyAuth(api_key=hash_token("fail-closed-key"))
+ user.metadata = {"priority": "high"}
+
+ with pytest.raises(HTTPException) as exc:
+ await handler._check_rate_limits(
+ model=model,
+ model_group_info=ModelGroupInfo(
+ model_group=model,
+ providers=["openai"],
+ rpm=None,
+ tpm=1000,
+ ),
+ user_api_key_dict=user,
+ priority="high",
+ saturation=0.0,
+ data={},
+ )
+ assert (
+ exc.value.status_code == 429
+ ), f"Expected 429 fail-closed on unknown descriptor; got {exc.value.status_code}"