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(cost): pin the nested-reasoning helper's clamps

mateo-berri36b0d80d3a14
diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
index e8eae73df3f..c9fe53651cd 100644
--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
@@ -4821,6 +4821,29 @@ def test_generic_cost_per_token_keeps_billing_reasoning_reported_beside_text_tok
assert completion_cost == pytest.approx(44 * info["output_cost_per_token"])
+def test_generic_cost_per_token_strips_only_the_reasoning_share_when_text_over_reports(
+ _local_model_cost_map: None,
+) -> None:
+ """Text over-reported past the reasoning share keeps its extra tokens billed; only the nested reasoning is netted out."""
+
+ model = "gpt-realtime-2.1-mini"
+ usage = Usage(
+ prompt_tokens=120,
+ completion_tokens=100,
+ total_tokens=220,
+ completion_tokens_details=CompletionTokensDetailsWrapper(text_tokens=100, audio_tokens=70, reasoning_tokens=10),
+ )
+
+ _, completion_cost = generic_cost_per_token(model=model, usage=usage, custom_llm_provider="openai")
+ breakdown = get_token_type_cost_breakdown(model=model, custom_llm_provider="openai", usage=usage)
+
+ info = litellm.get_model_info(model=model, custom_llm_provider="openai")
+ assert breakdown.reasoning_cost == pytest.approx(10 * info["output_cost_per_token"])
+ assert completion_cost == pytest.approx(
+ 100 * info["output_cost_per_token"] + 70 * info["output_cost_per_audio_token"]
+ )
+
+
def test_generic_cost_per_token_bills_nested_reasoning_once_beside_audio_output(_local_model_cost_map: None) -> None:
"""Audio-output realtime usage nests reasoning inside text_tokens next to audio_tokens; text is billed net of it."""
diff --git a/tests/test_litellm/types/test_types_utils.py b/tests/test_litellm/types/test_types_utils.py
index 554604ab200..5f44ba1773e 100644
--- a/tests/test_litellm/types/test_types_utils.py
+++ b/tests/test_litellm/types/test_types_utils.py
@@ -3,7 +3,7 @@ from typing import Final
import pytest
-from litellm.types.utils import HiddenParams, all_litellm_params
+from litellm.types.utils import HiddenParams, all_litellm_params, text_tokens_without_nested_reasoning
def test_rust_is_a_known_litellm_param():
@@ -768,3 +768,32 @@ def test_image_response_keeps_background():
response = ImageResponse(created=1, data=[{"b64_json": "aGk="}], background="transparent", output_format="png")
assert response.background == "transparent"
assert response.model_dump()["background"] == "transparent"
+
+
+ ("completion_tokens", "text_tokens", "reasoning_tokens", "other_modality_tokens", "expected_text_tokens"),
+ (
+ pytest.param(50, 30, 20, 0, 30, id="details_sum_to_completion_is_a_no_op"),
+ pytest.param(34, 30, 24, 0, 10, id="strip_is_capped_at_the_over_sum"),
+ pytest.param(100, 100, 10, 70, 90, id="only_the_reasoning_share_is_stripped_when_text_over_reports_further"),
+ pytest.param(10, 5, 20, 0, 0, id="text_never_goes_negative_when_reasoning_exceeds_it"),
+ ),
+)
+def test_text_tokens_without_nested_reasoning_clamps(
+ completion_tokens: int,
+ text_tokens: int,
+ reasoning_tokens: int,
+ other_modality_tokens: int,
+ expected_text_tokens: int,
+) -> None:
+ """The strip never exceeds the reasoning share, the reported text, or the over-sum past completion_tokens."""
+
+ assert (
+ text_tokens_without_nested_reasoning(
+ completion_tokens=completion_tokens,
+ text_tokens=text_tokens,
+ reasoning_tokens=reasoning_tokens,
+ other_modality_tokens=other_modality_tokens,
+ )
+ == expected_text_tokens
+ )