litellm/litellm/models/alert.py
moe-berri ea448a8951
feat(alerts): add email labels, bulk actions and threshold editing (#52139)
* feat(alerts): add bulk actions and enterprise thresholds

* fix(alerts): accept fractional multipliers above one
2026-11-16 15:32:19 -08:00

225 lines
9.6 KiB
Python

from datetime import date as Date
from datetime import datetime, timezone
from typing import Annotated, Final, Literal, Protocol, TypeAlias
from pydantic import AfterValidator, AwareDatetime, BaseModel, ConfigDict, Field, field_validator, model_validator
from typing_extensions import Self
MetricAlertType: TypeAlias = Literal["spend", "traffic", "latency", "reliability"]
AlertType: TypeAlias = MetricAlertType | Literal["model_deprecation", "prompt_cache"]
AlertFilter: TypeAlias = AlertType | Literal["performance"]
AlertStatus: TypeAlias = Literal["open", "resolved", "dismissed"]
AlertResourceType: TypeAlias = Literal["gateway", "key", "team", "user", "model", "deployment"]
METRIC_ALERT_TYPES: Final[tuple[MetricAlertType, ...]] = ("spend", "traffic", "latency", "reliability")
MAX_ALERT_ID: Final = 2**63 - 1
def _validate_alert_id(value: str) -> str:
if int(value) > MAX_ALERT_ID:
raise ValueError("Alert ID exceeds the supported range")
return value
AlertID: TypeAlias = Annotated[
str, Field(pattern=r"^[1-9][0-9]{0,18}$", max_length=19), AfterValidator(_validate_alert_id)
]
class MetricEvidence(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
value: float = Field(ge=0, allow_inf_nan=False, description="Observed metric value in the alert window.")
baseline: float = Field(ge=0, allow_inf_nan=False, description="Comparison value in the same unit.")
unit: Literal["usd", "requests", "milliseconds", "ratio"] = Field(description="Unit for both values.")
sample_size: int = Field(ge=0, le=MAX_ALERT_ID, description="Number of requests in the alert window.")
class DeprecationEvidence(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
deprecation_date: Date = Field(description="Date the affected model retires.")
replacement_model: str | None = Field(
default=None, min_length=1, max_length=1024, description="Suggested replacement."
)
class PromptCacheEvidence(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
config_fingerprint: str = Field(min_length=1, max_length=64, description="Cache-relevant deployment settings.")
prefix_fingerprint: str = Field(min_length=1, max_length=64, description="Keyed hash of the cached prefix.")
provider: Literal["anthropic"] = Field(description="Provider that served the requests.")
provider_model: str = Field(min_length=1, max_length=1024, description="Model the provider served.")
consecutive_misses: int = Field(
ge=1,
le=MAX_ALERT_ID,
description="Observed requests that reported no cache read or write, not necessarily consecutive.",
)
proven_write_tokens: int = Field(ge=1, le=MAX_ALERT_ID, description="Tokens the proving cache write stored.")
ttl_seconds: Literal[300] = Field(description="Cache lifetime of the prefix's breakpoint.")
class AlertSummary(BaseModel):
model_config = ConfigDict(frozen=True)
id: AlertID
type: AlertType
resource_type: AlertResourceType
resource_name: str
created_at: AwareDatetime
evidence: MetricEvidence
class AlertRecipient(BaseModel):
model_config = ConfigDict(frozen=True)
user_id: str
email: str
class AlertDetection(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
type: AlertType = Field(description="Built-in alert category.")
resource_type: AlertResourceType = Field(description="Kind of affected resource.")
resource_id: str = Field(
min_length=1,
max_length=1024,
description="Resource ID. Whole-gateway alerts use 'gateway'. Keys use their SHA-256 hash, never a secret.",
)
window_start: AwareDatetime = Field(
description="Inclusive start of the observed window, normalized to UTC milliseconds."
)
window_end: AwareDatetime = Field(
description="Exclusive end of the observed window, normalized to UTC milliseconds."
)
evidence: MetricEvidence | DeprecationEvidence | PromptCacheEvidence = Field(
description="Small saved snapshot explaining the alert."
)
@field_validator("window_start", "window_end")
@classmethod
def normalize_window(cls, value: datetime) -> datetime:
return value.astimezone(timezone.utc).replace(microsecond=value.microsecond // 1000 * 1000)
@model_validator(mode="after")
def validate_detection(self) -> Self:
if self.window_end <= self.window_start:
raise ValueError("window_end must be after window_start")
if self.resource_type == "gateway" and self.resource_id != "gateway":
raise ValueError("Whole-gateway alerts must use resource_id 'gateway'")
if self.resource_type == "key" and (
len(self.resource_id) != 64 or any(c not in "0123456789abcdef" for c in self.resource_id)
):
raise ValueError("Key resource IDs must be SHA-256 hashes")
if self.type == "model_deprecation":
if not isinstance(self.evidence, DeprecationEvidence) or self.resource_type != "model":
raise ValueError("Model deprecations require a model resource and deprecation evidence")
return self
if self.type == "prompt_cache":
if not isinstance(self.evidence, PromptCacheEvidence) or self.resource_type != "deployment":
raise ValueError("Prompt cache alerts require a deployment resource and prompt cache evidence")
return self
if self.resource_type == "deployment":
raise ValueError("Only prompt cache alerts use a deployment resource")
expected_unit: Final = {
"spend": "usd",
"traffic": "requests",
"latency": "milliseconds",
"reliability": "ratio",
}[self.type]
if not isinstance(self.evidence, MetricEvidence) or self.evidence.unit != expected_unit:
raise ValueError("Evidence unit must match the alert type")
if self.type == "reliability" and (self.evidence.value > 1 or self.evidence.baseline > 1):
raise ValueError("Error ratios must be between zero and one")
return self
def identity(self) -> tuple[str, ...]:
"""One prompt cache incident per deployment and configuration, whatever window or prefix found it, so a
dismissed or resolved one is never reopened by a later detection."""
if isinstance(self.evidence, PromptCacheEvidence):
return (self.type, self.resource_type, self.resource_id, self.evidence.config_fingerprint)
return (
self.type,
self.resource_type,
self.resource_id,
self.window_start.astimezone(timezone.utc).isoformat(),
self.window_end.astimezone(timezone.utc).isoformat(),
)
class Alert(AlertDetection):
id: AlertID = Field(description="Opaque alert ID, also used as a pagination cursor.")
status: AlertStatus = Field(description="Current review status.")
created_at: AwareDatetime = Field(description="When the alert was first saved.")
updated_at: AwareDatetime = Field(description="When its status last changed.")
updated_by: str | None = Field(default=None, max_length=1024, description="User who last changed the status.")
class AlertHistoryPoint(BaseModel):
model_config = ConfigDict(frozen=True)
date: Date = Field(description="UTC day represented by this value.")
value: float = Field(allow_inf_nan=False, description="Recorded daily value in the alert metric's unit.")
class AlertView(Alert):
resource_name: str | None = Field(default=None, max_length=1024, description="Current resource display name.")
history: tuple[AlertHistoryPoint, ...] | None = Field(
default=None,
max_length=8,
description="Current Usage values for the seven baseline days and observed day. Null if unavailable.",
)
class AlertUpdate(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
status: AlertStatus = Field(description="Mark resolved, dismiss, or reopen the alert.")
class AlertBulkUpdate(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
status: Literal["resolved", "dismissed"] = Field(description="Status to apply to every matching open saved alert.")
type: AlertFilter | None = Field(
default=None, description="Only update this category, or all saved alerts if omitted."
)
class AlertBulkResult(BaseModel):
model_config = ConfigDict(frozen=True)
updated: int = Field(ge=0, description="Number of open saved alerts changed.")
class AlertPage(BaseModel):
model_config = ConfigDict(frozen=True)
data: tuple[Alert, ...] = Field(description="Alerts ordered newest first.")
next_cursor: AlertID | None = Field(
default=None, description="Pass as cursor to fetch the next page, or null at the end."
)
class AlertViewPage(BaseModel):
model_config = ConfigDict(frozen=True)
data: tuple[AlertView, ...] = Field(description="Alerts ordered newest first, with current Usage history.")
next_cursor: AlertID | None = Field(default=None, description="Cursor for the next page, or null at the end.")
class AlertStore(Protocol):
async def record(self, detection: AlertDetection) -> Alert: ...
async def list(
self, status: AlertStatus, limit: int, cursor: int | None, alert_type: AlertFilter | None = None
) -> AlertPage: ...
async def enrich(self, alerts: tuple[Alert, ...]) -> tuple[AlertView, ...]: ...
async def get(self, alert_id: int) -> Alert | None: ...
async def set_status(self, alert_id: int, status: AlertStatus, actor: str) -> Alert | None: ...
async def set_open_status(self, data: AlertBulkUpdate, actor: str) -> int: ...