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]

refactor(logs): make the end-user filter scan cap a fixed constant

Yuneng Jiang48e77738ca00
diff --git a/litellm/constants.py b/litellm/constants.py
index f0a7690096a..b9b9c0ba604 100644
--- a/litellm/constants.py
+++ b/litellm/constants.py
@@ -1316,9 +1316,6 @@ STANDARD_CUSTOMER_ID_HEADERS = [
MAX_SPENDLOG_ROWS_TO_QUERY = int(
os.getenv("MAX_SPENDLOG_ROWS_TO_QUERY", 1_000_000)
) # if spendLogs has more than 1M rows, do not query the DB
-MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS = int(
- os.getenv("MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS", 10_000)
-) # hard cap on rows a UI filter dropdown may scan out of LiteLLM_SpendLogs
DEFAULT_SOFT_BUDGET = float(
os.getenv("DEFAULT_SOFT_BUDGET", 50.0)
) # by default all litellm proxy keys have a soft budget of 50.0
diff --git a/litellm/proxy/management_endpoints/customer_endpoints.py b/litellm/proxy/management_endpoints/customer_endpoints.py
index f32b0d0c3fc..a46481d5bb7 100644
--- a/litellm/proxy/management_endpoints/customer_endpoints.py
+++ b/litellm/proxy/management_endpoints/customer_endpoints.py
@@ -20,7 +20,6 @@ from pydantic import BaseModel
import litellm
from litellm._logging import verbose_proxy_logger
-from litellm.constants import MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS
from litellm.litellm_core_utils.duration_parser import duration_in_seconds
from litellm.proxy._types import *
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
@@ -45,6 +44,11 @@ from litellm.types.proxy.management_endpoints.customer_endpoints import (
router = APIRouter()
+# Rows the end-user filter query may read out of LiteLLM_SpendLogs before DISTINCT.
+# Matches SPEND_LOGS_PAGINATION_COUNT_CAP, the equivalent bound ui_view_spend_logs
+# puts on its count query, so both reads of the same table stop at the same depth.
+SPEND_LOGS_FILTER_SCAN_CAP = 10000
+
def _to_customer_response(record: BaseModel) -> CustomerResponse:
"""Validate a raw end-user DB row into the typed customer response.
@@ -870,7 +874,7 @@ async def list_customer_aliases(
Reads spend logs rather than LiteLLM_EndUserTable because only spend logs carry
the team attribution this scoping needs. The window is required and the inner
- scan is capped at MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS rows, so the query
+ scan is capped at SPEND_LOGS_FILTER_SCAN_CAP rows, so the query
cannot degrade into a full-table scan the way `/global/all_end_users` does.
Example curl:
@@ -919,7 +923,7 @@ async def list_customer_aliases(
# successive OFFSET pages agree on the set they are paging through; the
# (startTime, request_id) index means the tiebreaker costs nothing.
# size + 1: one row beyond the page reveals has_more without a COUNT(*).
- params = query_params + [MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS, size + 1, (page - 1) * size]
+ params = query_params + [SPEND_LOGS_FILTER_SCAN_CAP, size + 1, (page - 1) * size]
scan_idx = len(params) - 2
aliases_sql = (
f"SELECT DISTINCT end_user FROM ("
diff --git a/tests/test_litellm/proxy/management_endpoints/test_customer_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_customer_endpoints.py
index b3768419a37..98e93eea5f9 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_customer_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_customer_endpoints.py
@@ -818,7 +818,7 @@ def test_customer_aliases_reads_spend_logs_not_the_end_user_table(mock_prisma_cl
def test_customer_aliases_caps_the_rows_it_scans(mock_prisma_client, mock_user_api_key_auth):
"""The inner LIMIT is the crash guard: DISTINCT must never see an unbounded set."""
- from litellm.constants import MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS
+ from litellm.proxy.management_endpoints.customer_endpoints import SPEND_LOGS_FILTER_SCAN_CAP
query_raw = _mock_alias_rows(mock_prisma_client, [])
@@ -827,10 +827,26 @@ def test_customer_aliases_caps_the_rows_it_scans(mock_prisma_client, mock_user_a
sql = query_raw.call_args.args[0]
inner = sql[sql.index("FROM (") : sql.index(") recent")]
assert "LIMIT $3" in inner
- assert query_raw.call_args.args[3] == MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS
+ assert query_raw.call_args.args[3] == SPEND_LOGS_FILTER_SCAN_CAP
assert 'ORDER BY "startTime" DESC' in inner
+def test_spend_logs_filter_scan_cap_matches_the_logs_page_bound():
+ """Pin the cap's value, not just that it is passed through.
+
+ Asserting the param equals the constant is tautological: raising the constant
+ to a billion keeps that assertion green while removing the bound entirely.
+ The documented rationale is that both reads of LiteLLM_SpendLogs stop at the
+ same depth, so tie it to the count cap ui_view_spend_logs already uses.
+ """
+ from litellm.proxy.management_endpoints.customer_endpoints import SPEND_LOGS_FILTER_SCAN_CAP
+ from litellm.proxy.spend_tracking.spend_management_endpoints import (
+ SPEND_LOGS_PAGINATION_COUNT_CAP,
+ )
+
+ assert SPEND_LOGS_FILTER_SCAN_CAP == SPEND_LOGS_PAGINATION_COUNT_CAP
+
+
def test_customer_aliases_breaks_start_time_ties_deterministically(mock_prisma_client, mock_user_api_key_auth):
"""Without a unique tiebreaker the capped scan can cut differently per request,
so OFFSET page 2 would page through a different set than page 1 did."""
diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts
index a8b5dd5a28a..bf33e1ec270 100644
--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts
+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts
@@ -2789,7 +2789,7 @@ export interface paths {
*
* Reads spend logs rather than LiteLLM_EndUserTable because only spend logs carry
* the team attribution this scoping needs. The window is required and the inner
- * scan is capped at MAX_SPENDLOG_ROWS_TO_SCAN_FOR_FILTERS rows, so the query
+ * scan is capped at SPEND_LOGS_FILTER_SCAN_CAP rows, so the query
* cannot degrade into a full-table scan the way `/global/all_end_users` does.
*
* Example curl: