From 6549e114ccada84ddbb16021ed141110675223d3 Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Fri, 13 Nov 2026 11:22:14 -0800 Subject: [PATCH] fix(proxy): /customer/new budgets reset on the same schedule as other budgets (#51689) Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../customer_endpoints.py | 7 ++-- .../management/test_customer_budget_reset.py | 36 +++++++++++++++++++ .../test_customer_budget.py | 32 +++++++---------- 3 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 tests/integration/management/test_customer_budget_reset.py diff --git a/litellm/proxy/management_endpoints/customer_endpoints.py b/litellm/proxy/management_endpoints/customer_endpoints.py index b5e2e7393e6..530f717ffe6 100644 --- a/litellm/proxy/management_endpoints/customer_endpoints.py +++ b/litellm/proxy/management_endpoints/customer_endpoints.py @@ -12,7 +12,6 @@ All /customer management endpoints #### END-USER/CUSTOMER MANAGEMENT #### from collections.abc import Mapping, Sequence from collections.abc import Set as AbstractSet -from datetime import datetime, timedelta from types import MappingProxyType from typing import TYPE_CHECKING, Annotated, Final, NamedTuple, Protocol, TypeVar, overload @@ -28,9 +27,9 @@ if TYPE_CHECKING: import litellm from litellm._logging import verbose_proxy_logger -from litellm.litellm_core_utils.duration_parser import duration_in_seconds from litellm.proxy._types import * from litellm.proxy.auth.user_api_key_auth import user_api_key_auth +from litellm.proxy.common_utils.timezone_utils import get_budget_reset_time from litellm.proxy.common_utils.user_api_key_cache import ( end_user_cache_key, end_user_restricted_registry_cache_key, @@ -288,9 +287,7 @@ def new_budget_request(data: NewCustomerRequest) -> BudgetNewRequest | None: budget_request: Final = BudgetNewRequest.model_validate(budget_kv_pairs) validate_budget_duration(budget_request.budget_duration) if budget_request.budget_reset_at is None and budget_request.budget_duration is not None: - budget_request.budget_reset_at = datetime.utcnow() + timedelta( - seconds=duration_in_seconds(duration=budget_request.budget_duration) - ) + budget_request.budget_reset_at = get_budget_reset_time(budget_duration=budget_request.budget_duration) return budget_request return None diff --git a/tests/integration/management/test_customer_budget_reset.py b/tests/integration/management/test_customer_budget_reset.py new file mode 100644 index 00000000000..aeaaebd6132 --- /dev/null +++ b/tests/integration/management/test_customer_budget_reset.py @@ -0,0 +1,36 @@ +import uuid +from datetime import datetime +from typing import Final + +import pytest + +from tests.integration._support.client import Gateway, Scenario, object_value + +pytestmark: Final = pytest.mark.shares_worker_state + + +def _budget_reset_at(scenario: Scenario, budget_duration: str) -> datetime: + budget_id: Final = f"integration-budget-{uuid.uuid4().hex}" + created: Final = scenario.gateway.post( + "/budget/new", {"budget_id": budget_id, "max_budget": 10, "budget_duration": budget_duration} + ) + scenario.cleanups.callback(scenario.gateway.post, "/budget/delete", {"id": budget_id}) + return datetime.fromisoformat(str(created["budget_reset_at"])) + + +def _customer_reset_at(scenario: Scenario, budget_duration: str) -> datetime: + customer: Final = f"integration-customer-{uuid.uuid4().hex}" + created: Final = scenario.gateway.post( + "/customer/new", {"user_id": customer, "max_budget": 10, "budget_duration": budget_duration} + ) + scenario.cleanups.callback(scenario.gateway.post, "/customer/delete", {"user_ids": [customer]}) + return datetime.fromisoformat(str(object_value(created["litellm_budget_table"])["budget_reset_at"])) + + +@pytest.mark.parametrize("budget_duration", ["12mo", "1mo", "30d", "7d", "1d"]) +def test_customer_new_budget_resets_on_the_same_schedule_as_budget_new(gateway: Gateway, budget_duration: str) -> None: + with gateway.scenario() as scenario: + earliest: Final = _budget_reset_at(scenario, budget_duration) + customer: Final = _customer_reset_at(scenario, budget_duration) + latest: Final = _budget_reset_at(scenario, budget_duration) + assert earliest <= customer <= latest diff --git a/tests/unit/proxy/management_endpoints/test_customer_budget.py b/tests/unit/proxy/management_endpoints/test_customer_budget.py index fe99e554ccf..d7311d33295 100644 --- a/tests/unit/proxy/management_endpoints/test_customer_budget.py +++ b/tests/unit/proxy/management_endpoints/test_customer_budget.py @@ -9,7 +9,6 @@ Tests customer update functionality related to budget management: - Budget initialization on customer creation """ -from datetime import datetime, timedelta import pytest from unittest.mock import AsyncMock, MagicMock, patch @@ -21,6 +20,7 @@ from litellm.proxy._types import ( UpdateCustomerRequest, ) from litellm.proxy.auth.user_api_key_auth import UserAPIKeyAuth +from litellm.proxy.common_utils.timezone_utils import get_budget_reset_time from litellm.proxy.management_endpoints.customer_endpoints import ( new_budget_request, update_end_user, @@ -345,29 +345,21 @@ async def test_update_customer_with_budget_id_and_creation_fields( assert update_data["budget_id"] == "new-budget-combo" # From created budget -def test_new_budget_request_sets_budget_reset_at_when_duration_provided(): +@pytest.mark.parametrize("budget_duration", ["12mo", "1mo", "30d", "7d", "1d"]) +def test_new_budget_request_resets_on_the_same_schedule_as_other_budgets(budget_duration: str): """ - Test that new_budget_request auto-populates budget_reset_at when - budget_duration is provided but budget_reset_at is not. - - Without this fix, budgets created via /customer/new with a budget_duration - but no budget_reset_at would have budget_reset_at=NULL in the DB, causing - the ResetBudgetJob to immediately pick them up and zero out enduser spend. + A budget created inline by /customer/new must get the same first reset as one + created by /budget/new, /key/generate, /team/new or /user/new, which all use + get_budget_reset_time. A NULL budget_reset_at would make ResetBudgetJob pick the + row up immediately and zero out the customer's spend. """ - data = NewCustomerRequest( - user_id="test-user", - max_budget=10.0, - budget_duration="30d", - ) + data = NewCustomerRequest(user_id="test-user", max_budget=10.0, budget_duration=budget_duration) - before = datetime.utcnow() + earliest = get_budget_reset_time(budget_duration=budget_duration) result = new_budget_request(data) - after = datetime.utcnow() + latest = get_budget_reset_time(budget_duration=budget_duration) assert result is not None + assert result.budget_duration == budget_duration assert result.budget_reset_at is not None - assert result.budget_duration == "30d" - - expected_min = before + timedelta(days=30) - expected_max = after + timedelta(days=30) - assert expected_min <= result.budget_reset_at <= expected_max + assert earliest <= result.budget_reset_at <= latest