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(agent_365): classify Entra token-endpoint 408/429 as throttled, not caller fault

Yucheng Zhu28467d404a9a
diff --git a/litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py b/litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py
index 5d17a87e640..0d45b5ff238 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py
@@ -105,6 +105,12 @@ class Agent365MalformedResponseError(Exception):
pass
+class Agent365ThrottledError(Exception):
+ def __init__(self, status_code: int) -> None:
+ super().__init__(f"HTTP {status_code}")
+ self.status_code = status_code
+
+
class Agent365Guardrail(CustomGuardrail):
"""Pre-MCP-call guardrail enforcing Microsoft Agent 365 tool-evaluation verdicts."""
@@ -192,6 +198,13 @@ class Agent365Guardrail(CustomGuardrail):
status_code=401,
reason=f"the Entra On-Behalf-Of token exchange was rejected ({exc.error_code})",
)
+ except Agent365ThrottledError as exc:
+ self._handle_throttled(
+ data=data,
+ tool_name=tool_name,
+ reason=f"the Entra token endpoint returned HTTP {exc.status_code}",
+ latency_ms=None,
+ )
except (httpx.HTTPError, LitellmTimeout, TimeoutError) as exc:
return self._handle_unavailable(
data=data,
@@ -235,22 +248,12 @@ class Agent365Guardrail(CustomGuardrail):
latency_ms: float,
) -> dict | None: # mutable-ok: returns the request data dict per hook contract on fail_open
if response.status_code in (408, 429):
- self._record_verdict(
+ self._handle_throttled(
data=data,
- verdict="Throttled",
- guardrail_status="guardrail_failed_to_respond",
- defender_status=None,
- correlation_id=None,
- latency_ms=latency_ms,
+ tool_name=tool_name,
reason=f"the Agent 365 endpoint returned HTTP {response.status_code}",
+ latency_ms=latency_ms,
)
- throttled_detail: Final[_UnavailableDetail] = {
- "error": "Agent 365 guardrail could not authorize the tool call",
- "message": f"Tool call '{tool_name}' was blocked because the Agent 365 endpoint returned "
- f"HTTP {response.status_code}; throttled evaluations block regardless of unreachable_fallback.",
- "tool": tool_name,
- }
- raise HTTPException(status_code=503, detail=throttled_detail)
if 400 <= response.status_code < 500:
if response.status_code == 401:
self._evict_obo_token(assertion)
@@ -398,6 +401,8 @@ class Agent365Guardrail(CustomGuardrail):
},
headers={"Content-Type": "application/x-www-form-urlencoded"}, # mutable-ok: httpx header dict
)
+ if response.status_code in (408, 429):
+ raise Agent365ThrottledError(status_code=response.status_code)
if response.status_code >= 500:
raise httpx.HTTPStatusError(
f"Entra token endpoint returned {response.status_code}",
@@ -472,6 +477,30 @@ class Agent365Guardrail(CustomGuardrail):
}
raise HTTPException(status_code=status_code, detail=caller_fault_detail)
+ def _handle_throttled(
+ self,
+ data: dict, # mutable-ok: guardrail logging appends into the request metadata in place
+ tool_name: str,
+ reason: str,
+ latency_ms: float | None,
+ ) -> NoReturn:
+ self._record_verdict(
+ data=data,
+ verdict="Throttled",
+ guardrail_status="guardrail_failed_to_respond",
+ defender_status=None,
+ correlation_id=None,
+ latency_ms=latency_ms,
+ reason=reason,
+ )
+ throttled_detail: Final[_UnavailableDetail] = {
+ "error": "Agent 365 guardrail could not authorize the tool call",
+ "message": f"Tool call '{tool_name}' was blocked because {reason}; "
+ "throttled evaluations block regardless of unreachable_fallback.",
+ "tool": tool_name,
+ }
+ raise HTTPException(status_code=503, detail=throttled_detail)
+
def _evict_obo_token(self, assertion: str) -> None:
cache_key: Final = hashlib.sha256(assertion.encode("utf-8")).hexdigest()
with self._obo_cache_lock:
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_agent_365.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_agent_365.py
index 14dbfd21aa7..152674aab7f 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_agent_365.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_agent_365.py
@@ -686,6 +686,31 @@ class TestVeriaHardening:
assert exc_info.value.status_code == 503
assert "500" in exc_info.value.detail["message"]
+ @pytest.mark.asyncio
+ async def test_token_endpoint_429_blocks_even_fail_open_as_throttled(self):
+ handler: Final = FakeHandler(
+ [_response(429, {"error": "temporarily_throttled", "error_description": "AADSTS90056"})]
+ )
+ guardrail: Final = _make_guardrail(handler, unreachable_fallback="fail_open")
+ data: Final = _mcp_data()
+ with pytest.raises(HTTPException) as exc_info:
+ await _run(guardrail, data)
+ assert exc_info.value.status_code == 503
+ assert "429" in exc_info.value.detail["message"]
+ info: Final = _guardrail_info(data)
+ assert info["guardrail_status"] == "guardrail_failed_to_respond"
+ assert info["guardrail_response"]["verdict"] == "Throttled"
+
+ @pytest.mark.asyncio
+ async def test_token_endpoint_408_non_json_blocks_as_throttled(self):
+ handler: Final = FakeHandler([_response(408, text="Request Timeout")])
+ guardrail: Final = _make_guardrail(handler, unreachable_fallback="fail_open")
+ data: Final = _mcp_data()
+ with pytest.raises(HTTPException) as exc_info:
+ await _run(guardrail, data)
+ assert exc_info.value.status_code == 503
+ assert _guardrail_info(data)["guardrail_response"]["verdict"] == "Throttled"
+
@pytest.mark.asyncio
async def test_token_endpoint_4xx_html_stays_infra_fail_open(self):
handler: Final = FakeHandler([_response(403, text="<html>waf block page</html>")])