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
feat(proxy): add budget_exceeded_status_code setting to restore 429 for budget refusals
yassinbf804f518858
diff --git a/litellm/__init__.py b/litellm/__init__.pyindex 738dd0cac76..be8f59d210b 100644--- a/litellm/__init__.py+++ b/litellm/__init__.py@@ -400,6 +400,7 @@ default_redis_batch_cache_expiry: Optional[float] = Nonemodel_alias_map: Dict[str, str] = {}model_group_settings: Optional["ModelGroupSettings"] = Nonemax_budget: float = 0.0 # set the max budget across all providers+budget_exceeded_status_code: int = 422 # set to 429 to restore the pre-422 budget_exceeded response codebudget_duration: Optional[str] = (None # proxy only - resets budget after fixed duration. You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d").)diff --git a/litellm/exceptions.py b/litellm/exceptions.pyindex 4b236aec99c..c8de2ab12ed 100644--- a/litellm/exceptions.py+++ b/litellm/exceptions.py@@ -16,6 +16,7 @@ from typing import Any, Finalimport httpximport openai+import litellmfrom litellm.types.utils import LiteLLMCommonStringsfrom litellm.types.vector_stores import VectorStoreSearchFailure@@ -1002,7 +1003,7 @@ class BudgetExceededError(Exception):):self.current_cost = current_costself.max_budget = max_budget- self.status_code = 422+ self.status_code = litellm.budget_exceeded_status_codeself.llm_provider = llm_provider or ""self.entity_type = entity_typeself.entity_id = entity_iddiff --git a/tests/test_litellm/test_rate_limit_error_unification.py b/tests/test_litellm/test_rate_limit_error_unification.pyindex 256d33845a5..e5acba938c7 100644--- a/tests/test_litellm/test_rate_limit_error_unification.py+++ b/tests/test_litellm/test_rate_limit_error_unification.py@@ -1404,6 +1404,11 @@ class TestBudgetExceededErrorSurfacesUnifiedFields:assert "Current cost: 0.000109" in e.messageassert "Max budget: 0.0001" in e.message+ def test_should_honor_budget_exceeded_status_code_override(self, monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setattr(litellm, "budget_exceeded_status_code", 429)+ e = litellm.BudgetExceededError(current_cost=0.5, max_budget=0.1)+ assert e.status_code == 429+def test_should_still_be_catchable_as_exception_not_rate_limit_error(self):# Critical: we deliberately did NOT make BudgetExceededError a# RateLimitError subclass. Existing `except BudgetExceededError:`