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: address Greptile review — empty recipients guard, type annotation, task pre-filter

Ryan Crabbe44eb2ea56e88
diff --git a/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py b/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py
index 36163253839..533fb134937 100644
--- a/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py
+++ b/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py
@@ -615,7 +615,14 @@ class BaseEmailLogger(CustomLogger):
emails = _parse_email_list(raw_emails)
if user_info.user_email:
emails.append(user_info.user_email)
- recipient_emails = list(set(emails)) if emails else None
+ if not emails:
+ verbose_proxy_logger.warning(
+ "No recipients for %d%% threshold on key %s, skipping alert",
+ threshold_pct,
+ _id,
+ )
+ continue
+ recipient_emails = list(set(emails))
event_message = f"Max Budget Alert - {threshold_pct}% of Maximum Budget Reached"
webhook_event = WebhookEvent(
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index d3c83248706..d12414c19c7 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -3095,7 +3095,7 @@ class CallInfo(LiteLLMPydanticObjectBase):
default=None,
description="Additional email addresses to send alerts to (e.g., from team metadata)",
)
- max_budget_alert_emails: Optional[Dict[str, List[str]]] = Field(
+ max_budget_alert_emails: Optional[Dict[str, Union[str, List[str]]]] = Field(
default=None,
description="Map of threshold percentage to email recipients (e.g., {'50': ['[email protected]'], '75': ['[email protected]', '[email protected]']})",
)
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 19751f6edc6..65b75dffb65 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -2969,7 +2969,14 @@ async def _virtual_key_max_budget_alert_check(
) or litellm.default_key_max_budget_alert_emails
if isinstance(alert_email_config, dict) and alert_email_config:
- # New path: pass the map through, let the email handler decide what to fire
+ # New path: only create task if spend has crossed the lowest threshold
+ min_pct = min(
+ (int(k) for k in alert_email_config if k.isdigit()),
+ default=None,
+ )
+ if min_pct is None or valid_token.spend < valid_token.max_budget * (min_pct / 100.0):
+ return
+
call_info = CallInfo(
token=valid_token.token,
spend=valid_token.spend,
diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py
index 0f7ffa541af..d74c4883f63 100644
--- a/tests/test_litellm/proxy/auth/test_auth_checks.py
+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py
@@ -1562,7 +1562,7 @@ async def test_virtual_key_max_budget_alert_check_with_multi_threshold_map():
}
valid_token = UserAPIKeyAuth(
token="test-token",
- spend=30.0,
+ spend=60.0,
max_budget=100.0,
user_id="test-user",
key_alias="test-key",
@@ -1670,7 +1670,7 @@ async def test_virtual_key_max_budget_alert_check_global_fallback():
}
valid_token = UserAPIKeyAuth(
token="test-token",
- spend=30.0,
+ spend=60.0,
max_budget=100.0,
user_id="test-user",
key_alias="test-key",
@@ -1709,7 +1709,7 @@ async def test_virtual_key_max_budget_alert_check_per_key_overrides_global():
valid_token = UserAPIKeyAuth(
token="test-token",
- spend=30.0,
+ spend=60.0,
max_budget=100.0,
user_id="test-user",
key_alias="test-key",