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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into HEAD

user2ed4b5968d1f
diff --cc litellm/proxy/auth/auth_checks.py
index 06b28142a7d,113a8f538c0..450c905758f
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@@ -1592,14 -1616,12 +1619,15 @@@ async def _cache_key_object
## CACHE REFRESH TIME
user_api_key_obj.last_refreshed_at = time.time()
+ cached_key_obj = _copy_user_api_key_auth_for_cache(
+ user_api_key_obj=user_api_key_obj
+ )
await _cache_management_object(
key=key,
- value=user_api_key_obj,
+ value=cached_key_obj,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
+ model_type=UserAPIKeyAuth,
)
@@@ -2315,17 -2341,14 +2347,14 @@@ async def get_key_object
# check if in cache
key = hashed_token
- cached_key_obj: Optional[UserAPIKeyAuth] = await user_api_key_cache.async_get_cache(
- key=key
+ # Same flow as before: use cache only when we have a hit we can turn into UserAPIKeyAuth
+ # (dict from Redis / model_dump, or UserAPIKeyAuth from in-memory). Otherwise fall through to DB.
+ user_api_key_auth = await user_api_key_cache.async_get_cache(
+ key=key,
+ model_type=UserAPIKeyAuth,
)
-
- if cached_key_obj is not None:
- if isinstance(cached_key_obj, dict):
- return _copy_user_api_key_auth_for_cache(
- user_api_key_obj=UserAPIKeyAuth(**cached_key_obj)
- )
- elif isinstance(cached_key_obj, UserAPIKeyAuth):
- return _copy_user_api_key_auth_for_cache(user_api_key_obj=cached_key_obj)
+ if user_api_key_auth is not None:
- return user_api_key_auth
++ return _copy_user_api_key_auth_for_cache(user_api_key_obj=user_api_key_auth)
if check_cache_only:
raise Exception(
diff --cc litellm/proxy/auth/user_api_key_auth.py
index 2495d33a5cf,bfd1f2e0b3a..cdb4082a931
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@@ -1894,54 -1890,6 +1906,54 @@@ async def _noop_none() -> None
return None
+async def _reserve_budget_after_common_checks(
+ user_api_key_auth_obj: UserAPIKeyAuth,
+ request_data: dict,
+ route: str,
+ llm_router: Optional[Any],
+ team_object: Optional[LiteLLM_TeamTableCachedObj],
+ user_object: Optional[LiteLLM_UserTable],
+ prisma_client: Optional[PrismaClient],
- user_api_key_cache: DualCache,
++ user_api_key_cache: UserApiKeyCache,
+ proxy_logging_obj: ProxyLogging,
+ skip_budget_checks: bool,
+ end_user_id: Optional[str] = None,
+ end_user_object: Optional[LiteLLM_EndUserTable] = None,
+) -> None:
+ user_api_key_auth_obj.budget_reservation = None
+ if skip_budget_checks:
+ return
+
+ from litellm.proxy.spend_tracking.budget_reservation import (
+ reserve_budget_for_request,
+ )
+
+ user_api_key_auth_obj.budget_reservation = await reserve_budget_for_request(
+ request_body=request_data,
+ route=route,
+ llm_router=llm_router,
+ valid_token=user_api_key_auth_obj,
+ team_object=team_object,
+ user_object=user_object,
+ prisma_client=prisma_client,
+ user_api_key_cache=user_api_key_cache,
+ proxy_logging_obj=proxy_logging_obj,
+ end_user_id=end_user_id,
+ end_user_object=end_user_object,
+ )
+
+
+def _should_skip_budget_checks(
+ request_data: dict,
+ route: str,
+ llm_router: Optional[Any],
+) -> bool:
+ model = get_model_from_request(request_data, route)
+ if model is not None and llm_router is not None:
+ return _is_model_cost_zero(model=model, llm_router=llm_router)
+ return False
+
+
@tracer.wrap()
async def user_api_key_auth(
request: Request,