Use direct LiteLLM_SpendLogs query for global spend to reduce DB CPU and impact on other APIs

This commit is contained in:
Alexsander Hamir 2026-01-29 16:08:51 -08:00
parent 4314cf5029
commit 093fccde2e
2 changed files with 4 additions and 2 deletions

View file

@ -227,7 +227,8 @@ async def _fetch_global_spend_with_event_coordination(
"""
async def _load_global_spend() -> Optional[float]:
sql_query = """SELECT SUM(spend) AS total_spend FROM "MonthlyGlobalSpend";"""
# Single aggregation on base table (same result, less CPU than SUM over MonthlyGlobalSpend view)
sql_query = """SELECT SUM("spend") AS total_spend FROM "LiteLLM_SpendLogs" WHERE "startTime" >= (CURRENT_DATE - INTERVAL '30 days')"""
response = await prisma_client.db.query_raw(query=sql_query)
val = response[0]["total_spend"]
return float(val) if val is not None else None

View file

@ -2423,7 +2423,8 @@ async def global_spend():
if prisma_client is None:
raise HTTPException(status_code=500, detail={"error": "No db connected"})
sql_query = """SELECT SUM(spend) as total_spend FROM "MonthlyGlobalSpend";"""
# Single aggregation on base table (same result, less CPU than SUM over MonthlyGlobalSpend view)
sql_query = """SELECT SUM("spend") AS total_spend FROM "LiteLLM_SpendLogs" WHERE "startTime" >= (CURRENT_DATE - INTERVAL '30 days')"""
response = await prisma_client.db.query_raw(query=sql_query)
if response is not None:
if isinstance(response, list) and len(response) > 0: