fix(mavvrik): add api_key to ORDER BY for stable OFFSET pagination

Without dus.api_key in the ORDER BY, pagination is unstable: multiple rows
can share the same (date, user_id, model) with different api_key values.
When page boundaries fall inside such a group the DB can return those rows
in any order, causing duplicates or silent drops across pages.

Fix: add dus.api_key to the sort key, matching the table's @@unique
constraint: (user_id, date, api_key, model, custom_llm_provider, ...).
This guarantees a stable total order for all OFFSET-based pagination.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Praveen Ghuge 2026-04-26 08:23:37 +05:30
parent 5af15a2b59
commit b0a140647f

View file

@ -52,7 +52,7 @@ LEFT JOIN "LiteLLM_VerificationToken" vt ON dus.api_key = vt.token
LEFT JOIN "LiteLLM_TeamTable" tt ON vt.team_id = tt.team_id
LEFT JOIN "LiteLLM_UserTable" ut ON dus.user_id = ut.user_id
WHERE dus.date = $1
ORDER BY dus.date, dus.user_id, dus.model ASC
ORDER BY dus.date, dus.user_id, dus.api_key, dus.model ASC
"""
_EARLIEST_DATE_QUERY = 'SELECT MIN(date) AS earliest FROM "LiteLLM_DailyUserSpend"'