Repository navigation
perf(spend_tracking): cover spend in the LiteLLM_SpendLogs api_key index - #41816
Open
etiennechabert wants to merge 2 commits into
Open
etiennechabert wants to merge 2 commits into
etiennechabert wants to merge 2 commits into
Conversation
The per-key window aggregates that the (api_key, startTime) index from BerriAI#37983 was added for all read SUM(spend) WHERE api_key = ... AND "startTime" >= ... (budget-window reseed, BudgetWindowSpend seeding, the spend-logs fallback). api_key has ~zero physical correlation, so a key's rows sit about one per heap page and the plain index still pays one heap page per matching row just to read spend. Carrying spend in the index makes those sums index-only scans. spend is a trailing key column rather than INCLUDE (spend) because Prisma's @@index cannot express INCLUDE. Postgres answers the same query from either shape with an identical plan and index size. 20260823000000 has not reached a stable release yet, so it now builds the covering index directly: installs upgrading from a stable release pay for one index build on LiteLLM_SpendLogs, not two. 20260918120000 moves installs that already applied the original file (v1.103.0-dev.2) over, creating the covering index before dropping the old one, and is a no-op everywhere else. The partition runbooks carry the new name and columns. Verified on Postgres 18 with `prisma migrate deploy`: * stable upgrade: both migrations apply, one covering index, no drift * dev.2 upgrade: deploy accepts the edited 20260823000000, the new migration swaps the plain index for the covering one, no drift * partition/unpartition runbooks keep the covering index on the parent, the DEFAULT partition, and the round-tripped table 1.5M rows / 10k keys / 64-char hashed keys: 88 -> 8 buffers per key aggregate, index 151 MB -> 163 MB.
etiennechabert
requested review from
ryan-crabbe-berri and
yuneng-berri
as code owners
September 18, 2026 13:31
Contributor
Contributor
|
3 of 5 tasks
Postgres rejects CREATE INDEX CONCURRENTLY on a partitioned table even with IF NOT EXISTS and the index already present (verified on 18.4), so a concurrent build would fail 20260918120000 on every partitioned install, including stable upgrades where 20260823000000 already built the index. Record that, and the pre-create escape hatch, next to the statement.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Author
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
spendfrom the tableHow it solves it:
spendto the index:@@index([api_key, startTime, spend])spendis a key column because Prisma@@indexcan't expressINCLUDEUser Flow
Before: each budget-window check on a key re-reads one table page per request that key made in the window
budget_limitswith 1h / 1d / 7d windows)After: the same check is answered from the index alone
Relevant issues
Follow-up to #37983
Affected release
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
Screenshots / Proof of Fix
Shared setup: Postgres 18.4,
LiteLLM_SpendLogscreated byprisma migrate deployfrom this repo's migrations, 1.5M rows across 10k keys (64-char hashed keys, 600-byte payload per row, inserted in time order,api_keycorrelation 0.003),VACUUM ANALYZE. Query shape of the budget-window reseed and seeding reads:Before (8fc9c46)
Per-key 7-day spend sum
(api_key, startTime), 151 MBEXPLAIN (ANALYZE, BUFFERS):Upgrade from a stable release
prisma migrate deploy20260823000000, builds(api_key, startTime)Upgrade from v1.103.0-dev.2
20260823000000prisma migrate deploy: nothing pending, index stays(api_key, startTime)Partitioned install, upgrade from a stable release
partition_spend_logs.sql, thenprisma migrate deploy20260823000000;(api_key, startTime)on the parent and every partitionPartitioned install, upgrade from v1.103.0-dev.2
20260823000000prisma migrate deploy: nothing pending, index stays(api_key, startTime)Partition / unpartition runbooks
(api_key, startTime)After (3df010c)
Per-key 7-day spend sum
(api_key, startTime, spend), 163 MBEXPLAIN (ANALYZE, BUFFERS):(api_key, startTime) INCLUDE (spend)instead: identical plan, 8 buffers, 163 MBUpgrade from a stable release
prisma migrate deploy20260823000000and20260918120000; one index is built:(api_key, startTime, spend)prisma migrate diff --from-url ... --to-schema-datamodel schema.prisma: noLiteLLM_SpendLogsdriftUpgrade from v1.103.0-dev.2
20260823000000prisma migrate deployexits 0 and applies20260918120000(api_key, startTime, spend);(api_key, startTime)is gone; no driftPartitioned install, upgrade from a stable release
partition_spend_logs.sql, thenprisma migrate deploy20260823000000and20260918120000;(api_key, startTime, spend)on the parent andLiteLLM_SpendLogs_pdefaultPartitioned install, upgrade from v1.103.0-dev.2
20260823000000prisma migrate deployexits 0 and applies20260918120000LiteLLM_SpendLogs_pdefaultnow carry(api_key, startTime, spend); the old index is gonePartition / unpartition runbooks
partition_spend_logs.sql: covering index on the parent,LiteLLM_SpendLogs_pdefault, and the renamed legacy tableunpartition_spend_logs.sql: covering index on the rebuilt table, all rows keptType
🚄 Infrastructure
Caveats (if any)
Severe
CONCURRENTLY: Postgres rejects it on a partitioned table, even when the index existsCONCURRENTLY+ATTACH PARTITION, as in perf(spend_tracking): index LiteLLM_SpendLogs by (api_key, startTime) #37983Medium
20260823000000is edited after merge, so stable upgrades pay one build, not twoprisma migrate deploydoesn't re-check applied files; verified aboveprisma migrate devon a contributor DB that applied it will flag it as modifiedLow
spendis covered: token sums in/global/spend/reportalso group by modelINCLUDE (spend)index under the new name is kept; Prisma reports no drift for it