mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
Some checks failed
CodSpeed Benchmarks / benchmarks (push) Waiting to run
Cost map pins recheck / rerun-cost-map-guard (push) Waiting to run
Publish lint base counts / publish (basedpyright, litellm/scripts/type_check_gate.py) (push) Waiting to run
LiteLLM Rust DB / db-queries (push) Waiting to run
Terraform Modules / fmt, validate, test (aws) (push) Waiting to run
Terraform Modules / fmt, validate, test (gcp) (push) Waiting to run
unit / llms-providers (push) Blocked by required conditions
unit / proxy-endpoints (push) Blocked by required conditions
unit / proxy-feature-endpoints (push) Blocked by required conditions
unit / auth-checks (push) Blocked by required conditions
unit / budgets (push) Blocked by required conditions
unit / custom-logging (push) Blocked by required conditions
unit / db-and-spend (push) Blocked by required conditions
unit / endpoints-and-responses (push) Blocked by required conditions
unit / enterprise-routing (push) Blocked by required conditions
unit / guardrails-hooks (push) Blocked by required conditions
unit / jwt-and-keys (push) Blocked by required conditions
unit / key-generation (push) Blocked by required conditions
unit / logging-misc (push) Blocked by required conditions
unit / mcp-oauth (push) Blocked by required conditions
unit / proxy-runtime (push) Blocked by required conditions
unit / proxy-server-core (push) Blocked by required conditions
unit / core-auth-spend (push) Blocked by required conditions
unit / core-logging-request-path (push) Blocked by required conditions
unit / rust-bridge (push) Waiting to run
unit / mcp-server (push) Blocked by required conditions
unit / proxy-auth (push) Blocked by required conditions
unit / proxy-extras (push) Blocked by required conditions
Publish lint base counts / publish (openapi-docs, litellm/scripts/openapi_docs_gate.py) (push) Waiting to run
Publish lint base counts / publish (ruff-strict, litellm/scripts/ruff_strict_gate.py) (push) Waiting to run
Publish lint base counts / publish (test-quality, litellm/scripts/test_quality_gate.py) (push) Waiting to run
Publish lint base counts / publish (type-discipline, litellm/scripts/type_discipline_gate.py) (push) Waiting to run
Root entry limit / root-entry-limit (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
lint / lint plan (push) Waiting to run
lint / python (push) Blocked by required conditions
lint / typecheck (push) Blocked by required conditions
lint / e2e-harness (push) Blocked by required conditions
lint / secret-scan (push) Waiting to run
lint / ui (push) Blocked by required conditions
lint / ui-api-types (push) Blocked by required conditions
lint / code-quality (push) Waiting to run
lint / ci-coverage (push) Waiting to run
lint / semgrep (push) Waiting to run
lint / lint passed (push) Blocked by required conditions
smoke / all-extras-py (push) Blocked by required conditions
smoke / smoke-rust-bridge (push) Waiting to run
smoke / dashboard-build (push) Waiting to run
smoke / py (push) Blocked by required conditions
smoke / windows-sdk (push) Waiting to run
smoke / proxy-build-from-pip (push) Waiting to run
smoke / smoke passed (push) Blocked by required conditions
unit / root (push) Blocked by required conditions
unit / proxy-utils (push) Blocked by required conditions
unit / core-auth-request (push) Blocked by required conditions
unit / core-management-cost (push) Blocked by required conditions
unit / core-utils-route (push) Blocked by required conditions
unit / core-router (push) Blocked by required conditions
unit / core-router-core (push) Blocked by required conditions
unit / core-router-retries (push) Blocked by required conditions
unit / changed-tests (push) Blocked by required conditions
unit / lint-runtime (push) Blocked by required conditions
unit / plan (push) Waiting to run
unit / enterprise-managed-files (push) Blocked by required conditions
unit / enterprise-repositories-secrets (push) Blocked by required conditions
unit / proxy-server (push) Blocked by required conditions
unit / lens-python-310 (push) Blocked by required conditions
unit / caching-local (push) Blocked by required conditions
unit / core-utils (push) Blocked by required conditions
unit / endpoints (push) Blocked by required conditions
unit / enterprise-package (push) Blocked by required conditions
unit / integrations (push) Blocked by required conditions
unit / llms-anthropic (push) Blocked by required conditions
unit / llms-bedrock (push) Blocked by required conditions
unit / llms-openai-meta (push) Blocked by required conditions
unit / llms-vertex-ai (push) Blocked by required conditions
unit / mcp-elicitation (push) Blocked by required conditions
unit / proxy-hooks-client (push) Blocked by required conditions
unit / proxy-infra-routing (push) Blocked by required conditions
unit / proxy-subprocess (push) Blocked by required conditions
unit / redis-py 7.4.1 compatibility (push) Blocked by required conditions
unit / redis-py 8.0.1 compatibility (push) Blocked by required conditions
GitHub Actions Security Analysis / zizmor (push) Waiting to run
unit / proxy-infra-db-spend (push) Blocked by required conditions
unit / proxy-infra-root-a-p (push) Blocked by required conditions
unit / proxy-infra-root-q-z (push) Blocked by required conditions
unit / responses-caching-types (push) Blocked by required conditions
unit / router (push) Blocked by required conditions
unit / rust-bridge-harness (push) Blocked by required conditions
unit / ui-unit (push) Waiting to run
unit / docs (push) Blocked by required conditions
unit / helm (push) Blocked by required conditions
unit / OpenAI 2.20.0 compatibility (push) Blocked by required conditions
unit / OpenAI 3.0.0 compatibility (push) Blocked by required conditions
unit / OpenAI 3.25.0 compatibility (push) Blocked by required conditions
unit / redis-py 5.3.1 compatibility (push) Blocked by required conditions
unit / redis-py 6.4.0 compatibility (push) Blocked by required conditions
unit / unit passed (push) Blocked by required conditions
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
* feat(cost): add the price, bound and rates query engine to litellm-cost price() answers a PriceQuery tagged by kind over a RowSource that Model Catalog will implement. The tokens kind ports the token path of completion_cost for Claude on Anthropic, Bedrock, Vertex AI and Azure AI, with speed, geo and regional uplifts, the web search fee, provider-keyed discount and margin, the stored cost breakdown and prompt-caching savings. Every other kind and unported case answers NotPorted. bound() ports the legacy budget hold and rates() returns a row's rates, its raw key facts and one pricing status for the auth predicates. Parity fixtures captured once from main's Python pin totals, breakdowns, savings and holds to the last bit. Copied main bugs are listed in the crate AGENTS.md. * feat(python-bridge): price, bound, rates and adjust over model-catalog catalog rows Model Catalog now feeds the litellm-cost query engine. pricing_rows reads a CostLookup and fetches the rows one query reads into PricingRows, the core's RowSource, each candidate keyed by the model name main priced it with, and a lowered row carries the RowFacts the core reads (mode, max tokens, how float() reads provider_specific_entry, the raw keys the auth checks read). PricingContext also holds the discount and margin config. The python-bridge snapshot reads the host's fallback_generalizations rules with the table and is rebuilt when the generation or the config values move. NativeModelCostMap gains price, bound, rates and adjust. Python hands each a document of raw facts and the bridge reads them the way main's Python did before it builds the core's typed query. A call the core does not price answers None so Python keeps pricing it. NativeTokenUsage now takes the core's TokenUsage document and bills it through TokenUsage::billed, so the counts to meters conversion exists once. * feat(cost): price Claude token calls, budget holds and auth price checks in Rust completion_cost prices a Claude token call on Anthropic, Bedrock, Vertex AI and Azure AI through NativeModelCostMap.price and stores the receipt's breakdown, rates and adjustments on the logging object as before. Any call Rust leaves to Python (another provider or call kind, built-in tools other than a reported web search, a provider-stated cost, custom per-call prices or a deferred candidate list) keeps the Python candidate loop for that call. The Anthropic speed and geo uplifts and the Bedrock geo uplift now come from the Rust core, and the Python bodies are gone. Discount and margin resolve in Rust for every kind through NativeModelCostMap.adjust, replacing _apply_cost_discount, _apply_cost_margin and resolve_cost_discount. The legacy budget hold calls bound, and the auth checks read token rate keys and positive token rates from rates. Tests that patched deleted internals now drive the public functions, and new cases pin the adapter to main's totals and breakdowns for the captured Claude fixtures. * refactor(cost): keep the lint gates flat for the Rust pricing adapter Split the Rust route's eligibility checks into early returns, read hidden params and web search options through typed mapping helpers, read the generalization rules through as_object_list, and sort the stub's __all__. Adjust and store the token and search costs of a completion_cost candidate in one helper so no Final is bound inside the candidate loop, type the Model Router fee dict, and merge the litellm_core_utils imports of the host module. * test(cost): match the unreadable geo value in the Bedrock ValueError check * test(cost): bill a price edited in place in litellm.model_cost on the next Rust-priced call The python-bridge row cache was keyed by row object identity, so editing a price in place kept billing the old rate until something called changed_in_place(). Lowerings are now reused only while the row reads the same fields. * docs(cost): say the RowSource is PricingRows over a catalog's PricingTable * fix(cost): port main's budget hold estimator changes into bound Main reworked the budget hold while this stack was open (#51752, #53412 and the per-model input counts). The rebase kept main's reservation, window and carry-over code and the Rust bound, so this commit brings the hold up to main The adapter reads the request: per model, the output caps that reach the provider through each serving deployment (surface, drop params, the Responses minimum, Gemini generationConfig caps) and the output count (n, best_of, candidateCount, router and deployment defaults), plus whether a Gemini body holds inline audio or names a cached content. A model whose input could not be counted is held at its input window without recounting bound reads the rows: a cached content holds the rest of the input window at the highest cache-read rate, text or audio, inline audio holds input at the higher of the text and audio rates, a counted Gemini prompt reads the `_above_<n>_tokens` prices it reaches, the threshold included, and a group reads those from each deployment's `deployment_pricing`. A row ceiling that is not positive reads the default, and a row missing an input or output rate holds nothing for tokens it would bill there max_cost_for_cost_info and input_cost_for_cost_info hold one model info row through bound, so main's tests run against the Rust hold unchanged apart from the names * refactor(cost): carry the RustRefusal rename into the pricing entry point --------- Co-authored-by: narmstrong-berri <narmstrong@berri.ai>
490 lines
19 KiB
Python
490 lines
19 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from types import SimpleNamespace
|
|
from typing import Final
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from litellm.redis.stores.auth_cache import AuthObject
|
|
from litellm.proxy._types import (
|
|
LiteLLM_BudgetTable,
|
|
LiteLLM_EndUserTable,
|
|
LiteLLM_OrganizationTable,
|
|
LiteLLM_TagTable,
|
|
UserAPIKeyAuth,
|
|
)
|
|
from litellm.proxy.common_utils.reset_budget_job import _model_access_group_counter_key
|
|
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
|
|
from litellm.proxy.spend_tracking.budget_reservation import (
|
|
_get_model_access_group_budget_counters,
|
|
estimate_request_max_cost,
|
|
get_budget_window_start,
|
|
release_budget_reservation_on_cancel,
|
|
reserve_budget_for_request,
|
|
)
|
|
from litellm.proxy.utils import ProxyLogging
|
|
from litellm.redis.stores.connection import connect
|
|
from litellm.redis.stores.spend_counters import SpendCounterStore, model_access_group_counter
|
|
from litellm.types.proxy.model_access_group_budget import ModelAccessGroupBudget
|
|
from tests.unit.proxy.rust_auth_stores import GroupBudget, RustAuthStores, install_rust_auth_stores
|
|
from tests.unit.proxy.spend_tracking.native_budgets_replica import install_native_owner
|
|
from tests.unit.redis.stores.spend_counters_double import FakeSpendCounters
|
|
|
|
|
|
@pytest.fixture()
|
|
def spend_counter_state():
|
|
import litellm.proxy.proxy_server as ps
|
|
|
|
original_counter_store = ps.spend_counter_store
|
|
original_key_cache = ps.user_api_key_cache
|
|
original_prisma_client = ps.prisma_client
|
|
|
|
counter_cache = FakeSpendCounters(redis=None)
|
|
key_cache = UserApiKeyCache()
|
|
ps.spend_counter_store = counter_cache.store()
|
|
ps.user_api_key_cache = key_cache
|
|
ps.prisma_client = None
|
|
|
|
try:
|
|
yield counter_cache, key_cache
|
|
finally:
|
|
ps.spend_counter_store = original_counter_store
|
|
ps.user_api_key_cache = original_key_cache
|
|
ps.prisma_client = original_prisma_client
|
|
|
|
|
|
def _request_body() -> dict:
|
|
return {
|
|
"model": "gpt-4o-mini",
|
|
"messages": [{"role": "user", "content": "hello"}],
|
|
"max_tokens": 10,
|
|
}
|
|
|
|
|
|
def test_should_not_serialize_budget_reservation_on_user_api_key_auth():
|
|
auth = UserAPIKeyAuth(
|
|
token="key-budget-runtime-state",
|
|
budget_reservation={
|
|
"reserved_cost": 0.5,
|
|
"entries": [{"counter_key": "spend:key:key-budget-runtime-state"}],
|
|
},
|
|
)
|
|
|
|
assert "budget_reservation" not in auth.model_dump()
|
|
assert "budget_reservation" not in auth.model_dump(exclude_none=True)
|
|
assert "budget_reservation" not in auth.model_dump_json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_seed_and_update_end_user_and_tag_counters_without_reservation(
|
|
spend_counter_state,
|
|
):
|
|
counter_cache, key_cache = spend_counter_state
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.end_user("customer-1"),
|
|
value=LiteLLM_EndUserTable(
|
|
user_id="customer-1",
|
|
blocked=False,
|
|
spend=4.0,
|
|
litellm_budget_table=LiteLLM_BudgetTable(max_budget=10.0),
|
|
).model_dump(),
|
|
)
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.tag("paid-tag"),
|
|
value=LiteLLM_TagTable(
|
|
tag_name="paid-tag",
|
|
spend=7.0,
|
|
).model_dump(),
|
|
)
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.tag("other-tag"),
|
|
value=LiteLLM_TagTable(
|
|
tag_name="other-tag",
|
|
spend=2.0,
|
|
).model_dump(),
|
|
)
|
|
|
|
from litellm.proxy.proxy_server import increment_spend_counters
|
|
|
|
await increment_spend_counters(
|
|
token=None,
|
|
team_id=None,
|
|
user_id=None,
|
|
response_cost=0.50,
|
|
end_user_id="customer-1",
|
|
tags=["paid-tag", "paid-tag", "other-tag", ""],
|
|
)
|
|
|
|
assert counter_cache.get_local("spend:end_user:customer-1") == pytest.approx(4.50)
|
|
assert counter_cache.get_local("spend:tag:paid-tag") == pytest.approx(7.50)
|
|
assert counter_cache.get_local("spend:tag:other-tag") == pytest.approx(2.50)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_seed_org_counter_from_with_budget_cache(spend_counter_state):
|
|
counter_cache, key_cache = spend_counter_state
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.org_with_budget("org-counter-with-budget"),
|
|
value=LiteLLM_OrganizationTable(
|
|
organization_id="org-counter-with-budget",
|
|
organization_alias="shared-org",
|
|
budget_id="org-budget-id",
|
|
spend=2.0,
|
|
models=[],
|
|
created_by="test",
|
|
updated_by="test",
|
|
litellm_budget_table=LiteLLM_BudgetTable(max_budget=10.0),
|
|
).model_dump(),
|
|
)
|
|
|
|
from litellm.proxy.proxy_server import increment_spend_counters
|
|
|
|
await increment_spend_counters(
|
|
token=None,
|
|
team_id=None,
|
|
user_id=None,
|
|
org_id="org-counter-with-budget",
|
|
response_cost=0.25,
|
|
)
|
|
|
|
assert counter_cache.get_local("spend:org:org-counter-with-budget") == pytest.approx(2.25)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_seed_org_counter_from_plain_org_cache(spend_counter_state):
|
|
counter_cache, key_cache = spend_counter_state
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.org("org-counter-plain"),
|
|
value=LiteLLM_OrganizationTable(
|
|
organization_id="org-counter-plain",
|
|
organization_alias="shared-org",
|
|
budget_id="org-budget-id",
|
|
spend=2.0,
|
|
models=[],
|
|
created_by="test",
|
|
updated_by="test",
|
|
).model_dump(),
|
|
)
|
|
|
|
from litellm.proxy.proxy_server import increment_spend_counters
|
|
|
|
await increment_spend_counters(
|
|
token=None,
|
|
team_id=None,
|
|
user_id=None,
|
|
org_id="org-counter-plain",
|
|
response_cost=0.25,
|
|
)
|
|
|
|
assert counter_cache.get_local("spend:org:org-counter-plain") == pytest.approx(2.25)
|
|
|
|
|
|
def test_tiered_reservation_is_all_or_nothing_with_output_tier_from_input_length():
|
|
"""Dashscope tiered pricing is all-or-nothing: the tier is chosen by the total
|
|
input tokens and every token (input and output) is billed at that tier's rate.
|
|
|
|
A long-context request with a large output allowance must reserve the output at
|
|
the input-selected tier, not at the cheapest tier picked from the output volume.
|
|
The earlier graduated calculation under-reserved such requests, letting a caller
|
|
slip past a depleted budget."""
|
|
tiered_pricing = [
|
|
{"range": [0, 32000], "input_cost_per_token": 1e-06, "output_cost_per_token": 2e-06},
|
|
{"range": [32000, 128000], "input_cost_per_token": 4e-06, "output_cost_per_token": 8e-06},
|
|
]
|
|
input_tokens = 100000 # falls entirely in the second tier
|
|
output_tokens = 1000
|
|
|
|
with (
|
|
patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_model_cost_info",
|
|
return_value={"tiered_pricing": tiered_pricing, "max_output_tokens": 200000},
|
|
),
|
|
):
|
|
estimated = estimate_request_max_cost(
|
|
request_body={**_request_body(), "max_tokens": output_tokens},
|
|
route="/chat/completions",
|
|
llm_router=None,
|
|
input_token_counts={"gpt-4o-mini": input_tokens},
|
|
)
|
|
|
|
expected = (input_tokens * 4e-06) + (output_tokens * 8e-06)
|
|
assert estimated == pytest.approx(expected)
|
|
|
|
# What the old graduated math (with the output tier taken from output volume)
|
|
# would have reserved. The all-or-nothing estimate must be strictly larger.
|
|
graduated_under_reserve = (32000 * 1e-06) + (68000 * 4e-06) + (output_tokens * 2e-06)
|
|
assert estimated > graduated_under_reserve
|
|
|
|
|
|
def test_tiered_reservation_uses_higher_reasoning_output_rate():
|
|
"""Some tiered models price reasoning output above standard output. The
|
|
reasoning-token share is unknown before the request runs, so reservation must
|
|
charge every output token at the higher of the two rates to avoid under-reserving
|
|
reasoning-heavy requests."""
|
|
tiered_pricing = [
|
|
{
|
|
"range": [0, 32000],
|
|
"input_cost_per_token": 1e-06,
|
|
"output_cost_per_token": 1.2e-06,
|
|
"output_cost_per_reasoning_token": 4e-06,
|
|
}
|
|
]
|
|
input_tokens = 1000
|
|
output_tokens = 500
|
|
|
|
with (
|
|
patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_model_cost_info",
|
|
return_value={"tiered_pricing": tiered_pricing, "max_output_tokens": 200000},
|
|
),
|
|
):
|
|
estimated = estimate_request_max_cost(
|
|
request_body={**_request_body(), "max_tokens": output_tokens},
|
|
route="/chat/completions",
|
|
llm_router=None,
|
|
input_token_counts={"gpt-4o-mini": input_tokens},
|
|
)
|
|
|
|
expected = (input_tokens * 1e-06) + (output_tokens * 4e-06)
|
|
assert estimated == pytest.approx(expected)
|
|
|
|
# Reserving output at the plain rate would under-reserve reasoning-heavy calls.
|
|
under_reserve = (input_tokens * 1e-06) + (output_tokens * 1.2e-06)
|
|
assert estimated > under_reserve
|
|
|
|
|
|
def test_flat_reservation_uses_higher_reasoning_output_rate():
|
|
"""The same reasoning under-reservation gap exists for flat-rate models that
|
|
declare output_cost_per_reasoning_token above output_cost_per_token."""
|
|
input_tokens = 1000
|
|
output_tokens = 500
|
|
|
|
with (
|
|
patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_model_cost_info",
|
|
return_value={
|
|
"input_cost_per_token": 1e-06,
|
|
"output_cost_per_token": 1.2e-06,
|
|
"output_cost_per_reasoning_token": 4e-06,
|
|
"max_output_tokens": 200000,
|
|
},
|
|
),
|
|
):
|
|
estimated = estimate_request_max_cost(
|
|
request_body={**_request_body(), "max_tokens": output_tokens},
|
|
route="/chat/completions",
|
|
llm_router=None,
|
|
input_token_counts={"gpt-4o-mini": input_tokens},
|
|
)
|
|
|
|
expected = (input_tokens * 1e-06) + (output_tokens * 4e-06)
|
|
assert estimated == pytest.approx(expected)
|
|
under_reserve = (input_tokens * 1e-06) + (output_tokens * 1.2e-06)
|
|
assert estimated > under_reserve
|
|
|
|
|
|
def test_reservation_uses_most_expensive_deployment_in_group():
|
|
"""When a model group mixes deployments with different tiered rates, reservation
|
|
must estimate against the most expensive one. Reserving the cheaper sibling would
|
|
let a caller repeatedly hit the alias and exceed the budget once routed to the
|
|
costlier deployment."""
|
|
cheap = [{"range": [0, 32000], "input_cost_per_token": 1e-06, "output_cost_per_token": 2e-06}]
|
|
expensive = [{"range": [0, 32000], "input_cost_per_token": 5e-06, "output_cost_per_token": 1e-05}]
|
|
input_tokens = 1000
|
|
output_tokens = 10
|
|
|
|
with (
|
|
patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_model_cost_info",
|
|
return_value={"max_output_tokens": 200000},
|
|
),
|
|
patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_deployment_tiered_pricing_tables",
|
|
return_value=[cheap, expensive],
|
|
),
|
|
):
|
|
estimated = estimate_request_max_cost(
|
|
request_body={**_request_body(), "max_tokens": output_tokens},
|
|
route="/chat/completions",
|
|
llm_router=None,
|
|
input_token_counts={"gpt-4o-mini": input_tokens},
|
|
)
|
|
|
|
expected_expensive = (input_tokens * 5e-06) + (output_tokens * 1e-05)
|
|
expected_cheap = (input_tokens * 1e-06) + (output_tokens * 2e-06)
|
|
assert expected_expensive > expected_cheap
|
|
assert estimated == pytest.approx(expected_expensive)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_skip_reservation_for_per_pixel_image_model(spend_counter_state, native_budgets_on, monkeypatch):
|
|
"""DALL-E 2-style per-pixel pricing depends on the requested ``size``,
|
|
which we don't decode here. Fall through to read-time enforcement
|
|
rather than guess."""
|
|
import litellm.proxy.proxy_server as ps
|
|
|
|
counter_cache, key_cache = spend_counter_state
|
|
monkeypatch.setattr(ps, "spend_counter_store", SpendCounterStore(connect(None).spend_counters()))
|
|
install_native_owner(ps.spend_counter_store)
|
|
proxy_logging_obj = ProxyLogging(user_api_key_cache=key_cache)
|
|
valid_token = UserAPIKeyAuth(
|
|
token="key-image-per-pixel",
|
|
spend=0.0,
|
|
max_budget=1.0,
|
|
)
|
|
await key_cache.async_set_cache(key=AuthObject.virtual_key("key-image-per-pixel"), value=valid_token)
|
|
|
|
request_body = {"model": "dall-e-2", "prompt": "a cat", "size": "256x256"}
|
|
|
|
with patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation._get_model_cost_info",
|
|
return_value={
|
|
"mode": "image_generation",
|
|
"input_cost_per_pixel": 2.4414e-07,
|
|
"output_cost_per_pixel": 0.0,
|
|
},
|
|
):
|
|
reservation = await reserve_budget_for_request(
|
|
request_body=request_body,
|
|
route="/v1/images/generations",
|
|
llm_router=None,
|
|
valid_token=valid_token,
|
|
team_object=None,
|
|
user_object=None,
|
|
prisma_client=None,
|
|
user_api_key_cache=key_cache,
|
|
proxy_logging_obj=proxy_logging_obj,
|
|
)
|
|
|
|
assert reservation is None
|
|
|
|
|
|
@pytest.mark.parametrize("microsecond", (0, 123456, 999999))
|
|
def test_should_start_window_without_reset_at_at_duration_boundary(microsecond: int) -> None:
|
|
now: Final = datetime(2026, 11, 15, 5, 48, 2, microsecond, tzinfo=timezone.utc)
|
|
with patch("litellm.proxy.spend_tracking.budget_reservation.datetime") as clock:
|
|
clock.now.return_value = now
|
|
window_start: Final = get_budget_window_start({"budget_duration": "1h"})
|
|
|
|
assert window_start == now.replace(microsecond=microsecond // 1000 * 1000) - timedelta(hours=1)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_budget_reservation_on_cancel_swallows_release_errors():
|
|
# If the release itself fails (e.g. Redis unavailable) it must not escape
|
|
# the helper: doing so would replace the in-flight CancelledError /
|
|
# GeneratorExit at the call site and disrupt the disconnect teardown.
|
|
reservation = {
|
|
"reserved_cost": 3.0,
|
|
"entries": [{"counter_key": "spend:key:key-cancel-error"}],
|
|
"finalized": False,
|
|
"input_cost": 0.5,
|
|
}
|
|
with patch(
|
|
"litellm.proxy.spend_tracking.budget_reservation.reconcile_budget_reservation",
|
|
new=AsyncMock(side_effect=RuntimeError("redis down")),
|
|
):
|
|
# must return without raising
|
|
await release_budget_reservation_on_cancel(reservation)
|
|
|
|
|
|
class _ExplodingPrismaClient:
|
|
"""Model access group budget rows come from the Rust store; a Prisma read is a bug."""
|
|
|
|
def __getattr__(self, name: str) -> object:
|
|
raise AssertionError(f"unexpected database access: {name}")
|
|
|
|
|
|
@pytest.fixture
|
|
def rust_auth_stores(monkeypatch: pytest.MonkeyPatch) -> RustAuthStores:
|
|
return install_rust_auth_stores(monkeypatch)
|
|
|
|
|
|
def _seed_model_access_group_budgets(stores: RustAuthStores, **max_budget_by_group: float | None) -> None:
|
|
"""``LiteLLM_ModelAccessGroupBudgetTable`` rows as the Rust model access group store serves them."""
|
|
stores.model_access_group.groups.update(
|
|
{group: GroupBudget(spend=7.0, max_budget=max_budget) for group, max_budget in max_budget_by_group.items()}
|
|
)
|
|
|
|
|
|
async def _model_access_group_counters(stores: RustAuthStores, matched, **max_budget_by_group):
|
|
_seed_model_access_group_budgets(stores, **max_budget_by_group)
|
|
return await _get_model_access_group_budget_counters(
|
|
valid_token=UserAPIKeyAuth(api_key="hashed", matched_model_access_groups=matched),
|
|
prisma_client=_ExplodingPrismaClient(),
|
|
user_api_key_cache=UserApiKeyCache(),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_access_group_with_a_budget_reserves_against_the_reset_jobs_counter_key(
|
|
rust_auth_stores: RustAuthStores,
|
|
):
|
|
counters = await _model_access_group_counters(rust_auth_stores, ["premium"], premium=25.0)
|
|
|
|
assert len(counters) == 1
|
|
counter = counters[0]
|
|
assert counter.counter_key == _model_access_group_counter_key(SimpleNamespace(access_group_name="premium"))
|
|
assert counter.source_cache_key == AuthObject.model_access_group("premium")
|
|
assert counter.max_budget == 25.0
|
|
assert counter.fallback_spend == 7.0
|
|
assert counter.entity_type == "Model access group"
|
|
assert counter.entity_id == "premium"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_access_group_without_a_budget_reserves_nothing(rust_auth_stores: RustAuthStores):
|
|
assert await _model_access_group_counters(rust_auth_stores, ["premium"], premium=None) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_access_group_with_a_zero_budget_reserves_nothing(rust_auth_stores: RustAuthStores):
|
|
"""Zero is how a budget is cleared, not a ceiling that blocks every request."""
|
|
assert await _model_access_group_counters(rust_auth_stores, ["premium"], premium=0.0) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_access_group_counters_come_from_the_auth_object(rust_auth_stores: RustAuthStores):
|
|
"""Auth already resolved which granted groups serve the model; re-deriving it here would drift."""
|
|
assert await _model_access_group_counters(rust_auth_stores, None, premium=25.0) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_repeated_model_access_group_reserves_once(rust_auth_stores: RustAuthStores):
|
|
counters = await _model_access_group_counters(rust_auth_stores, ["premium", "premium"], premium=25.0)
|
|
|
|
assert [counter.entity_id for counter in counters] == ["premium"]
|
|
|
|
|
|
async def _cache_model_access_group_budget(key_cache, group, spend, max_budget=None):
|
|
await key_cache.async_set_cache(
|
|
key=AuthObject.model_access_group(group),
|
|
value=ModelAccessGroupBudget(access_group_name=group, spend=spend, max_budget=max_budget),
|
|
model_type=ModelAccessGroupBudget,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_access_group_counter_accumulates_across_calls_without_a_reservation(spend_counter_state):
|
|
"""With reservations disabled nothing writes the counter up front, so the cost callback must.
|
|
|
|
Otherwise the read-time budget check enforces against the DB row's spend, which the cache
|
|
holds for the full TTL, and a caller runs past the ceiling for that whole window.
|
|
"""
|
|
counter_cache, key_cache = spend_counter_state
|
|
await _cache_model_access_group_budget(key_cache, "premium", spend=1.0, max_budget=25.0)
|
|
|
|
from litellm.proxy.proxy_server import increment_spend_counters
|
|
|
|
counter_key = model_access_group_counter("premium")
|
|
|
|
await increment_spend_counters(
|
|
token=None, team_id=None, user_id=None, response_cost=0.25, model_access_groups=["premium"]
|
|
)
|
|
assert counter_cache.get_local(counter_key) == pytest.approx(1.25)
|
|
|
|
await increment_spend_counters(
|
|
token=None, team_id=None, user_id=None, response_cost=0.75, model_access_groups=["premium", "premium", ""]
|
|
)
|
|
assert counter_cache.get_local(counter_key) == pytest.approx(2.0)
|
|
assert counter_cache.get_local(model_access_group_counter("")) is None
|