mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
* feat(proxy): add per-team chargeback cost margins Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com> * fix(proxy): secure team pricing and surcharge budget reservations Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com> * chore(ui): regenerate team pricing endpoint descriptions Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com> * fix(proxy): preserve team pricing when archiving deleted teams Co-Authored-By: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com> * feat(proxy): replace the per-team margin with a surcharge on customer requests A cost_margin_config entry keyed customer adds a percentage to requests made for a customer organization. The surcharge is part of spend, so customer budgets count it, and each request records it in cost_breakdown and in a customer_margin_spend column on the daily spend tables so cost before the surcharge stays exact Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * chore: regenerate the lazy openapi snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style: keep the accumulation suppression on its line Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: read the recorded surcharge through a typed model Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): import the surcharge breakdown on the customer usage page Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): type the surcharge field where daily metrics are built Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): show a small surcharge to four decimals Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): reload margins after a refused save and drop the fixed amount for customer requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style(ui): format the surcharge breakdown Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: carry the surcharge column in daily activity fixtures Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * 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. * feat(proxy): let each app decide whether it may create unknown customers Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style: sort imports Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): lock the app customer switch while its save is pending Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): surcharge requests charged to a customer through the key's binding Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: forward the surcharge through the grouped daily activity fixture Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): surcharge billed failure rows charged to a customer Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style: keep the callback tests' existing formatting Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): refuse an unknown organization alias sent with a known member Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(proxy): let an app set its own surcharge on customer requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): refuse an unknown customer before reporting its organization's budget Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): surcharge a customer-bound key at settlement and record no fields for a zero surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: a zero surcharge is covered by its own case Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(ui): move the no-app customer switch into its own component Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): read a key's customer binding from the trusted metadata bucket of any route Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(proxy): apply an app's surcharge to keys bound to a customer Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * 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. * test(proxy): let the customer usage fake answer app lookups for customer-bound keys Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): type the payload metadata read for customer attribution Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): surcharge billed failures on customer-bound keys and record the surcharge without a snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(proxy): surcharge billed failures whose customer binding sits beside litellm_params Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): drive a billed Responses failure on a customer-bound key through the failure callback Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): drive a billed Responses failure through the failure callback at each app surcharge Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * 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. * fix(customer-portal): preserve app attribution for deferred costs Persist the original trusted customer and app billing context on managed jobs. Restore it in batch and background cost polling with the original request time, preserving admission windows and existing claim behavior. Recognize the India Bedrock model prefix in dashboard model setup. * fix(ci): make migration repeatable and await scoped callbacks Set the required migration lock timeout and add the billing-context column only if it is absent. Use per-request completion test callbacks and await the MCP panel failure notification. Keep response, pricing, and complete callback-sequence assertions. * fix(ui): align Bedrock profile parsing with main Use the upstream country-prefix handling to clear the model-setup merge conflict. * refactor(proxy): resolve a customer under its registration rules in one helper 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: Ishaan Jaffer <155045088+ishaan-berri@users.noreply.github.com> Co-authored-by: ishaan-j-berri <ishaan.j@berri.ai>
249 lines
11 KiB
Python
249 lines
11 KiB
Python
from types import SimpleNamespace
|
|
from typing import TYPE_CHECKING, Final, cast # noqa: TID251 # in-memory adapters implement the external Prisma edge
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
import litellm
|
|
from litellm.proxy._types import ProxyException, UserAPIKeyAuth
|
|
from litellm.proxy.auth.customer_hierarchy_auth import CustomerBudgetExceededError
|
|
from litellm.proxy.auth.customer_provisioning import (
|
|
gated_registrars,
|
|
refuse_unknown_customer,
|
|
registration_refusal,
|
|
resolve_registered_customer,
|
|
unknown_customer_header,
|
|
)
|
|
from litellm.proxy.management_helpers.apps import AppStore
|
|
from litellm.proxy.management_helpers.customer_hierarchy import ErrorCode, Failure
|
|
from litellm.types.proxy.auth.customer_hierarchy import CustomerHierarchyAuth, CustomerRequestIdentity
|
|
from litellm.types.proxy.management_endpoints.apps import (
|
|
AppCreate,
|
|
AppDefinition,
|
|
AppFailure,
|
|
AppPatch,
|
|
AppResourceType,
|
|
)
|
|
from tests.unit.proxy.management_helpers.test_apps import AppDatabase, seed_resources
|
|
from tests.unit.proxy.management_helpers.test_customer_hierarchy import NOW
|
|
|
|
if TYPE_CHECKING:
|
|
from prisma import Prisma
|
|
|
|
from litellm.proxy.utils import PrismaClient
|
|
|
|
KNOWN: Final = CustomerHierarchyAuth(
|
|
customer_org_id="org", customer_org_alias="org", customer_member_id="member", scopes=()
|
|
)
|
|
ORG_ONLY: Final = CustomerHierarchyAuth(customer_org_id="org", customer_org_alias="org", scopes=())
|
|
|
|
|
|
async def _refusal(
|
|
app_may_create: bool | None, without_app_may_create: bool, authenticated: UserAPIKeyAuth
|
|
) -> str | None:
|
|
database: Final = AppDatabase()
|
|
store: Final = AppStore(cast("Prisma", database), NOW) # cast-ok: in-memory Prisma edge
|
|
await seed_resources(database)
|
|
await store.put_definition(
|
|
AppDefinition(resource_type=AppResourceType.TEAM, auto_create_customers_without_app=without_app_may_create)
|
|
)
|
|
app: Final = await store.create(AppCreate(resource_id="chat", auto_create_customers=app_may_create), "admin")
|
|
assert not isinstance(app, AppFailure)
|
|
connection: Final = cast("PrismaClient", SimpleNamespace(db=database)) # cast-ok: in-memory Prisma edge
|
|
return await registration_refusal(connection, authenticated)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("app_may_create", "without_app_may_create", "team_id", "refused"),
|
|
(
|
|
(None, True, "chat", False),
|
|
(True, False, "chat", False),
|
|
(False, True, "chat", True),
|
|
(False, True, "search", False),
|
|
(True, False, "search", True),
|
|
(True, False, None, True),
|
|
(False, True, None, False),
|
|
),
|
|
)
|
|
async def test_a_request_may_register_customers_only_when_its_app_or_the_no_app_setting_allows(
|
|
app_may_create: bool | None, without_app_may_create: bool, team_id: str | None, refused: bool
|
|
) -> None:
|
|
reason: Final = await _refusal(app_may_create, without_app_may_create, UserAPIKeyAuth(team_id=team_id))
|
|
|
|
assert (reason is not None) is refused, reason
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_request_may_register_customers_when_no_app_definition_was_ever_chosen() -> None:
|
|
connection: Final = cast("PrismaClient", SimpleNamespace(db=AppDatabase())) # cast-ok: in-memory Prisma edge
|
|
|
|
assert await registration_refusal(connection, UserAPIKeyAuth(team_id="chat")) is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_turning_registration_off_for_an_app_is_stored_and_survives_an_unrelated_patch() -> None:
|
|
database: Final = AppDatabase()
|
|
store: Final = AppStore(cast("Prisma", database), NOW) # cast-ok: in-memory Prisma edge
|
|
await seed_resources(database)
|
|
await store.put_definition(AppDefinition(resource_type=AppResourceType.TEAM))
|
|
app: Final = await store.create(AppCreate(resource_id="chat"), "admin")
|
|
assert not isinstance(app, AppFailure)
|
|
assert app.auto_create_customers is True
|
|
|
|
off: Final = await store.patch(app.app_id, AppPatch(auto_create_customers=False), "admin")
|
|
unrelated: Final = await store.patch(app.app_id, AppPatch(default_tier_budget_id=None), "admin")
|
|
|
|
assert not isinstance(off, AppFailure) and off.auto_create_customers is False
|
|
assert not isinstance(unrelated, AppFailure) and unrelated.auto_create_customers is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_the_no_app_setting_survives_a_definition_write_that_omits_it_and_an_app_being_configured() -> None:
|
|
database: Final = AppDatabase()
|
|
store: Final = AppStore(cast("Prisma", database), NOW) # cast-ok: in-memory Prisma edge
|
|
await seed_resources(database)
|
|
await store.put_definition(
|
|
AppDefinition(resource_type=AppResourceType.TEAM, auto_create_customers_without_app=False)
|
|
)
|
|
|
|
await store.put_definition(AppDefinition(resource_type=AppResourceType.TEAM))
|
|
await store.create(AppCreate(resource_id="chat"), "admin")
|
|
|
|
assert await store.definition() == AppDefinition(
|
|
resource_type=AppResourceType.TEAM, auto_create_customers_without_app=False
|
|
)
|
|
changed: Final = await store.put_definition(
|
|
AppDefinition(resource_type=AppResourceType.TEAM, auto_create_customers_without_app=True)
|
|
)
|
|
assert changed == AppDefinition(resource_type=AppResourceType.TEAM, auto_create_customers_without_app=True)
|
|
assert await store.put_definition(AppDefinition(resource_type=AppResourceType.PROJECT)) == AppFailure(
|
|
409, "The app resource definition cannot change after a resource has been configured."
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("identity", "hierarchy", "header"),
|
|
(
|
|
(CustomerRequestIdentity(customer_org_alias="org"), None, "x-litellm-customer-org-alias"),
|
|
(CustomerRequestIdentity(customer_org_alias="org"), ORG_ONLY, None),
|
|
(CustomerRequestIdentity(customer_member_id="member"), None, "x-litellm-customer-member-id"),
|
|
(
|
|
CustomerRequestIdentity(customer_org_alias="org", customer_member_id="member"),
|
|
ORG_ONLY,
|
|
"x-litellm-customer-member-id",
|
|
),
|
|
(CustomerRequestIdentity(customer_org_alias="org", customer_member_id="member"), KNOWN, None),
|
|
(
|
|
CustomerRequestIdentity(customer_org_alias="another-org", customer_member_id="member"),
|
|
KNOWN,
|
|
"x-litellm-customer-org-alias",
|
|
),
|
|
),
|
|
)
|
|
def test_the_unknown_customer_header_is_the_one_the_resolved_hierarchy_lacks(
|
|
identity: CustomerRequestIdentity, hierarchy: CustomerHierarchyAuth | None, header: str | None
|
|
) -> None:
|
|
assert unknown_customer_header(identity, hierarchy) == header
|
|
|
|
|
|
async def _refused() -> str | None:
|
|
return "App chat may not create customers."
|
|
|
|
|
|
async def _allowed() -> str | None:
|
|
return None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_unknown_customer_is_refused_with_403_naming_its_header_when_registration_is_off() -> None:
|
|
with pytest.raises(ProxyException) as refused:
|
|
await refuse_unknown_customer(CustomerRequestIdentity(customer_org_alias="new-tenant"), None, _refused)
|
|
|
|
assert (refused.value.code, refused.value.param) == ("403", "x-litellm-customer-org-alias")
|
|
assert refused.value.message == (
|
|
"Unknown customer in x-litellm-customer-org-alias. App chat may not create customers."
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_unknown_member_of_a_known_organization_is_refused_from_the_organization_only_hierarchy() -> None:
|
|
identity: Final = CustomerRequestIdentity(customer_org_alias="org", customer_member_id="new-member")
|
|
|
|
with pytest.raises(ProxyException) as refused:
|
|
await refuse_unknown_customer(identity, ORG_ONLY, _refused)
|
|
|
|
assert (refused.value.code, refused.value.param) == ("403", "x-litellm-customer-member-id")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("identity", "hierarchy", "refusal"),
|
|
(
|
|
(CustomerRequestIdentity(customer_org_alias="org", customer_member_id="member"), KNOWN, _refused),
|
|
(CustomerRequestIdentity(customer_org_alias="new-tenant"), None, _allowed),
|
|
),
|
|
)
|
|
async def test_a_known_customer_or_an_allowed_registration_is_not_refused(
|
|
identity: CustomerRequestIdentity, hierarchy: CustomerHierarchyAuth | None, refusal: object
|
|
) -> None:
|
|
assert await refuse_unknown_customer(identity, hierarchy, refusal) is None # pyright: ignore[reportArgumentType] # parametrized callables
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_registrars_create_nothing_when_registration_is_off() -> None:
|
|
member, organization = gated_registrars(_refused)
|
|
no_database: Final = cast("PrismaClient", None) # cast-ok: a refused registrar must never reach the database
|
|
|
|
assert await organization("new-tenant", no_database) == Failure(ErrorCode.NOT_FOUND)
|
|
assert await member("org", "new-user", no_database) == Failure(ErrorCode.NOT_FOUND)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_apps_surcharge_is_stored_survives_an_unrelated_patch_and_clears_with_null() -> None:
|
|
database: Final = AppDatabase()
|
|
store: Final = AppStore(cast("Prisma", database), NOW) # cast-ok: in-memory Prisma edge
|
|
await seed_resources(database)
|
|
await store.put_definition(AppDefinition(resource_type=AppResourceType.TEAM))
|
|
app: Final = await store.create(AppCreate(resource_id="chat"), "admin")
|
|
assert not isinstance(app, AppFailure)
|
|
assert app.customer_margin is None
|
|
|
|
set_to_five: Final = await store.patch(app.app_id, AppPatch(customer_margin=0.05), "admin")
|
|
unrelated: Final = await store.patch(app.app_id, AppPatch(auto_create_customers=False), "admin")
|
|
cleared: Final = await store.patch(app.app_id, AppPatch(customer_margin=None), "admin")
|
|
|
|
assert not isinstance(set_to_five, AppFailure) and set_to_five.customer_margin == 0.05
|
|
assert not isinstance(unrelated, AppFailure) and unrelated.customer_margin == 0.05
|
|
assert not isinstance(cleared, AppFailure) and cleared.customer_margin is None
|
|
|
|
|
|
@pytest.mark.parametrize("value", [-0.01, 10.01])
|
|
def test_an_apps_surcharge_outside_zero_to_ten_is_rejected(value: float) -> None:
|
|
with pytest.raises(ValidationError):
|
|
AppPatch(customer_margin=value)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_over_budget_organization_does_not_hide_that_its_new_member_may_not_register() -> None:
|
|
identity: Final = CustomerRequestIdentity(customer_org_alias="org", customer_member_id="new-member")
|
|
|
|
async def over_budget(register: object, register_organization: object) -> CustomerHierarchyAuth | None:
|
|
raise CustomerBudgetExceededError(ORG_ONLY, litellm.BudgetExceededError(current_cost=2, max_budget=1))
|
|
|
|
with pytest.raises(ProxyException) as refused:
|
|
await resolve_registered_customer(identity, _refused, over_budget)
|
|
assert (refused.value.code, refused.value.param) == ("403", "x-litellm-customer-member-id")
|
|
|
|
with pytest.raises(CustomerBudgetExceededError):
|
|
await resolve_registered_customer(identity, _allowed, over_budget)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_known_customer_resolves_to_its_hierarchy_when_registration_is_off() -> None:
|
|
identity: Final = CustomerRequestIdentity(customer_org_alias="org", customer_member_id="member")
|
|
|
|
async def known(register: object, register_organization: object) -> CustomerHierarchyAuth | None:
|
|
return KNOWN
|
|
|
|
assert await resolve_registered_customer(identity, _refused, known) is KNOWN
|