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
Revert "fix: make cache updates synchronous for budget enforcement"
Ishaan Jaffer54286fca6047
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.pyindex faca49ccc6e..e0b056d450f 100644--- a/litellm/proxy/auth/auth_checks.py+++ b/litellm/proxy/auth/auth_checks.py@@ -2295,13 +2295,7 @@ 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- # 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.0+ 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 8f681c727ea..dab5fb1bfd5 100644--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@ -181,15 +181,17 @@ class _ProxyDBLogger(CustomLogger):org_id=org_id,)- # 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,+ # 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,+ ))await proxy_logging_obj.slack_alerting_instance.customer_spend_alert(diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 91094c49c9f..f12d4d6ab4c 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -1,5 +1,6 @@import asyncioimport copy+import enumimport inspectimport ioimport os@@ -12,7 +13,6 @@ import timeimport tracebackimport warningsfrom datetime import datetime, timedelta, timezone-import enumfrom typing import (TYPE_CHECKING,Any,@@ -29,41 +29,9 @@ from typing import (get_origin,get_type_hints,)+from pydantic import BaseModel, Json-from litellm.proxy._types import (- ProxyException,- UserAPIKeyAuth,- LiteLLM_UserTable,- CommonProxyErrors,- LitellmUserRoles,- ConfigList,- ConfigYAML,- ConfigFieldUpdate,- ConfigGeneralSettings,- ConfigFieldInfo,- PassThroughGenericEndpoint,- FieldDetail,- ConfigFieldDelete,- CallbackDelete,- InvitationClaim,- InvitationModel,- InvitationNew,- InvitationUpdate,- InvitationDelete,- CallInfo,- Litellm_EntityType,- TeamDefaultSettings,- RoleBasedPermissions,- SupportedDBObjectType,- ProxyErrorTypes,- EnterpriseLicenseData,- LiteLLM_JWTAuth,- TokenCountRequest,- TransformRequestBody,- LiteLLM_TeamTable,- SpecialModelNames,-)from litellm._uuid import uuidfrom litellm.constants import (AIOHTTP_CONNECTOR_LIMIT,@@ -84,6 +52,39 @@ from litellm.litellm_core_utils.litellm_logging import (_init_custom_logger_compatible_class,)from litellm.litellm_core_utils.safe_json_dumps import safe_dumps+from litellm.proxy._types import (+ CallbackDelete,+ CallInfo,+ CommonProxyErrors,+ ConfigFieldDelete,+ ConfigFieldInfo,+ ConfigFieldUpdate,+ ConfigGeneralSettings,+ ConfigList,+ ConfigYAML,+ EnterpriseLicenseData,+ FieldDetail,+ InvitationClaim,+ InvitationDelete,+ InvitationModel,+ InvitationNew,+ InvitationUpdate,+ Litellm_EntityType,+ LiteLLM_JWTAuth,+ LiteLLM_TeamTable,+ LiteLLM_UserTable,+ LitellmUserRoles,+ PassThroughGenericEndpoint,+ ProxyErrorTypes,+ ProxyException,+ RoleBasedPermissions,+ SpecialModelNames,+ SupportedDBObjectType,+ TeamDefaultSettings,+ TokenCountRequest,+ TransformRequestBody,+ UserAPIKeyAuth,+)from litellm.proxy.common_utils.callback_utils import (normalize_callback_names,process_callback,@@ -1703,11 +1704,12 @@ async def update_cache( # noqa: PLR0915if tags is not None:await _update_tag_cache()- # 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,+ 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,+ ))@@ -3643,7 +3645,9 @@ class ProxyConfig:)else:# Interval-based scheduling (existing behavior)- from litellm.litellm_core_utils.duration_parser import duration_in_seconds+ from litellm.litellm_core_utils.duration_parser import (+ duration_in_seconds,+ )retention_interval = general_settings.get("maximum_spend_logs_retention_interval", "1d"