litellm/tests/integration/management/test_budget_updates.py
Ankit Jha 5f381ad472
fix(proxy): keep carried end-user spend when a budget reset is retried (#51143)
* fix(proxy): keep carried end-user spend when a budget reset is retried

End-user pages commit before the budget window advance. When the advance
failed, the tier stayed due and the next tick replayed every page, so a
rollover-capped row already cut to its carryover (15 -> 5) matched the
under-cap zero and lost it, and spend accrued since the first pass was
zeroed again. Each end-user reset now stamps the tier window it closes
in a new spend_reset_window column and skips rows already stamped with it.

Fixes #50898

* fix(proxy): leave never-reset tiers unguarded and test each tier's own window

A tier with no budget_reset_at falls back to the old unguarded reset
instead of stamping created_at, which an admin clearing the reset time
would reuse and silently skip end users. Tests now give tiers distinct
windows and cover a second window closing.

* test(proxy): give the end-user ledger its own fake prisma client

* fix(proxy): pin a reset time on tiers without one before end-user pages commit

* fix(proxy): read the reset-time pin from the writer and skip tiers another sweep advanced

* fix(proxy): keep linked rows of a tier another sweep advanced out of the rollover reset

* fix(proxy): order the end-user reset window migration after upstream's same-timestamp migration

* fix(proxy): reset end users again when an admin moves a tier's reset time back to a closed window

* fix(proxy): clear end-user reset stamps in the same transaction as the tier's reset time

A failed /budget/update used to leave the new reset time written with the
stamps still set, so repeating the request found the time unchanged and
skipped the clear. The update and the clear now commit together, and a
reset time in the future no longer scans the tier's end users

* fix(proxy): name each end-user reset window by the tier's last write

Stamp end-user resets with the tier's updated_at read on the writer instead
of its reset time. A failed window advance leaves updated_at as it was, so the
retry still skips the end users already reset, while the advance or any
/budget/update moves it and opens a new window. That covers a forced reset on
the default end-user tier and drops the stamp clear, so /budget/update is the
one-row update it is on main. The writer read also leaves out a tier another
sweep already advanced, which a lagging read replica still showed as due

* fix(proxy): name end-user reset windows by the tier and its advance count

Tiers advanced in one batch share updated_at, so a customer moved between
them could skip the new tier's reset, and any unrelated tier edit during a
failed advance reopened the window. A budget_reset_count that only the
window advance bumps, prefixed with the tier id, names the window instead.

* fix(proxy): keep carried spend for customer teams and members on a retried reset

Customer team and member pages commit before the window advance like end-user
pages, so they now stamp the window they close and a retry skips them too. A
member runs on its unexpired override when that has its own duration, else on
its template.

The window name now joins the tier id, its creation time, its count of closed
windows and its reset time, so a duration change or a tier recreated under the
same id opens a new window even when the advance failed.

* test(integration): list every budget column and relation in the budget update field check

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-14 01:03:19 +00:00

228 lines
10 KiB
Python

from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Final
import pytest
from pydantic import JsonValue, TypeAdapter
from tests.integration._support.client import Gateway, string_value
from tests.integration._support.database import read_rows
from tests.integration._support.process import owned_proxy
pytestmark: Final = pytest.mark.shares_worker_state
def _persisted_reset_at(budget_id: str) -> datetime:
rows: Final = read_rows(
'SELECT budget_reset_at::text AS reset_at FROM "LiteLLM_BudgetTable" WHERE budget_id = %s', (budget_id,)
)
assert len(rows) == 1, rows
reset_at: Final = datetime.fromisoformat(string_value(rows[0]["reset_at"]))
return reset_at if reset_at.tzinfo is not None else reset_at.replace(tzinfo=timezone.utc)
@pytest.mark.covers("mgmt.budget.update.duration_change_recomputes_reset_at")
def test_shortening_budget_duration_moves_reset_at_onto_the_new_schedule(gateway: Gateway) -> None:
with gateway.scenario() as scenario:
budget_id: Final = scenario.budget(max_budget=10.0, budget_duration="10d")
ten_day_reset_at: Final = _persisted_reset_at(budget_id)
before: Final = datetime.now(timezone.utc)
response: Final = gateway.request("POST", "/budget/update", {"budget_id": budget_id, "budget_duration": "1d"})
assert response.status_code == 200, response.text
updated: Final = _persisted_reset_at(budget_id)
assert updated < ten_day_reset_at, f"{updated} not before {ten_day_reset_at}"
assert before < updated <= before + timedelta(days=1, minutes=5), f"{updated} not within 1d of {before}"
BUDGET_COLUMNS: Final = (
"budget_id, name, max_budget, soft_budget, max_parallel_requests, tpm_limit, rpm_limit, tpd_limit, "
"model_max_budget, model_tpm_limit, model_rpm_limit, budget_duration, budget_limits, cap_policy, "
"budget_reset_count, allowed_models, temp_budget_increase, temp_budget_expiry, created_by, updated_by"
)
VOLATILE_INFO_FIELDS: Final = frozenset({"created_at", "updated_at", "budget_reset_at"})
UNLOADED_RELATIONS: Final[dict[str, JsonValue]] = dict.fromkeys(
(
"customer_organization_defaults",
"customer_organizations",
"customer_teams",
"customer_members",
"customer_member_overrides",
"customer_team_overrides",
"organization",
"projects",
"keys",
"end_users",
"tags",
"model_access_groups",
"team_membership",
"organization_membership",
)
)
def _budget_row(budget_id: str) -> dict[str, JsonValue]:
rows: Final = read_rows(
f'SELECT {BUDGET_COLUMNS}, budget_reset_at::text AS reset_at FROM "LiteLLM_BudgetTable" WHERE budget_id = %s',
(budget_id,),
)
assert len(rows) == 1, rows
return rows[0]
def _budget_info(gateway: Gateway, budget_id: str) -> dict[str, JsonValue]:
response: Final = gateway.request("POST", "/budget/info", {"budgets": [budget_id]})
assert response.status_code == 200, response.text
entries: Final = TypeAdapter(list[dict[str, JsonValue]]).validate_json(response.content)
assert len(entries) == 1, entries
return entries[0]
def _reset_at(value: JsonValue) -> datetime | None:
if value is None:
return None
parsed: Final = datetime.fromisoformat(string_value(value).replace("Z", "+00:00"))
return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=timezone.utc)
def _assert_budget(
gateway: Gateway,
budget_id: str,
expected: dict[str, JsonValue],
reset_after: datetime | None,
reset_within: timedelta,
) -> None:
row: Final = _budget_row(budget_id)
reset_at: Final = _reset_at(row.pop("reset_at"))
assert row == expected, row
info: Final = _budget_info(gateway, budget_id)
assert {name: value for name, value in info.items() if name not in VOLATILE_INFO_FIELDS} == {
**expected,
**UNLOADED_RELATIONS,
}, info
assert VOLATILE_INFO_FIELDS <= info.keys(), info
assert _reset_at(info["budget_reset_at"]) == reset_at, info
if reset_after is None:
assert reset_at is None, row
else:
assert reset_at is not None and reset_after < reset_at <= reset_after + reset_within, (reset_at, reset_after)
def _update(gateway: Gateway, body: dict[str, JsonValue]) -> None:
response: Final = gateway.request("POST", "/budget/update", body)
assert response.status_code == 200, response.text
def test_partial_update_keeps_sibling_budget_fields(gateway: Gateway) -> None:
model_budget: Final[dict[str, JsonValue]] = {"openai/gpt-4o-mini": {"max_budget": 1, "budget_duration": "1d"}}
with gateway.scenario() as scenario:
created_at: Final = datetime.now(timezone.utc)
budget_id: Final = scenario.budget(
max_budget=10.0,
soft_budget=5.0,
tpm_limit=1000,
rpm_limit=60,
tpd_limit=100000,
model_max_budget=model_budget,
budget_duration="30d",
)
creator: Final = string_value(_budget_row(budget_id)["created_by"])
expected: Final[dict[str, JsonValue]] = {
"budget_id": budget_id,
"name": None,
"max_budget": 10.0,
"soft_budget": 5.0,
"max_parallel_requests": None,
"tpm_limit": 1000,
"rpm_limit": 60,
"tpd_limit": 100000,
"model_max_budget": {"openai/gpt-4o-mini": {"max_budget": 1.0, "budget_duration": "1d"}},
"model_tpm_limit": None,
"model_rpm_limit": None,
"budget_duration": "30d",
"budget_limits": None,
"cap_policy": None,
"budget_reset_count": 0,
"allowed_models": [],
"temp_budget_increase": None,
"temp_budget_expiry": None,
"created_by": creator,
"updated_by": creator,
}
_assert_budget(gateway, budget_id, expected, created_at, timedelta(days=30, minutes=5))
before_partial: Final = datetime.now(timezone.utc)
_update(gateway, {"budget_id": budget_id, "max_budget": 20.0})
expected["max_budget"] = 20.0
_assert_budget(gateway, budget_id, expected, created_at, timedelta(days=30, minutes=5))
_update(
gateway,
{
"budget_id": budget_id,
"model_max_budget": {"openai/gpt-4o-mini": {"max_budget": 2, "budget_duration": "1d"}},
},
)
expected["model_max_budget"] = {"openai/gpt-4o-mini": {"max_budget": 2.0, "budget_duration": "1d"}}
_assert_budget(gateway, budget_id, expected, created_at, timedelta(days=30, minutes=5))
_update(gateway, {"budget_id": budget_id, "soft_budget": None})
expected["soft_budget"] = None
_assert_budget(gateway, budget_id, expected, created_at, timedelta(days=30, minutes=5))
before_hourly: Final = datetime.now(timezone.utc)
_update(gateway, {"budget_id": budget_id, "budget_duration": "1h"})
expected["budget_duration"] = "1h"
_assert_budget(gateway, budget_id, expected, before_hourly, timedelta(hours=1, minutes=5))
assert before_partial <= before_hourly
_update(gateway, {"budget_id": budget_id, "budget_duration": None})
expected["budget_duration"] = None
_assert_budget(gateway, budget_id, expected, None, timedelta())
pinned: Final = datetime(2030, 1, 1, tzinfo=timezone.utc)
_update(gateway, {"budget_id": budget_id, "budget_duration": None, "budget_reset_at": pinned.isoformat()})
_assert_budget(gateway, budget_id, expected, pinned - timedelta(microseconds=1), timedelta(microseconds=1))
rejected: Final = gateway.request(
"POST",
"/budget/update",
{"budget_id": budget_id, "model_max_budget": {"openai/gpt-4o-mini": {"max_budget": "lots"}}},
)
assert rejected.status_code == 422, rejected.text
assert rejected.json() == {
"detail": [
{
"type": "float_parsing",
"loc": ["body", "model_max_budget", "openai/gpt-4o-mini", "max_budget"],
"msg": "Input should be a valid number, unable to parse string as a number",
}
]
}, rejected.text
_assert_budget(gateway, budget_id, expected, pinned - timedelta(microseconds=1), timedelta(microseconds=1))
def test_model_max_budget_update_is_refused_by_the_handler_without_a_license(gateway: Gateway, tmp_path: Path) -> None:
model_budget: Final[dict[str, JsonValue]] = {"openai/gpt-4o-mini": {"max_budget": 1, "budget_duration": "1d"}}
with gateway.scenario() as scenario:
budget_id: Final = scenario.budget(max_budget=10.0, model_max_budget=model_budget)
before: Final = _budget_row(budget_id)
with owned_proxy(gateway, tmp_path, {}, remove_environment=("LITELLM_LICENSE",)) as unlicensed:
rejected: Final = unlicensed.request(
"POST",
"/budget/update",
{"budget_id": budget_id, "model_max_budget": {"openai/gpt-4o-mini": {"max_budget": 2}}},
)
assert rejected.status_code == 400, rejected.text
assert rejected.json() == {
"detail": {
"error": "Invalid model_max_budget: You must have an enterprise license to set model_max_budget. "
"You must be a LiteLLM Enterprise user to use this feature. If you have a license please set "
"`LITELLM_LICENSE` in your env. Get a 7 day trial key here: https://www.litellm.ai/enterprise#trial. "
"\nPricing: https://www.litellm.ai/#pricing. Example of valid model_max_budget: "
"https://docs.litellm.ai/docs/proxy/users"
}
}, rejected.text
assert _budget_row(budget_id) == before
served: Final = unlicensed.request("POST", "/budget/update", {"budget_id": budget_id, "max_budget": 20.0})
assert served.status_code == 200, served.text
assert _budget_row(budget_id) == {**before, "max_budget": 20.0}