diff --git a/litellm/proxy/db/accounting_ledger.py b/litellm/proxy/db/accounting_ledger.py index 7ebdfb70506..0dbc3d8c86a 100644 --- a/litellm/proxy/db/accounting_ledger.py +++ b/litellm/proxy/db/accounting_ledger.py @@ -165,7 +165,7 @@ _PENDING_COMPILATION: Final = """ SELECT payload::text AS payload FROM "LiteLLM_AccountingEvent" WHERE kind = 'outbound_usage' AND compilation_state = 'pending' AND (compilation_retry_at IS NULL OR compilation_retry_at <= CURRENT_TIMESTAMP) -ORDER BY recorded_at, event_id LIMIT $1::int +ORDER BY compilation_retry_at NULLS FIRST, recorded_at, event_id LIMIT $1::int """ _COMPILATION_RESULT: Final = """ UPDATE "LiteLLM_AccountingEvent" SET compilation_state = $2, compilation_error = $3, compilation_retry_at = NULL diff --git a/tests/integration/database/test_accounting_ledger.py b/tests/integration/database/test_accounting_ledger.py index 8703559d6aa..0171d3683ca 100644 --- a/tests/integration/database/test_accounting_ledger.py +++ b/tests/integration/database/test_accounting_ledger.py @@ -1010,6 +1010,11 @@ async def test_failing_compilation_page_does_not_starve_later_usage(writer: _Wri return _cost().snapshot await LoggingCostCompiler(store, price, _TARGETS).compile_pending(limit=1) + _rows( + ledger_database, + 'UPDATE "LiteLLM_AccountingEvent" SET compilation_retry_at = CURRENT_TIMESTAMP WHERE event_id = %s', + (broken.event_id,), + ) await LoggingCostCompiler(store, price, _TARGETS).compile_pending(limit=1) assert _rows(ledger_database, "SELECT source_event_id FROM \"LiteLLM_AccountingEvent\" WHERE kind = 'cost'") == [ (healthy.event_id,)