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]

coalesce malformed window reservation seeding

user719e891c3af3
diff --git a/litellm/proxy/spend_tracking/budget_reservation.py b/litellm/proxy/spend_tracking/budget_reservation.py
index a7413e23709..c23f444595c 100644
--- a/litellm/proxy/spend_tracking/budget_reservation.py
+++ b/litellm/proxy/spend_tracking/budget_reservation.py
@@ -584,6 +584,7 @@ async def _ensure_malformed_window_counter_initialized(
if counter.parent_counter_key is None:
return
+ from litellm.proxy.db.spend_counter_reseed import SpendCounterReseed
from litellm.proxy.proxy_server import (
_increment_spend_counter_cache,
get_current_spend,
@@ -594,14 +595,20 @@ async def _ensure_malformed_window_counter_initialized(
if current is not None:
return
- parent_spend = await get_current_spend(
- counter_key=counter.parent_counter_key,
- fallback_spend=counter.fallback_spend,
- )
- await _increment_spend_counter_cache(
- counter_key=counter.counter_key,
- increment=parent_spend,
- )
+ lock = await SpendCounterReseed._get_lock(counter.counter_key)
+ async with lock:
+ current = await spend_counter_cache.async_get_cache(key=counter.counter_key)
+ if current is not None:
+ return
+
+ parent_spend = await get_current_spend(
+ counter_key=counter.parent_counter_key,
+ fallback_spend=counter.fallback_spend,
+ )
+ await _increment_spend_counter_cache(
+ counter_key=counter.counter_key,
+ increment=parent_spend,
+ )
async def _set_reserved_entries_adjustment(
diff --git a/tests/test_litellm/proxy/test_budget_reservation.py b/tests/test_litellm/proxy/test_budget_reservation.py
index 9873dcdf727..7ed62788083 100644
--- a/tests/test_litellm/proxy/test_budget_reservation.py
+++ b/tests/test_litellm/proxy/test_budget_reservation.py
@@ -494,6 +494,82 @@ async def test_should_seed_malformed_window_counter_from_parent_authoritative_sp
) == pytest.approx(0.9)
+async def test_should_coalesce_malformed_window_counter_initialization(
+ spend_counter_state,
+):
+ import asyncio as _asyncio
+
+ counter_cache, key_cache = spend_counter_state
+ proxy_logging_obj = ProxyLogging(user_api_key_cache=key_cache)
+ token = "key-budget-malformed-window-concurrent"
+ malformed_window_counter_key = f"spend:key:{token}:window:not-a-duration"
+ valid_token = UserAPIKeyAuth(
+ token=token,
+ spend=0.0,
+ budget_limits=[
+ {
+ "budget_duration": "not-a-duration",
+ "max_budget": 1.0,
+ }
+ ],
+ )
+
+ import litellm.proxy.proxy_server as ps
+
+ db_call_count = 0
+
+ async def slow_find_unique(**kwargs):
+ nonlocal db_call_count
+ db_call_count += 1
+ await _asyncio.sleep(0.05)
+ db_row = MagicMock()
+ db_row.spend = 0.35
+ return db_row
+
+ prisma_client = MagicMock()
+ prisma_client.db.litellm_verificationtoken.find_unique = AsyncMock(
+ side_effect=slow_find_unique
+ )
+ ps.prisma_client = prisma_client
+
+ with patch(
+ "litellm.proxy.spend_tracking.budget_reservation.estimate_request_max_cost",
+ return_value=0.2,
+ ):
+ results = await _asyncio.gather(
+ *[
+ reserve_budget_for_request(
+ request_body=_request_body(),
+ route="/chat/completions",
+ llm_router=None,
+ valid_token=valid_token,
+ team_object=None,
+ user_object=None,
+ prisma_client=prisma_client,
+ user_api_key_cache=key_cache,
+ proxy_logging_obj=proxy_logging_obj,
+ )
+ for _ in range(2)
+ ],
+ return_exceptions=True,
+ )
+
+ assert not any(isinstance(result, Exception) for result in results), results
+ assert all(result is not None for result in results)
+ assert db_call_count == 1
+ assert counter_cache.in_memory_cache.get_cache(
+ key=malformed_window_counter_key
+ ) == pytest.approx(0.75)
+
+ for reservation in results:
+ await release_budget_reservation(reservation)
+
+ assert counter_cache.in_memory_cache.get_cache(
+ key=malformed_window_counter_key
+ ) == pytest.approx(0.35)
+
+
@pytest.mark.asyncio
async def test_should_not_re_read_uncapped_budget_after_reservation_fallback(
spend_counter_state,