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]

Adding files

yuneng-jiangc1e3364c4ff0
diff --git a/litellm/constants.py b/litellm/constants.py
index 38d3e8a1753..23766874841 100644
--- a/litellm/constants.py
+++ b/litellm/constants.py
@@ -313,6 +313,8 @@ DD_TRACER_STREAMING_CHUNK_YIELD_RESOURCE = os.getenv(
"DD_TRACER_STREAMING_CHUNK_YIELD_RESOURCE", "streaming.chunk.yield"
)
+EMAIL_BUDGET_ALERT_TTL = int(os.getenv("EMAIL_BUDGET_ALERT_TTL", 24 * 60 * 60)) # 24 hours in seconds
+EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE = float(os.getenv("EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE", 0.8)) # 80% of max budget
############### LLM Provider Constants ###############
### ANTHROPIC CONSTANTS ###
ANTHROPIC_SKILLS_API_BETA_VERSION = "skills-2025-10-02"
diff --git a/litellm/integrations/email_templates/templates.py b/litellm/integrations/email_templates/templates.py
index 2ec6372caa4..5de23db0f24 100644
--- a/litellm/integrations/email_templates/templates.py
+++ b/litellm/integrations/email_templates/templates.py
@@ -81,6 +81,30 @@ SOFT_BUDGET_ALERT_EMAIL_TEMPLATE = """
If you have any questions, please send an email to {email_support_contact} <br /> <br />
+ Best, <br />
+ The LiteLLM team <br />
+"""
+
+MAX_BUDGET_ALERT_EMAIL_TEMPLATE = """
+ <img src="{email_logo_url}" alt="LiteLLM Logo" width="150" height="50" />
+
+ <p> Hi {recipient_email}, <br/>
+
+ Your LiteLLM API key has reached <b>{percentage}% of its maximum budget</b>. <br /> <br />
+
+ <b>Current Spend:</b> {spend} <br />
+ <b>Maximum Budget:</b> {max_budget} <br />
+ <b>Alert Threshold:</b> {alert_threshold} ({percentage}%) <br />
+
+ <p style="color: #dc2626; font-weight: 500;">
+ ⚠️ Warning: You are approaching your maximum budget limit.
+ Once you reach your maximum budget of {max_budget}, all API requests will be rejected.
+ </p>
+
+ You can view your usage and manage your budget in the <a href="{base_url}">LiteLLM Dashboard</a>. <br /> <br />
+
+ If you have any questions, please send an email to {email_support_contact} <br /> <br />
+
Best, <br />
The LiteLLM team <br />
"""
\ No newline at end of file
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 0a6310c3bc9..6a0223b35ba 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -24,6 +24,7 @@ from litellm.constants import (
DEFAULT_IN_MEMORY_TTL,
DEFAULT_MANAGEMENT_OBJECT_IN_MEMORY_CACHE_TTL,
DEFAULT_MAX_RECURSE_DEPTH,
+ EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE,
)
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
from litellm.proxy._types import (
@@ -1976,6 +1977,56 @@ async def _virtual_key_soft_budget_check(
)
+async def _virtual_key_max_budget_alert_check(
+ valid_token: UserAPIKeyAuth,
+ proxy_logging_obj: ProxyLogging,
+ user_obj: Optional[LiteLLM_UserTable] = None,
+):
+ """
+ Triggers a budget alert if the token has reached EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE
+ (default 80%) of its max budget.
+ This is a warning alert before the token actually exceeds the max budget.
+
+ """
+
+ if (
+ valid_token.max_budget is not None
+ and valid_token.spend is not None
+ and valid_token.spend > 0
+ ):
+ alert_threshold = valid_token.max_budget * EMAIL_BUDGET_ALERT_MAX_SPEND_ALERT_PERCENTAGE
+
+ # Only alert if we've crossed the threshold but haven't exceeded max_budget yet
+ if valid_token.spend >= alert_threshold and valid_token.spend < valid_token.max_budget:
+ verbose_proxy_logger.debug(
+ "Reached Max Budget Alert Threshold for token %s, spend %s, max_budget %s, alert_threshold %s",
+ valid_token.token,
+ valid_token.spend,
+ valid_token.max_budget,
+ alert_threshold,
+ )
+ call_info = CallInfo(
+ token=valid_token.token,
+ spend=valid_token.spend,
+ max_budget=valid_token.max_budget,
+ soft_budget=valid_token.soft_budget,
+ user_id=valid_token.user_id,
+ team_id=valid_token.team_id,
+ team_alias=valid_token.team_alias,
+ organization_id=valid_token.org_id,
+ user_email=user_obj.user_email if user_obj else None,
+ key_alias=valid_token.key_alias,
+ event_group=Litellm_EntityType.KEY,
+ )
+
+ asyncio.create_task(
+ proxy_logging_obj.budget_alerts(
+ type="max_budget_alert",
+ user_info=call_info,
+ )
+ )
+
+
async def _team_max_budget_check(
team_object: Optional[LiteLLM_TeamTable],
valid_token: Optional[UserAPIKeyAuth],
diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py
index 584b06ba792..495d4db304c 100644
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@ -29,6 +29,7 @@ from litellm.proxy.auth.auth_checks import (
_get_user_role,
_is_user_proxy_admin,
_virtual_key_max_budget_check,
+ _virtual_key_max_budget_alert_check,
_virtual_key_soft_budget_check,
can_key_call_model,
common_checks,
@@ -1062,7 +1063,14 @@ async def _user_api_key_auth_builder( # noqa: PLR0915
user_obj=user_obj,
)
- # Check 5. Soft Budget Check
+ # Check 5. Max Budget Alert Check
+ await _virtual_key_max_budget_alert_check(
+ valid_token=valid_token,
+ proxy_logging_obj=proxy_logging_obj,
+ user_obj=user_obj,
+ )
+
+ # Check 6. Soft Budget Check
await _virtual_key_soft_budget_check(
valid_token=valid_token,
proxy_logging_obj=proxy_logging_obj,
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 2c6d818696c..6f42c485f59 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -1195,6 +1195,7 @@ class ProxyLogging:
"token_budget",
"user_budget",
"soft_budget",
+ "max_budget_alert",
"team_budget",
"organization_budget",
"proxy_budget",