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]

Merge branch 'litellm_internal_staging' into litellm_gpt56_cache_token_pricing

mateo-berribd04520d9814
diff --cc litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
index c3f0c8912ca,56d8a34ad5c..f12dd979338
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py
@@@ -229,17 -227,24 +230,17 @@@ class AnthropicResponsesStreamWrapper
event.get("response") if isinstance(event, dict) else None
)
stop_reason = "end_turn"
- input_tokens = 0
- output_tokens = 0
- cache_creation_tokens = 0
- cache_read_tokens = 0
+ anthropic_usage: AnthropicUsage = AnthropicUsage(input_tokens=0, output_tokens=0)
if response_obj is not None:
- status = getattr(response_obj, "status", None)
+ status: Final = getattr(response_obj, "status", None)
if status == "incomplete":
stop_reason = "max_tokens"
- usage: Final = getattr(response_obj, "usage", None)
- if usage is not None:
- input_tokens = getattr(usage, "input_tokens", 0) or 0
- output_tokens = getattr(usage, "output_tokens", 0) or 0
- cache_creation_tokens = getattr(usage, "input_tokens_details", None) # type: ignore[assignment]
- cache_read_tokens = getattr(usage, "output_tokens_details", None) # type: ignore[assignment]
- # Prefer direct cache fields if present
- cache_creation_tokens = int(getattr(usage, "cache_creation_input_tokens", 0) or 0)
- cache_read_tokens = int(getattr(usage, "cache_read_input_tokens", 0) or 0)
+ anthropic_usage = (
+ LiteLLMAnthropicToResponsesAPIAdapter.translate_responses_api_usage_to_anthropic_usage(
+ getattr(response_obj, "usage", None)
+ )
+ )
# Check if tool_use was in the output to override stop_reason
if response_obj is not None:
diff --cc litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
index 8b75b0447fa,1fb5a88cb2b..25874b3c558
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
@@@ -38,24 -38,6 +38,24 @@@ class LiteLLMAnthropicToResponsesAPIAda
converts Responses API responses back to Anthropic format.
"""
+ @staticmethod
+ def translate_responses_api_usage_to_anthropic_usage(
- raw_usage: Optional[ResponseAPIUsage],
++ raw_usage: ResponseAPIUsage | None,
+ ) -> AnthropicUsage:
+ """Map Responses API usage onto Anthropic usage, where ``input_tokens``
+ excludes the cache-read and cache-write tokens reported alongside it.
+ """
+ if raw_usage is None:
+ return AnthropicUsage(input_tokens=0, output_tokens=0)
+
+ from litellm.llms.anthropic.experimental_pass_through.adapters.transformation import (
+ LiteLLMAnthropicMessagesAdapter,
+ )
+ from litellm.responses.utils import ResponseAPILoggingUtils
+
+ chat_usage = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage(raw_usage)
+ return LiteLLMAnthropicMessagesAdapter._translate_openai_usage_to_anthropic_usage(chat_usage)
+
# ------------------------------------------------------------------ #
# Request translation: Anthropic -> Responses API #
# ------------------------------------------------------------------ #
@@@ -414,7 -386,9 +404,7 @@@
ResponseReasoningItem,
)
- content: List[Dict[str, Any]] = []
- from litellm.types.llms.openai import ResponseAPIUsage
-
+ content: Final[list[dict[str, Any]]] = []
stop_reason: AnthropicFinishReason = "end_turn"
for item in response.output:
@@@ -479,7 -453,15 +469,7 @@@
if response.status == "incomplete":
stop_reason = "max_tokens"
- anthropic_usage = self.translate_responses_api_usage_to_anthropic_usage(response.usage)
- # usage
- raw_usage: Final[ResponseAPIUsage | None] = response.usage
- input_tokens: Final = int(getattr(raw_usage, "input_tokens", 0) or 0)
- output_tokens: Final = int(getattr(raw_usage, "output_tokens", 0) or 0)
-
- anthropic_usage: Final = AnthropicUsage(
- input_tokens=input_tokens,
- output_tokens=output_tokens,
- )
++ anthropic_usage: Final = self.translate_responses_api_usage_to_anthropic_usage(response.usage)
return AnthropicMessagesResponse(
id=response.id,
diff --cc litellm/types/utils.py
index 04388edaf05,77f83c5b6f8..70b7beefcce
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@@ -198,10 -201,13 +201,13 @@@ class ModelInfoBase(ProviderSpecificMod
input_cost_per_token_flex: Optional[float] # OpenAI flex service tier pricing
input_cost_per_token_priority: Optional[float] # OpenAI priority service tier pricing
cache_creation_input_token_cost: Optional[float]
+ cache_creation_input_token_cost_flex: Optional[float] # OpenAI flex service tier pricing
+ cache_creation_input_token_cost_priority: Optional[float] # OpenAI priority service tier pricing
cache_creation_input_token_cost_above_200k_tokens: Optional[float]
+ cache_creation_input_token_cost_above_272k_tokens: Optional[float]
+ cache_creation_input_token_cost_above_272k_tokens_priority: Optional[float]
+ cache_creation_input_token_cost_above_272k_tokens_flex: Optional[float]
cache_creation_input_token_cost_above_1hr: Optional[float]
- cache_creation_input_token_cost_flex: Optional[float] # OpenAI flex service tier pricing
- cache_creation_input_token_cost_priority: Optional[float] # OpenAI priority service tier pricing
cache_read_input_token_cost: Optional[float]
cache_read_input_token_cost_flex: Optional[float] # OpenAI flex service tier pricing
cache_read_input_token_cost_priority: Optional[float] # OpenAI priority service tier pricing
@@@ -3089,10 -3258,13 +3258,13 @@@ class CustomPricingLiteLLMParams(BaseMo
input_cost_per_token_flex: Optional[float] = None
input_cost_per_token_priority: Optional[float] = None
cache_creation_input_token_cost: Optional[float] = None
+ cache_creation_input_token_cost_flex: Optional[float] = None
+ cache_creation_input_token_cost_priority: Optional[float] = None
cache_creation_input_token_cost_above_1hr: Optional[float] = None
cache_creation_input_token_cost_above_200k_tokens: Optional[float] = None
+ cache_creation_input_token_cost_above_272k_tokens: Optional[float] = None
+ cache_creation_input_token_cost_above_272k_tokens_priority: Optional[float] = None
+ cache_creation_input_token_cost_above_272k_tokens_flex: Optional[float] = None
- cache_creation_input_token_cost_flex: Optional[float] = None
- cache_creation_input_token_cost_priority: Optional[float] = None
cache_creation_input_audio_token_cost: Optional[float] = None
cache_read_input_token_cost: Optional[float] = None
cache_read_input_token_cost_flex: Optional[float] = None
diff --cc litellm/utils.py
index 3296b39e708,d24a4dc928f..f3e90298306
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@@ -5414,6 -5408,19 +5412,15 @@@ def _get_model_info_helper
cache_creation_input_token_cost_above_200k_tokens=_model_info.get(
"cache_creation_input_token_cost_above_200k_tokens", None
),
+ cache_creation_input_token_cost_above_272k_tokens=_model_info.get(
+ "cache_creation_input_token_cost_above_272k_tokens", None
+ ),
+ cache_creation_input_token_cost_above_272k_tokens_priority=_model_info.get(
+ "cache_creation_input_token_cost_above_272k_tokens_priority", None
+ ),
+ cache_creation_input_token_cost_above_272k_tokens_flex=_model_info.get(
+ "cache_creation_input_token_cost_above_272k_tokens_flex", None
+ ),
- cache_creation_input_token_cost_flex=_model_info.get("cache_creation_input_token_cost_flex", None),
- cache_creation_input_token_cost_priority=_model_info.get(
- "cache_creation_input_token_cost_priority", None
- ),
cache_read_input_token_cost=_model_info.get("cache_read_input_token_cost", None),
prompt_cache_min_tokens=_model_info.get("prompt_cache_min_tokens", None),
cache_read_input_token_cost_above_200k_tokens=_model_info.get(
diff --cc tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
index d1b488190a0,9145e5dc76d..29a6a85b942
--- 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
@@@ -2401,42 -2492,64 +2492,105 @@@ def test_generic_cost_per_token_gemini_
assert completion_cost == pytest.approx(0.00125)
+ "service_tier,input_rate,cache_read_rate,cache_write_rate,output_rate",
+ [
+ ("flex", 2.5e-6, 2.5e-7, 3.125e-6, 1.5e-5),
+ ("priority", 1e-5, 1e-6, 1.25e-5, 6e-5),
+ ],
+)
+def test_service_tier_cache_creation_rates_for_gpt_5_6(
+ _local_model_cost_map,
+ service_tier,
+ input_rate,
+ cache_read_rate,
+ cache_write_rate,
+ output_rate,
+):
+ """Regression: gpt-5.6 publishes cache_creation_input_token_cost_flex/_priority, so a
+ flex or priority request must bill cache writes at that tier's rate instead of falling
+ back to the standard 6.25e-6 rate."""
+ usage = Usage(
+ prompt_tokens=10_000,
+ completion_tokens=500,
+ total_tokens=10_500,
+ prompt_tokens_details=PromptTokensDetailsWrapper(
+ cached_tokens=6_000,
+ cache_write_tokens=3_000,
+ text_tokens=1_000,
+ ),
+ )
+
+ prompt_cost, completion_cost = generic_cost_per_token(
+ model="gpt-5.6-sol",
+ usage=usage,
+ custom_llm_provider="openai",
+ service_tier=service_tier,
+ )
+
+ expected_prompt = 1_000 * input_rate + 6_000 * cache_read_rate + 3_000 * cache_write_rate
+ assert prompt_cost == pytest.approx(expected_prompt, rel=1e-9)
+ assert completion_cost == pytest.approx(500 * output_rate, rel=1e-9)
++
++
+ def test_fast_service_tier_bills_at_the_priority_rate(_local_model_cost_map):
+ """Regression: OpenAI's Fast mode replaced Priority Processing and costs 2x standard.
+
+ Before the fix "fast" fell through to standard pricing, so a Fast mode request
+ was billed at half of what it actually costs."""
+ from litellm.types.utils import Usage
+
+ usage = Usage(
+ prompt_tokens=1_000,
+ completion_tokens=500,
+ prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=200),
+ )
+
+ standard = generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier=None
+ )
+ priority = generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="priority"
+ )
+ fast = generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="fast"
+ )
+
+ expected_prompt = 800 * 1e-05 + 200 * 1e-06
+ expected_completion = 500 * 6e-05
+
+ assert fast == priority
+ assert fast[0] == pytest.approx(expected_prompt, rel=1e-9)
+ assert fast[1] == pytest.approx(expected_completion, rel=1e-9)
+ assert fast[0] == pytest.approx(standard[0] * 2, rel=1e-9)
+ assert fast[1] == pytest.approx(standard[1] * 2, rel=1e-9)
+
+
+ def test_fast_service_tier_is_case_insensitive(_local_model_cost_map):
+ from litellm.types.utils import Usage
+
+ usage = Usage(prompt_tokens=1_000, completion_tokens=500)
+
+ assert generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="FAST"
+ ) == generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="fast"
+ )
+
+
+ def test_fast_service_tier_matches_priority_above_the_context_threshold(_local_model_cost_map):
+ """The above-threshold branch resolves its own cost keys, so the alias has to hold there too."""
+ from litellm.types.utils import Usage
+
+ usage = Usage(prompt_tokens=300_000, completion_tokens=1_000)
+
+ fast = generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="fast"
+ )
+ priority = generic_cost_per_token(
+ model="gpt-5.6-sol", usage=usage, custom_llm_provider="openai", service_tier="priority"
+ )
+
+ assert fast == priority
+ assert fast[0] == pytest.approx(300_000 * 1e-05, rel=1e-9)
+ assert fast[1] == pytest.approx(1_000 * 4.5e-05, rel=1e-9)