Repository navigation
fix(schema): index LiteLLM_SpendLogs on (api_key, startTime) - #37167
Neznakomec16 wants to merge 11 commits into
Conversation
…ticated requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…stency Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Greptile SummaryThe PR adds a composite
Confidence Score: 4/5The missing runtime schema update should be fixed before merging so schema generation and drift checks agree with the migration The database migration creates the index, but litellm/proxy/schema.prisma remains out of sync with both changed schema copies Files Needing Attention: litellm/proxy/schema.prisma, schema.prisma
|
| Filename | Overview |
|---|---|
| schema.prisma | Adds the intended composite index, but the corresponding runtime proxy schema was not synchronized |
| litellm-proxy-extras/litellm_proxy_extras/schema.prisma | Correctly adds the composite index to the migration-generation schema, while exposing inconsistency with the unchanged proxy schema |
| litellm-proxy-extras/litellm_proxy_extras/migrations/20260817120000_add_spendlogs_api_key_starttime_idx/migration.sql | Adds the matching PostgreSQL index with an idempotent CREATE INDEX statement |
Reviews (1): Last reviewed commit: "fix(schema): index LiteLLM_SpendLogs on ..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
The budget-window spend counter is recomputed from raw spend logs in
SpendCounterReseed.window_from_spend_logs (litellm/proxy/db/spend_counter_reseed.py),
which issues
SELECT api_key, SUM(spend) FROM "LiteLLM_SpendLogs"
WHERE api_key = $1 AND "startTime" >= $2 GROUP BY api_key
No index covers this predicate: the only usable one is on startTime alone, so the
scan reads the whole time window and discards every row belonging to another key.
Cost therefore scales with total traffic in the window rather than with the key's
own traffic. On a table in the 10^7-row range this takes tens of seconds, and past
the Prisma client's 30s default timeout it surfaces as httpx.ReadTimeout plus
retries rather than a slow query.
Adding (api_key, startTime) turns it into an index scan bounded by the key.
fcb832c to
e96d4a0
Compare
|
|
…nd Model Hub UI Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…mutation Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ow read fails Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…iption feat(model_hub): surface model_info.description in Model Hub
…ip_save_on_failed_read fix(health): skip background health check DB writes when the latest-row read fails
feat(dd_span_tagger): emit litellm.user_email span tag for JWT-authenticated requests
…arttime-index Keep both new SpendLogs indexes: upstream's litellm_call_id and this branch's composite (api_key, startTime). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Status
mainalready contains this index via1bdc1b9b17. The branch is synced withmainand has no remaining diffProblem
SpendCounterReseed.window_from_spend_logs()(litellm/proxy/db/spend_counter_reseed.py) recomputes a budget-window spend counter straight from the raw spend logs:which reaches Postgres as:
LiteLLM_SpendLogshas no index that covers this predicate — the closest is@@index([startTime]). So Postgres scans the entire time window and drops every row that belongs to a different key. Cost therefore scales with total traffic in the window rather than with the key's own traffic, and grows with the length of the budget window.On a deployment with a spend-log table in the 10^7-row range and a 30-day window, we measured a single such call at ~97 seconds, discarding >99% of the rows it read. Because the Prisma Python client's default HTTP timeout is 30s (
prisma/http_abstract.py), calls past that threshold surface ashttpx.ReadTimeoutinstead of slow-but-successful queries, and the retries make the user-visible latency land on multiples of ~30s.Fix
Add
@@index([api_key, startTime])toLiteLLM_SpendLogs(bothschema.prismacopies) plus the corresponding migration.Measurements
Same query, same 30-day window, same key, before and after (identifiers parameterised, absolute call counts omitted):
Roughly three orders of magnitude in execution time and ~57x fewer buffers touched. Index size was ~780 MB for that table.
To be precise about what was measured: the "After" plan comes from a local variant of the index that adds
INCLUDE (spend), which is why it reportsIndex Only Scan. Prisma cannot express covering indexes, so this PR ships the plain two-column index. That one produces the same key-bounded index scan — the win here is not reading rows of other keys — with a heap fetch per matched row instead of the covering shortcut.Note for operators of large tables
The migration uses plain
CREATE INDEX(matching the convention of the existing migrations, and required because Prisma runs migrations inside a transaction whereCONCURRENTLYis not allowed). On a large, busyLiteLLM_SpendLogsthat takes a write lock for the duration of the build. Operators can avoid it by creating the index ahead of the upgrade:The migration's
IF NOT EXISTSthen makes it a no-op.Related, not addressed here
The reseed is described as coalesced and cache-backed, yet in our deployment it ran often enough to dominate total database time — so the counter cache appears not to hold in a multi-worker setup. There is also a broader question of why the budget window is recomputed from raw spend logs at all when daily aggregate tables (
LiteLLM_DailyUserSpend,LiteLLM_DailyTeamSpend, ...) are maintained alongside. Both feel like separate issues; happy to open one if maintainers agree.