mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* feat(budgets): add per-model tpm and rpm limit maps to budgets and customers Budgets gain model_tpm_limit and model_rpm_limit (model name to whole number, 0 or more). /budget/new, /budget/update, /budget/info and /customer/new carry them, and the auth path loads a customer's maps onto UserAPIKeyAuth as end_user_model_tpm_limit and end_user_model_rpm_limit so the limiter can enforce them in a follow-up. * fix(budgets): keep org, team and project per-model limits in metadata; lock_timeout and type gate fixes Organizations, teams and projects store model_tpm_limit / model_rpm_limit in their own metadata, which the limiter reads, so the shared budget allowlist leaves those two fields to customers. Migration starts with SET LOCAL lock_timeout. Mapping annotations and rebind-ok reasons satisfy the type discipline gate. Customer info tests expect the two new budget keys. * fix(organizations): keep stripping budget_id and flattening nested per-model maps on update The update path pops every budget field from the organization row again, so a caller's budget_id never changes the attached budget, and a nested litellm_budget_table still flattens the two maps into org metadata. Drops the explanatory comment on CUSTOMER_ONLY_BUDGET_FIELDS. * fix(types): keep basedpyright and ruff strict counts flat for the new budget fields Reads the per-model maps off the end-user params with membership checks instead of .get on an untyped dict, writes the inline customer budget through the client's typed jsonify_object, and builds budget rows in handle_budget_for_entity with model_validate so the two Mapping fields never receive object-typed kwargs. * chore: drop unrelated reformatting from project endpoints Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
"""
|
|
Budget table model.
|
|
|
|
Canonical definition for ``litellm_budgettable``. Re-exported from
|
|
``litellm.proxy._types`` for backwards compatibility.
|
|
"""
|
|
|
|
from collections.abc import Mapping
|
|
from datetime import datetime, timezone
|
|
from typing import Final
|
|
|
|
from pydantic import ConfigDict
|
|
|
|
from litellm.types.llms.base import LiteLLMPydanticObjectBase
|
|
|
|
|
|
class LiteLLM_BudgetTable(LiteLLMPydanticObjectBase):
|
|
"""Represents user-controllable params for a LiteLLM_BudgetTable record.
|
|
|
|
Budget-write paths use `model_fields.keys()` on this class as an allowlist
|
|
for user input. Keep server-managed fields (e.g. `budget_reset_at`) on
|
|
`LiteLLM_BudgetTableFull` so they aren't user-settable.
|
|
"""
|
|
|
|
budget_id: str | None = None
|
|
soft_budget: float | None = None
|
|
max_budget: float | None = None
|
|
max_parallel_requests: int | None = None
|
|
tpm_limit: int | None = None
|
|
rpm_limit: int | None = None
|
|
tpd_limit: int | None = None
|
|
model_max_budget: dict | None = None
|
|
model_tpm_limit: Mapping[str, int] | None = None
|
|
model_rpm_limit: Mapping[str, int] | None = None
|
|
budget_duration: str | None = None
|
|
allowed_models: list[str] | None = None # per-member model scope; empty = inherit team models
|
|
temp_budget_increase: float | None = None
|
|
temp_budget_expiry: datetime | None = None
|
|
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
def active_temp_budget_increase(self, now: datetime) -> float:
|
|
if self.temp_budget_increase is None or self.temp_budget_expiry is None:
|
|
return 0.0
|
|
expiry: Final = (
|
|
self.temp_budget_expiry.replace(tzinfo=timezone.utc)
|
|
if self.temp_budget_expiry.tzinfo is None
|
|
else self.temp_budget_expiry
|
|
)
|
|
return 0.0 if expiry <= now else self.temp_budget_increase
|
|
|
|
def effective_max_budget(self, now: datetime) -> float | None:
|
|
if self.max_budget is None:
|
|
return None
|
|
return self.max_budget + self.active_temp_budget_increase(now)
|
|
|
|
|
|
class LiteLLM_BudgetTableFull(LiteLLM_BudgetTable):
|
|
"""LiteLLM_BudgetTable + server-managed fields returned on API responses."""
|
|
|
|
budget_reset_at: datetime | None = None
|
|
created_at: datetime
|
|
|
|
|
|
class LiteLLM_TeamMemberTable(LiteLLM_BudgetTable):
|
|
"""
|
|
Used to track spend of a user_id within a team_id
|
|
"""
|
|
|
|
spend: float | None = None
|
|
user_id: str | None = None
|
|
team_id: str | None = None
|
|
budget_id: str | None = None
|
|
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
|
CUSTOMER_ONLY_BUDGET_FIELDS: Final = frozenset({"model_tpm_limit", "model_rpm_limit"})
|