mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-03 01:38:46 +00:00
* feat(customer-portal): set budgets per customer and gateway app Let administrators choose Teams, Projects, or Virtual Keys as the gateway-wide app scope and assign customer budgets from existing budget groups. Includes the shared budget-group integration from PR #52411 and builds on the existing app accounting implementation. * fix(customer-portal): show app usage and resolve CI regressions Use the existing dashboard controls and compact budget rows. Add app filtering to customer usage and retain trusted app context for budget rejections. Update request-header handling, tracing registry coverage, bounded app discovery, and optional caller-metadata tests. * fix(customer-portal): enforce app caps and charge each window once Enforce an authenticated app budget when optional customer and app headers are omitted. Normalize overlapping scalar and explicit budget windows to a single charge with the stricter cap and explicit reset time. Cover all app primitives through native HTTP auth and use realistic empty gateway configuration in existing test doubles. * fix(customer-portal): reuse budget controls and admission Use shared customer budget controls for app forms and resolve member budgets at one owner. Remove unused app lifecycle and listing routes. Parse request identity once, check each global and app scope once, and keep app counters alive through the existing database flush window.
189 lines
7.3 KiB
Python
189 lines
7.3 KiB
Python
import json
|
|
from collections.abc import Mapping
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import Final
|
|
from urllib.parse import unquote
|
|
|
|
from litellm.accounting.delivery_projection import entity_transactions, project_deliveries
|
|
from litellm.accounting.projection import Contribution
|
|
from litellm.repositories.accounting_delivery_repository import DeliveryProjection
|
|
|
|
_NOW: Final = datetime(2026, 11, 2, tzinfo=timezone.utc)
|
|
|
|
|
|
def _payload(spend: float, started: datetime = _NOW, window_start: datetime = _NOW) -> Mapping[str, object]:
|
|
return {
|
|
"request_id": f"request-{spend}-{started.isoformat()}",
|
|
"startTime": started,
|
|
"api_key": "key",
|
|
"user": "user/1",
|
|
"team_id": "team",
|
|
"organization_id": "org::1",
|
|
"spend": spend,
|
|
"metadata": json.dumps(
|
|
{
|
|
"accounting_entities": {
|
|
"api_key": "key",
|
|
"user": "user/1",
|
|
"team_id": "team",
|
|
"organization_id": "org::1",
|
|
},
|
|
"accounting_windows": [
|
|
{
|
|
"entity_type": "key",
|
|
"entity_id": "key",
|
|
"window_duration": "1d",
|
|
"window_start": window_start.isoformat(),
|
|
}
|
|
],
|
|
}
|
|
),
|
|
}
|
|
|
|
|
|
def _spend(contributions: tuple[Contribution, ...], table: str) -> tuple[float, ...]:
|
|
return tuple(float(dict(item.increments)["spend"]) for item in contributions if item.table == table)
|
|
|
|
|
|
def test_app_charge_is_additional_to_global_customers_and_daily_attribution() -> None:
|
|
row: Final = {
|
|
"request_id": "request",
|
|
"startTime": _NOW,
|
|
"spend": 0.25,
|
|
"api_key": "key",
|
|
"model": "model",
|
|
"custom_llm_provider": "provider",
|
|
"model_group": "model",
|
|
"metadata": {
|
|
"accounting_entities": {
|
|
"app_id": "app",
|
|
"customer_org_id": "org",
|
|
"customer_team_id": "team",
|
|
"customer_member_id": "member",
|
|
}
|
|
},
|
|
}
|
|
contributions: Final = project_deliveries((DeliveryProjection(row),))
|
|
totals: Final = entity_transactions(contributions)
|
|
assert totals["customer_org_list_transactions"] == {"org": 0.25}
|
|
assert totals["customer_team_list_transactions"] == {"team": 0.25}
|
|
assert totals["customer_member_list_transactions"] == {"member": 0.25}
|
|
assert totals["app_list_transactions"] == {"app": 0.25}
|
|
assert totals["app_customer_org_list_transactions"] == {"app:org": 0.25}
|
|
assert totals["app_customer_member_list_transactions"] == {"app:member": 0.25}
|
|
daily: Final = tuple(item for item in contributions if item.table == "LiteLLM_DailyCustomerMemberSpend")
|
|
assert len(daily) == 1 and dict(daily[0].key)["app_id"] == "app"
|
|
|
|
|
|
def _app_delivery(app_id: str, organization_id: str, member_id: str, spend: float) -> DeliveryProjection:
|
|
return DeliveryProjection(
|
|
{
|
|
"request_id": f"{app_id}-{organization_id}-{member_id}",
|
|
"startTime": _NOW,
|
|
"spend": spend,
|
|
"api_key": "key",
|
|
"model": "model",
|
|
"custom_llm_provider": "provider",
|
|
"model_group": "model",
|
|
"metadata": {
|
|
"accounting_entities": {
|
|
"app_id": app_id,
|
|
"customer_org_id": organization_id,
|
|
"customer_member_id": member_id,
|
|
}
|
|
},
|
|
}
|
|
)
|
|
|
|
|
|
def test_one_customers_spend_across_apps_keeps_one_global_total_and_distinct_daily_rows() -> None:
|
|
contributions: Final = project_deliveries(
|
|
(
|
|
_app_delivery("chat", "org", "member", 0.25),
|
|
_app_delivery("search", "org", "member", 0.5),
|
|
)
|
|
)
|
|
totals: Final = entity_transactions(contributions)
|
|
assert totals["customer_org_list_transactions"] == {"org": 0.75}
|
|
assert totals["customer_member_list_transactions"] == {"member": 0.75}
|
|
assert totals["app_list_transactions"] == {"chat": 0.25, "search": 0.5}
|
|
assert totals["app_customer_org_list_transactions"] == {
|
|
"chat:org": 0.25,
|
|
"search:org": 0.5,
|
|
}
|
|
assert totals["app_customer_member_list_transactions"] == {
|
|
"chat:member": 0.25,
|
|
"search:member": 0.5,
|
|
}
|
|
daily: Final = {
|
|
dict(item.key)["app_id"]: dict(item.increments)["spend"]
|
|
for item in contributions
|
|
if item.table == "LiteLLM_DailyCustomerMemberSpend"
|
|
}
|
|
assert daily == {"chat": 0.25, "search": 0.5}
|
|
assert sum(daily.values()) == totals["customer_org_list_transactions"]["org"]
|
|
|
|
|
|
def test_customer_balances_within_one_app_remain_independent() -> None:
|
|
totals: Final = entity_transactions(
|
|
project_deliveries(
|
|
(
|
|
_app_delivery("chat", "first", "member-a", 0.25),
|
|
_app_delivery("chat", "second", "member-b", 0.5),
|
|
)
|
|
)
|
|
)
|
|
assert totals["customer_org_list_transactions"] == {"first": 0.25, "second": 0.5}
|
|
assert totals["customer_member_list_transactions"] == {
|
|
"member-a": 0.25,
|
|
"member-b": 0.5,
|
|
}
|
|
assert totals["app_list_transactions"] == {"chat": 0.75}
|
|
assert totals["app_customer_org_list_transactions"] == {
|
|
"chat:first": 0.25,
|
|
"chat:second": 0.5,
|
|
}
|
|
assert totals["app_customer_member_list_transactions"] == {
|
|
"chat:member-a": 0.25,
|
|
"chat:member-b": 0.5,
|
|
}
|
|
|
|
|
|
def test_deliveries_for_one_destination_sum_and_keep_the_earliest_request_start() -> None:
|
|
later: Final = _NOW + timedelta(hours=1)
|
|
|
|
contributions: Final = project_deliveries(
|
|
(DeliveryProjection(_payload(0.5, started=later)), DeliveryProjection(_payload(0.25)))
|
|
)
|
|
|
|
assert _spend(contributions, "LiteLLM_VerificationToken") == (0.75,)
|
|
windows: Final = tuple(item for item in contributions if item.table == "LiteLLM_BudgetWindowSpend")
|
|
assert len(windows) == 1
|
|
assert dict(windows[0].increments) == {"spend": 0.75}
|
|
assert dict(windows[0].values)["started_at"] == str(_NOW)
|
|
|
|
|
|
def test_the_same_window_in_a_new_period_is_charged_to_its_own_row() -> None:
|
|
next_day: Final = _NOW + timedelta(days=1)
|
|
|
|
contributions: Final = project_deliveries(
|
|
(DeliveryProjection(_payload(0.25)), DeliveryProjection(_payload(0.5, window_start=next_day)))
|
|
)
|
|
|
|
windows: Final = sorted(
|
|
(dict(item.values)["window_start"], dict(item.increments)["spend"])
|
|
for item in contributions
|
|
if item.table == "LiteLLM_BudgetWindowSpend"
|
|
)
|
|
assert windows == [(_NOW.isoformat(), 0.25), (next_day.isoformat(), 0.5)]
|
|
|
|
|
|
def test_entity_transactions_key_memberships_the_way_the_writer_applies_them() -> None:
|
|
transactions: Final = entity_transactions(project_deliveries((DeliveryProjection(_payload(0.25)),)))
|
|
|
|
assert transactions["key_list_transactions"] == {"key": 0.25}
|
|
assert transactions["org_list_transactions"] == {"org::1": 0.25}
|
|
assert transactions["team_member_list_transactions"] == {"team_id::team::user_id::user/1": 0.25}
|
|
((org_member_key, org_member_spend),) = transactions["org_member_list_transactions"].items()
|
|
_, quoted_org_id, _, quoted_user_id = org_member_key.split("::")
|
|
assert (unquote(quoted_org_id), unquote(quoted_user_id), org_member_spend) == ("org::1", "user/1", 0.25)
|