Skip to content

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]

fix(proxy): isolate project spend enqueue failures from sibling spend writes

ryan70164dabd279
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py
index e0985d5b5ae..2d6c7a0a74c 100644
--- a/litellm/proxy/db/db_spend_update_writer.py
+++ b/litellm/proxy/db/db_spend_update_writer.py
@@ -706,11 +706,17 @@ class DBSpendUpdateWriter:
traceback.format_exc(),
)
- await self._update_project_db(
- response_cost=response_cost,
- project_id=project_id,
- prisma_client=prisma_client,
- )
+ try:
+ await self._update_project_db(
+ response_cost=response_cost,
+ project_id=project_id,
+ prisma_client=prisma_client,
+ )
+ except Exception: # noqa: BLE001 # a project enqueue failure must not skip the sibling spend writes
+ verbose_proxy_logger.debug(
+ "_batch_database_updates: _update_project_db failed: %s",
+ traceback.format_exc(),
+ )
try:
await self._update_tag_db(
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.py
index f9494cd6332..9a2b15d8a30 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
@@ -1175,6 +1175,33 @@ async def test_batch_database_updates_without_project_id_touches_no_project_row(
assert transactions["project_list_transactions"] == {}
+async def test_project_enqueue_failure_does_not_stop_sibling_spend_updates():
+ db_writer: Final = DBSpendUpdateWriter()
+ db_writer._update_project_db = AsyncMock(side_effect=RuntimeError("project queue boom"))
+ db_writer._update_tag_db = AsyncMock()
+ db_writer._update_agent_db = AsyncMock()
+ db_writer.add_spend_log_transaction_to_daily_user_transaction = AsyncMock()
+
+ await db_writer._batch_database_updates(
+ response_cost=0.1,
+ user_id="u1",
+ hashed_token="t1",
+ team_id="team-1",
+ org_id=None,
+ end_user_id=None,
+ prisma_client=MagicMock(),
+ litellm_proxy_budget_name=None,
+ payload={"request_id": "req-1", "model": "gpt-4o-mini", "spend": 0.1, "request_tags": ["t"]},
+ project_id="proj-1",
+ )
+
+ db_writer._update_project_db.assert_awaited_once()
+ db_writer._update_tag_db.assert_awaited_once()
+ db_writer._update_agent_db.assert_awaited_once()
+ db_writer.add_spend_log_transaction_to_daily_user_transaction.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_add_spend_log_transaction_to_daily_tag_transaction_with_request_id():
"""