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]

fix: as per request changes

Harshit Jainf33a7ca7d6db
diff --git a/litellm/proxy/common_utils/key_rotation_manager.py b/litellm/proxy/common_utils/key_rotation_manager.py
index 88fd08f6ab4..5a0a1fabc7d 100644
--- a/litellm/proxy/common_utils/key_rotation_manager.py
+++ b/litellm/proxy/common_utils/key_rotation_manager.py
@@ -112,9 +112,9 @@ class KeyRotationManager:
result = await self.prisma_client.db.litellm_deprecatedverificationtoken.delete_many(
where={"revoke_at": {"lt": now}}
)
- if result.get("count", 0) > 0:
+ if result > 0:
verbose_proxy_logger.debug(
- "Cleaned up %s expired deprecated key(s)", result["count"]
+ "Cleaned up %s expired deprecated key(s)", result
)
except Exception as e:
verbose_proxy_logger.debug(
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 528415c891d..4084284f7d0 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -76,6 +76,7 @@ from litellm import (
from litellm._logging import verbose_proxy_logger
from litellm._service_logger import ServiceLogging, ServiceTypes
from litellm.caching.caching import DualCache, RedisCache
+from litellm.caching.dual_cache import LimitedSizeOrderedDict
from litellm.exceptions import RejectedRequestError
from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.integrations.custom_logger import CustomLogger
@@ -2033,7 +2034,8 @@ def jsonify_object(data: dict) -> dict:
# In-memory cache for deprecated key lookups: maps old_token_hash -> (active_token_id, expires_at_ts)
# Avoids a DB query on every auth request for non-deprecated keys.
-_deprecated_key_cache: Dict[str, tuple] = {}
+# Bounded to prevent memory leaks from accumulated rotations.
+_deprecated_key_cache: LimitedSizeOrderedDict = LimitedSizeOrderedDict(max_size=1000)
_DEPRECATED_KEY_CACHE_TTL_SECONDS = 60
@@ -2073,6 +2075,11 @@ async def _lookup_deprecated_key(
now_ts + _DEPRECATED_KEY_CACHE_TTL_SECONDS,
)
return deprecated_row.active_token_id
+ # Cache negative result to avoid repeated DB lookups for invalid tokens
+ _deprecated_key_cache[hashed_token] = (
+ None,
+ now_ts + _DEPRECATED_KEY_CACHE_TTL_SECONDS,
+ )
except Exception as e:
verbose_proxy_logger.debug("Deprecated key lookup skipped: %s", e)
@@ -2414,6 +2421,7 @@ class PrismaClient:
parent_otel_span: Optional[Span] = None,
proxy_logging_obj: Optional[ProxyLogging] = None,
budget_id_list: Optional[List[str]] = None,
+ check_deprecated: bool = True,
):
args_passed_in = locals()
start_time = time.time()
@@ -2712,7 +2720,12 @@ class PrismaClient:
)
# If not found in main table, check deprecated keys (grace period)
- if response is None and hashed_token is not None:
+ # check_deprecated=False on the recursive call prevents unbounded chaining
+ if (
+ response is None
+ and hashed_token is not None
+ and check_deprecated
+ ):
active_token_id = await _lookup_deprecated_key(
db=self.db, hashed_token=hashed_token
)
@@ -2723,6 +2736,7 @@ class PrismaClient:
query_type="find_unique",
parent_otel_span=parent_otel_span,
proxy_logging_obj=proxy_logging_obj,
+ check_deprecated=False,
)
if response is not None:
verbose_proxy_logger.debug(
diff --git a/tests/test_litellm/proxy/common_utils/test_key_rotation_manager.py b/tests/test_litellm/proxy/common_utils/test_key_rotation_manager.py
index 002b015d960..24828cdff36 100644
--- a/tests/test_litellm/proxy/common_utils/test_key_rotation_manager.py
+++ b/tests/test_litellm/proxy/common_utils/test_key_rotation_manager.py
@@ -233,9 +233,9 @@ class TestKeyRotationManager:
Test that _cleanup_expired_deprecated_keys deletes expired deprecated keys.
"""
mock_prisma_client = AsyncMock()
- mock_prisma_client.db.litellm_deprecatedverificationtoken.delete_many.return_value = {
- "count": 3
- }
+ mock_prisma_client.db.litellm_deprecatedverificationtoken.delete_many.return_value = (
+ 3
+ )
manager = KeyRotationManager(mock_prisma_client)
await manager._cleanup_expired_deprecated_keys()