Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_/management-v1-budgets

Yuneng Jiangf0d1bc3c5ae7
diff --cc litellm/proxy/management_endpoints/management_v1/budgets.py
index 79c3876c7ab,00000000000..d9104eb5da2
mode 100644,000000..100644
--- a/litellm/proxy/management_endpoints/management_v1/budgets.py
+++ b/litellm/proxy/management_endpoints/management_v1/budgets.py
@@@ -1,195 -1,0 +1,200 @@@
+"""`GET /management/v1/budgets`."""
+
- from collections.abc import Mapping, Sequence
++from collections.abc import Sequence
+from dataclasses import dataclass
+from datetime import datetime
+from typing import Annotated
+
+from fastapi import APIRouter, Depends, Request
- from pydantic import BaseModel, ConfigDict, JsonValue, TypeAdapter
++from pydantic import BaseModel, TypeAdapter
+
+from litellm._logging import verbose_proxy_logger
+from litellm.proxy._types import (
+ CommonProxyErrors,
+ UserAPIKeyAuth,
+ user_api_key_has_admin_view,
+)
+from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
+from litellm.proxy.management_endpoints.management_v1.common import (
+ MANAGEMENT_V1_PREFIX,
+ PROBLEM_TYPE_BASE,
+ ManagementProblem,
+)
+from litellm.proxy.management_endpoints.management_v1.list_framework import (
+ FilterSpec,
+ ListSpec,
- OrderBy,
++ Predicate,
++ QueryPlan,
+ Scope,
+ ScopeAll,
+ ScopeDenied,
+ SortKey,
- Where,
+ handle_list,
++ order_by_sql,
++ where_sql,
+)
+from litellm.proxy.utils import PrismaClient
+from litellm.types.proxy.management_endpoints.management_v1 import (
+ ListResponse,
+ ProblemDetail,
+)
+
+router = APIRouter(prefix=MANAGEMENT_V1_PREFIX)
+
++BUDGET_TABLE = '"LiteLLM_BudgetTable"'
+
- class BudgetRow(BaseModel):
- """The `LiteLLM_BudgetTable` columns this list serves.
+
- Validating the untyped Prisma row through here is what makes `tpm_limit` /
- `rpm_limit` ints: they are `BigInt?` in the schema, which the query engine can
- hand back as a decimal string.
- """
++class BudgetListItem(BaseModel):
++ """One budget as the Budgets page reads it, and as it comes back off the table.
+
- model_config = ConfigDict(from_attributes=True)
++ Validating the raw row through here is what makes `tpm_limit` / `rpm_limit`
++ numbers: they are `BigInt?` in the schema, which the query engine hands back as
++ decimal strings, and a quoted "60000" breaks arithmetic in the dashboard.
++ """
+
+ budget_id: str
+ max_budget: float | None = None
+ soft_budget: float | None = None
+ tpm_limit: int | None = None
+ rpm_limit: int | None = None
+ budget_duration: str | None = None
+ budget_reset_at: datetime | None = None
+ created_at: datetime
+ updated_at: datetime
+
+
- _BUDGET_ROWS = TypeAdapter(tuple[BudgetRow, ...])
++class _RowCount(BaseModel):
++ count: int
++
++
++_BUDGET_ROWS = TypeAdapter(tuple[BudgetListItem, ...])
++_ROW_COUNTS = TypeAdapter(tuple[_RowCount, ...])
++
++SELECTED_COLUMNS = ", ".join(f'"{name}"' for name in BudgetListItem.model_fields)
+
+
+@dataclass(frozen=True, slots=True)
+class PrismaBudgetListExecutor:
- """The `ListExecutor` half of the budgets list: everything Prisma-shaped lives here."""
++ """The database half of the budgets list. Every caller-supplied value is bound to a
++ placeholder by `where_sql`; only the spec's own column names reach the SQL text."""
+
+ prisma_client: PrismaClient
+
- async def count(self, where: Where) -> int:
- return int(await self.prisma_client.db.litellm_budgettable.count(where=dict(where)))
-
- async def find_many(self, where: Where, order: OrderBy, skip: int, take: int) -> Sequence[BudgetRow]:
- rows = await self.prisma_client.db.litellm_budgettable.find_many(
- where=dict(where), order=list(order), skip=skip, take=take
++ async def count(self, where: tuple[Predicate, ...]) -> int:
++ clauses, params = where_sql(where)
++ sql = f"SELECT COUNT(*) AS count FROM {BUDGET_TABLE}" + (f" WHERE {clauses}" if clauses else "")
++ rows = await self.prisma_client.db.query_raw(sql, *params)
++ counted = _ROW_COUNTS.validate_python(rows)
++ return counted[0].count if counted else 0
++
++ async def find_many(self, plan: QueryPlan) -> Sequence[BudgetListItem]:
++ clauses, params = where_sql(plan.where)
++ sql = (
++ f"SELECT {SELECTED_COLUMNS} FROM {BUDGET_TABLE}"
++ + (f" WHERE {clauses}" if clauses else "")
++ + f" ORDER BY {order_by_sql(plan.order)}"
++ + f" LIMIT ${len(params) + 1} OFFSET ${len(params) + 2}"
+ )
++ rows = await self.prisma_client.db.query_raw(sql, *params, plan.take, plan.skip)
+ return _BUDGET_ROWS.validate_python(rows)
+
+
- def _iso(value: datetime | None) -> str | None:
- return value.isoformat() if value is not None else None
-
-
- def _serialize(row: BudgetRow) -> Mapping[str, JsonValue]:
- return {
- "budget_id": row.budget_id,
- "max_budget": row.max_budget,
- "soft_budget": row.soft_budget,
- "tpm_limit": row.tpm_limit,
- "rpm_limit": row.rpm_limit,
- "budget_duration": row.budget_duration,
- "budget_reset_at": _iso(row.budget_reset_at),
- "created_at": _iso(row.created_at),
- "updated_at": _iso(row.updated_at),
- }
++def _serialize(row: BudgetListItem) -> BudgetListItem:
++ """The row shape is the wire shape: the query selects exactly the columns served."""
++ return row
+
+
+def _scope(caller: UserAPIKeyAuth) -> Scope:
+ if user_api_key_has_admin_view(caller):
+ return ScopeAll()
- return ScopeDenied(
- detail="Only proxy admins can list budgets, your role={}".format(caller.user_role),
- )
++ return ScopeDenied(reason="Only proxy admins can list budgets, your role={}".format(caller.user_role))
+
+
+# budget_duration is deliberately absent from `sortable`: the column holds strings
+# like "7d" and "30d", so a lexicographic ORDER BY puts "30d" ahead of "7d".
- BUDGETS_LIST_SPEC: ListSpec[BudgetRow] = ListSpec(
++BUDGETS_LIST_SPEC: ListSpec[BudgetListItem, BudgetListItem] = ListSpec(
+ resource="budgets",
+ sortable=frozenset({"budget_id", "max_budget", "tpm_limit", "rpm_limit", "created_at"}),
+ searchable=frozenset({"budget_id"}),
+ filters={
- "budget_duration": FilterSpec(type="string", ops=frozenset({"in", "is_null"})),
- "max_budget": FilterSpec(type="number", ops=frozenset({"gte", "lte", "is_null"})),
- "created_at": FilterSpec(type="datetime", ops=frozenset({"gte", "lte"})),
++ "budget_duration": FilterSpec(type=str, ops=frozenset({"in", "is_null"})),
++ "max_budget": FilterSpec(type=float, ops=frozenset({"gte", "lte", "is_null"})),
++ "created_at": FilterSpec(type=datetime, ops=frozenset({"gte", "lte"})),
+ },
- default_sort=(SortKey(field="created_at", descending=True), SortKey(field="budget_id", descending=False)),
++ default_sort=(SortKey(field="created_at", descending=True),),
+ default_page_size=50,
+ max_page_size=100,
+ scope=_scope,
+ serialize=_serialize,
+ tiebreaker="budget_id",
+)
+
+
+ "/budgets",
+ tags=["budget management"],
+ dependencies=[Depends(user_api_key_auth)],
- response_model=ListResponse,
++ response_model=ListResponse[BudgetListItem],
+)
+async def list_budgets(
+ request: Request,
+ user_api_key_dict: Annotated[UserAPIKeyAuth, Depends(user_api_key_auth)],
- ) -> ListResponse:
++) -> ListResponse[BudgetListItem]:
+ """
+ The budgets defined on this proxy, paged, sortable and filterable, for the
+ Budgets page.
+
+ Readable by a proxy admin or an admin viewer; anyone else is refused 403. The
+ older `/budget/list` answers with the whole table as a bare array and has no
+ way to page, sort or filter it.
+
+ `sort` takes a comma-separated list of `budget_id`, `max_budget`, `tpm_limit`,
+ `rpm_limit` or `created_at`, each optionally prefixed with `-` for descending,
- and defaults to `-created_at,budget_id`. `q` is a case-insensitive substring
- match on `budget_id`. `page_size` defaults to 50 and is capped at 100.
- Filters are `filter[budget_duration][in|is_null]`,
- `filter[max_budget][gte|lte|is_null]` and `filter[created_at][gte|lte]`.
++ and defaults to `-created_at`. `budget_id` is appended to every sort as the
++ tiebreaker. `q` is a case-insensitive substring match on `budget_id`.
++ `page_size` defaults to 50 and is capped at 100. Filters are
++ `filter[budget_duration][in|is_null]`, `filter[max_budget][gte|lte|is_null]`
++ and `filter[created_at][gte|lte]`.
+
+ Example curl:
+ ```
+ curl --location --globoff 'http://0.0.0.0:4000/management/v1/budgets?sort=-max_budget&filter[budget_duration][in]=7d,30d&page_size=25' \
+ --header 'Authorization: Bearer sk-1234'
+ ```
+ """
+ try:
+ from litellm.proxy.proxy_server import prisma_client
+
+ if prisma_client is None:
+ raise ManagementProblem(
+ ProblemDetail(
+ type=f"{PROBLEM_TYPE_BASE}database-not-connected",
+ title="Database not connected",
+ status=503,
+ detail=CommonProxyErrors.db_not_connected_error.value,
+ )
+ )
+
+ return await handle_list(
- request=request,
+ spec=BUDGETS_LIST_SPEC,
+ executor=PrismaBudgetListExecutor(prisma_client=prisma_client),
++ request=request,
+ caller=user_api_key_dict,
+ )
+
+ except ManagementProblem:
+ raise
+ except Exception as e: # noqa: BLE001 # a driver error answers as a problem document, not the OpenAI error shape
+ verbose_proxy_logger.exception(
+ "litellm.proxy.management_endpoints.management_v1.budgets.list_budgets(): Exception occured - {}".format(
+ str(e)
+ )
+ )
+ raise ManagementProblem(
+ ProblemDetail(
+ type=f"{PROBLEM_TYPE_BASE}internal-server-error",
+ title="Internal server error",
+ status=500,
+ detail="Failed to list budgets.",
+ )
+ )
diff --cc tests/test_litellm/proxy/auth/test_admin_viewer_handler_access.py
index 9f4a801eb83,b0d2595e48c..36f656a7adc
--- a/tests/test_litellm/proxy/auth/test_admin_viewer_handler_access.py
+++ b/tests/test_litellm/proxy/auth/test_admin_viewer_handler_access.py
@@@ -59,10 -58,10 +58,14 @@@ def admin_viewer_client(monkeypatch)
mock_config_table = MagicMock()
mock_config_table.find_first = AsyncMock(return_value=None)
++ # /management/v1/budgets reads through query_raw: count first, then the page.
++ mock_query_raw = AsyncMock(side_effect=[[{"count": 0}], []])
++
mock_prisma.db = types.SimpleNamespace(
litellm_budgettable=mock_budget_table,
litellm_invitationlink=mock_invitation_table,
litellm_config=mock_config_table,
++ query_raw=mock_query_raw,
)
monkeypatch.setattr(ps, "prisma_client", mock_prisma)
diff --cc tests/test_litellm/proxy/management_endpoints/management_v1/test_budgets.py
index 500c072bc7d,00000000000..fe6d0289e53
mode 100644,000000..100644
--- a/tests/test_litellm/proxy/management_endpoints/management_v1/test_budgets.py
+++ b/tests/test_litellm/proxy/management_endpoints/management_v1/test_budgets.py
@@@ -1,471 -1,0 +1,498 @@@
++from dataclasses import replace
+from datetime import datetime, timezone
+from typing import Any
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from fastapi import FastAPI, Request
+from fastapi.exceptions import RequestValidationError
+from fastapi.testclient import TestClient
+
+from litellm.proxy._types import LiteLLMRoutes, LitellmUserRoles
+from litellm.proxy.auth.user_api_key_auth import UserAPIKeyAuth, user_api_key_auth
+from litellm.proxy.management_endpoints.management_v1 import router
- from litellm.proxy.management_endpoints.management_v1.budgets import BUDGETS_LIST_SPEC
++from litellm.proxy.management_endpoints.management_v1.budgets import (
++ BUDGETS_LIST_SPEC,
++ BudgetListItem,
++)
+from litellm.proxy.management_endpoints.management_v1.common import (
+ MANAGEMENT_V1_PREFIX,
+ PROBLEM_TYPE_BASE,
+ ManagementProblem,
+ problem_response,
+)
+from litellm.proxy.management_endpoints.management_v1.list_framework import (
++ Compare,
+ ScopeWhere,
+ build_query_plan,
+)
+from litellm.types.proxy.management_endpoints.management_v1 import ProblemDetail
+
+app = FastAPI()
+
+
[email protected]_handler(ManagementProblem)
+async def management_problem_exception_handler(request: Request, exc: ManagementProblem):
+ return problem_response(exc.problem)
+
+
[email protected]_handler(RequestValidationError)
+async def validation_exception_handler(request: Request, exc: RequestValidationError):
+ return problem_response(
+ ProblemDetail(
+ type=f"{PROBLEM_TYPE_BASE}invalid-query-parameter",
+ title="Invalid query parameter",
+ status=400,
+ detail="The request query parameters are invalid.",
+ )
+ )
+
+
+app.include_router(router)
+client = TestClient(app)
+
+BUDGETS_PATH = f"{MANAGEMENT_V1_PREFIX}/budgets"
+SORTABLE = ["budget_id", "created_at", "max_budget", "rpm_limit", "tpm_limit"]
+
+
+def _row(budget_id: str, **overrides: Any) -> dict[str, Any]:
+ return {
+ "budget_id": budget_id,
+ "max_budget": 10.0,
+ "soft_budget": None,
+ "tpm_limit": None,
+ "rpm_limit": None,
+ "budget_duration": "30d",
+ "budget_reset_at": None,
- "created_at": datetime(2026, 7, 20, 12, 0, tzinfo=timezone.utc),
- "updated_at": datetime(2026, 7, 21, 12, 0, tzinfo=timezone.utc),
++ "created_at": "2026-07-20T12:00:00+00:00",
++ "updated_at": "2026-07-21T12:00:00+00:00",
+ **overrides,
+ }
+
+
- def budget_table(monkeypatch):
- table = MagicMock()
- table.count = AsyncMock(return_value=0)
- table.find_many = AsyncMock(return_value=[])
++def query_raw(monkeypatch):
++ """Mocks the one call the executor makes. `count` reads the first result, `find_many` the second."""
++ mock = AsyncMock(side_effect=[[{"count": 0}], []])
+ prisma_client = MagicMock()
- prisma_client.db.litellm_budgettable = table
++ prisma_client.db.query_raw = mock
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", prisma_client)
- return table
++ return mock
++
++
++def _serve(query_raw, rows: list[dict[str, Any]], total: int | None = None) -> None:
++ query_raw.side_effect = [[{"count": len(rows) if total is None else total}], rows]
+
+
+def as_proxy_admin():
+ app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth(
+ user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN
+ )
+ yield
+ app.dependency_overrides.clear()
+
+
- def _serve(budget_table, rows: list[dict[str, Any]], total: int | None = None) -> None:
- budget_table.find_many = AsyncMock(return_value=rows)
- budget_table.count = AsyncMock(return_value=len(rows) if total is None else total)
-
-
+def _as_role(role: LitellmUserRoles):
+ original = app.dependency_overrides.copy()
+ app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth(user_id="u", user_role=role)
+ return original
+
+
+def _get(query: str = ""):
+ suffix = f"?{query}" if query else ""
+ return client.get(f"{BUDGETS_PATH}{suffix}", headers={"Authorization": "Bearer k"})
+
+
- def test_returns_flat_rows_in_the_control_plane_envelope(budget_table, as_proxy_admin):
++def _select_call(query_raw):
++ """The find_many call: (sql, *params). The count call comes first."""
++ return query_raw.call_args_list[1].args
++
++
++def test_returns_flat_rows_in_the_control_plane_envelope(query_raw, as_proxy_admin):
+ """`{data, meta, links}` with flat rows; no JSON:API `{type, id, attributes}` wrapper."""
- _serve(budget_table, [_row("b-1")])
++ _serve(query_raw, [_row("b-1")])
+
+ response = _get()
+
+ assert response.status_code == 200
+ body = response.json()
+ assert set(body) == {"data", "meta", "links"}
+ assert body["data"][0]["budget_id"] == "b-1"
+ assert "attributes" not in body["data"][0]
+
+
- def test_serves_the_columns_the_budgets_page_renders(budget_table, as_proxy_admin):
- _serve(budget_table, [_row("b-1", soft_budget=5.0, budget_reset_at=datetime(2026, 8, 1, tzinfo=timezone.utc))])
++def test_serves_the_columns_the_budgets_page_renders(query_raw, as_proxy_admin):
++ _serve(query_raw, [_row("b-1", soft_budget=5.0, budget_reset_at="2026-08-01T00:00:00+00:00")])
+
+ row = _get().json()["data"][0]
+
+ assert set(row) == {
+ "budget_id",
+ "max_budget",
+ "soft_budget",
+ "tpm_limit",
+ "rpm_limit",
+ "budget_duration",
+ "budget_reset_at",
+ "created_at",
+ "updated_at",
+ }
+ assert row["soft_budget"] == 5.0
+ assert row["budget_reset_at"].startswith("2026-08-01T00:00:00")
+
+
- def test_defaults_to_newest_first_with_budget_id_breaking_ties(budget_table, as_proxy_admin):
- """Two budgets created in the same transaction share a created_at; without the
- tiebreaker their relative order is undefined and pages can repeat or drop rows."""
- _serve(budget_table, [])
++def test_selects_only_the_columns_it_serves(query_raw, as_proxy_admin):
++ """A `SELECT *` would ship created_by/updated_by and model_max_budget to the browser."""
++ _serve(query_raw, [])
+
+ _get()
+
- assert budget_table.find_many.call_args.kwargs["order"] == [
- {"created_at": "desc"},
- {"budget_id": "asc"},
- ]
++ sql = _select_call(query_raw)[0]
++ assert "SELECT *" not in sql
++ assert '"budget_id"' in sql and '"max_budget"' in sql
++ assert "created_by" not in sql and "model_max_budget" not in sql
+
+
- def test_appends_the_tiebreaker_to_an_explicit_sort(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_defaults_to_newest_first_with_budget_id_breaking_ties(query_raw, as_proxy_admin):
++ """Two budgets created in the same transaction share a created_at; without the
++ tiebreaker their relative order is undefined and pages can repeat or drop rows."""
++ _serve(query_raw, [])
+
- _get("sort=-max_budget")
++ _get()
+
- assert budget_table.find_many.call_args.kwargs["order"] == [
- {"max_budget": "desc"},
- {"budget_id": "asc"},
- ]
++ assert 'ORDER BY "created_at" DESC NULLS LAST, "budget_id" ASC NULLS LAST' in _select_call(query_raw)[0]
+
+
- def test_does_not_duplicate_the_tiebreaker_when_it_is_sorted_on(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_appends_the_tiebreaker_to_an_explicit_sort(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
- _get("sort=-budget_id")
++ _get("sort=-max_budget")
+
- assert budget_table.find_many.call_args.kwargs["order"] == [{"budget_id": "desc"}]
++ assert 'ORDER BY "max_budget" DESC NULLS LAST, "budget_id" ASC NULLS LAST' in _select_call(query_raw)[0]
+
+
- def test_refuses_to_sort_on_budget_duration(budget_table, as_proxy_admin):
++def test_refuses_to_sort_on_budget_duration(query_raw, as_proxy_admin):
+ """The column holds "7d"/"30d", so a lexicographic ORDER BY would put "30d"
+ before "7d" and silently mis-order the page."""
- _serve(budget_table, [])
++ _serve(query_raw, [])
+
+ response = _get("sort=budget_duration")
+
+ assert response.status_code == 400
+ assert response.headers["content-type"].startswith("application/problem+json")
+ body = response.json()
+ assert "budget_duration" in body["detail"]
+ assert body["allowed"] == SORTABLE
- budget_table.find_many.assert_not_called()
++ query_raw.assert_not_called()
+
+
- def test_the_advertised_sort_fields_are_the_ones_that_work(budget_table, as_proxy_admin):
++def test_the_advertised_sort_fields_are_the_ones_that_work(query_raw, as_proxy_admin):
+ """Guards the rejection above against drifting from what the spec actually accepts."""
- _serve(budget_table, [])
-
+ for field in SORTABLE:
++ _serve(query_raw, [])
+ assert _get(f"sort={field}").status_code == 200, field
+ assert sorted(BUDGETS_LIST_SPEC.sortable) == SORTABLE
+
+
- def test_rejects_an_unknown_query_parameter(budget_table, as_proxy_admin):
++def test_rejects_an_unknown_query_parameter(query_raw, as_proxy_admin):
+ """A silently ignored filter over-returns budgets, which is worse than a rejected request."""
- _serve(budget_table, [])
++ _serve(query_raw, [])
+
+ response = _get("filtre[max_budget][gte]=5")
+
+ assert response.status_code == 400
+ assert response.headers["content-type"].startswith("application/problem+json")
+ body = response.json()
+ assert "filtre[max_budget][gte]" in body["detail"]
+ assert "filter[max_budget][gte]" in body["allowed"]
- budget_table.count.assert_not_called()
- budget_table.find_many.assert_not_called()
++ query_raw.assert_not_called()
+
+
- def test_rejects_an_operator_the_filter_does_not_declare(budget_table, as_proxy_admin):
++def test_rejects_an_operator_the_filter_does_not_declare(query_raw, as_proxy_admin):
+ """`max_budget` takes ranges, not `in`; accepting an undeclared operator is how a
+ filter starts meaning something the query planner never checked."""
- _serve(budget_table, [])
++ for query in ("filter[max_budget][in]=5,10", "filter[created_at][is_null]=true"):
++ _serve(query_raw, [])
++ assert _get(query).status_code == 400, query
+
- assert _get("filter[max_budget][in]=5,10").status_code == 400
- assert _get("filter[created_at][is_null]=true").status_code == 400
+
-
- def test_omitted_page_size_serves_fifty(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_omitted_page_size_serves_fifty(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
+ body = _get().json()
+
+ assert body["meta"]["page_size"] == 50
- assert budget_table.find_many.call_args.kwargs["take"] == 50
++ assert _select_call(query_raw)[-2] == 50
+
+
- def test_clamps_an_oversized_page_size_to_a_hundred(budget_table, as_proxy_admin):
++def test_clamps_an_oversized_page_size_to_a_hundred(query_raw, as_proxy_admin):
+ """Unclamped, one request can ask the proxy to serialize the whole budget table."""
- _serve(budget_table, [])
++ _serve(query_raw, [])
+
+ body = _get("page_size=500").json()
+
+ assert body["meta"]["page_size"] == 100
- assert budget_table.find_many.call_args.kwargs["take"] == 100
++ assert _select_call(query_raw)[-2] == 100
+
+
- def test_offsets_by_page(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_offsets_by_page(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
+ _get("page=3&page_size=25")
+
- assert budget_table.find_many.call_args.kwargs["skip"] == 50
- assert budget_table.find_many.call_args.kwargs["take"] == 25
++ assert _select_call(query_raw)[-2:] == (25, 50)
+
+
+ "role",
+ [
+ LitellmUserRoles.INTERNAL_USER,
+ LitellmUserRoles.INTERNAL_USER_VIEW_ONLY,
+ LitellmUserRoles.TEAM,
+ ],
+)
- def test_refuses_a_caller_without_admin_view(budget_table, role):
++def test_refuses_a_caller_without_admin_view(query_raw, role):
+ """Budgets are proxy-wide, so a caller who cannot read all of them must be told
+ so. Answering 200 with an empty list would read as "there are no budgets"."""
- _serve(budget_table, [_row("b-1")])
++ _serve(query_raw, [_row("b-1")])
+ original = _as_role(role)
+ try:
+ response = _get()
+ finally:
+ app.dependency_overrides = original
+
+ assert response.status_code == 403
+ assert response.headers["content-type"].startswith("application/problem+json")
+ assert response.json()["status"] == 403
- budget_table.count.assert_not_called()
- budget_table.find_many.assert_not_called()
++ query_raw.assert_not_called()
+
+
[email protected]("role", [LitellmUserRoles.PROXY_ADMIN, LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY])
- def test_admins_and_admin_viewers_may_read_every_budget(budget_table, role):
- _serve(budget_table, [_row("b-1")])
++def test_admins_and_admin_viewers_may_read_every_budget(query_raw, role):
++ _serve(query_raw, [_row("b-1")])
+ original = _as_role(role)
+ try:
+ response = _get()
+ finally:
+ app.dependency_overrides = original
+
+ assert response.status_code == 200
+ assert [row["budget_id"] for row in response.json()["data"]] == ["b-1"]
++ assert "WHERE" not in _select_call(query_raw)[0]
+
+
- def test_a_denied_caller_stays_denied_whatever_they_filter_on(budget_table):
++def test_a_denied_caller_stays_denied_whatever_they_filter_on(query_raw):
+ """The scope decision reads the caller, never the query string."""
- _serve(budget_table, [_row("b-1")])
++ _serve(query_raw, [_row("b-1")])
+ original = _as_role(LitellmUserRoles.INTERNAL_USER)
+ try:
+ response = _get("filter[max_budget][gte]=0&q=b-")
+ finally:
+ app.dependency_overrides = original
+
+ assert response.status_code == 403
+
+
- def test_a_filter_narrows_the_scope_predicate_instead_of_replacing_it(budget_table, as_proxy_admin):
- """A filter is ANDed in. Assigning it over the scope clause is what would let a
- caller widen their own read."""
- _serve(budget_table, [])
-
- _get("filter[max_budget][gte]=5")
-
- where = budget_table.find_many.call_args.kwargs["where"]
- assert {"max_budget": {"gte": 5.0}} in where["AND"]
-
-
- def test_a_scoped_caller_keeps_their_scope_clause_alongside_their_filter():
- """Same spec, driven through the planner with a row-scoped caller: the scope
- clause has to survive next to whatever the caller filtered on."""
- request = Request(
- {
- "type": "http",
- "method": "GET",
- "path": BUDGETS_PATH,
- "headers": [],
- "query_string": b"filter[max_budget][gte]=5",
- }
++def test_a_filter_sits_behind_the_scope_predicate_instead_of_replacing_it():
++ """Same spec, planned for a row-scoped caller: the scope clause has to survive, and
++ lead, whatever the caller filtered on. Replacing it would let a filter widen a read."""
++ scoped = replace(
++ BUDGETS_LIST_SPEC,
++ scope=lambda _caller: ScopeWhere(where=(Compare(field="budget_id", op="eq", value="b-1"),)),
+ )
+
- plan = build_query_plan(request, BUDGETS_LIST_SPEC, ScopeWhere(where={"budget_id": {"in": ("b-1",)}}))
++ plan = build_query_plan(
++ spec=scoped,
++ params={"filter[max_budget][gte]": "5"},
++ caller=UserAPIKeyAuth(user_id="u", user_role=LitellmUserRoles.INTERNAL_USER),
++ )
+
- assert {"budget_id": {"in": ("b-1",)}} in plan.where["AND"]
- assert {"max_budget": {"gte": 5.0}} in plan.where["AND"]
++ assert plan.where[0] == Compare(field="budget_id", op="eq", value="b-1")
++ assert Compare(field="max_budget", op="gte", value=5.0) in plan.where
+
+
- def test_q_matches_budget_id_case_insensitively(budget_table, as_proxy_admin):
++def test_q_matches_budget_id_case_insensitively(query_raw, as_proxy_admin):
+ """budget_id is the only text identity on the row; matching anything else would
+ return budgets whose ids do not contain what the user typed."""
- _serve(budget_table, [])
++ _serve(query_raw, [])
+
+ _get("q=Prod")
+
- where = budget_table.find_many.call_args.kwargs["where"]
- assert {"OR": ({"budget_id": {"contains": "Prod", "mode": "insensitive"}},)} in where["AND"]
++ sql, *params = _select_call(query_raw)
++ assert '"budget_id" ILIKE $1' in sql
++ assert params[0] == "%Prod%"
++
++
++def test_q_escapes_like_metacharacters(query_raw, as_proxy_admin):
++ """Budget ids routinely contain '_'; unescaped it is a single-character wildcard."""
++ _serve(query_raw, [])
++
++ _get("q=team_a%25")
++
++ assert _select_call(query_raw)[1] == r"%team\_a\%%"
+
+
- def test_q_does_not_search_any_other_column(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_q_does_not_search_any_other_column(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
+ _get("q=30d")
+
- searched = budget_table.find_many.call_args.kwargs["where"]["AND"][0]["OR"]
- assert [next(iter(clause)) for clause in searched] == ["budget_id"]
++ assert "budget_duration" not in _select_call(query_raw)[0].split("WHERE")[1]
+ assert BUDGETS_LIST_SPEC.searchable == frozenset({"budget_id"})
+
+
- def test_is_null_selects_the_unlimited_budgets(budget_table, as_proxy_admin):
++def test_is_null_selects_the_unlimited_budgets(query_raw, as_proxy_admin):
+ """"Unlimited" is max_budget IS NULL; `max_budget = 0` would be a hard zero cap."""
- _serve(budget_table, [_row("b-unlimited", max_budget=None)])
++ _serve(query_raw, [_row("b-unlimited", max_budget=None)])
+
+ body = _get("filter[max_budget][is_null]=true").json()
+
- assert {"max_budget": None} in budget_table.find_many.call_args.kwargs["where"]["AND"]
++ assert '"max_budget" IS NULL' in _select_call(query_raw)[0]
+ assert body["data"][0]["max_budget"] is None
+
+
- def test_is_null_false_selects_the_capped_budgets(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_is_null_false_selects_the_capped_budgets(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
+ _get("filter[max_budget][is_null]=false")
+
- assert {"max_budget": {"not": None}} in budget_table.find_many.call_args.kwargs["where"]["AND"]
++ assert '"max_budget" IS NOT NULL' in _select_call(query_raw)[0]
+
+
- def test_in_filter_splits_the_requested_durations(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_in_filter_binds_each_requested_duration(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
+ _get("filter[budget_duration][in]=7d,30d")
+
- assert {"budget_duration": {"in": ("7d", "30d")}} in budget_table.find_many.call_args.kwargs["where"]["AND"]
++ sql, *params = _select_call(query_raw)
++ assert '"budget_duration" IN ($1, $2)' in sql
++ assert params[:2] == ["7d", "30d"]
+
+
- def test_created_at_range_is_read_as_a_timestamp(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_created_at_range_is_bound_as_a_timestamp(query_raw, as_proxy_admin):
++ _serve(query_raw, [])
+
- _get("filter[created_at][gte]=2026-07-01T00:00:00%2B00:00")
++ _get("filter[created_at][gte]=2026-07-01T00:00:00Z")
+
- assert {
- "created_at": {"gte": datetime(2026, 7, 1, tzinfo=timezone.utc)}
- } in budget_table.find_many.call_args.kwargs["where"]["AND"]
++ sql, *params = _select_call(query_raw)
++ assert '"created_at" >= $1' in sql
++ assert params[0] == datetime(2026, 7, 1, tzinfo=timezone.utc)
+
+
- def test_rejects_a_filter_value_that_is_not_of_the_declared_type(budget_table, as_proxy_admin):
- _serve(budget_table, [])
++def test_an_offsetless_created_at_bound_is_read_as_utc(query_raw, as_proxy_admin):
++ """The dashboard sends 'YYYY-MM-DDTHH:MM:SS' with no offset. Left naive, Postgres
++ would compare it in the session timezone and shift the window off the rows shown."""
++ _serve(query_raw, [])
+
- assert _get("filter[max_budget][gte]=lots").status_code == 400
- assert _get("filter[created_at][gte]=yesterday").status_code == 400
++ _get("filter[created_at][gte]=2026-07-01T00:00:00")
+
++ bound = _select_call(query_raw)[1]
++ assert bound == datetime(2026, 7, 1, tzinfo=timezone.utc)
++ assert bound.tzinfo is not None
+
- def test_reports_the_total_and_links_every_page_on_a_middle_page(budget_table, as_proxy_admin):
++
++def test_rejects_a_filter_value_that_is_not_of_the_declared_type(query_raw, as_proxy_admin):
++ for query in ("filter[max_budget][gte]=lots", "filter[created_at][gte]=yesterday"):
++ _serve(query_raw, [])
++ assert _get(query).status_code == 400, query
++
++
++def test_reports_the_total_and_links_every_page_on_a_middle_page(query_raw, as_proxy_admin):
+ """The Budgets page renders a page count, so the total has to be the match total,
+ not the length of the page it just received."""
- _serve(budget_table, [_row("b-3"), _row("b-4")], total=7)
++ _serve(query_raw, [_row("b-3"), _row("b-4")], total=7)
+
+ body = _get("page=2&page_size=2").json()
+
+ assert body["meta"] == {"page": 2, "page_size": 2, "total_count": 7, "total_pages": 4}
+ links = body["links"]
+ assert "page=1" in links["first"] and "page_size=2" in links["first"]
+ assert "page=1" in links["prev"]
+ assert "page=3" in links["next"]
+ assert "page=4" in links["last"]
+ assert "page=2" in links["self"]
+
+
- def test_the_last_page_has_no_next(budget_table, as_proxy_admin):
- _serve(budget_table, [_row("b-5")], total=5)
++def test_the_last_page_has_no_next(query_raw, as_proxy_admin):
++ _serve(query_raw, [_row("b-5")], total=5)
+
+ links = _get("page=3&page_size=2").json()["links"]
+
+ assert links["next"] is None
+ assert "page=2" in links["prev"]
+
+
- def test_the_first_page_has_no_prev(budget_table, as_proxy_admin):
- _serve(budget_table, [_row("b-1")], total=5)
++def test_the_first_page_has_no_prev(query_raw, as_proxy_admin):
++ _serve(query_raw, [_row("b-1")], total=5)
+
+ links = _get("page_size=2").json()["links"]
+
+ assert links["prev"] is None
+ assert "page=2" in links["next"]
+
+
- def test_an_empty_table_still_links_a_first_and_last_page(budget_table, as_proxy_admin):
- _serve(budget_table, [], total=0)
++def test_an_empty_table_still_links_a_first_and_last_page(query_raw, as_proxy_admin):
++ _serve(query_raw, [], total=0)
+
+ body = _get().json()
+
+ assert body["meta"]["total_count"] == 0
+ assert body["meta"]["total_pages"] == 0
+ assert "page=1" in body["links"]["first"] and "page=1" in body["links"]["last"]
+
+
- def test_counts_over_the_same_predicate_it_pages(budget_table, as_proxy_admin):
++def test_counts_over_the_same_predicate_it_pages(query_raw, as_proxy_admin):
+ """A total counted without the caller's filter would page through rows the
+ filter excluded."""
- _serve(budget_table, [], total=0)
++ _serve(query_raw, [], total=0)
+
+ _get("filter[budget_duration][in]=30d")
+
- assert budget_table.count.call_args.kwargs["where"] == budget_table.find_many.call_args.kwargs["where"]
++ count_sql, *count_params = query_raw.call_args_list[0].args
++ assert '"budget_duration" IN ($1)' in count_sql
++ assert count_params == ["30d"]
++ assert "COUNT(*)" in count_sql
+
+
- def test_bigint_limits_serialize_as_json_numbers(budget_table, as_proxy_admin):
++def test_bigint_limits_serialize_as_json_numbers(query_raw, as_proxy_admin):
+ """tpm_limit/rpm_limit are BigInt? in Prisma; the query engine can hand them back
+ as decimal strings, and a quoted "60000" breaks arithmetic in the dashboard."""
- _serve(budget_table, [_row("b-1", tpm_limit="60000", rpm_limit=1200)])
++ _serve(query_raw, [_row("b-1", tpm_limit="60000", rpm_limit=1200)])
+
- row = _get().json()["data"][0]
++ response = _get()
++ row = response.json()["data"][0]
+
+ assert row["tpm_limit"] == 60000
+ assert row["rpm_limit"] == 1200
+ assert isinstance(row["tpm_limit"], int) and not isinstance(row["tpm_limit"], bool)
- assert '"tpm_limit": "60000"' not in _get().text
++ assert '"tpm_limit": "60000"' not in response.text
+
+
+def test_reports_a_missing_database_as_a_problem_document(monkeypatch, as_proxy_admin):
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", None)
+
+ response = _get()
+
+ assert response.status_code == 503
+ assert response.headers["content-type"].startswith("application/problem+json")
+
+
++def test_the_spec_serves_what_the_row_model_declares():
++ """SELECTED_COLUMNS is built off the model, so a field added to one cannot go
++ missing from the other and produce a row the validator rejects."""
++ from litellm.proxy.management_endpoints.management_v1.budgets import SELECTED_COLUMNS
++
++ assert SELECTED_COLUMNS == ", ".join(f'"{name}"' for name in BudgetListItem.model_fields)
++ assert BUDGETS_LIST_SPEC.tiebreaker in BudgetListItem.model_fields
++ assert BUDGETS_LIST_SPEC.sortable <= frozenset(BudgetListItem.model_fields)
++ assert frozenset(BUDGETS_LIST_SPEC.filters) <= frozenset(BudgetListItem.model_fields)
++
++
+def test_is_reachable_by_the_roles_that_can_open_the_budgets_page():
+ """Route-level auth gate, which the dependency_overrides above bypass. The handler's
+ admin-view check is dead code if RouteChecks rejects the role first."""
+ assert BUDGETS_PATH in LiteLLMRoutes.admin_viewer_routes.value
+ assert ("/budget/list" in LiteLLMRoutes.admin_viewer_routes.value) == (
+ BUDGETS_PATH in LiteLLMRoutes.admin_viewer_routes.value
+ )