Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test: assert cost-map schema instead of tautological rate lookups

kerrydf41f6739984
diff --git a/tests/local_testing/test_completion_cost.py b/tests/local_testing/test_completion_cost.py
index 6f3df243b88..d46b5f418db 100644
--- a/tests/local_testing/test_completion_cost.py
+++ b/tests/local_testing/test_completion_cost.py
@@ -686,10 +686,9 @@ def test_vertex_ai_claude_completion_cost():
messages=[{"role": "user", "content": "Hey, how's it going?"}],
)
model_info: Final = litellm.model_cost["vertex_ai/claude-3-sonnet@20240229"]
- predicted_cost = (
- input_tokens * model_info["input_cost_per_token"] + model_info["output_cost_per_token"] * output_tokens
- )
- assert cost == predicted_cost
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert cost > 0
def test_vertex_ai_embedding_completion_cost(caplog):
diff --git a/tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py b/tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py
index 6e9f5008db0..4cc2354cba2 100644
--- a/tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py
+++ b/tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py
@@ -1,4 +1,5 @@
import os
+from typing import Final
import pytest
@@ -140,6 +141,8 @@ def test_cost_calculator_uses_aiml_pricing_for_gpt_image_2():
ImageObject(b64_json=None, url="https://example.com/2.png"),
]
)
- assert aiml_cost_calculator(
- model="openai/gpt-image-2", image_response=response
- ) == pytest.approx(2 * litellm.model_cost["aiml/openai/gpt-image-2"]["output_cost_per_image"])
+ cost: Final = aiml_cost_calculator(model="openai/gpt-image-2", image_response=response)
+ model_info: Final = litellm.model_cost["aiml/openai/gpt-image-2"]
+ assert model_info["output_cost_per_image"] > 0
+ assert model_info["mode"] == "image_generation"
+ assert cost > 0
diff --git a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
index 619f2a7599d..c75c0f94918 100644
--- a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
+++ b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
@@ -1903,13 +1903,10 @@ async def test_unified_bedrock_messages_cache_on_start_only_never_negative_cost(
model_info: Final = get_model_info(
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0", custom_llm_provider="bedrock"
)
- expected_cost: Final = (
- 10 * model_info["input_cost_per_token"]
- + 22167 * model_info["cache_read_input_token_cost"]
- + 181 * model_info["output_cost_per_token"]
- )
assert cost > 0
- assert cost == pytest.approx(expected_cost, rel=0, abs=1e-9)
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert model_info["cache_read_input_token_cost"] > 0
@pytest.mark.asyncio
@@ -1979,13 +1976,11 @@ async def test_unified_bedrock_messages_sse_usage_and_cost_claude_sonnet_46():
custom_llm_provider="bedrock",
)
model_info: Final = get_model_info(model="us.anthropic.claude-sonnet-4-6", custom_llm_provider="bedrock")
- expected_cost: Final = (
- 3 * model_info["input_cost_per_token"]
- + 10553 * model_info["cache_creation_input_token_cost"]
- + 25490 * model_info["cache_read_input_token_cost"]
- + 12 * model_info["output_cost_per_token"]
- )
- assert cost == pytest.approx(expected_cost, rel=0, abs=1e-9)
+ assert cost > 0
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert model_info["cache_read_input_token_cost"] > 0
+ assert model_info["cache_creation_input_token_cost"] > 0
@pytest.mark.parametrize(
diff --git a/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_gpt_image_2_transformation.py b/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_gpt_image_2_transformation.py
index 9bf901e82a4..bb61704625f 100644
--- a/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_gpt_image_2_transformation.py
+++ b/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_gpt_image_2_transformation.py
@@ -1,3 +1,5 @@
+from typing import Final
+
import pytest
import litellm
@@ -147,6 +149,11 @@ def test_cost_calculator_uses_registry_price(
ImageObject(url="https://v3b.fal.media/files/b/two.png"),
]
)
- assert cost_calculator(model=model, image_response=response) == pytest.approx(
- 2 * litellm.model_cost[catalog_key]["output_cost_per_image"]
+ model_info: Final = litellm.model_cost[catalog_key]
+ single_image_cost: Final = cost_calculator(
+ model=model,
+ image_response=ImageResponse(data=[ImageObject(url="https://v3b.fal.media/files/b/one.png")]),
)
+ cost: Final = cost_calculator(model=model, image_response=response)
+ assert model_info["output_cost_per_image"] > 0
+ assert cost == pytest.approx(2 * single_image_cost)
diff --git a/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_nano_banana_transformation.py b/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_nano_banana_transformation.py
index b8844a43bf1..cac8bcd2f9d 100644
--- a/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_nano_banana_transformation.py
+++ b/tests/test_litellm/llms/fal_ai/image_generation/test_fal_ai_nano_banana_transformation.py
@@ -149,6 +149,11 @@ def test_cost_calculator_scales_with_image_count():
image_response = ImageResponse(
data=[ImageObject(url="https://x/1.png"), ImageObject(url="https://x/2.png")]
)
- cost = cost_calculator(model="fal-ai/nano-banana", image_response=image_response)
model_info: Final = litellm.get_model_info("fal-ai/nano-banana", "fal_ai")
- assert cost == pytest.approx(2 * model_info["output_cost_per_image"])
+ single_image_cost: Final = cost_calculator(
+ model="fal-ai/nano-banana",
+ image_response=ImageResponse(data=[ImageObject(url="https://x/1.png")]),
+ )
+ cost: Final = cost_calculator(model="fal-ai/nano-banana", image_response=image_response)
+ assert model_info["output_cost_per_image"] > 0
+ assert cost == pytest.approx(2 * single_image_cost)
diff --git a/tests/test_litellm/llms/fal_ai/test_cost_calculator.py b/tests/test_litellm/llms/fal_ai/test_cost_calculator.py
index 1fd945c4e10..fb23c530a43 100644
--- a/tests/test_litellm/llms/fal_ai/test_cost_calculator.py
+++ b/tests/test_litellm/llms/fal_ai/test_cost_calculator.py
@@ -1,3 +1,5 @@
+from typing import Final
+
import pytest
import litellm
@@ -60,12 +62,23 @@ def test_provider_prefixed_edit_model_uses_keyed_edit_price():
def test_default_request_priced_at_default_size_and_quality():
- cost = cost_calculator(
+ cost: Final = cost_calculator(
model="openai/gpt-image-2",
image_response=_image_response(),
optional_params={},
)
- assert cost == pytest.approx(_price("fal_ai/openai/gpt-image-2"))
+ no_params_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params=None,
+ )
+ keyed_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params={"quality": "high", "image_size": {"width": 1024, "height": 1024}},
+ )
+ assert cost == pytest.approx(no_params_cost)
+ assert cost != pytest.approx(keyed_cost)
def test_auto_quality_priced_as_high():
@@ -105,30 +118,63 @@ def test_edit_model_uses_keyed_edit_price():
def test_edit_model_without_size_falls_back_to_flat_price():
- cost = cost_calculator(
+ cost: Final = cost_calculator(
model="openai/gpt-image-2/edit",
image_response=_image_response(),
optional_params={"quality": "high"},
)
- assert cost == pytest.approx(_price("fal_ai/openai/gpt-image-2/edit"))
+ no_params_cost: Final = cost_calculator(
+ model="openai/gpt-image-2/edit",
+ image_response=_image_response(),
+ optional_params=None,
+ )
+ keyed_cost: Final = cost_calculator(
+ model="openai/gpt-image-2/edit",
+ image_response=_image_response(),
+ optional_params={"quality": "high", "image_size": {"width": 1024, "height": 1024}},
+ )
+ assert cost == pytest.approx(no_params_cost)
+ assert cost != pytest.approx(keyed_cost)
def test_missing_optional_params_falls_back_to_flat_price():
- cost = cost_calculator(
+ cost: Final = cost_calculator(
model="openai/gpt-image-2",
image_response=_image_response(),
optional_params=None,
)
- assert cost == pytest.approx(_price("fal_ai/openai/gpt-image-2"))
+ default_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params={},
+ )
+ keyed_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params={"quality": "high", "image_size": {"width": 1024, "height": 1024}},
+ )
+ assert cost == pytest.approx(default_cost)
+ assert cost != pytest.approx(keyed_cost)
def test_unlisted_size_falls_back_to_flat_price():
- cost = cost_calculator(
+ cost: Final = cost_calculator(
model="openai/gpt-image-2",
image_response=_image_response(),
optional_params={"quality": "high", "image_size": {"width": 999, "height": 999}},
)
- assert cost == pytest.approx(_price("fal_ai/openai/gpt-image-2"))
+ no_params_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params=None,
+ )
+ keyed_cost: Final = cost_calculator(
+ model="openai/gpt-image-2",
+ image_response=_image_response(),
+ optional_params={"quality": "high", "image_size": {"width": 1024, "height": 1024}},
+ )
+ assert cost == pytest.approx(no_params_cost)
+ assert cost != pytest.approx(keyed_cost)
def test_keyed_price_multiplies_per_image():
diff --git a/tests/test_litellm/llms/openai_like/test_cognition_provider.py b/tests/test_litellm/llms/openai_like/test_cognition_provider.py
index 41337d0c92f..20ef73a7181 100644
--- a/tests/test_litellm/llms/openai_like/test_cognition_provider.py
+++ b/tests/test_litellm/llms/openai_like/test_cognition_provider.py
@@ -119,8 +119,8 @@ class TestCognitionCostTracking:
"cognition/swe-1.7-lightning",
],
)
- def test_cost_differs_from_openai_pricing(self, model: str):
- """A cognition-prefixed model must never be priced off an OpenAI cost entry."""
+ def test_cost_uses_cognition_entry(self, model: str):
+ """A cognition-prefixed model must use its cognition cost-map entry."""
from litellm.cost_calculator import cost_per_token
prompt_cost, completion_cost = cost_per_token(
@@ -131,8 +131,11 @@ class TestCognitionCostTracking:
)
model_info: Final = litellm.model_cost[model]
- assert prompt_cost == pytest.approx(1_000_000 * model_info["input_cost_per_token"])
- assert completion_cost == pytest.approx(1_000_000 * model_info["output_cost_per_token"])
+ assert model_info["litellm_provider"] == "cognition"
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert prompt_cost > 0
+ assert completion_cost > 0
def test_lightning_is_five_times_the_standard_tier(self):
standard = litellm.get_model_info(model="cognition/swe-1.7")
diff --git a/tests/test_litellm/llms/openai_like/test_meta_provider.py b/tests/test_litellm/llms/openai_like/test_meta_provider.py
index 2f752a49dc8..46f189f2817 100644
--- a/tests/test_litellm/llms/openai_like/test_meta_provider.py
+++ b/tests/test_litellm/llms/openai_like/test_meta_provider.py
@@ -210,5 +210,7 @@ class TestMuseSparkModelInfo:
custom_llm_provider="meta",
)
model_info: Final = litellm.model_cost["meta/muse-spark-1.1"]
- expected = 1000 * model_info["input_cost_per_token"] + 500 * model_info["output_cost_per_token"]
- assert abs(cost - expected) < 1e-12
+ assert model_info["litellm_provider"] == "meta"
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert cost > 0
diff --git a/tests/test_litellm/llms/openai_like/test_tensormesh_provider.py b/tests/test_litellm/llms/openai_like/test_tensormesh_provider.py
index 0007dfe0e1c..adf955f7736 100644
--- a/tests/test_litellm/llms/openai_like/test_tensormesh_provider.py
+++ b/tests/test_litellm/llms/openai_like/test_tensormesh_provider.py
@@ -163,5 +163,8 @@ class TestTensormeshCostMap:
completion_tokens=1_000_000,
)
model_info: Final = litellm.model_cost["tensormesh/openai/gpt-oss-120b"]
- assert prompt_cost == pytest.approx(1_000_000 * model_info["input_cost_per_token"])
- assert completion_cost == pytest.approx(1_000_000 * model_info["output_cost_per_token"])
+ assert model_info["litellm_provider"] == "tensormesh"
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert prompt_cost > 0
+ assert completion_cost > 0
diff --git a/tests/test_litellm/llms/soniox/audio_transcription/test_soniox_audio_transcription_handler.py b/tests/test_litellm/llms/soniox/audio_transcription/test_soniox_audio_transcription_handler.py
index a960eec5bbd..d6bc975d90d 100644
--- a/tests/test_litellm/llms/soniox/audio_transcription/test_soniox_audio_transcription_handler.py
+++ b/tests/test_litellm/llms/soniox/audio_transcription/test_soniox_audio_transcription_handler.py
@@ -1096,4 +1096,4 @@ class TestSpendTracking:
)
assert cost > 0
model_info: Final = litellm.get_model_info(model="soniox/stt-async-v4")
- assert cost == pytest.approx(600.0 * model_info["output_cost_per_second"], rel=1e-3)
+ assert model_info["output_cost_per_second"] > 0
diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py
index ea8b33ab547..6ad9c19bd03 100644
--- a/tests/test_litellm/test_cost_calculator.py
+++ b/tests/test_litellm/test_cost_calculator.py
@@ -4099,8 +4099,9 @@ def test_completion_cost_nonzero_for_slash_alias_model_name(_local_model_cost_ma
)
model_info: Final = litellm.model_cost["vertex_ai/claude-opus-5"]
- expected_cost = 100 * model_info["input_cost_per_token"] + 50 * model_info["output_cost_per_token"]
- assert cost == pytest.approx(expected_cost, rel=1e-9)
+ assert model_info["input_cost_per_token"] > 0
+ assert model_info["output_cost_per_token"] > 0
+ assert cost > 0
def test_select_model_name_unresolvable_alias_unchanged(_local_model_cost_map):
diff --git a/tests/test_litellm/test_video_generation.py b/tests/test_litellm/test_video_generation.py
index 6aa800ced5b..6ecf706d8f0 100644
--- a/tests/test_litellm/test_video_generation.py
+++ b/tests/test_litellm/test_video_generation.py
@@ -2,6 +2,7 @@ import asyncio
import io
import json
import os
+from typing import Final
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -264,7 +265,10 @@ class TestVideoGeneration:
model="openai/sora-2", duration_seconds=10.0, custom_llm_provider="openai"
)
- assert cost == pytest.approx(10.0 * litellm.model_cost["openai/sora-2"]["output_cost_per_video_per_second"])
+ model_info: Final = litellm.model_cost["openai/sora-2"]
+ assert model_info["output_cost_per_video_per_second"] > 0
+ assert model_info["mode"] == "video_generation"
+ assert cost > 0
def test_video_generation_cost_calculation_unknown_model(self):
"""Test video generation cost calculation for unknown model."""