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
review: address atomic limiter review feedback
Krrish Dholakia6496e584175b
diff --git a/litellm/proxy/hooks/parallel_request_limiter_v3.py b/litellm/proxy/hooks/parallel_request_limiter_v3.pyindex 9de14f55c94..cd85843ef16 100644--- a/litellm/proxy/hooks/parallel_request_limiter_v3.py+++ b/litellm/proxy/hooks/parallel_request_limiter_v3.py@@ -86,19 +86,22 @@ CHECK_AND_INCREMENT_BY_N_SCRIPT = """-- All-or-nothing: if any descriptor would exceed its limit, no counter is-- modified.--+-- Uses Redis server time (`redis.call('TIME')`) instead of a client-supplied+-- timestamp so that window resets are deterministic across replicas with+-- skewed wall-clocks. This prevents a clock-skew-induced reopening of the+-- TOCTOU window across multi-replica deployments.+---- KEYS layout: pairs of (window_key, counter_key), one pair per descriptor.--- ARGV layout:--- ARGV[1] = now (unix seconds)--- ARGV[2] = window_size (seconds)--- For each descriptor i (1..N), starting at ARGV[3]:--- ARGV[3 + (i-1)*3 + 0] = limit--- ARGV[3 + (i-1)*3 + 1] = increment--- ARGV[3 + (i-1)*3 + 2] = ttl (counter TTL when window resets)+-- ARGV layout: per-descriptor 4-tuple, starting at ARGV[1]:+-- ARGV[(i-1)*4 + 1] = limit+-- ARGV[(i-1)*4 + 2] = increment+-- ARGV[(i-1)*4 + 3] = ttl_seconds (counter TTL when window resets)+-- ARGV[(i-1)*4 + 4] = window_size_seconds (sliding-window length)---- Return on success: { 0, new_counter_1, new_counter_2, ... }-- Return on over-limit: { 1, descriptor_index, current_counter, limit }-local now = tonumber(ARGV[1])-local window_size = tonumber(ARGV[2])+local time_reply = redis.call('TIME')+local now = tonumber(time_reply[1])local descriptor_count = #KEYS / 2-- Pass 1: read state, validate. Abort without writing if any over limit.@@ -106,9 +109,10 @@ local descriptor_state = {}for i = 1, descriptor_count dolocal window_key = KEYS[(i - 1) * 2 + 1]local counter_key = KEYS[(i - 1) * 2 + 2]- local arg_base = 3 + (i - 1) * 3+ local arg_base = (i - 1) * 4 + 1local limit = tonumber(ARGV[arg_base])local increment = tonumber(ARGV[arg_base + 1])+ local window_size = tonumber(ARGV[arg_base + 3])local window_start = redis.call('GET', window_key)local window_expired = (not window_start) or@@ -133,9 +137,10 @@ local results = { 0 }for i = 1, descriptor_count dolocal window_key = KEYS[(i - 1) * 2 + 1]local counter_key = KEYS[(i - 1) * 2 + 2]- local arg_base = 3 + (i - 1) * 3+ local arg_base = (i - 1) * 4 + 1local increment = tonumber(ARGV[arg_base + 1])local ttl = tonumber(ARGV[arg_base + 2])+ local window_size = tonumber(ARGV[arg_base + 3])local window_expired = descriptor_state[i][1]@@ -261,6 +266,16 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):# limiters) within this process to close the TOCTOU window between# read-only check and counter increment. Multi-replica deployments# additionally rely on Redis Lua atomicity for cross-process safety.+ #+ # Coarse granularity: this single lock serializes ALL atomic check++ # increment operations across batch and dynamic limiters on this+ # instance. A slow batch input-file fetch (which happens upstream of+ # the lock) does not block here, but Redis Lua latency does. If+ # contention shows up under load (visible as p99 latency spikes+ # correlated with batch traffic), shard to a per-descriptor-key lock+ # via a `weakref.WeakValueDictionary[str, asyncio.Lock]`. Punted as a+ # follow-up because Lua dominates wall-time and the lock is held for+ # one round-trip.self._check_and_increment_lock = asyncio.Lock()def _get_batch_rate_limiter(self) -> Optional[Any]:@@ -740,7 +755,18 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):descriptor_key, descriptor_value, rlt)keys.extend([window_key, counter_key])- script_args.extend([int(limit_value), inc_amount, int(window_size)])+ # Per-descriptor 4-tuple: limit, increment, ttl, window_size.+ # window_size is per-descriptor — descriptors may carry custom+ # windows distinct from self.window_size, and the Lua script+ # uses this slot to evaluate window expiry.+ script_args.extend(+ [+ int(limit_value),+ inc_amount,+ int(window_size),+ int(window_size),+ ]+ )per_counter_meta.append({"descriptor_key": descriptor_key,@@ -750,34 +776,44 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):"counter_key": counter_key,"increment": inc_amount,"ttl": int(window_size),+ "window_size": int(window_size),})if not keys:return RateLimitResponse(overall_code="OK", statuses=[])- current_time = self._get_current_time()- now_int = int(current_time.timestamp())-# Multi-process atomicity via Redis Lua. Single-process atomicity# falls back to the asyncio.Lock + in-memory sliding window below.+ # Note: in-memory state diverges from Redis state — if Lua fails+ # mid-write, retrying via in-memory may double-count. See fallback+ # warning below.if self.check_and_increment_by_n_script is not None:try:raw = await self.check_and_increment_by_n_script(keys=keys,- args=[now_int, self.window_size] + script_args,+ args=script_args,)return self._build_atomic_response(raw, per_counter_meta)except Exception as e:- verbose_proxy_logger.warning(- f"atomic_check_and_increment_by_n Lua failed, falling back "- f"to in-memory: {str(e)}"+ # Escalated from warning to error: Lua failures (script timeout,+ # Redis OOM, network partition) leave counter state ambiguous.+ # The fallback path below uses LOCAL DualCache, which is a+ # different store from Redis — counters here will diverge from+ # Redis until that key's window expires (TTL bounds divergence).+ # Operators should alert on this log line; sustained occurrences+ # indicate Redis health degradation that may erode rate-limit+ # accuracy.+ verbose_proxy_logger.error(+ f"atomic_check_and_increment_by_n: Redis Lua execution "+ f"failed ({type(e).__name__}: {e}). Falling back to "+ f"in-memory enforcement — counters will diverge from Redis "+ f"state until window expires (window_size={self.window_size}s).")async with self._check_and_increment_lock:return await self._atomic_check_and_increment_in_memory(per_counter_meta=per_counter_meta,- now_int=now_int,parent_otel_span=parent_otel_span,)@@ -826,21 +862,30 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):async def _atomic_check_and_increment_in_memory(self,per_counter_meta: List[Dict[str, Any]],- now_int: int,parent_otel_span: Optional[Span] = None,) -> RateLimitResponse:- """In-memory all-or-nothing check-and-increment. Caller holds lock."""+ """In-memory all-or-nothing check-and-increment. Caller holds lock.++ Reads/writes the LOCAL DualCache (`local_only=True`) — note this is+ a different store from Redis. When this fallback fires after a Lua+ failure, in-memory counters will diverge from Redis until each key's+ window expires (TTL bounds divergence).+ """+ # Use a single 'now' for the duration of this critical section so all+ # descriptors evaluate window expiry consistently.+ now_int = int(self._get_current_time().timestamp())+# Pass 1: read state, validate.descriptor_state: List[Dict[str, Any]] = []for meta in per_counter_meta:+ window_size = meta["window_size"]window_start = await self.internal_usage_cache.async_get_cache(key=meta["window_key"],litellm_parent_otel_span=parent_otel_span,local_only=True,)window_expired = (- window_start is None- or (now_int - int(window_start)) >= self.window_size+ window_start is None or (now_int - int(window_start)) >= window_size)current_counter = (0@@ -885,7 +930,7 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):await self.internal_usage_cache.async_set_cache(key=meta["window_key"],value=str(now_int),- ttl=self.window_size,+ ttl=meta["window_size"],litellm_parent_otel_span=parent_otel_span,local_only=True,)diff --git a/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py b/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.pyindex 076cb9d3f13..b8ced4e6615 100644--- a/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py+++ b/tests/test_litellm/proxy/hooks/test_rate_limiter_toctou.py@@ -344,3 +344,71 @@ async def test_dynamic_rate_limiter_v3_uses_atomic_check_and_increment():f"Expected model_saturation_check in atomic descriptor set. "f"Got: {atomic_descriptors_observed}")+++async def test_batch_zero_token_consumes_rpm_only():+ """+ Zero-token batch (e.g. metadata-only call) should still increment RPM+ counter but NOT TPM counter.++ Edge case from review: `if inc_amount <= 0: continue` in+ `atomic_check_and_increment_by_n` skips descriptor counters whose+ increment is zero. Verifies asymmetric quota consumption is intentional+ and observable: an RPM-bounded but TPM-free request path stays bounded+ by RPM alone.+ """+ dual_cache = DualCache()+ internal_usage_cache = InternalUsageCache(dual_cache=dual_cache)+ rate_limiter = _PROXY_MaxParallelRequestsHandler_v3(+ internal_usage_cache=internal_usage_cache+ )+ batch_limiter = rate_limiter._get_batch_rate_limiter()+ assert batch_limiter is not None++ user_api_key_dict = UserAPIKeyAuth(+ api_key=hash_token("zero-token-key"),+ tpm_limit=100,+ rpm_limit=3,+ )+ zero_batch = BatchFileUsage(total_tokens=0, request_count=1)++ # 3 zero-token batches must succeed (RPM=3 allows). 4th must hit RPM cap,+ # NOT TPM (because token counter never increments past 0).+ for i in range(3):+ await batch_limiter._check_and_increment_batch_counters(+ user_api_key_dict=user_api_key_dict,+ data={},+ batch_usage=zero_batch,+ )++ # Inspect counters: RPM key incremented to 3, TPM key absent (or 0).+ rpm_key = rate_limiter.create_rate_limit_keys(+ "api_key", user_api_key_dict.api_key or "", "requests"+ )+ tpm_key = rate_limiter.create_rate_limit_keys(+ "api_key", user_api_key_dict.api_key or "", "tokens"+ )+ rpm_val = await internal_usage_cache.async_get_cache(+ key=rpm_key, litellm_parent_otel_span=None, local_only=True+ )+ tpm_val = await internal_usage_cache.async_get_cache(+ key=tpm_key, litellm_parent_otel_span=None, local_only=True+ )+ assert int(rpm_val or 0) == 3, f"RPM counter must reach 3, got {rpm_val}"+ assert tpm_val in (+ None,+ 0,+ "0",+ ), f"TPM counter must remain unset/0 for zero-token batches, got {tpm_val}"++ # 4th attempt: RPM exhausted -> 429.+ from fastapi import HTTPException++ with pytest.raises(HTTPException) as exc:+ await batch_limiter._check_and_increment_batch_counters(+ user_api_key_dict=user_api_key_dict,+ data={},+ batch_usage=zero_batch,+ )+ assert exc.value.status_code == 429