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] Perf - Reduce memory accumulation of spend_logs (#17742)
Alexsander Hamir9a0432d01395
diff --git a/litellm/constants.py b/litellm/constants.pyindex 2c982ee41c4..87e873e35bc 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -152,7 +152,7 @@ REDIS_DAILY_ORG_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_org_spend_update_bufferREDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_end_user_spend_update_buffer"REDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_tag_spend_update_buffer"MAX_REDIS_BUFFER_DEQUEUE_COUNT = int(os.getenv("MAX_REDIS_BUFFER_DEQUEUE_COUNT", 100))-MAX_SIZE_IN_MEMORY_QUEUE = int(os.getenv("MAX_SIZE_IN_MEMORY_QUEUE", 10000))+MAX_SIZE_IN_MEMORY_QUEUE = int(os.getenv("MAX_SIZE_IN_MEMORY_QUEUE", 2000))MAX_IN_MEMORY_QUEUE_FLUSH_COUNT = int(os.getenv("MAX_IN_MEMORY_QUEUE_FLUSH_COUNT", 1000))@@ -1112,6 +1112,8 @@ CLOUDZERO_MAX_FETCHED_DATA_RECORDS = int(SPEND_LOG_CLEANUP_JOB_NAME = "spend_log_cleanup"SPEND_LOG_RUN_LOOPS = int(os.getenv("SPEND_LOG_RUN_LOOPS", 500))SPEND_LOG_CLEANUP_BATCH_SIZE = int(os.getenv("SPEND_LOG_CLEANUP_BATCH_SIZE", 1000))+SPEND_LOG_QUEUE_SIZE_THRESHOLD = int(os.getenv("SPEND_LOG_QUEUE_SIZE_THRESHOLD", 100))+SPEND_LOG_QUEUE_POLL_INTERVAL = float(os.getenv("SPEND_LOG_QUEUE_POLL_INTERVAL", 2.0))DEFAULT_CRON_JOB_LOCK_TTL_SECONDS = int(os.getenv("DEFAULT_CRON_JOB_LOCK_TTL_SECONDS", 60)) # 1 minutediff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 19cdbf225ba..88eb1cec6e7 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -4429,6 +4429,19 @@ class ProxyStartupEvent:misfire_grace_time=APSCHEDULER_MISFIRE_GRACE_TIME,)+ ### MONITOR SPEND LOGS QUEUE (queue-size-based job) ###+ if general_settings.get("disable_spend_logs", False) is False:+ from litellm.proxy.utils import _monitor_spend_logs_queue++ # Start background task to monitor spend logs queue size+ asyncio.create_task(+ _monitor_spend_logs_queue(+ prisma_client=prisma_client,+ db_writer_client=db_writer_client,+ proxy_logging_obj=proxy_logging_obj,+ )+ )+### ADD NEW MODELS ###store_model_in_db = (get_secret_bool("STORE_MODEL_IN_DB", store_model_in_db) or store_model_in_dbdiff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.pyindex ec379df7114..ddb9cb90e3c 100644--- a/litellm/proxy/spend_tracking/spend_tracking_utils.py+++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py@@ -415,10 +415,15 @@ def get_logging_payload( # noqa: PLR0915)verbose_proxy_logger.debug(- "SpendTable: created payload - payload: %s\n\n",- json.dumps(payload, indent=4, default=str),+ "SpendTable: created payload - request_id: %s, model: %s, spend: %s",+ payload.get("request_id"),+ payload.get("model"),+ payload.get("spend"),)+ # Explicitly clear large intermediate objects to reduce memory pressure+ del response_obj_dict, usage, clean_metadata, additional_usage_values+return payloadexcept Exception as e:verbose_proxy_logger.exception(diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.pyindex 09f3af20e7c..24fc59ce8c5 100644--- a/litellm/proxy/utils.py+++ b/litellm/proxy/utils.py@@ -1,5 +1,6 @@import asyncioimport copy+import gcimport hashlibimport jsonimport os@@ -66,6 +67,7 @@ from litellm.litellm_core_utils.litellm_logging import Loggingfrom litellm.litellm_core_utils.safe_json_dumps import safe_dumpsfrom litellm.litellm_core_utils.safe_json_loads import safe_json_loadsfrom litellm.llms.custom_httpx.httpx_handler import HTTPHandler+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandlerfrom litellm.proxy._types import (AlertType,CallInfo,@@ -3342,14 +3344,14 @@ class ProxyUpdateSpend:async def update_spend_logs(n_retry_times: int,prisma_client: PrismaClient,- db_writer_client: Optional[HTTPHandler],+ db_writer_client: Optional[AsyncHTTPHandler],proxy_logging_obj: ProxyLogging,):- BATCH_SIZE = 100 # Preferred size of each batch to write to the database+ BATCH_SIZE = 1000 # Preferred size of each batch to write to the databaseMAX_LOGS_PER_INTERVAL = (- 1000 # Maximum number of logs to flush in a single interval+ 10000 # Maximum number of logs to flush in a single interval)- # Get initial logs to process+ # Get initial logs to proceslogs_to_process = prisma_client.spend_log_transactions[:MAX_LOGS_PER_INTERVAL]start_time = time.time()try:@@ -3364,11 +3366,14 @@ class ProxyUpdateSpend:if not base_url.endswith("/"):base_url += "/"verbose_proxy_logger.debug("base_url: {}".format(base_url))+ json_data = json.dumps(logs_to_process)response = await db_writer_client.post(url=base_url + "spend/update",- data=json.dumps(logs_to_process),+ data=json_data,headers={"Content-Type": "application/json"},)+ del json_data+ gc.collect()if response.status_code == 200:prisma_client.spend_log_transactions = (prisma_client.spend_log_transactions[@@ -3388,12 +3393,18 @@ class ProxyUpdateSpend:verbose_proxy_logger.debug(f"Flushed {len(batch)} logs to the DB.")+ # Explicitly clear batch memory+ del batch, batch_with_dates+ # Only run gc every 5 batches to reduce overhead+ if j % (BATCH_SIZE * 5) == 0:+ gc.collect()prisma_client.spend_log_transactions = (prisma_client.spend_log_transactions[len(logs_to_process) :])+ remaining_count = len(prisma_client.spend_log_transactions)verbose_proxy_logger.debug(- f"{len(logs_to_process)} logs processed. Remaining in queue: {len(prisma_client.spend_log_transactions)}"+ f"{len(logs_to_process)} logs processed. Remaining in queue: {remaining_count}")breakexcept DB_CONNECTION_ERROR_TYPES:@@ -3409,6 +3420,10 @@ class ProxyUpdateSpend:_raise_failed_update_spend_exception(e=e, start_time=start_time, proxy_logging_obj=proxy_logging_obj)+ finally:+ # Clean up logs_to_process after all processing is complete+ del logs_to_process+ gc.collect()@staticmethoddef disable_spend_updates() -> bool:@@ -3425,7 +3440,7 @@ class ProxyUpdateSpend:async def update_spend( # noqa: PLR0915prisma_client: PrismaClient,- db_writer_client: Optional[HTTPHandler],+ db_writer_client: Optional[AsyncHTTPHandler],proxy_logging_obj: ProxyLogging,):"""@@ -3451,13 +3466,98 @@ async def update_spend( # noqa: PLR0915"Spend Logs transactions: {}".format(len(prisma_client.spend_log_transactions)))- if len(prisma_client.spend_log_transactions) > 0:- await ProxyUpdateSpend.update_spend_logs(- n_retry_times=n_retry_times,- prisma_client=prisma_client,- proxy_logging_obj=proxy_logging_obj,- db_writer_client=db_writer_client,- )+ # Spend log transactions are now processed by a separate queue-size-based job+ # See update_spend_logs_job and _monitor_spend_logs_queue+++async def update_spend_logs_job(+ prisma_client: PrismaClient,+ db_writer_client: Optional[AsyncHTTPHandler],+ proxy_logging_obj: ProxyLogging,+):+ """+ Job to process spend_log_transactions queue.++ This job is triggered based on queue size rather than time.+ Processes spend log transactions when the queue reaches a threshold.+ """+ n_retry_times = 3++ queue_size = len(prisma_client.spend_log_transactions)++ if queue_size == 0:+ return++ await ProxyUpdateSpend.update_spend_logs(+ n_retry_times=n_retry_times,+ prisma_client=prisma_client,+ proxy_logging_obj=proxy_logging_obj,+ db_writer_client=db_writer_client,+ )+++async def _monitor_spend_logs_queue(+ prisma_client: PrismaClient,+ db_writer_client: Optional[AsyncHTTPHandler],+ proxy_logging_obj: ProxyLogging,+):+ """+ Background task that monitors the spend_log_transactions queue size+ and triggers processing when the threshold is reached.++ Args:+ prisma_client: Prisma client instance+ db_writer_client: Optional HTTP handler for external spend logs endpoint+ proxy_logging_obj: Proxy logging object+ """+ from litellm.constants import SPEND_LOG_QUEUE_SIZE_THRESHOLD, SPEND_LOG_QUEUE_POLL_INTERVAL++ threshold = SPEND_LOG_QUEUE_SIZE_THRESHOLD+ base_interval = SPEND_LOG_QUEUE_POLL_INTERVAL+ max_backoff = 30.0 # Maximum backoff interval in seconds+ backoff_multiplier = 1.5 # Exponential backoff multiplier+ current_interval = base_interval++ verbose_proxy_logger.info(+ f"Starting spend logs queue monitor (threshold: {threshold}, poll_interval: {base_interval}s)"+ )++ while True:+ try:+ queue_size = len(prisma_client.spend_log_transactions)++ if queue_size > 0:+ if queue_size >= threshold:+ verbose_proxy_logger.debug(+ f"Spend logs queue size ({queue_size}) reached threshold ({threshold}), triggering processing"+ )+ # Reset to base interval when threshold is reached+ current_interval = base_interval+ else:+ verbose_proxy_logger.debug(+ f"Spend logs queue size ({queue_size}) below threshold ({threshold}), processing with backoff"+ )+ # Exponential backoff when below threshold but still processing+ current_interval = min(current_interval * backoff_multiplier, max_backoff)++ await update_spend_logs_job(+ prisma_client=prisma_client,+ db_writer_client=db_writer_client,+ proxy_logging_obj=proxy_logging_obj,+ )+ else:+ # Exponential backoff when no logs to process+ current_interval = min(current_interval * backoff_multiplier, max_backoff)++ await asyncio.sleep(current_interval)+ except Exception as e:+ verbose_proxy_logger.error(+ f"Error in spend logs queue monitor: {str(e)}\n{traceback.format_exc()}"+ )+ # Continue monitoring even if there's an error, with exponential backoff+ current_interval = min(current_interval * backoff_multiplier, max_backoff)+ await asyncio.sleep(current_interval)+def _raise_failed_update_spend_exception(