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' of https://github.com/BerriAI/litellm into litellm_gemini_latest_cache_read_rates

mateo-berri901bea41e709
diff --cc tests/test_litellm/llms/gemini/test_cost_calculator.py
index 2f6f9a556c0,1e70803d39e..f1d62800337
--- a/tests/test_litellm/llms/gemini/test_cost_calculator.py
+++ b/tests/test_litellm/llms/gemini/test_cost_calculator.py
@@@ -363,50 -363,31 +363,80 @@@ def test_gemini_image_generation_cost_n
assert cost_zero == cost_none
+ @pytest.mark.parametrize(
+ "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: str | None, expected_service_tier: str | None
+ ):
+ """
+ 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
+ )
++
++
+ "model,custom_llm_provider,expected_cache_read_cost",
+ [
+ ("gemini/gemini-flash-latest", "gemini", 3e-08),
+ ("gemini/gemini-flash-lite-latest", "gemini", 1e-08),
+ ("gemini/gemini-2.5-flash-preview-09-2025", "gemini", 3e-08),
+ ("gemini/gemini-2.5-flash-lite-preview-06-17", "gemini", 1e-08),
+ ("vertex_ai/gemini-2.5-flash-preview-09-2025", "vertex_ai", 3e-08),
+ ("vertex_ai/gemini-2.5-flash-lite-preview-06-17", "vertex_ai", 1e-08),
+ ],
+)
+def test_flash_alias_cache_read_is_ten_percent_of_input(
+ monkeypatch, model, custom_llm_provider, expected_cache_read_cost
+):
+ monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
- litellm.model_cost = litellm.get_model_cost_map(url="")
++ monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url=""))
+
+ model_info = litellm.get_model_info(
+ model=model, custom_llm_provider=custom_llm_provider
+ )
+
+ assert model_info["cache_read_input_token_cost"] == expected_cache_read_cost
+ assert model_info["cache_read_input_token_cost"] == pytest.approx(
+ 0.10 * model_info["input_cost_per_token"]
+ )
+
+
+ "prefixed,bare",
+ [
+ ("gemini/gemini-flash-latest", "gemini-flash-latest"),
+ ("gemini/gemini-flash-lite-latest", "gemini-flash-lite-latest"),
+ ],
+)
+def test_flash_latest_alias_spellings_price_identically(monkeypatch, prefixed, bare):
+ monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
- litellm.model_cost = litellm.get_model_cost_map(url="")
++ monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url=""))
+
+ prefixed_entry = litellm.model_cost[prefixed]
+ bare_entry = litellm.model_cost[bare]
+
+ for cost_key in (
+ "input_cost_per_token",
+ "output_cost_per_token",
+ "cache_read_input_token_cost",
+ ):
+ assert prefixed_entry[cost_key] == bare_entry[cost_key]