mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-03 01:38:46 +00:00
* feat(proxy): enforce customer org, team and member budgets and limits on the request path Resolve x-litellm-customer-member-id at auth, walk member -> team -> org, enforce max_budget, allowed_models, tpm/rpm and per-model limits at every level in the v3 limiter, honor the hierarchy allowlists in alternate-model steering, and settle spend to the member, team and org rows. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(auth): customer hierarchy survives virtual key auth without a legacy end user Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(auth): keys pinned to a customer organization may only act for that organization's members Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style(proxy): keep the customer hierarchy spend maps within the line limit Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(auth): check the key's customer org scope after the key is resolved Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): declare the hierarchy cache key family and keep type counts flat Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(auth): resolve the customer hierarchy in the shared authorization gate Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): drop a member's cached hierarchy through the declared cache target Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style(auth): keep the hierarchy reset within the line limit Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(auth): type the requested models as a sequence Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(spend): add daily customer usage and budget resets (#51493) * feat(spend): add daily customer usage and budget resets * chore(spend): document customer reset query bounds * fix(spend): chart customer queue metrics and update recovery test * fix(proxy): read stamped customer ids from either metadata bucket, alias-aware model check, broadcast member evictions Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Revert "feat(spend): add daily customer usage and budget resets (#51493)" This reverts commitccac47608f. * fix(proxy): evict cached member hierarchies when team, org or template budgets change Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): declare the auth cache dependency alias as a TypeAlias Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Reapply "feat(spend): add daily customer usage and budget resets (#51493)" This reverts commit7542471e17. * fix(spend): reconcile the daily customer spend table with main Keep main's LiteLLM_DailyCustomerMemberSpend shape, make model_group nullable and add the identity unique index the bulk upsert arbitrates on in a new migration, drop the duplicate model and migration, and feed both the member-header hierarchy and the key's customer usage attribution through the shared daily spend projection so neither path double-writes. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(pagerduty): tolerate failure metadata without customer hierarchy ids Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(accounting): key daily customer spend rows on main's hashed identity Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(pagerduty): read customer ids from failure metadata through a typed lookup 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> Co-authored-by: moe-berri <moe@berri.ai>
81 lines
3.9 KiB
Python
81 lines
3.9 KiB
Python
from collections.abc import Iterable
|
|
from itertools import chain, groupby
|
|
from typing import TYPE_CHECKING, Final
|
|
from urllib.parse import quote
|
|
|
|
from litellm.accounting.projection import Contribution, project
|
|
from litellm.repositories.accounting_delivery_repository import DeliveryProjection
|
|
|
|
|
|
def _destination(contribution: Contribution) -> tuple[str, tuple[tuple[str, str], ...], str]:
|
|
return (
|
|
contribution.table,
|
|
contribution.key,
|
|
(dict(contribution.values).get("window_start") or "")
|
|
if contribution.table == "LiteLLM_BudgetWindowSpend"
|
|
else "",
|
|
)
|
|
|
|
|
|
def _sum(contributions: tuple[Contribution, ...]) -> Contribution:
|
|
first: Final = contributions[0]
|
|
increments: Final = tuple(
|
|
(column, sum(dict(item.increments).get(column, 0) for item in contributions))
|
|
for column in dict.fromkeys(
|
|
column for column, _ in chain.from_iterable(item.increments for item in contributions)
|
|
)
|
|
)
|
|
starts: Final = tuple(
|
|
value
|
|
for column, value in chain.from_iterable(item.values for item in contributions)
|
|
if column == "started_at" and value
|
|
)
|
|
values: Final = tuple(
|
|
(column, min(starts) if column == "started_at" and starts else value) for column, value in first.values
|
|
)
|
|
return Contribution(first.table, first.key, increments, values)
|
|
|
|
|
|
def project_deliveries(deliveries: Iterable[DeliveryProjection]) -> tuple[Contribution, ...]:
|
|
ordered: Final = sorted(chain.from_iterable(project(delivery.row) for delivery in deliveries), key=_destination)
|
|
return tuple(_sum(tuple(group)) for _, group in groupby(ordered, key=_destination))
|
|
|
|
|
|
def entity_transactions(contributions: tuple[Contribution, ...]) -> "DBSpendUpdateTransactions":
|
|
return {
|
|
"user_list_transactions": dict(_entity_totals(contributions, "LiteLLM_UserTable")),
|
|
"end_user_list_transactions": dict(_entity_totals(contributions, "LiteLLM_EndUserTable")),
|
|
"key_list_transactions": dict(_entity_totals(contributions, "LiteLLM_VerificationToken")),
|
|
"team_list_transactions": dict(_entity_totals(contributions, "LiteLLM_TeamTable")),
|
|
"team_member_list_transactions": dict(_entity_totals(contributions, "LiteLLM_TeamMembership")),
|
|
"org_list_transactions": dict(_entity_totals(contributions, "LiteLLM_OrganizationTable")),
|
|
"org_member_list_transactions": dict(_entity_totals(contributions, "LiteLLM_OrganizationMembership")),
|
|
"project_list_transactions": dict(_entity_totals(contributions, "LiteLLM_ProjectTable")),
|
|
"tag_list_transactions": dict(_entity_totals(contributions, "LiteLLM_TagTable")),
|
|
"agent_list_transactions": dict(_entity_totals(contributions, "LiteLLM_AgentsTable")),
|
|
"model_access_group_list_transactions": dict(
|
|
_entity_totals(contributions, "LiteLLM_ModelAccessGroupBudgetTable")
|
|
),
|
|
"customer_org_list_transactions": dict(_entity_totals(contributions, "LiteLLM_CustomerOrganizationTable")),
|
|
"customer_team_list_transactions": dict(_entity_totals(contributions, "LiteLLM_CustomerTeamTable")),
|
|
"customer_member_list_transactions": dict(_entity_totals(contributions, "LiteLLM_CustomerMemberTable")),
|
|
}
|
|
|
|
|
|
def _entity_totals(contributions: tuple[Contribution, ...], table: str) -> tuple[tuple[str, float], ...]:
|
|
return tuple(
|
|
(_entity_key(item), float(dict(item.increments)["spend"])) for item in contributions if item.table == table
|
|
)
|
|
|
|
|
|
def _entity_key(item: Contribution) -> str:
|
|
fields: Final = dict(item.key)
|
|
if item.table == "LiteLLM_TeamMembership":
|
|
return f"team_id::{fields['team_id']}::user_id::{fields['user_id']}"
|
|
if item.table == "LiteLLM_OrganizationMembership":
|
|
return f"org_id::{quote(fields['organization_id'], safe='')}::user_id::{quote(fields['user_id'], safe='')}"
|
|
return item.key[0][1]
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from litellm.proxy._types import DBSpendUpdateTransactions
|