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]

chore: merge litellm_internal_staging into litellm_fix_redis_spend_buffer_requeue_33872

Devin AIf3c64029e162
diff --cc litellm/proxy/db/db_spend_update_writer.py
index f13bf2e2105,b0130db232a..cb3ed4c9520
--- a/litellm/proxy/db/db_spend_update_writer.py
+++ b/litellm/proxy/db/db_spend_update_writer.py
@@@ -1044,12 -1083,11 +1107,12 @@@ class DBSpendUpdateWriter
cronjob_id=DB_DAILY_TAG_SPEND_UPDATE_JOB_NAME,
):
verbose_proxy_logger.debug("acquired lock for daily tag spend updates")
+ daily_tag_spend_update_transactions: dict[str, DailyTagSpendTransaction] | None = None
+ committed = False
try:
- daily_tag_spend_update_transactions = (
+ daily_tag_spend_update_transactions: Final = (
await self.redis_update_buffer.get_all_daily_tag_spend_update_transactions_from_redis_buffer()
)
-
if daily_tag_spend_update_transactions:
await DBSpendUpdateWriter.update_daily_tag_spend(
n_retry_times=n_retry_times,
diff --cc litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
index 660fd514d99,6879284a6fd..573da72b873
--- a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
+++ b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
@@@ -6,11 -6,8 +6,11 @@@ This is to prevent deadlocks and improv
import asyncio
import json
+from collections.abc import Mapping
- from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
+ from typing import TYPE_CHECKING, Any, Final, cast
+from redis.exceptions import RedisError
+
from litellm._logging import verbose_proxy_logger
from litellm.caching import RedisCache
from litellm.constants import (
@@@ -377,59 -372,6 +375,59 @@@ class RedisUpdateBuffer
if daily_txns:
await daily_queue.update_queue.put(daily_txns)
+ async def restore_transactions_to_redis(
+ self,
+ db_spend_update_transactions: DBSpendUpdateTransactions | None = None,
+ daily_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ daily_team_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ daily_org_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ daily_end_user_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ daily_agent_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ daily_tag_spend_update_transactions: Mapping[str, BaseDailySpendTransaction] | None = None,
+ ) -> None:
+ """
+ Re-push transactions that were popped from Redis but not committed to the DB.
+
+ The leader drains the buffers with a destructive ``lpop`` before committing to
+ the database. When a commit fails after its retries are exhausted, the popped
+ transactions must be pushed back so a later scheduler tick can retry them;
+ otherwise the aggregated spend is lost permanently. The re-pushed payloads use
+ the same JSON encoding as the store path, so the next drain parses them normally.
+ """
+ if self.redis_cache is None:
+ return
+
- _configs = (
++ restore_configs: Final = (
+ (db_spend_update_transactions, REDIS_UPDATE_BUFFER_KEY),
+ (daily_spend_update_transactions, REDIS_DAILY_SPEND_UPDATE_BUFFER_KEY),
+ (daily_team_spend_update_transactions, REDIS_DAILY_TEAM_SPEND_UPDATE_BUFFER_KEY),
+ (daily_org_spend_update_transactions, REDIS_DAILY_ORG_SPEND_UPDATE_BUFFER_KEY),
+ (daily_end_user_spend_update_transactions, REDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY),
+ (daily_agent_spend_update_transactions, REDIS_DAILY_AGENT_SPEND_UPDATE_BUFFER_KEY),
+ (daily_tag_spend_update_transactions, REDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY),
+ )
+
- rpush_list: list[RedisPipelineRpushOperation] = [ # mutable-ok: async_rpush_pipeline requires a list arg
++ rpush_list: Final[list[RedisPipelineRpushOperation]] = [ # mutable-ok: async_rpush_pipeline requires a list arg
+ RedisPipelineRpushOperation(key=redis_key, values=[safe_dumps(transactions)])
- for transactions, redis_key in _configs
++ for transactions, redis_key in restore_configs
+ if transactions
+ ]
+ if len(rpush_list) == 0:
+ return
+
+ try:
+ await self.redis_cache.async_rpush_pipeline(rpush_list=rpush_list)
+ verbose_proxy_logger.info(
+ "Spend tracking - restored %d uncommitted transaction set(s) to Redis for retry on next tick.",
+ len(rpush_list),
+ )
+ except RedisError as e:
+ verbose_proxy_logger.error(
+ "Spend tracking - failed to restore uncommitted transactions to Redis. "
+ "These spend updates are lost. Error: %s",
+ str(e),
+ )
+
@staticmethod
def _number_of_transactions_to_store_in_redis(
db_spend_update_transactions: DBSpendUpdateTransactions,