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]

Merge remote-tracking branch 'origin/main' into litellm_redis_durable_spend_log_buffer

yassin0c1841affc58
diff --cc litellm/proxy/utils.py
index 434a6179d14,f6f437bea75..2ecce7725e5
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@@ -7263,19 -7236,17 +7284,24 @@@ async def update_spend_logs_job
This job is triggered based on queue size rather than time.
Pops the batch once, writes spend logs, then runs guardrail usage tracking.
"""
- n_retry_times: Final = 3
- MAX_LOGS_PER_INTERVAL: Final = 10000
-
- # Atomically pop batch from queue. The tool usage queue counts toward the
- # emptiness check: a spend-log write failure aborts a run before the tool
- # drain below, and those entries must not strand once the spend queue drains.
+ from litellm.proxy.db.baseline_accounting import flush_baseline_accounting
+
if await _total_queued_spend_transactions(prisma_client) == 0:
+ await flush_baseline_accounting(prisma_client)
return
+ async with prisma_client.spend_log_write_lock:
+ await _run_spend_logs_job(prisma_client, db_writer_client, proxy_logging_obj)
+
+
+async def _run_spend_logs_job(
+ prisma_client: PrismaClient,
+ db_writer_client: AsyncHTTPHandler | None,
+ proxy_logging_obj: ProxyLogging,
+) -> None:
++ from litellm.proxy.db.baseline_accounting import flush_baseline_accounting
++
+ n_retry_times: Final = 3
+ MAX_LOGS_PER_INTERVAL: Final = 10000
logs_to_process: Final = await dequeue_spend_logs(prisma_client, MAX_LOGS_PER_INTERVAL)
diff --cc tests/proxy_unit_tests/test_update_spend.py
index 8d78b4b61c1,0b158c33c73..ebe505b3d60
--- a/tests/proxy_unit_tests/test_update_spend.py
+++ b/tests/proxy_unit_tests/test_update_spend.py
@@@ -37,12 -37,16 +37,17 @@@ class MockPrismaClient
self.daily_user_spend_transactions = {}
self.tool_usage_transactions = []
self.autorouter_turn_transactions = []
+ self.baseline_accounting_transactions = []
+ self.baseline_accounting_lock = asyncio.Lock()
+ self.spend_log_flush_requested = None
+ self.db.tx = MagicMock()
+ self.db.tx.return_value.__aenter__ = AsyncMock(return_value=self.db)
+ self.db.tx.return_value.__aexit__ = AsyncMock(return_value=None)
+ self.db.query_raw.return_value = []
# Add locks for the transaction queues (matches real PrismaClient)
- import asyncio
-
self._spend_log_transactions_lock = asyncio.Lock()
+ self.spend_log_write_lock = asyncio.Lock()
self._tool_usage_transactions_lock = asyncio.Lock()
self._autorouter_turn_transactions_lock = asyncio.Lock()