mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-05 01:59:27 +00:00
* test(cost): verify basic provider pricing from returned usage * test(cost): distinguish catalog prices from runtime prices in harness * test(cost): reuse existing translation calls for default pricing * test(cost): escape literal model name in billing assertion check * test(cost): check observed prices without forcing a bundled map * test(cost): isolate failing resource setup assertion * test(cost): cover unavailable price observation boundaries * test(cost): fail bills naming the wrong Azure model family * test(cost): retry failed shared calls during fixture setup --------- Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
122 lines
5.5 KiB
Python
122 lines
5.5 KiB
Python
from __future__ import annotations
|
|
|
|
from math import isclose
|
|
from typing import Final
|
|
|
|
import pytest
|
|
from models import (
|
|
CompletionTokensDetails,
|
|
CostMapEntry,
|
|
PromptTokensDetails,
|
|
Usage,
|
|
default_token_charge,
|
|
served_snapshot_key,
|
|
uncached_token_charge,
|
|
)
|
|
|
|
INPUT_RATE: Final = 2e-06
|
|
OUTPUT_RATE: Final = 7e-06
|
|
REASONING_RATE: Final = 11e-06
|
|
|
|
|
|
def _charge(usage: Usage) -> tuple[float, float]:
|
|
charge: Final = uncached_token_charge(
|
|
usage, input_rate=INPUT_RATE, output_rate=OUTPUT_RATE, reasoning_rate=REASONING_RATE
|
|
)
|
|
assert isclose(charge.total, charge.input_cost + charge.output_cost)
|
|
return charge.input_cost, charge.output_cost
|
|
|
|
|
|
class TestUncachedTokenCharge:
|
|
def test_prices_prompt_and_completion_at_their_own_rates(self) -> None:
|
|
input_cost, output_cost = _charge(Usage(prompt_tokens=30, completion_tokens=5))
|
|
assert isclose(input_cost, 30 * INPUT_RATE)
|
|
assert isclose(output_cost, 5 * OUTPUT_RATE)
|
|
|
|
def test_prices_reasoning_inside_completion_at_the_reasoning_rate(self) -> None:
|
|
usage: Final = Usage(
|
|
prompt_tokens=30, completion_tokens=9, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=4)
|
|
)
|
|
input_cost, output_cost = _charge(usage)
|
|
assert isclose(input_cost, 30 * INPUT_RATE)
|
|
assert isclose(output_cost, 5 * OUTPUT_RATE + 4 * REASONING_RATE)
|
|
|
|
@pytest.mark.parametrize(
|
|
"usage",
|
|
[
|
|
Usage(prompt_tokens=30, completion_tokens=5, cache_read_input_tokens=1),
|
|
Usage(prompt_tokens=30, completion_tokens=5, cache_creation_input_tokens=1),
|
|
Usage(prompt_tokens=30, completion_tokens=5, prompt_tokens_details=PromptTokensDetails(cached_tokens=1)),
|
|
Usage(
|
|
prompt_tokens=30, completion_tokens=5, prompt_tokens_details=PromptTokensDetails(cache_write_tokens=1)
|
|
),
|
|
],
|
|
ids=["cache-read", "cache-creation", "cached-tokens-detail", "cache-write-detail"],
|
|
)
|
|
def test_refuses_usage_with_cache_tokens(self, usage: Usage) -> None:
|
|
with pytest.raises(AssertionError, match="cache tokens"):
|
|
uncached_token_charge(usage, input_rate=INPUT_RATE, output_rate=OUTPUT_RATE, reasoning_rate=REASONING_RATE)
|
|
|
|
@pytest.mark.parametrize(
|
|
"usage",
|
|
[Usage(prompt_tokens=0, completion_tokens=5), Usage(prompt_tokens=30, completion_tokens=None)],
|
|
ids=["no-prompt", "no-completion"],
|
|
)
|
|
def test_refuses_usage_without_billable_tokens(self, usage: Usage) -> None:
|
|
with pytest.raises(AssertionError, match="no billable"):
|
|
uncached_token_charge(usage, input_rate=INPUT_RATE, output_rate=OUTPUT_RATE, reasoning_rate=REASONING_RATE)
|
|
|
|
def test_refuses_reasoning_beyond_completion(self) -> None:
|
|
usage: Final = Usage(
|
|
prompt_tokens=30, completion_tokens=3, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=4)
|
|
)
|
|
with pytest.raises(AssertionError, match="inside completion_tokens"):
|
|
uncached_token_charge(usage, input_rate=INPUT_RATE, output_rate=OUTPUT_RATE, reasoning_rate=REASONING_RATE)
|
|
|
|
|
|
REASONING_USAGE: Final = Usage(
|
|
prompt_tokens=30, completion_tokens=9, completion_tokens_details=CompletionTokensDetails(reasoning_tokens=4)
|
|
)
|
|
|
|
|
|
class TestDefaultTokenCharge:
|
|
def test_a_zero_reasoning_price_bills_reasoning_at_zero_not_the_output_rate(self) -> None:
|
|
rates: Final = CostMapEntry(
|
|
input_cost_per_token=INPUT_RATE, output_cost_per_token=OUTPUT_RATE, output_cost_per_reasoning_token=0.0
|
|
)
|
|
assert isclose(default_token_charge(REASONING_USAGE, rates).output_cost, 5 * OUTPUT_RATE)
|
|
|
|
def test_a_row_without_a_reasoning_price_bills_reasoning_at_the_output_rate(self) -> None:
|
|
rates: Final = CostMapEntry(input_cost_per_token=INPUT_RATE, output_cost_per_token=OUTPUT_RATE)
|
|
assert isclose(default_token_charge(REASONING_USAGE, rates).output_cost, 9 * OUTPUT_RATE)
|
|
|
|
def test_a_zero_input_price_is_a_real_price(self) -> None:
|
|
rates: Final = CostMapEntry(input_cost_per_token=0.0, output_cost_per_token=OUTPUT_RATE)
|
|
assert default_token_charge(REASONING_USAGE, rates).input_cost == 0.0
|
|
|
|
def test_a_row_without_an_output_price_is_refused(self) -> None:
|
|
with pytest.raises(AssertionError, match="no token rates"):
|
|
default_token_charge(REASONING_USAGE, CostMapEntry(input_cost_per_token=INPUT_RATE))
|
|
|
|
|
|
class TestServedSnapshotKey:
|
|
@pytest.mark.parametrize(
|
|
("model_map_key", "expected"),
|
|
[
|
|
("azure/gpt-5.4-nano", "azure/gpt-5.4-nano"),
|
|
("azure/gpt-5.4-nano-2026-03-17", "azure/gpt-5.4-nano-2026-03-17"),
|
|
("gpt-5.4-nano-2026-03-17", "azure/gpt-5.4-nano-2026-03-17"),
|
|
("gpt-5.4-nano", "azure/gpt-5.4-nano"),
|
|
],
|
|
ids=["deployment-model", "dated-snapshot", "unprefixed-snapshot", "unprefixed-model"],
|
|
)
|
|
def test_names_the_deployment_model_or_its_dated_snapshot(self, model_map_key: str, expected: str) -> None:
|
|
assert served_snapshot_key(model_map_key, "azure/gpt-5.4-nano") == expected
|
|
|
|
@pytest.mark.parametrize(
|
|
"model_map_key",
|
|
["azure/gpt-5.4-nano-pro", "azure/us/gpt-5.4-nano", "azure/gpt-5.4", "azure/gpt-5.4-nano-2026-03", None, ""],
|
|
ids=["sibling-model", "data-zone-row", "other-model", "partial-date", "missing", "empty"],
|
|
)
|
|
def test_any_other_row_is_refused(self, model_map_key: str | None) -> None:
|
|
assert served_snapshot_key(model_map_key, "azure/gpt-5.4-nano") is None
|