The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): requeue the daily tag rollup on commit failure without the Redis buffer
mateo-berri3edbf60e9c1d
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.pyindex a4f1713d61d..b2e6f9dc54d 100644--- a/litellm/proxy/db/db_spend_update_writer.py+++ b/litellm/proxy/db/db_spend_update_writer.py@@ -1305,7 +1305,7 @@ class DBSpendUpdateWriter:async def _flush_daily_spend_queue(self,queue: DailySpendUpdateQueue,- entity_type: Literal["user", "team", "org", "end_user", "agent"],+ entity_type: Literal["user", "team", "org", "tag", "end_user", "agent"],commit: _DailySpendCommit[_DailySpendTransactionT],n_retry_times: int,prisma_client: PrismaClient,@@ -1447,19 +1447,15 @@ class DBSpendUpdateWriter:Commit only tag spend updates to database.This is called by a separate scheduler job at a longer interval."""- daily_tag_spend_update_transactions: Final = cast(- dict[str, DailyTagSpendTransaction],- await self.daily_tag_spend_update_queue.flush_and_get_aggregated_daily_spend_update_transactions(),+ await self._flush_daily_spend_queue(+ queue=self.daily_tag_spend_update_queue,+ entity_type="tag",+ commit=DBSpendUpdateWriter.update_daily_tag_spend,+ n_retry_times=n_retry_times,+ prisma_client=prisma_client,+ proxy_logging_obj=proxy_logging_obj,)- if daily_tag_spend_update_transactions:- await DBSpendUpdateWriter.update_daily_tag_spend(- n_retry_times=n_retry_times,- prisma_client=prisma_client,- proxy_logging_obj=proxy_logging_obj,- daily_spend_transactions=daily_tag_spend_update_transactions,- )-async def _commit_daily_tag_spend_to_db_with_redis(self,prisma_client: PrismaClient,diff --git a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py b/tests/test_litellm/proxy/db/test_db_spend_update_writer.pyindex 3feb117edb7..b27b838133b 100644--- a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py+++ b/tests/test_litellm/proxy/db/test_db_spend_update_writer.py@@ -2864,6 +2864,35 @@ async def test_failed_daily_spend_commit_requeues_the_rows_and_flushes_the_otherassert db_writer.daily_spend_update_queue.update_queue.empty()+async def test_failed_daily_tag_spend_commit_requeues_the_rows():+ """The tag rollup drains on its own scheduler job with the same no-Redis drop:+ a failed LiteLLM_DailyTagSpend commit has to put the rows back for the next tick."""+ db_writer = DBSpendUpdateWriter()+ tag_txn = {key: value for key, value in _daily_txn().items() if key != "user_id"} | {"tag": "tag-1"}+ await db_writer.daily_tag_spend_update_queue.add_update({"tag-key": tag_txn})+ db = _DailySpendFakeDB(failing_table="LiteLLM_DailyTagSpend")+ proxy_logging_obj = MagicMock()+ proxy_logging_obj.failure_handler = AsyncMock()++ await db_writer._commit_daily_tag_spend_to_db(+ prisma_client=_WindowSpendFakePrisma(db), n_retry_times=0, proxy_logging_obj=proxy_logging_obj+ )++ assert _daily_upserts(db, "LiteLLM_DailyTagSpend") == []+ assert not db_writer.daily_tag_spend_update_queue.update_queue.empty()++ db.failing_table = None+ await db_writer._commit_daily_tag_spend_to_db(+ prisma_client=_WindowSpendFakePrisma(db), n_retry_times=0, proxy_logging_obj=proxy_logging_obj+ )++ (tag_upsert,) = _daily_upserts(db, "LiteLLM_DailyTagSpend")+ assert _row_values(tag_upsert, "tag") == ["tag-1"]+ assert _row_values(tag_upsert, "spend") == [0.1]+ assert db_writer.daily_tag_spend_update_queue.update_queue.empty()++@pytest.mark.asyncioasync def test_failed_window_spend_commit_from_redis_is_restored_to_redis():"""The Redis drain is destructive, so a failed window commit has to push