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]

added more tests, fixed tests

shivam0117b35a6bc1
diff --git a/litellm/proxy/common_utils/reset_budget_job.py b/litellm/proxy/common_utils/reset_budget_job.py
index 4933e679d71..8ce73d29c84 100644
--- a/litellm/proxy/common_utils/reset_budget_job.py
+++ b/litellm/proxy/common_utils/reset_budget_job.py
@@ -83,7 +83,6 @@ class ResetBudgetJob:
reset_budget_for_litellm_keys() and are excluded here to avoid
double-resetting.
"""
- """
budget_ids = [
budget.budget_id
for budget in budgets_to_reset
@@ -617,4 +616,4 @@ class ResetBudgetJob:
await ResetBudgetJob._reset_budget_common(
item=key, current_time=current_time, item_type="key"
)
- return key
+ return key
\ No newline at end of file
diff --git a/tests/litellm_utils_tests/test_proxy_budget_reset.py b/tests/litellm_utils_tests/test_proxy_budget_reset.py
index 7cddde30421..34423a88da4 100644
--- a/tests/litellm_utils_tests/test_proxy_budget_reset.py
+++ b/tests/litellm_utils_tests/test_proxy_budget_reset.py
@@ -229,6 +229,10 @@ async def test_reset_budget_endusers_partial_failure():
prisma_client.get_data.side_effect = get_data_mock
prisma_client.update_data = AsyncMock()
+ # Mock db.litellm_verificationtoken.update_many (used by reset_budget_for_keys_linked_to_budgets)
+ prisma_client.db.litellm_verificationtoken.update_many = AsyncMock(
+ return_value={"count": 0}
+ )
proxy_logging_obj = MagicMock()
proxy_logging_obj.service_logging_obj = MagicMock()
@@ -389,6 +393,10 @@ async def test_reset_budget_continues_other_categories_on_failure():
prisma_client.get_data = AsyncMock(side_effect=fake_get_data)
prisma_client.update_data = AsyncMock()
+ # Mock db.litellm_verificationtoken.update_many (used by reset_budget_for_keys_linked_to_budgets)
+ prisma_client.db.litellm_verificationtoken.update_many = AsyncMock(
+ return_value={"count": 0}
+ )
proxy_logging_obj = MagicMock()
proxy_logging_obj.service_logging_obj = MagicMock()
@@ -863,6 +871,10 @@ async def test_service_logger_endusers_success():
prisma_client = MagicMock()
prisma_client.get_data = AsyncMock(side_effect=fake_get_data)
prisma_client.update_data = AsyncMock()
+ # Mock db.litellm_verificationtoken.update_many (used by reset_budget_for_keys_linked_to_budgets)
+ prisma_client.db.litellm_verificationtoken.update_many = AsyncMock(
+ return_value={"count": 0}
+ )
proxy_logging_obj = MagicMock()
proxy_logging_obj.service_logging_obj = MagicMock()
@@ -938,6 +950,10 @@ async def test_service_logger_endusers_failure():
prisma_client = MagicMock()
prisma_client.get_data = AsyncMock(side_effect=fake_get_data)
prisma_client.update_data = AsyncMock()
+ # Mock db.litellm_verificationtoken.update_many (used by reset_budget_for_keys_linked_to_budgets)
+ prisma_client.db.litellm_verificationtoken.update_many = AsyncMock(
+ return_value={"count": 0}
+ )
proxy_logging_obj = MagicMock()
proxy_logging_obj.service_logging_obj = MagicMock()
diff --git a/tests/test_litellm/proxy/common_utils/test_reset_budget_job.py b/tests/test_litellm/proxy/common_utils/test_reset_budget_job.py
index f63c77c1fc8..f975460836a 100644
--- a/tests/test_litellm/proxy/common_utils/test_reset_budget_job.py
+++ b/tests/test_litellm/proxy/common_utils/test_reset_budget_job.py
@@ -382,6 +382,49 @@ def test_reset_budget_for_keys_linked_to_budgets(reset_budget_job, mock_prisma_c
assert call["data"]["spend"] == 0
+def test_reset_budget_for_keys_linked_to_budgets_excludes_keys_with_own_budget_duration(
+ reset_budget_job, mock_prisma_client
+):
+ """
+ Test that keys with BOTH budget_id AND budget_duration are excluded from
+ reset_budget_for_keys_linked_to_budgets. Such keys have their own reset
+ schedule and are handled only by reset_budget_for_litellm_keys(). The
+ budget_duration=None filter ensures they are NOT double-reset when the
+ linked budget tier expires.
+ """
+ now = datetime.now(timezone.utc)
+
+ test_budget = type(
+ "LiteLLM_BudgetTableFull",
+ (),
+ {
+ "max_budget": 10.0,
+ "budget_duration": "7d",
+ "budget_reset_at": now - timedelta(hours=1),
+ "budget_id": "7d-budget-tier",
+ "created_at": now - timedelta(days=7),
+ },
+ )
+
+ budgets_to_reset = [test_budget]
+
+ asyncio.run(
+ reset_budget_job.reset_budget_for_keys_linked_to_budgets(
+ budgets_to_reset=budgets_to_reset
+ )
+ )
+
+ calls = mock_prisma_client.db.litellm_verificationtoken.update_many_calls
+ assert len(calls) == 1
+ call = calls[0]
+
+ # Critical: budget_duration must be None so keys with their own budget_duration
+ # (e.g. key has budget_id="X" AND budget_duration=60) are excluded.
+ # Those keys are reset only by reset_budget_for_litellm_keys() - no double-reset.
+ assert call["where"]["budget_duration"] is None
+ assert call["where"]["budget_id"] == {"in": ["7d-budget-tier"]}
+
+
def test_reset_budget_for_keys_linked_to_budgets_empty(
reset_budget_job, mock_prisma_client
):