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: make cache updates synchronous for budget enforcement
shin-bot-litellmd0383412e8a4
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.pyindex e0b056d450f..faca49ccc6e 100644--- a/litellm/proxy/auth/auth_checks.py+++ b/litellm/proxy/auth/auth_checks.py@@ -2295,7 +2295,13 @@ async def _check_team_member_budget(and team_membership.litellm_budget_table.max_budget is not None):team_member_budget = team_membership.litellm_budget_table.max_budget- team_member_spend = team_membership.spend or 0.0+ # Prefer valid_token.team_member_spend (from token cache) over team_membership.spend+ # The token cache is updated synchronously after each request, while the team membership+ # cache may have stale data since DB updates are batched+ if valid_token.team_member_spend is not None:+ team_member_spend = valid_token.team_member_spend+ else:+ team_member_spend = team_membership.spend or 0.0if team_member_spend >= team_member_budget:raise litellm.BudgetExceededError(diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.pyindex dab5fb1bfd5..8f681c727ea 100644--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@ -181,17 +181,15 @@ class _ProxyDBLogger(CustomLogger):org_id=org_id,)- # update cache- asyncio.create_task(- update_cache(- token=user_api_key,- user_id=user_id,- end_user_id=end_user_id,- response_cost=response_cost,- team_id=team_id,- parent_otel_span=parent_otel_span,- tags=tags,- )+ # update cache - await to ensure budget checks see updated spend+ await update_cache(+ token=user_api_key,+ user_id=user_id,+ end_user_id=end_user_id,+ response_cost=response_cost,+ team_id=team_id,+ parent_otel_span=parent_otel_span,+ tags=tags,)await proxy_logging_obj.slack_alerting_instance.customer_spend_alert(diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex ffa2da4cfbe..91094c49c9f 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -1703,12 +1703,11 @@ async def update_cache( # noqa: PLR0915if tags is not None:await _update_tag_cache()- asyncio.create_task(- user_api_key_cache.async_set_cache_pipeline(- cache_list=values_to_update_in_cache,- ttl=60,- litellm_parent_otel_span=parent_otel_span,- )+ # Await cache update to ensure budget checks see updated spend values+ await user_api_key_cache.async_set_cache_pipeline(+ cache_list=values_to_update_in_cache,+ ttl=60,+ litellm_parent_otel_span=parent_otel_span,)