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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: map Gemini ON_DEMAND_FLEX traffic type to flex service tier
Bisma Nawazd317c5621fd1
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.pyindex 8f7cd09d364..46a42b616f8 100644--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@ -830,9 +830,11 @@ def _get_response_model(completion_response: object) -> str | None:_GEMINI_TRAFFIC_TYPE_TO_SERVICE_TIER: Final[dict] = {# ON_DEMAND_PRIORITY maps to "priority" — selects input_cost_per_token_priority, etc."ON_DEMAND_PRIORITY": "priority",- # FLEX / BATCH maps to "flex" — selects input_cost_per_token_flex, etc.+ # FLEX / BATCH / ON_DEMAND_FLEX maps to "flex" — selects input_cost_per_token_flex, etc.+ # Vertex AI reports flex/shared-capacity traffic as ON_DEMAND_FLEX, not FLEX."FLEX": "flex","BATCH": "flex",+ "ON_DEMAND_FLEX": "flex",# ON_DEMAND is standard pricing — no service_tier suffix applied"ON_DEMAND": None,}@@ -847,9 +849,9 @@ def _map_traffic_type_to_service_tier(traffic_type: str | None) -> str | None:trafficType values seen in practice------------------------------------- ON_DEMAND -> standard pricing (service_tier = None)- ON_DEMAND_PRIORITY -> priority pricing (service_tier = "priority")- FLEX / BATCH -> batch/flex pricing (service_tier = "flex")+ ON_DEMAND -> standard pricing (service_tier = None)+ ON_DEMAND_PRIORITY -> priority pricing (service_tier = "priority")+ FLEX / BATCH / ON_DEMAND_FLEX -> batch/flex pricing (service_tier = "flex")"""if traffic_type is None:return Nonediff --git a/tests/test_litellm/llms/gemini/test_cost_calculator.py b/tests/test_litellm/llms/gemini/test_cost_calculator.pyindex 6917092966b..c44f29ba168 100644--- a/tests/test_litellm/llms/gemini/test_cost_calculator.py+++ b/tests/test_litellm/llms/gemini/test_cost_calculator.py@@ -301,3 +301,31 @@ def test_gemini_image_generation_cost_no_web_search_when_absent():)assert cost_zero == cost_none+++ "traffic_type, expected_service_tier",+ [+ ("ON_DEMAND", None),+ ("ON_DEMAND_PRIORITY", "priority"),+ ("FLEX", "flex"),+ ("BATCH", "flex"),+ # Vertex AI reports flex/shared-capacity traffic as ON_DEMAND_FLEX.+ ("ON_DEMAND_FLEX", "flex"),+ # trafficType is matched case-insensitively.+ ("on_demand_flex", "flex"),+ (None, None),+ ("SOMETHING_UNKNOWN", None),+ ],+)+def test_map_traffic_type_to_service_tier(traffic_type, expected_service_tier):+ """+ Gemini/Vertex usageMetadata.trafficType maps to the LiteLLM service_tier+ that selects flex/priority cost keys. ON_DEMAND_FLEX (Vertex's flex opt-in+ value) must map to "flex" so flex-tier requests are not billed as standard.+ """+ from litellm.cost_calculator import _map_traffic_type_to_service_tier++ assert (+ _map_traffic_type_to_service_tier(traffic_type) == expected_service_tier+ )