From 74dc2e782ba54cd55038f07c7bb6239598928a26 Mon Sep 17 00:00:00 2001 From: 72004 Date: Fri, 21 Aug 2026 01:26:59 +0500 Subject: [PATCH] fix: map Anthropic API usage-limit errors to RateLimitError (429) not BadRequest (400) --- .../litellm_core_utils/exception_mapping_utils.py | 8 ++++++++ .../test_exception_mapping_utils.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index b875f7c09a6..3cdd045d362 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -78,6 +78,14 @@ class ExceptionCheckers: if "service tier capacity exceeded" in _error_str_lower: return True + ####################################### + # Anthropic returns spend/usage-limit exhaustion typed as an + # invalid_request_error (HTTP 400), e.g. "You have reached your specified + # API usage limits." It is a usage limit and should map to 429. (#37599) + ####################################### + if "api usage limit" in _error_str_lower: + return True + return False @staticmethod diff --git a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py index fa8b437a64b..8dc0da93410 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -206,6 +206,18 @@ class TestExceptionCheckers: is True ) + def test_anthropic_usage_limit_is_treated_as_rate_limit(self): + """Anthropic reports spend/usage-limit exhaustion as an invalid_request_error + (HTTP 400), but it is a usage limit and should map to 429, not 400 (#37599). + """ + error_str = ( + "litellm.BadRequestError: AnthropicException - " + '{"type":"error","error":{"type":"invalid_request_error",' + '"message":"You have reached your specified API usage limits. ' + 'You will regain access on 2026-09-01 at 00:00 UTC."}}' + ) + assert ExceptionCheckers.is_error_str_rate_limit(error_str, status_code=400) is True + def test_is_azure_content_policy_violation_error_with_policy_violation_text(self): """Test detection of Azure content policy violation with explicit policy violation text"""