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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(router): correct num_retries tracking in retry logic (#18712)
Rohit Ravikant Ranea7c39ccc12f2
diff --git a/litellm/router.py b/litellm/router.pyindex d980b5f74d8..fa58340b9b1 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -4705,7 +4705,7 @@ class Router:except Exception as e:## LOGGINGkwargs = self.log_retry(kwargs=kwargs, e=e)- remaining_retries = num_retries - current_attempt+ remaining_retries = num_retries - current_attempt - 1_model: Optional[str] = kwargs.get("model") # type: ignoreif _model is not None:(@@ -4728,7 +4728,15 @@ class Router:if type(original_exception) in litellm.LITELLM_EXCEPTION_TYPES:setattr(original_exception, "max_retries", num_retries)- setattr(original_exception, "num_retries", current_attempt)+ # current_attempt is 0-indexed (0 to num_retries-1), so after loop completion+ # it represents the last attempt index. The actual number of retries attempted+ # is current_attempt + 1, which equals num_retries when all retries are exhausted.+ # We've already verified num_retries > 0 before entering the loop, so current_attempt+ # will always be set (never None) when we reach this point.+ actual_retries_attempted = (+ current_attempt + 1 if current_attempt is not None else num_retries+ )+ setattr(original_exception, "num_retries", actual_retries_attempted)raise original_exceptiondiff --git a/tests/local_testing/test_router_retries.py b/tests/local_testing/test_router_retries.pyindex 70c3437627c..65539cfeee0 100644--- a/tests/local_testing/test_router_retries.py+++ b/tests/local_testing/test_router_retries.py@@ -803,3 +803,99 @@ async def test_router_timeout_model_specific_and_global():mock_client.assert_called()assert mock_client.call_args.kwargs["timeout"] == 1+++async def test_router_retry_num_retries_tracking():+ """+ Test that num_retries attribute is correctly set on exceptions when all retries are exhausted.++ This verifies the fix for the bug where num_retries was incorrectly set to current_attempt+ (0-indexed) instead of the actual number of retries attempted.+ """+ from unittest.mock import AsyncMock, patch++ router = Router(+ model_list=[+ {+ "model_name": "gpt-3.5-turbo",+ "litellm_params": {+ "model": "gpt-3.5-turbo",+ "api_key": os.getenv("OPENAI_API_KEY"),+ },+ }+ ],+ num_retries=3, # Set at router level to ensure it's used+ )++ # Mock make_call to always raise a RateLimitError+ async def mock_make_call(*args, **kwargs):+ raise litellm.RateLimitError(+ message="Rate limit exceeded",+ model="gpt-3.5-turbo",+ llm_provider="openai",+ )++ with patch.object(router, "make_call", side_effect=mock_make_call):+ with patch.object(router, "_async_get_healthy_deployments", return_value=([{"model_info": {"id": "test-id"}}], [{"model_info": {"id": "test-id"}}])):+ with patch.object(router, "_time_to_sleep_before_retry", return_value=0.01): # Fast retries for testing+ try:+ await router.acompletion(+ model="gpt-3.5-turbo",+ messages=[{"role": "user", "content": "Hello"}],+ )+ pytest.fail("Expected exception to be raised")+ except litellm.RateLimitError as e:+ # Verify num_retries is correctly set to 3 (not 2, which would be current_attempt)+ assert hasattr(e, "num_retries"), "Exception should have num_retries attribute"+ assert hasattr(e, "max_retries"), "Exception should have max_retries attribute"+ assert e.num_retries == 3, f"Expected num_retries to be 3, got {e.num_retries}"+ assert e.max_retries == 3, f"Expected max_retries to be 3, got {e.max_retries}"++ # Verify the error message includes correct retry information+ error_str = str(e)+ assert "LiteLLM Retried: 3 times" in error_str, f"Error message should indicate 3 retries: {error_str}"+ assert "LiteLLM Max Retries: 3" in error_str, f"Error message should show max retries: {error_str}"+++async def test_router_retry_num_retries_single_retry():+ """+ Test num_retries tracking with a single retry to verify edge case handling.+ """+ from unittest.mock import patch++ router = Router(+ model_list=[+ {+ "model_name": "gpt-3.5-turbo",+ "litellm_params": {+ "model": "gpt-3.5-turbo",+ "api_key": os.getenv("OPENAI_API_KEY"),+ },+ }+ ],+ num_retries=1, # Set at router level - single retry+ )++ # Mock make_call to always raise a Timeout error+ async def mock_make_call(*args, **kwargs):+ raise litellm.Timeout(+ message="Request timed out",+ model="gpt-3.5-turbo",+ llm_provider="openai",+ )++ with patch.object(router, "make_call", side_effect=mock_make_call):+ with patch.object(router, "_async_get_healthy_deployments", return_value=([{"model_info": {"id": "test-id"}}], [{"model_info": {"id": "test-id"}}])):+ with patch.object(router, "_time_to_sleep_before_retry", return_value=0.01):+ try:+ await router.acompletion(+ model="gpt-3.5-turbo",+ messages=[{"role": "user", "content": "Hello"}],+ )+ pytest.fail("Expected exception to be raised")+ except litellm.Timeout as e:+ # With num_retries=1, we should attempt 1 retry+ assert e.num_retries == 1, f"Expected num_retries to be 1, got {e.num_retries}"+ assert e.max_retries == 1, f"Expected max_retries to be 1, got {e.max_retries}"\ No newline at end of file