From 4314cf5029b187ccaf3ec8d66ab745bd61a01638 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Thu, 29 Jan 2026 16:06:09 -0800 Subject: [PATCH] Warm global spend cache at startup (fire-and-forget) when max_budget and DB are set --- litellm/proxy/proxy_server.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 183c25ed463..0cfdcfeeb09 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -226,6 +226,7 @@ from litellm.proxy.auth.model_checks import ( get_team_models, ) from litellm.proxy.auth.user_api_key_auth import ( + _fetch_global_spend_with_event_coordination, user_api_key_auth, user_api_key_auth_websocket, ) @@ -768,6 +769,13 @@ async def proxy_startup_event(app: FastAPI): # noqa: PLR0915 ProxyStartupEvent._add_proxy_budget_to_db( litellm_proxy_budget_name=litellm_proxy_admin_name ) + asyncio.create_task( + ProxyStartupEvent._warm_global_spend_cache( + litellm_proxy_admin_name=litellm_proxy_admin_name, + user_api_key_cache=user_api_key_cache, + prisma_client=prisma_client, + ) + ) ### START BATCH WRITING DB + CHECKING NEW MODELS### if prisma_client is not None: @@ -4660,6 +4668,26 @@ class ProxyStartupEvent: ) ) + @classmethod + async def _warm_global_spend_cache( + cls, + litellm_proxy_admin_name: str, + user_api_key_cache: DualCache, + prisma_client: PrismaClient, + ) -> None: + """Warm global spend cache once at startup to reduce impact of first wave of requests.""" + try: + cache_key = "{}:spend".format(litellm_proxy_admin_name) + await _fetch_global_spend_with_event_coordination( + cache_key=cache_key, + user_api_key_cache=user_api_key_cache, + prisma_client=prisma_client, + ) + except Exception as e: + verbose_proxy_logger.debug( + "Global spend cache warm-up at startup skipped or failed: %s", e + ) + @classmethod async def _update_default_team_member_budget(cls): """Update the default team member budget"""