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 'main' into feat/gemini-cache-control-pass-through

mateo-berri6019e451ce34
diff --cc litellm/llms/vertex_ai/context_caching/transformation.py
index 7be01cd39e5,e23374d57a1..1563fb80d1b
--- a/litellm/llms/vertex_ai/context_caching/transformation.py
+++ b/litellm/llms/vertex_ai/context_caching/transformation.py
@@@ -140,66 -115,9 +141,66 @@@ def _is_valid_ttl_format(ttl: str) -> b
return False
+def _normalize_ttl_to_seconds(ttl: object) -> str | None:
+ """
+ Normalize a cache_control TTL into Gemini's "<seconds>s" format.
+
+ Accepts Gemini-native seconds (e.g. "3600s", "1.5s") and Anthropic-style
+ minute/hour units (e.g. "5m", "1h") that Claude Code and the Anthropic
+ /v1/messages spec use. Caps the requested TTL at 24 hours (86400s) to
+ prevent unbounded persistent storage costs. Returns None for missing or
+ unparseable values so Gemini falls back to its own default TTL.
+ """
+ if not isinstance(ttl, str):
+ return None
+
+ match = re.match(r"^([0-9]*\.?[0-9]+)(s|m|h)$", ttl)
+ if not match:
+ return None
+
+ value = float(match.group(1))
+
+ if value <= 0:
+ return None
+
+ multiplier = {"s": 1, "m": 60, "h": 3600}[match.group(2)]
+ seconds = value * multiplier
+
+ # Cap explicit caches to 24 hours to prevent unbounded billing costs
+ seconds = min(seconds, 86400.0)
+
+ # Google Protobuf Duration requires up to 9 fractional digits
+ seconds = round(seconds, 9)
+ return f"{int(seconds)}s" if seconds.is_integer() else f"{seconds}s"
+
+
+def get_gemini_context_caching_min_tokens(model: str) -> int:
+ """
+ Minimum input token count required to create an explicit Gemini context cache.
+
+ Looks up the `cache_creation_min_tokens` property from model_prices_and_context_window.json.
+ Defaults to string-matching fallbacks for unknown models.
+ """
+ import litellm
+
+ try:
+ model_info = litellm.get_model_info(model=model)
+ if model_info and "cache_creation_min_tokens" in model_info:
+ return int(model_info["cache_creation_min_tokens"])
+ except Exception: # noqa: BLE001 # fallback to string-matching heuristic if model lookup fails
+ pass
+
+ model_lower = model.lower()
+ if "gemini-2.5" in model_lower or "gemini-2-5" in model_lower:
+ return 2048
+ if "gemini-3" in model_lower:
+ return 4096
+ return 32768
+
+
def separate_cached_messages(
- messages: List[AllMessageValues],
- ) -> Tuple[List[AllMessageValues], List[AllMessageValues]]:
+ messages: list[AllMessageValues],
+ ) -> tuple[list[AllMessageValues], list[AllMessageValues]]:
"""
Returns separated cached and non-cached messages.
diff --cc tests/test_litellm/test_utils.py
index 1840f87360b,2fda5dfc490..c1c4613f7a9
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@@ -712,10 -686,13 +686,14 @@@ def test_aaamodel_prices_and_context_wi
"supports_computer_use": {"type": "boolean"},
"cache_creation_input_audio_token_cost": {"type": "number"},
"cache_creation_input_token_cost": {"type": "number"},
+ "cache_creation_min_tokens": {"type": "number"},
"cache_creation_input_token_cost_above_1hr": {"type": "number"},
+ "cache_creation_input_token_cost_above_128k_tokens": {"type": "number"},
"cache_creation_input_token_cost_above_200k_tokens": {"type": "number"},
+ "cache_creation_input_token_cost_above_256k_tokens": {"type": "number"},
"cache_creation_input_token_cost_above_272k_tokens": {"type": "number"},
+ "cache_creation_input_token_cost_above_272k_tokens_flex": {"type": "number"},
+ "cache_creation_input_token_cost_above_272k_tokens_priority": {"type": "number"},
"cache_creation_input_token_cost_flex": {"type": "number"},
"cache_creation_input_token_cost_priority": {"type": "number"},
"cache_read_input_token_cost": {"type": "number"},