litellm/tests/unit/proxy/spend_tracking/test_native_budgets.py
Nate Armstrong 2aa57d7e04
feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth (#51535)
* feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth

* fix(budgets): keep native_call_id out of the budget reservation like main
2026-11-15 16:03:51 -08:00

701 lines
28 KiB
Python

from __future__ import annotations
import gc
from collections.abc import Awaitable, Callable, Iterator, Mapping
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from types import MappingProxyType
from typing import Final, TypeAlias
import pytest
from fastapi import HTTPException
import litellm
from litellm.caching import DualCache
from litellm.models.budget import BudgetLimit, LiteLLM_BudgetTable
from litellm.models.tag import LiteLLM_TagTable
from litellm.models.team_membership import LiteLLM_TeamMembership
from litellm.proxy import proxy_server
from litellm.proxy._types import LiteLLM_TeamTable, LiteLLM_UserTable, ProxyException, UserAPIKeyAuth
from litellm.proxy.common_utils.user_api_key_cache import (
UserApiKeyCache,
model_access_group_cache_key,
project_cache_key,
tag_cache_key,
team_membership_reservation_cache_key,
)
from litellm.proxy.db.spend_counter_reseed import SpendCounterReseed
from litellm.proxy.spend_tracking import native_budgets
from litellm.proxy.spend_tracking.budget_reservation import (
customer_window_counters,
estimate_request_max_cost,
get_reserved_counter_keys,
reconcile_budget_reservation,
release_budget_reservation,
release_budget_reservation_on_cancel,
release_or_invalidate_budget_reservation,
release_unbound_budget_reservation,
reserve_budget_for_request,
)
from litellm.proxy.utils import ProxyLogging, hash_token
from litellm.redis.stores.connection import connect
from litellm.redis.stores.spend_counters import SpendCounterStore
from litellm.types.proxy.auth.customer_hierarchy import CustomerHierarchyAuth, CustomerScopeAuth, CustomerScopeKey
from litellm.types.proxy.model_access_group_budget import ModelAccessGroupBudget
from tests.unit.proxy.spend_tracking.native_budgets_golden.golden import NATIVE_BUDGETS, Golden
ROUTE: Final = "/chat/completions"
BODY: Final = {"model": "gpt-4o", "messages": [{"role": "user", "content": "hello"}], "max_tokens": 1000}
API_KEY: Final = "sk-native"
TOKEN: Final = hash_token(API_KEY)
ESTIMATE: Final = estimate_request_max_cost(request_body=BODY, route=ROUTE, llm_router=None, input_token_counts={})
KEY: Final = f"spend:key:{TOKEN}"
TEAM: Final = "spend:team:team-1"
USER: Final = "spend:user:user-1"
END_USER: Final = "spend:end_user:customer-1"
@dataclass(frozen=True, slots=True)
class Identity:
key_budget: float | None = 100.0
team_budget: float | None = None
user_budget: float | None = None
end_user_budget: float | None = None
throttle: bool = False
fail_closed: bool = False
spend: Mapping[str, float] = field(default_factory=dict)
def token(self) -> UserAPIKeyAuth:
return UserAPIKeyAuth(
api_key=API_KEY,
token=TOKEN,
max_budget=self.key_budget,
spend=0.0,
team_id="team-1" if self.team_budget is not None else None,
user_id="user-1",
end_user_id="customer-1" if self.end_user_budget is not None else None,
end_user_max_budget=self.end_user_budget,
tpm_limit=1000 if self.throttle else None,
metadata={"throttle_on_budget_exceeded": True} if self.throttle else {},
)
def team(self) -> LiteLLM_TeamTable | None:
return None if self.team_budget is None else LiteLLM_TeamTable(team_id="team-1", max_budget=self.team_budget)
def user(self) -> LiteLLM_UserTable | None:
return None if self.user_budget is None else LiteLLM_UserTable(user_id="user-1", max_budget=self.user_budget)
@pytest.fixture
def store(monkeypatch: pytest.MonkeyPatch) -> Iterator[SpendCounterStore]:
spend_counter_store: Final = SpendCounterStore(connect(None).spend_counters())
monkeypatch.setattr(proxy_server, "spend_counter_store", spend_counter_store)
monkeypatch.setattr(proxy_server, "prisma_client", None)
monkeypatch.setattr(litellm, "budget_exceeded_throttle_percentage", 0.5)
monkeypatch.setattr(litellm, "callbacks", [])
yield spend_counter_store
native_budgets._engine.clear() # pyright: ignore[reportPrivateUsage] # later tests build their own engine
def _seed(store: SpendCounterStore, identity: Identity) -> None:
for counter, value in identity.spend.items():
store.set_local(counter, value)
def _snapshot(store: SpendCounterStore) -> Mapping[str, float]:
return {key: round(float(value), 12) for key, value in store.snapshot().items()}
def _arguments(identity: Identity) -> Mapping[str, object]:
return {
"request_body": dict(BODY),
"route": ROUTE,
"llm_router": None,
"valid_token": identity.token(),
"team_object": identity.team(),
"user_object": identity.user(),
"prisma_client": None,
"user_api_key_cache": UserApiKeyCache(),
"proxy_logging_obj": ProxyLogging(user_api_key_cache=DualCache()),
"end_user_id": "customer-1" if identity.end_user_budget is not None else None,
"fail_closed_budget_enforcement": identity.fail_closed,
}
@dataclass(frozen=True, slots=True)
class Admitted:
reservation: dict[str, object] | None # mutable-ok: settlement stamps finalized on the shared reservation
refusal: Exception | None
counters: Mapping[str, float]
abort_status: int | None = None
def _install_native_engine() -> None:
native_budgets._engine.clear() # pyright: ignore[reportPrivateUsage] # each case builds its engine over its own store
_REFUSALS: Final = (litellm.BudgetExceededError, HTTPException, ProxyException)
async def _admitted(store: SpendCounterStore, arguments: Mapping[str, object]) -> Admitted:
try:
reservation: Final = await reserve_budget_for_request(**arguments) # pyright: ignore[reportArgumentType] # the argument tables are typed per key
except _REFUSALS as refusal:
return Admitted(
reservation=None,
refusal=refusal,
counters=_snapshot(store),
abort_status=getattr(refusal, "status_code", None),
)
return Admitted(
reservation=reservation,
refusal=None,
counters=_snapshot(store),
)
async def _native_admit(store: SpendCounterStore, identity: Identity) -> Admitted:
_install_native_engine()
_seed(store, identity)
return await _admitted(store, _arguments(identity))
def _refusal_fields(error: Exception | None) -> object:
if error is None:
return None
return (
type(error),
str(error),
{name: getattr(error, name, None) for name in ("current_cost", "max_budget", "entity_type", "entity_id")},
getattr(error, "status_code", None),
)
def _entries(reservation: Mapping[str, object] | None) -> object:
if reservation is None:
return None
return (
reservation["reserved_cost"],
reservation["entries"],
reservation["input_cost"],
reservation["input_tokens"],
)
assert ESTIMATE is not None and ESTIMATE > 0
CASES: Final = {
"key_and_team_fit": Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0}),
"team_shrinks_to_what_is_left": Identity(team_budget=10.0, spend={KEY: 1.0, TEAM: 10.0 - ESTIMATE / 3}),
"key_spent_refuses": Identity(key_budget=5.0, spend={KEY: 5.0}),
"team_at_max_refuses": Identity(team_budget=10.0, spend={KEY: 0.0, TEAM: 10.0}),
"user_on_a_personal_key": Identity(user_budget=50.0, spend={KEY: 1.0, USER: 3.0}),
"end_user_over_budget": Identity(end_user_budget=2.0, spend={KEY: 1.0, END_USER: 2.0}),
"throttled_key_releases_its_hold": Identity(key_budget=5.0, team_budget=100.0, throttle=True, spend={KEY: 5.0}),
"fail_closed_refuses_any_overshoot": Identity(
team_budget=10.0, fail_closed=True, spend={KEY: 0.0, TEAM: 10.0 - ESTIMATE / 3}
),
"no_budget_reserves_nothing": Identity(key_budget=None),
}
@pytest.mark.asyncio
@pytest.mark.parametrize("identity", CASES.values(), ids=CASES.keys())
async def test_native_admission_matches_python(store: SpendCounterStore, identity: Identity, golden: Golden) -> None:
native: Final = await _native_admit(store, identity)
recorded: Final = (_refusal_fields(native.refusal), native.counters, _entries(native.reservation))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
@pytest.mark.asyncio
async def test_a_runtime_change_to_the_budget_exceeded_status_code_reaches_the_next_refusal(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, golden: Golden
) -> None:
identity: Final = CASES["team_at_max_refuses"]
seen: Final[dict[int, tuple[object, int | None]]] = {} # mutable-ok: one entry per status code
for code in (402, 429):
monkeypatch.setattr(litellm, "budget_exceeded_status_code", code)
store.flush_local()
native = await _native_admit(store, identity)
seen[code] = (_refusal_fields(native.refusal), native.abort_status)
assert {code: status for code, (_, status) in seen.items()} == {402: 402, 429: 429}
assert seen[402][0] != seen[429][0]
refusals: Final = {code: refusal for code, (refusal, _) in seen.items()}
assert golden.canonical(refusals) == golden.recorded("python", refusals)
Settle: TypeAlias = Callable[[dict[str, object]], Awaitable[object]]
SETTLEMENTS: Final = {
"success_charges_the_actual_cost": lambda reservation: reconcile_budget_reservation(reservation, 0.002),
"failure_refunds_the_hold": release_or_invalidate_budget_reservation,
"cancel_keeps_the_input_cost": release_budget_reservation_on_cancel,
}
@pytest.mark.asyncio
@pytest.mark.parametrize("settle", SETTLEMENTS.values(), ids=SETTLEMENTS.keys())
async def test_native_settlement_matches_python(
store: SpendCounterStore,
settle: Settle,
golden: Golden,
) -> None:
identity: Final = Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0})
native: Final = await _native_admit(store, identity)
assert native.reservation is not None
await settle(native.reservation)
await settle(native.reservation)
settled: Final = _snapshot(store)
assert settled != native.counters
assert golden.canonical(settled) == golden.recorded("python", settled)
async def _db_logger_success(reservation: dict[str, object]) -> None: # mutable-ok: the shared reservation
_ = await reconcile_budget_reservation(reservation, 0.002, finalize=False, apply_consistent=False)
await proxy_server.increment_spend_counters(
token=TOKEN, team_id="team-1", user_id="user-1", response_cost=0.002, budget_reservation=reservation
)
EXITS: Final[Mapping[str, Settle]] = {
"db_logger_charges_the_row": _db_logger_success,
"db_logger_releases_an_uncharged_row": release_budget_reservation,
"db_logger_releases_after_a_failed_write": release_or_invalidate_budget_reservation,
"failure_hook_releases": release_or_invalidate_budget_reservation,
"client_disconnect_keeps_the_input_cost": release_budget_reservation_on_cancel,
"request_end_releases_an_unbound_hold": release_unbound_budget_reservation,
}
@pytest.mark.asyncio
@pytest.mark.parametrize("exit_path", EXITS.values(), ids=EXITS.keys())
async def test_every_exit_settles_the_native_hold_like_python(
store: SpendCounterStore, exit_path: Settle, golden: Golden
) -> None:
identity: Final = Identity(team_budget=100.0, user_budget=100.0, spend={KEY: 1.0, TEAM: 2.0, USER: 0.5})
native: Final = await _native_admit(store, identity)
assert native.reservation is not None
await exit_path(native.reservation)
recorded: Final = (_snapshot(store), native.reservation.get("finalized"))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
@pytest.mark.asyncio
async def test_a_stream_settles_after_its_request_state_is_gone(store: SpendCounterStore, golden: Golden) -> None:
identity: Final = Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0})
native: Final = await _native_admit(store, identity)
assert native.reservation is not None
reservation: Final = native.reservation
del native
gc.collect()
await _db_logger_success(reservation)
settled: Final = _snapshot(store)
assert golden.canonical(settled) == golden.recorded("python", settled)
DB_FLOOR: Final = 0.25
DISTURBANCES: Final[Mapping[str, float | None]] = {"counter_expired": None, "counter_would_go_negative": 1e-9}
@pytest.mark.asyncio
@pytest.mark.parametrize("disturbance", DISTURBANCES.values(), ids=DISTURBANCES.keys())
@pytest.mark.parametrize("exit_path", EXITS.values(), ids=EXITS.keys())
async def test_settling_a_counter_that_went_inconsistent_reseeds_it_like_python(
store: SpendCounterStore,
monkeypatch: pytest.MonkeyPatch,
disturbance: float | None,
exit_path: Settle,
golden: Golden,
) -> None:
async def reseed_from_db(counter_key: str) -> bool:
store.set_local(counter_key, DB_FLOOR)
return True
monkeypatch.setattr(proxy_server, "reseed_spend_counter_from_db", reseed_from_db)
identity: Final = Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0})
admitted: Final = await _native_admit(store, identity)
assert admitted.reservation is not None
if disturbance is None:
store.flush_local()
_seed(store, Identity(spend={TEAM: identity.spend[TEAM] + ESTIMATE}))
else:
store.set_local(KEY, disturbance)
await exit_path(admitted.reservation)
settled: Final = _snapshot(store)
assert golden.canonical(settled) == golden.recorded("python", settled)
@pytest.mark.asyncio
async def test_a_throttled_key_holds_only_its_other_counters_on_both_sides(
store: SpendCounterStore, golden: Golden
) -> None:
identity: Final = CASES["throttled_key_releases_its_hold"]
native: Final = await _native_admit(store, identity)
assert native.reservation is not None
held: Final = [entry["counter_key"] for entry in native.reservation["entries"]] # pyright: ignore[reportGeneralTypeIssues] # the entries list the reservation carries
assert held == [TEAM]
recorded: Final = (held, native.counters)
assert golden.canonical(recorded) == golden.recorded("python", recorded)
class _AnyDatabase:
pass
ORG: Final = "spend:org:org-1"
PROJECT: Final = "spend:project:project-1"
MAG: Final = "spend:model_access_group:mag-1"
MEMBER: Final = "spend:team_member:user-1:team-1"
TAG: Final = "spend:tag:tag-1"
KEY_WINDOW: Final = f"{KEY}:window:1d"
@dataclass(frozen=True, slots=True)
class Kind:
counter: str
entity_type: str
token: Mapping[str, object] = field(default_factory=dict)
cache: Mapping[str, object] = field(default_factory=dict)
body: Mapping[str, object] = field(default_factory=dict)
member: bool = False
def _budget(max_budget: float) -> LiteLLM_BudgetTable:
return LiteLLM_BudgetTable(max_budget=max_budget)
KIND_BUDGET: Final = 10.0
CUSTOMER_LIMITS: Final = (BudgetLimit(budget_duration="1mo", max_budget=KIND_BUDGET),)
def _customer_kind(scope: CustomerScopeKey, entity_id: str) -> Kind:
hierarchy: Final = CustomerHierarchyAuth(
customer_org_id="customer-org-1",
scopes=(CustomerScopeAuth(key=scope, entity_id=entity_id, budget_limits=CUSTOMER_LIMITS),),
)
return Kind(
customer_window_counters(scope, entity_id, CUSTOMER_LIMITS)[0].counter_key,
scope,
token={"customer_hierarchy": hierarchy},
)
KINDS: Final[Mapping[str, Kind]] = {
"organization": Kind(
ORG,
"Organization",
token={"org_id": "org-1"},
cache={"org_id:org-1:with_budget": {"litellm_budget_table": {"max_budget": KIND_BUDGET}, "spend": 0.0}},
),
"project": Kind(
PROJECT,
"Project",
token={"project_id": "project-1"},
cache={project_cache_key("project-1"): {"litellm_budget_table": {"max_budget": KIND_BUDGET}, "spend": 0.0}},
),
"model_access_group": Kind(
MAG,
"Model access group",
token={"matched_model_access_groups": ["mag-1"]},
cache={
model_access_group_cache_key("mag-1"): ModelAccessGroupBudget(
access_group_name="mag-1", max_budget=KIND_BUDGET
)
},
),
"team_member": Kind(
MEMBER,
"TeamMember",
cache={
team_membership_reservation_cache_key(user_id="user-1", team_id="team-1"): LiteLLM_TeamMembership(
user_id="user-1", team_id="team-1", litellm_budget_table=_budget(KIND_BUDGET)
)
},
member=True,
),
"tag": Kind(
TAG,
"Tag",
cache={tag_cache_key("tag-1"): LiteLLM_TagTable(tag_name="tag-1", litellm_budget_table=_budget(KIND_BUDGET))},
body={"metadata": {"tags": ["tag-1"]}},
),
"key_window": Kind(
KEY_WINDOW,
"Key",
token={"budget_limits": [{"budget_duration": "1d", "max_budget": KIND_BUDGET}]},
),
"customer_org": _customer_kind("customer_org", "customer-org-1"),
"customer_team": _customer_kind("customer_team", "customer-team-1"),
"customer_member": _customer_kind("customer_member", "member:1"),
}
CUSTOMER_WINDOW_RESET: Final = customer_window_counters("customer_member", "member:1", CUSTOMER_LIMITS)[0].reset_at
assert CUSTOMER_WINDOW_RESET is not None
WINDOW_COUNTERS: Final = MappingProxyType(
{
CUSTOMER_WINDOW_RESET.isoformat(): "<customer window reset>",
**{
KINDS[name].counter: f"<{name} window counter>"
for name in ("customer_org", "customer_team", "customer_member")
},
}
)
@pytest.fixture
def golden(request: pytest.FixtureRequest) -> Golden:
return Golden(NATIVE_BUDGETS, request.node.name, WINDOW_COUNTERS)
KIND_SPEND: Final[Mapping[str, float]] = {
"fits": 1.0,
"shrinks_to_what_is_left": KIND_BUDGET - ESTIMATE / 3,
"at_max_refuses": KIND_BUDGET,
}
async def _kind_admit(store: SpendCounterStore, kind: Kind, spend: float) -> Admitted:
cache: Final = UserApiKeyCache()
for key, value in kind.cache.items():
await cache.async_set_cache(key=key, value=value)
token: Final = UserAPIKeyAuth(
api_key=API_KEY,
token=TOKEN,
max_budget=100.0,
spend=0.0,
user_id="user-1",
team_id="team-1" if kind.member else None,
**kind.token, # pyright: ignore[reportArgumentType] # each kind sets the identity fields it reads
)
arguments: Final = {
"request_body": {**BODY, **kind.body},
"route": ROUTE,
"llm_router": None,
"valid_token": token,
"team_object": LiteLLM_TeamTable(team_id="team-1") if kind.member else None,
"user_object": LiteLLM_UserTable(user_id="user-1") if kind.member else None,
"prisma_client": _AnyDatabase(),
"user_api_key_cache": cache,
"proxy_logging_obj": ProxyLogging(user_api_key_cache=DualCache()),
}
store.flush_local()
_install_native_engine()
store.set_local(KEY, 1.0)
store.set_local(kind.counter, spend)
return await _admitted(store, arguments)
@pytest.mark.asyncio
@pytest.mark.parametrize("spend", KIND_SPEND.values(), ids=KIND_SPEND.keys())
@pytest.mark.parametrize("kind", KINDS.values(), ids=KINDS.keys())
async def test_every_counter_kind_admits_like_python(
store: SpendCounterStore, kind: Kind, spend: float, golden: Golden
) -> None:
native: Final = await _kind_admit(store, kind, spend)
held: Final = (
[] if native.reservation is None else [entry["entity_type"] for entry in native.reservation["entries"]] # pyright: ignore[reportGeneralTypeIssues] # the entries list the reservation carries
)
assert native.refusal is not None or held.count(kind.entity_type) >= 1, "the run must reserve this kind"
recorded: Final = (_refusal_fields(native.refusal), native.counters, _entries(native.reservation))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
WINDOW_RESETS: Final[Mapping[str, timedelta]] = {
"window_reset_before_settlement": -timedelta(minutes=5),
"window_still_open": timedelta(days=1),
}
CUSTOMER_KIND: Final = KINDS["customer_member"]
async def _settled_after_reset(
store: SpendCounterStore, reset: str, exit_path: Settle, expire: bool
) -> tuple[Mapping[str, float], object]:
admitted: Final = await _kind_admit(store, CUSTOMER_KIND, 1.0)
reservation: Final = admitted.reservation
assert reservation is not None
entries: Final = reservation["entries"]
assert isinstance(entries, list)
for entry in entries:
if entry["counter_key"] == CUSTOMER_KIND.counter:
entry["window_reset_at"] = reset
if expire:
store.flush_local()
store.set_local(KEY, 1.0 + float(reservation["reserved_cost"])) # pyright: ignore[reportArgumentType] # the reserved cost the reservation holds
await exit_path(reservation)
return _snapshot(store), reservation["entries"]
@pytest.mark.asyncio
@pytest.mark.parametrize("reset", WINDOW_RESETS.values(), ids=WINDOW_RESETS.keys())
@pytest.mark.parametrize("exit_path", EXITS.values(), ids=EXITS.keys())
async def test_a_customer_window_that_reset_before_settlement_settles_like_python(
store: SpendCounterStore, reset: timedelta, exit_path: Settle, golden: Golden
) -> None:
reset_at: Final = (datetime.now(timezone.utc) + reset).isoformat()
native: Final = await _settled_after_reset(store, reset_at, exit_path, expire=False)
at_reset: Final = golden.replacing({reset_at: "<reset_at>"})
assert at_reset.canonical(native) == at_reset.recorded("python", native)
@pytest.mark.asyncio
@pytest.mark.parametrize("reset", WINDOW_RESETS.values(), ids=WINDOW_RESETS.keys())
async def test_an_expired_customer_window_is_reseeded_with_the_cost_its_window_settles_like_python(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, reset: timedelta, golden: Golden
) -> None:
reseeded: Final[list[str]] = [] # mutable-ok: the counters each side reseeds
async def reseed_from_db(counter_key: str) -> bool:
reseeded.append(counter_key)
store.set_local(counter_key, DB_FLOOR)
return True
monkeypatch.setattr(proxy_server, "reseed_spend_counter_from_db", reseed_from_db)
settle: Final = SETTLEMENTS["success_charges_the_actual_cost"]
reset_at: Final = (datetime.now(timezone.utc) + reset).isoformat()
native: Final = await _settled_after_reset(store, reset_at, settle, expire=True)
assert tuple(reseeded) == (CUSTOMER_KIND.counter,)
recorded: Final = (tuple(reseeded), native)
at_reset: Final = golden.replacing({reset_at: "<reset_at>"})
assert at_reset.canonical(recorded) == at_reset.recorded("python", recorded)
@dataclass(slots=True)
class _SpendReads:
db_spend: float | Exception
db_reads: int = 0
current_spend_calls: int = 0
def _count_spend_reads(monkeypatch: pytest.MonkeyPatch, db_spend: float | Exception) -> _SpendReads:
reads: Final = _SpendReads(db_spend=db_spend)
current_spend: Final = proxy_server.get_current_spend
async def from_db(prisma_client: object, counter_key: str) -> float | None:
reads.db_reads += 1
if isinstance(reads.db_spend, Exception):
raise reads.db_spend
return reads.db_spend
async def counted_current_spend(counter_key: str, fallback_spend: float, **kwargs: object) -> float:
reads.current_spend_calls += 1
return await current_spend(counter_key, fallback_spend, **kwargs) # pyright: ignore[reportArgumentType] # passes the keywords the caller gave
async def cold(counter_key: str, source_cache_key: str) -> None:
return None
monkeypatch.setattr(SpendCounterReseed, "from_db", staticmethod(from_db))
monkeypatch.setattr(proxy_server, "get_current_spend", counted_current_spend)
monkeypatch.setattr(proxy_server, "ensure_spend_counter_initialized", cold)
return reads
def _reset_reads(reads: _SpendReads) -> None:
reads.db_reads = 0
reads.current_spend_calls = 0
@pytest.mark.asyncio
async def test_a_warm_counter_is_read_through_python_without_a_db_read(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, golden: Golden
) -> None:
reads: Final = _count_spend_reads(monkeypatch, 0.0)
identity: Final = Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0})
native: Final = await _native_admit(store, identity)
assert (reads.current_spend_calls, reads.db_reads) == (2, 0)
recorded: Final = (native.counters, _entries(native.reservation))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
COLD_SPEND: Final[Mapping[str, float]] = {"room_left": 1.0, "reseeded_spend_refuses": 100.0}
@pytest.mark.asyncio
@pytest.mark.parametrize("db_spend", COLD_SPEND.values(), ids=COLD_SPEND.keys())
async def test_a_cold_counter_is_reseeded_through_python_like_python(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, db_spend: float, golden: Golden
) -> None:
reads: Final = _count_spend_reads(monkeypatch, db_spend)
identity: Final = Identity(team_budget=100.0, spend={TEAM: 2.0})
native: Final = await _native_admit(store, identity)
assert (reads.current_spend_calls, reads.db_reads) == (2, 1)
recorded: Final = (_refusal_fields(native.refusal), native.counters, _entries(native.reservation))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
@pytest.mark.asyncio
async def test_a_reseed_that_raises_fails_the_admission_like_python(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, golden: Golden
) -> None:
_ = _count_spend_reads(monkeypatch, ConnectionError("database unreachable"))
identity: Final = Identity(team_budget=100.0, spend={TEAM: 2.0})
with pytest.raises(ConnectionError) as native:
_ = await _native_admit(store, identity)
recorded: Final = (str(native.value), _snapshot(store))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
@pytest.mark.asyncio
async def test_python_reserves_again_once_the_native_owner_leaves_the_callbacks(
store: SpendCounterStore, monkeypatch: pytest.MonkeyPatch, golden: Golden
) -> None:
identity: Final = Identity(team_budget=100.0, spend={KEY: 1.0, TEAM: 2.0})
_install_native_engine()
monkeypatch.setattr(litellm, "callbacks", [])
_seed(store, identity)
reservation: Final = await reserve_budget_for_request(**_arguments(identity)) # pyright: ignore[reportArgumentType] # the shared argument table is typed per key above
recorded: Final = (_entries(reservation), _snapshot(store))
assert golden.canonical(recorded) == golden.recorded("python", recorded)
MALFORMED_COSTS: Final[Mapping[str, tuple[object, type[Exception]]]] = {
"unparseable_string": ("not-a-number", ValueError),
"list": ([1.0], TypeError),
"mapping": ({"cost": 1.0}, TypeError),
}
@pytest.mark.asyncio
@pytest.mark.parametrize("cost", MALFORMED_COSTS.values(), ids=MALFORMED_COSTS.keys())
async def test_a_malformed_reserved_cost_fails_settlement_as_main_does(cost: tuple[object, type[Exception]]) -> None:
value, error = cost
with pytest.raises(error):
_ = await reconcile_budget_reservation({"reserved_cost": value, "entries": []}, 0.0)
@pytest.mark.asyncio
@pytest.mark.parametrize("cost", MALFORMED_COSTS.values(), ids=MALFORMED_COSTS.keys())
async def test_a_malformed_input_cost_settles_the_cancel_release_at_zero_as_main_does(
cost: tuple[object, type[Exception]],
) -> None:
value, _ = cost
reservation: Final[dict[str, object]] = {"input_cost": value, "entries": []}
await release_budget_reservation_on_cancel(reservation)
assert reservation["finalized"] is True
MALFORMED_ENTRIES: Final[Mapping[str, tuple[object, frozenset[object]]]] = {
"a_mapping_of_entries": ({"counter_key": "spend:key:a"}, frozenset()),
"a_tuple_of_entries": (({"counter_key": "spend:key:a"},), frozenset({"spend:key:a"})),
"a_non_string_counter_key": ([{"counter_key": 5}], frozenset({5})),
"a_non_mapping_entry": (["spend:key:a", {"counter_key": "spend:key:b"}], frozenset({"spend:key:b"})),
}
@pytest.mark.parametrize("entries", MALFORMED_ENTRIES.values(), ids=MALFORMED_ENTRIES.keys())
def test_reserved_counter_keys_read_malformed_entries_as_main_does(entries: tuple[object, frozenset[object]]) -> None:
value, keys = entries
assert get_reserved_counter_keys({"entries": value}) == keys