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
Map cache_control_injection_points to OpenAI prompt_cache_breakpoint on GPT-5.6+ targets
mateo-berri5f6d22e7921d
diff --git a/litellm/integrations/anthropic_cache_control_hook.py b/litellm/integrations/anthropic_cache_control_hook.pyindex 4df6fce74c0..05e2a37a230 100644--- a/litellm/integrations/anthropic_cache_control_hook.py+++ b/litellm/integrations/anthropic_cache_control_hook.py@@ -10,17 +10,29 @@ Supported for both `v1/chat/completions` (via the prompt-management hook) and"""import copy+import re+from collections.abc import Iterable, Mapping, Sequencefrom typing import TYPE_CHECKING, Any, Final, castfrom litellm._logging import verbose_loggerfrom litellm.integrations.custom_logger import CustomLoggerfrom litellm.integrations.custom_prompt_management import CustomPromptManagementfrom litellm.integrations.prompt_management_base import PromptManagementClient+from litellm.litellm_core_utils.prompt_templates.common_utils import (+ with_prompt_cache_breakpoint,+)from litellm.types.integrations.anthropic_cache_control_hook import (CacheControlInjectionPoint,CacheControlMessageInjectionPoint,)-from litellm.types.llms.openai import AllMessageValues, ChatCompletionCachedContent+from litellm.types.llms.anthropic import AnthropicSystemMessageContent+from litellm.types.llms.openai import (+ AllMessageValues,+ ChatCompletionCachedContent,+ ChatCompletionTextObject,+ PromptCacheBreakpoint,+ PromptCacheOptions,+)from litellm.types.prompts.init_prompts import PromptSpecfrom litellm.types.utils import StandardCallbackDynamicParams@@ -34,6 +46,27 @@ else:# breakpoints: "A maximum of 4 blocks with cache_control may be provided."MAX_CACHE_CONTROL_BLOCKS: Final = 4+CACHE_BREAKPOINT_KEYS: Final = ("cache_control", "prompt_cache_breakpoint")+OPENAI_PROMPT_CACHE_BREAKPOINT_MIN_GPT_VERSION: Final = (5, 6)+_GPT_VERSION_PATTERN: Final = re.compile(r"^gpt-(\d+)(?:\.(\d+))?")+OPENAI_PROMPT_CACHE_BREAKPOINT_BLOCK_TYPES: Final = frozenset({"text", "image", "image_url", "file", "input_audio"})+++def supports_openai_prompt_cache_breakpoint(model: str) -> bool:+ version_match: Final = _GPT_VERSION_PATTERN.match(model.rsplit("/", 1)[-1].lower())+ if version_match is None:+ return False+ version: Final = (int(version_match.group(1)), int(version_match.group(2) or 0))+ return version >= OPENAI_PROMPT_CACHE_BREAKPOINT_MIN_GPT_VERSION+++def _carries_cache_breakpoint(block: object) -> bool:+ return isinstance(block, dict) and any(block.get(key) is not None for key in CACHE_BREAKPOINT_KEYS)+++def _accepts_prompt_cache_breakpoint(block: object) -> bool:+ return isinstance(block, dict) and block.get("type") in OPENAI_PROMPT_CACHE_BREAKPOINT_BLOCK_TYPES+class AnthropicCacheControlHook(CustomPromptManagement):def get_chat_completion_prompt(@@ -83,11 +116,21 @@ class AnthropicCacheControlHook(CustomPromptManagement):# limit, so reserve a slot for it here to leave room.reserved_blocks: Final = 1 if any(p.get("location") == "tool_config" for p in remaining_points) else 0+ openai_dialect: Final = AnthropicCacheControlHook._targets_openai_prompt_cache_breakpoint(+ model, injection_points[0].get("_litellm_provider")+ )+ breakpoints_before: Final = AnthropicCacheControlHook._count_request_cache_breakpoints(processed_messages)processed_messages = self._apply_message_injections(points=message_points,messages=processed_messages,max_blocks=MAX_CACHE_CONTROL_BLOCKS - reserved_blocks,+ openai_dialect=openai_dialect,)+ if (+ openai_dialect+ and AnthropicCacheControlHook._count_request_cache_breakpoints(processed_messages) > breakpoints_before+ ):+ non_default_params.setdefault("prompt_cache_options", PromptCacheOptions(mode="explicit"))# Pass through non-message injection points for provider-specific handlingif remaining_points:@@ -97,11 +140,36 @@ class AnthropicCacheControlHook(CustomPromptManagement):return model, processed_messages, non_default_params+ @staticmethod+ def _targets_openai_prompt_cache_breakpoint(model: str | None, custom_llm_provider: str | None) -> bool:+ if model is None or not supports_openai_prompt_cache_breakpoint(model):+ return False+ return (custom_llm_provider or AnthropicCacheControlHook._resolve_provider(model)) == "openai"++ @staticmethod+ def _resolve_provider(model: str) -> str | None:+ from litellm.exceptions import BadRequestError+ from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider++ try:+ _, provider, _, _ = get_llm_provider(model=model)+ except BadRequestError:+ return None+ return provider++ @staticmethod+ def _count_request_cache_breakpoints(messages: Iterable[object], system: object = None) -> int:+ system_blocks: Final = (+ sum(1 for block in system if _carries_cache_breakpoint(block)) if isinstance(system, list) else 0+ )+ return system_blocks + sum(AnthropicCacheControlHook._count_cache_control_blocks(msg) for msg in messages)+@staticmethoddef _apply_message_injections(points: list[CacheControlMessageInjectionPoint],messages: list[AllMessageValues],max_blocks: int,+ openai_dialect: bool = False,) -> list[AllMessageValues]:"""Apply message-level cache control injection points in order.@@ -112,7 +180,7 @@ class AnthropicCacheControlHook(CustomPromptManagement):``max_blocks`` is reached. Injection points are honored in config order,so earlier points win when slots are scarce."""- used_blocks = sum(AnthropicCacheControlHook._count_cache_control_blocks(msg) for msg in messages)+ used_blocks = AnthropicCacheControlHook._count_request_cache_breakpoints(messages)limit_reached = Falsefor point in points:@@ -134,16 +202,17 @@ class AnthropicCacheControlHook(CustomPromptManagement):continuemessages[target_index] = AnthropicCacheControlHook._safe_insert_cache_control_in_message(- messages[target_index], control+ messages[target_index], control, openai_dialect)- used_blocks += 1+ if AnthropicCacheControlHook._message_has_cache_control(messages[target_index]):+ used_blocks += 1if limit_reached:breakif limit_reached:verbose_logger.warning(- "AnthropicCacheControlHook: Reached the Anthropic limit of %s cache_control blocks. Skipping further injection.",+ "AnthropicCacheControlHook: Reached the provider limit of %s cache breakpoints. Skipping further injection.",MAX_CACHE_CONTROL_BLOCKS,)@@ -189,16 +258,13 @@ class AnthropicCacheControlHook(CustomPromptManagement):return []@staticmethod- def _count_cache_control_blocks(message: AllMessageValues) -> int:- """Count cache_control breakpoints on a message (message + content level)."""- count = 0- if message.get("cache_control") is not None:- count += 1+ def _count_cache_control_blocks(message: object) -> int:+ if not isinstance(message, dict):+ return 0+ count = 1 if _carries_cache_breakpoint(message) else 0content: Final = message.get("content")if isinstance(content, list):- for block in content:- if isinstance(block, dict) and block.get("cache_control") is not None:- count += 1+ count += sum(1 for block in content if _carries_cache_breakpoint(block))return count@staticmethod@@ -208,7 +274,7 @@ class AnthropicCacheControlHook(CustomPromptManagement):@staticmethoddef _safe_insert_cache_control_in_message(- message: AllMessageValues, control: ChatCompletionCachedContent+ message: AllMessageValues, control: ChatCompletionCachedContent, openai_dialect: bool = False) -> AllMessageValues:"""Safe way to insert cache control in a message@@ -221,6 +287,9 @@ class AnthropicCacheControlHook(CustomPromptManagement):Per Anthropic's API specification, when using multiple content blocks,only the last content block can have cache_control."""+ if openai_dialect:+ return AnthropicCacheControlHook._insert_prompt_cache_breakpoint_in_message(message)+message_content: Final = message.get("content", None)# 1. if string, insert cache control in the message@@ -232,11 +301,43 @@ class AnthropicCacheControlHook(CustomPromptManagement):message_content[-1]["cache_control"] = controlreturn message+ @staticmethod+ def _insert_prompt_cache_breakpoint_in_message(message: AllMessageValues) -> AllMessageValues:+ if message.get("role") == "assistant":+ return message+ message_content: Final = message.get("content", None)+ if isinstance(message_content, str):+ marked: Final = copy.copy(message)+ marked["content"] = [+ with_prompt_cache_breakpoint(+ ChatCompletionTextObject(type="text", text=message_content), PromptCacheBreakpoint(mode="explicit")+ )+ ]+ return marked+ if isinstance(message_content, list):+ with_prompt_cache_breakpoint(+ next((block for block in reversed(message_content) if _accepts_prompt_cache_breakpoint(block)), None),+ PromptCacheBreakpoint(mode="explicit"),+ )+ return message++ @staticmethod+ def _system_block_with_breakpoint(+ block: Mapping[str, object], control: ChatCompletionCachedContent, openai_dialect: bool+ ) -> Mapping[str, object]:+ marker: Final = (+ ("prompt_cache_breakpoint", PromptCacheBreakpoint(mode="explicit"))+ if openai_dialect+ else ("cache_control", control)+ )+ return {**block, marker[0]: marker[1]}+@staticmethoddef apply_to_anthropic_messages_request(messages: list[dict],system: str | list | None,injection_points: list[CacheControlInjectionPoint],+ openai_dialect: bool = False,) -> tuple[list[dict], str | list | None, list[CacheControlInjectionPoint]]:"""Apply cache control injection for the Anthropic-native v1/messages endpoint.@@ -265,27 +366,27 @@ class AnthropicCacheControlHook(CustomPromptManagement):reserved_blocks: Final = 1 if any(p.get("location") == "tool_config" for p in remaining_points) else 0max_blocks: Final = MAX_CACHE_CONTROL_BLOCKS - reserved_blocks- used_blocks = sum(- AnthropicCacheControlHook._count_cache_control_blocks(cast(AllMessageValues, msg))- for msg in processed_messages- )- if isinstance(processed_system, list):- used_blocks += sum(- 1 for b in processed_system if isinstance(b, dict) and b.get("cache_control") is not None- )+ message_blocks: Final = AnthropicCacheControlHook._count_request_cache_breakpoints(processed_messages)+ system_blocks = AnthropicCacheControlHook._count_request_cache_breakpoints((), processed_system)- if system_points and processed_system is not None and used_blocks < max_blocks:+ if system_points and processed_system is not None and message_blocks + system_blocks < max_blocks:system_already_has_cc: Final = isinstance(processed_system, list) and any(- isinstance(b, dict) and b.get("cache_control") is not None for b in processed_system+ _carries_cache_breakpoint(b) for b in processed_system)if not system_already_has_cc:control: Final = system_points[0].get("control") or ChatCompletionCachedContent(type="ephemeral")if isinstance(processed_system, str):- processed_system = [{"type": "text", "text": processed_system, "cache_control": control}]- used_blocks += 1+ processed_system = [+ AnthropicCacheControlHook._system_block_with_breakpoint(+ AnthropicSystemMessageContent(type="text", text=processed_system), control, openai_dialect+ )+ ]+ system_blocks += 1elif len(processed_system) > 0 and isinstance(processed_system[-1], dict):- processed_system[-1] = {**processed_system[-1], "cache_control": control}- used_blocks += 1+ processed_system[-1] = AnthropicCacheControlHook._system_block_with_breakpoint(+ processed_system[-1], control, openai_dialect+ )+ system_blocks += 1for i, msg in enumerate(processed_messages):content = msg.get("content")@@ -295,7 +396,8 @@ class AnthropicCacheControlHook(CustomPromptManagement):processed_messages = AnthropicCacheControlHook._apply_message_injections(points=message_points,messages=cast(list[AllMessageValues], processed_messages),- max_blocks=max_blocks - used_blocks,+ max_blocks=max_blocks - system_blocks,+ openai_dialect=openai_dialect,)return processed_messages, processed_system, remaining_points@@ -315,17 +417,43 @@ class AnthropicCacheControlHook(CustomPromptManagement):return ChatCompletionCachedContent(type="ephemeral")@staticmethod- def _stamped_as_judged(points: list[CacheControlInjectionPoint]) -> list[dict[str, object]]:+ def _stamped_as_judged(points: Sequence[CacheControlInjectionPoint]) -> Sequence[Mapping[str, object]]:"""Mark written-back points as having passed the client cache_control judgment.Builds copies because config-owned point dicts are shared acrossrequests; mutating them would leak the stamp into future requests."""- return [{**point, "_litellm_judged": True} for point in points]+ return AnthropicCacheControlHook._stamped(points, "_litellm_judged", True)++ @staticmethod+ def _judged_configured_points(+ points: Sequence[CacheControlInjectionPoint],+ messages: list[AllMessageValues],+ tools: list[object] | None,+ model: str,+ custom_llm_provider: str | None,+ ) -> Sequence[Mapping[str, object]] | None:+ if AnthropicCacheControlHook._should_stand_down(points, messages, None, tools):+ return None+ return AnthropicCacheControlHook._stamped_with_provider(points, model, custom_llm_provider)++ @staticmethod+ def _stamped_with_provider(+ points: Sequence[CacheControlInjectionPoint], model: str, custom_llm_provider: str | None+ ) -> Sequence[Mapping[str, object]]:+ if custom_llm_provider is None or not supports_openai_prompt_cache_breakpoint(model):+ return points+ return AnthropicCacheControlHook._stamped(points, "_litellm_provider", custom_llm_provider)++ @staticmethod+ def _stamped(+ points: Sequence[CacheControlInjectionPoint], key: str, value: object+ ) -> Sequence[Mapping[str, object]]:+ return [{**point, key: value} for point in points]@staticmethoddef _should_stand_down(- points: list[CacheControlInjectionPoint],+ points: Sequence[CacheControlInjectionPoint],messages: list[AllMessageValues],system: str | list | None,tools: list | None,@@ -359,11 +487,8 @@ class AnthropicCacheControlHook(CustomPromptManagement):carry the mark either at the top level (Anthropic shape) or nested under``function`` (OpenAI shape); the Anthropic chat transform accepts both."""- if any(AnthropicCacheControlHook._count_cache_control_blocks(msg) for msg in messages):+ if AnthropicCacheControlHook._count_request_cache_breakpoints(messages, system) > 0:return True- if isinstance(system, list):- if any(isinstance(block, dict) and block.get("cache_control") is not None for block in system):- return Trueif tools is not None:return any(isinstance(tool, dict)@@ -452,10 +577,13 @@ class AnthropicCacheControlHook(CustomPromptManagement):unchanged."""if non_default_params.get("cache_control_injection_points"):- if AnthropicCacheControlHook._should_stand_down(- non_default_params["cache_control_injection_points"], messages, None, tools- ):+ judged: Final = AnthropicCacheControlHook._judged_configured_points(+ non_default_params["cache_control_injection_points"], messages, tools, model, custom_llm_provider+ )+ if judged is None:non_default_params.pop("cache_control_injection_points")+ else:+ non_default_params["cache_control_injection_points"] = judgedreturnpoints: Final = AnthropicCacheControlHook.get_default_injection_points(messages=messages,@@ -513,11 +641,21 @@ class AnthropicCacheControlHook(CustomPromptManagement):if not injection_points:return messages, system+ openai_dialect: Final = AnthropicCacheControlHook._targets_openai_prompt_cache_breakpoint(+ model, custom_llm_provider+ )+ breakpoints_before: Final = AnthropicCacheControlHook._count_request_cache_breakpoints(messages, system)messages, system, remaining = AnthropicCacheControlHook.apply_to_anthropic_messages_request(messages=messages,system=system,injection_points=injection_points,+ openai_dialect=openai_dialect,)+ if (+ openai_dialect+ and AnthropicCacheControlHook._count_request_cache_breakpoints(messages, system) > breakpoints_before+ ):+ kwargs.setdefault("prompt_cache_options", PromptCacheOptions(mode="explicit"))if remaining:kwargs["cache_control_injection_points"] = AnthropicCacheControlHook._stamped_as_judged(remaining)return messages, systemdiff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.pyindex e4c1c9fc5cf..5904a14e79f 100644--- a/litellm/litellm_core_utils/prompt_templates/common_utils.py+++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py@@ -10,7 +10,7 @@ from collections.abc import Iterable, Mapping, Sequencefrom itertools import groupbyfrom os import PathLikefrom pathlib import Path-from typing import TYPE_CHECKING, Any, Final, Literal, cast+from typing import TYPE_CHECKING, Any, Final, Literal, TypeVar, castfrom openai.types.chat.chat_completion_custom_tool_param import (CustomFormatGrammar,@@ -1325,6 +1325,19 @@ def check_is_function_call(logging_obj: "LoggingClass") -> bool:return False+_MarkedT = TypeVar("_MarkedT")+++def _set_prompt_cache_breakpoint(target: object, marker: object) -> None:+ if marker is not None and isinstance(target, dict):+ target["prompt_cache_breakpoint"] = marker+++def with_prompt_cache_breakpoint(target: _MarkedT, marker: object) -> _MarkedT:+ _set_prompt_cache_breakpoint(target, marker)+ return target++def filter_value_from_dict(dictionary: dict, key: str, depth: int = 0) -> Any:"""Filters a value from a dictionarydiff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.pyindex e45414b4a73..2ee50e2a55b 100644--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py@@ -61,6 +61,7 @@ from openai.types.chat.chat_completion_chunk import Choice as OpenAIStreamingChofrom litellm.litellm_core_utils.prompt_templates.common_utils import (parse_tool_call_arguments,+ with_prompt_cache_breakpoint,)from litellm.litellm_core_utils.prompt_templates.factory import (THOUGHT_SIGNATURE_SEPARATOR,@@ -308,6 +309,11 @@ class LiteLLMAnthropicMessagesAdapter:# Fallback for non-dict objects (shouldn't happen in practice)cast(dict[str, object], target)["cache_control"] = cache_control+ @staticmethod+ def _add_prompt_cache_breakpoint_if_present(source: object, target: object) -> None:+ if isinstance(source, dict) and "prompt_cache_breakpoint" in source:+ with_prompt_cache_breakpoint(target, source["prompt_cache_breakpoint"])+def translatable_anthropic_params(self) -> list[str]:"""Which anthropic params, we need to translate to the openai format.@@ -368,6 +374,7 @@ class LiteLLMAnthropicMessagesAdapter:if content.get("type") == "text":text_obj = ChatCompletionTextObject(type="text", text=content.get("text", ""))self._add_cache_control_if_applicable(content, text_obj, model)+ self._add_prompt_cache_breakpoint_if_present(content, text_obj)new_user_content_list.append(text_obj)elif content.get("type") == "image":# Convert Anthropic image format to OpenAI format@@ -378,6 +385,7 @@ class LiteLLMAnthropicMessagesAdapter:image_url_obj = ChatCompletionImageUrlObject(url=openai_image_url)image_obj = ChatCompletionImageObject(type="image_url", image_url=image_url_obj)self._add_cache_control_if_applicable(content, image_obj, model)+ self._add_prompt_cache_breakpoint_if_present(content, image_obj)new_user_content_list.append(image_obj)elif content.get("type") == "document":# Convert Anthropic document format (PDF, etc.) to OpenAI format@@ -869,6 +877,7 @@ class LiteLLMAnthropicMessagesAdapter:continuetext_obj = ChatCompletionTextObject(type="text", text=text)self._add_cache_control_if_applicable(block, text_obj, model)+ self._add_prompt_cache_breakpoint_if_present(block, text_obj)text_parts.append(text_obj)return ChatCompletionSystemMessage(role="system", content=text_parts) if text_parts else None@@ -900,6 +909,7 @@ class LiteLLMAnthropicMessagesAdapter:"text": block.get("text", ""),}self._add_cache_control_if_applicable(block, text_block, model_name)+ self._add_prompt_cache_breakpoint_if_present(block, text_block)openai_system_content.append(text_block)if openai_system_content:new_messages.insert(diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.pyindex 21a8cb9501e..18f33c63e4d 100644--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py@@ -12,6 +12,7 @@ from typing import Any, Final, castfrom litellm.litellm_core_utils.prompt_templates.common_utils import (TOOL_RESULT_IMAGE_BOUNDARY,TOOL_RESULT_IMAGE_PLACEHOLDER,+ with_prompt_cache_breakpoint,)from litellm.litellm_core_utils.reasoning_effort_utils import (reasoning_effort_from_thinking_budget,@@ -82,7 +83,7 @@ class LiteLLMAnthropicToResponsesAPIAdapter:@staticmethoddef _translate_midturn_system_content_to_responses(content: str | Iterable[AnthropicSystemMessageContent],- ) -> list[dict[str, str]]: # mutable-ok: API message payload+ ) -> list[dict[str, object]]: # mutable-ok: API message payload"""Convert in-sequence system content to Responses input-text parts."""if isinstance(content, str):return (@@ -91,7 +92,9 @@ class LiteLLMAnthropicToResponsesAPIAdapter:if not isinstance(content, list):return [] # mutable-ok: API message payloadreturn [ # mutable-ok: API message payload- {"type": "input_text", "text": text} # mutable-ok: API message payload+ with_prompt_cache_breakpoint(+ {"type": "input_text", "text": text}, block.get("prompt_cache_breakpoint")+ ) # mutable-ok: API message payloadfor block in contentif isinstance(block, dict) and block.get("type") == "text" and (text := block.get("text")) # pyright: ignore[reportUnnecessaryIsInstance] # untrusted client payload]@@ -146,11 +149,20 @@ class LiteLLMAnthropicToResponsesAPIAdapter:continuebtype = block.get("type")if btype == "text":- user_parts.append({"type": "input_text", "text": block.get("text", "")})+ user_parts.append(+ with_prompt_cache_breakpoint(+ {"type": "input_text", "text": block.get("text", "")},+ block.get("prompt_cache_breakpoint"),+ )+ )elif btype == "image":url = self._translate_anthropic_image_source_to_url(cast(dict, block.get("source", {})))if url:- user_parts.append({"type": "input_image", "image_url": url})+ user_parts.append(+ with_prompt_cache_breakpoint(+ {"type": "input_image", "image_url": url}, block.get("prompt_cache_breakpoint")+ )+ )elif btype == "tool_result":tool_use_id = block.get("tool_use_id", "")inner = block.get("content")@@ -376,19 +388,36 @@ class LiteLLMAnthropicToResponsesAPIAdapter:anthropic_request["messages"],)+ input_items: Final = self.translate_messages_to_responses_input(messages_list)+ system: Final = anthropic_request.get("system")+ developer_parts: Final = (+ self._translate_midturn_system_content_to_responses(system)+ if isinstance(system, list)+ and any(isinstance(block, dict) and block.get("prompt_cache_breakpoint") is not None for block in system)+ else ()+ )+ if developer_parts:+ input_items.insert(+ 0,+ { # mutable-ok: API message payload+ "type": "message",+ "role": "developer",+ "content": developer_parts,+ },+ )+responses_kwargs: Final[dict[str, Any]] = {"model": model,- "input": self.translate_messages_to_responses_input(messages_list),+ "input": input_items,}- # system -> instructions- system: Final = anthropic_request.get("system")- if system:+ if system and not developer_parts:if isinstance(system, str):responses_kwargs["instructions"] = systemelif isinstance(system, list):- text_parts = [b.get("text", "") for b in system if isinstance(b, dict) and b.get("type") == "text"]- responses_kwargs["instructions"] = "\n".join(filter(None, text_parts))+ responses_kwargs["instructions"] = "\n".join(+ filter(None, (b.get("text", "") for b in system if isinstance(b, dict) and b.get("type") == "text"))+ )# max_tokens -> max_output_tokensmax_tokens: Final = anthropic_request.get("max_tokens")diff --git a/litellm/types/integrations/anthropic_cache_control_hook.py b/litellm/types/integrations/anthropic_cache_control_hook.pyindex da9b26ebbd8..3b3dcf5c86d 100644--- a/litellm/types/integrations/anthropic_cache_control_hook.py+++ b/litellm/types/integrations/anthropic_cache_control_hook.py@@ -1,6 +1,6 @@from typing import Literal-from typing_extensions import NotRequired, TypedDict+from typing_extensions import NotRequired, ReadOnly, TypedDictfrom litellm.types.llms.openai import ChatCompletionCachedContent@@ -13,6 +13,7 @@ class CacheControlMessageInjectionPoint(TypedDict):index: int | str | None # Optional: target by specific indexcontrol: ChatCompletionCachedContent | None_litellm_judged: NotRequired[bool] # Internal: written back by litellm once the client cache_control judgment ran+ _litellm_provider: NotRequired[ReadOnly[str]]class CacheControlToolConfigInjectionPoint(TypedDict):@@ -21,6 +22,7 @@ class CacheControlToolConfigInjectionPoint(TypedDict):location: Literal["tool_config"]control: ChatCompletionCachedContent | None_litellm_judged: NotRequired[bool] # Internal: written back by litellm once the client cache_control judgment ran+ _litellm_provider: NotRequired[ReadOnly[str]]CacheControlInjectionPoint = CacheControlMessageInjectionPoint | CacheControlToolConfigInjectionPointdiff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.pyindex 5179210f942..43e1d3a4e11 100644--- a/litellm/types/llms/anthropic.py+++ b/litellm/types/llms/anthropic.py@@ -9,6 +9,7 @@ from .openai import (ChatCompletionCachedContent,ChatCompletionRedactedThinkingBlock,ChatCompletionThinkingBlock,+ PromptCacheBreakpoint,)@@ -201,6 +202,7 @@ class AnthropicMessagesTextParam(TypedDict, total=False):type: Required[Literal["text"]]text: Required[str]cache_control: dict | ChatCompletionCachedContent | None+ prompt_cache_breakpoint: ReadOnly[PromptCacheBreakpoint]class AnthropicMessagesToolUseParam(TypedDict, total=False):@@ -261,6 +263,7 @@ class AnthropicMessagesImageParam(TypedDict, total=False):type: Required[Literal["image"]]source: Required[AnthropicContentParamSource | AnthropicContentParamSourceFileId | AnthropicContentParamSourceUrl]cache_control: dict | ChatCompletionCachedContent | None+ prompt_cache_breakpoint: ReadOnly[PromptCacheBreakpoint]class CitationsObject(TypedDict):@@ -347,6 +350,7 @@ class AnthropicSystemMessageContent(TypedDict, total=False):type: strtext: strcache_control: dict | ChatCompletionCachedContent | None+ prompt_cache_breakpoint: ReadOnly[PromptCacheBreakpoint]class AnthropicMessagesSystemMessageParam(TypedDict, total=False):diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.pyindex edfc50c99f6..4c7f416ab8c 100644--- a/litellm/types/llms/openai.py+++ b/litellm/types/llms/openai.py@@ -71,6 +71,7 @@ from pydantic import ()from typing_extensions import (NotRequired,+ ReadOnly,Required,TypedDict,override,@@ -510,6 +511,15 @@ class ChatCompletionCachedContent(TypedDict):ttl: NotRequired[Literal["5m", "1h"]]+class PromptCacheBreakpoint(TypedDict):+ mode: ReadOnly[Literal["explicit"]]+++class PromptCacheOptions(TypedDict, total=False):+ mode: ReadOnly[Literal["implicit", "explicit"]]+ ttl: ReadOnly[Literal["30m"]]++class ChatCompletionThinkingBlock(TypedDict, total=False):type: Required[Literal["thinking"]]thinking: str@@ -1148,6 +1158,7 @@ class ResponsesAPIOptionalRequestParams(TypedDict, total=False):max_tool_calls: int | Noneprompt_cache_key: str | Noneprompt_cache_retention: str | None+ prompt_cache_options: ReadOnly[PromptCacheOptions | None]stream_options: ResponsesAPIStreamOptions | Nonetop_logprobs: int | Nonepartial_images: int | None # Number of partial images to generate (1-3) for streaming image generationdiff --git a/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py b/tests/test_litellm/integrations/test_anthropic_cache_control_hook.pyindex cc43a424419..04876b9bf12 100644--- a/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py+++ b/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py@@ -14,7 +14,10 @@ import pytestsys.path.insert(0, os.path.abspath("../..")) # Adds the parent directory to the system-pathimport litellm-from litellm.integrations.anthropic_cache_control_hook import AnthropicCacheControlHook+from litellm.integrations.anthropic_cache_control_hook import (+ AnthropicCacheControlHook,+ supports_openai_prompt_cache_breakpoint,+)from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandlerfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import StandardCallbackDynamicParams@@ -1996,3 +1999,475 @@ class TestAnthropicPromptCachingEnvVars:"""An unparseable TTL must fall back to Anthropic's 5m default, never reach the provider verbatim."""_, ttl = self._import_litellm_with_env({"LITELLM_ANTHROPIC_PROMPT_CACHING_TTL": value})assert ttl is None+++def _contains_key(value, key) -> bool:+ if isinstance(value, dict):+ return key in value or any(_contains_key(v, key) for v in value.values())+ if isinstance(value, list):+ return any(_contains_key(v, key) for v in value)+ return False+++class TestOpenAIPromptCacheBreakpoint:+ """OpenAI GPT-5.6+ targets get content-block `prompt_cache_breakpoint` markers and a+ request-level `prompt_cache_options` instead of Anthropic `cache_control` (#37509)."""++ EXPLICIT = {"mode": "explicit"}+ SYSTEM_POINT = [{"location": "message", "role": "system"}]++ @staticmethod+ def _inject(messages, system, kwargs, model="openai/gpt-5.6", custom_llm_provider=None):+ return AnthropicCacheControlHook.maybe_inject_cache_control(+ copy.deepcopy(messages),+ copy.deepcopy(system),+ kwargs,+ model=model,+ custom_llm_provider=custom_llm_provider,+ )++ @staticmethod+ def _chat(messages, params, model="openai/gpt-5.6"):+ return AnthropicCacheControlHook().get_chat_completion_prompt(+ model=model,+ messages=copy.deepcopy(messages),+ non_default_params=params,+ prompt_id=None,+ prompt_variables=None,+ dynamic_callback_params={},+ )++ @pytest.mark.parametrize(+ "model,expected",+ [+ ("gpt-5.6", True),+ ("openai/gpt-5.6", True),+ ("gpt-5.6-sol", True),+ ("gpt-5.6-luna", True),+ ("gpt-5.7", True),+ ("gpt-6", True),+ ("GPT-5.6", True),+ ("gpt-5.5", False),+ ("gpt-5", False),+ ("gpt-5-chat-latest", False),+ ("gpt-4.1", False),+ ("o3", False),+ ("claude-sonnet-4-5", False),+ ],+ )+ def test_model_support_truth_table(self, model, expected):+ assert supports_openai_prompt_cache_breakpoint(model) is expected++ @pytest.mark.parametrize(+ "model,provider,expected",+ [+ ("openai/gpt-5.6", None, True),+ ("gpt-5.6", None, True),+ ("gpt-5.6", "openai", True),+ ("gpt-5.6", "azure", False),+ ("azure/gpt-5.6", None, False),+ ("openai/gpt-4.1", None, False),+ ("anthropic/claude-sonnet-4-5", None, False),+ ("no-provider-can-route-this-model", None, False),+ (None, "openai", False),+ ],+ )+ def test_dialect_resolution(self, model, provider, expected):+ assert AnthropicCacheControlHook._targets_openai_prompt_cache_breakpoint(model, provider) is expected++ def test_count_covers_both_marker_kinds(self):+ message = {+ "role": "user",+ "cache_control": {"type": "ephemeral"},+ "content": [+ {"type": "text", "text": "a", "prompt_cache_breakpoint": self.EXPLICIT},+ {"type": "text", "text": "b", "cache_control": {"type": "ephemeral"}},+ {"type": "text", "text": "c"},+ ],+ }+ assert AnthropicCacheControlHook._count_cache_control_blocks(message) == 3++ def test_v1_messages_string_system_gets_block_breakpoint(self):+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ messages, system = self._inject([{"role": "user", "content": "hi"}], "sys", kwargs)+ assert system == [{"type": "text", "text": "sys", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert messages == [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]+ assert kwargs == {"prompt_cache_options": self.EXPLICIT}+ assert not _contains_key(system, "cache_control")++ def test_v1_messages_list_system_marks_last_block_only(self):+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ system = [{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]+ _, result_system = self._inject([{"role": "user", "content": "hi"}], system, kwargs)+ assert result_system == [+ {"type": "text", "text": "a"},+ {"type": "text", "text": "b", "prompt_cache_breakpoint": self.EXPLICIT},+ ]+ assert kwargs["prompt_cache_options"] == self.EXPLICIT++ def test_v1_messages_targets_by_role(self):+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "first"}, {"type": "text", "text": "second"}]},+ {"role": "assistant", "content": [{"type": "text", "text": "reply"}]},+ {"role": "user", "content": "last"},+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "role": "user"}]}+ result, _ = self._inject(messages, None, kwargs)+ assert result[0]["content"] == [+ {"type": "text", "text": "first"},+ {"type": "text", "text": "second", "prompt_cache_breakpoint": self.EXPLICIT},+ ]+ assert result[1] == messages[1]+ assert result[2]["content"] == [{"type": "text", "text": "last", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert kwargs["prompt_cache_options"] == self.EXPLICIT++ def test_v1_messages_targets_by_index(self):+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "first"}]},+ {"role": "assistant", "content": [{"type": "text", "text": "reply"}]},+ {"role": "user", "content": [{"type": "text", "text": "last"}]},+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ result, _ = self._inject(messages, None, kwargs)+ assert result[:2] == messages[:2]+ assert result[2]["content"] == [{"type": "text", "text": "last", "prompt_cache_breakpoint": self.EXPLICIT}]++ def test_v1_messages_control_field_is_ignored(self):+ ttl_control = {"type": "ephemeral", "ttl": "1h"}+ kwargs = {+ "cache_control_injection_points": [+ {"location": "message", "role": "system", "control": ttl_control},+ {"location": "message", "index": -1, "control": ttl_control},+ ]+ }+ messages, system = self._inject([{"role": "user", "content": "hi"}], "sys", kwargs)+ assert system[0]["prompt_cache_breakpoint"] == self.EXPLICIT+ assert messages[0]["content"][-1]["prompt_cache_breakpoint"] == self.EXPLICIT+ assert not _contains_key(system, "cache_control")+ assert not _contains_key(messages, "cache_control")++ def test_v1_messages_keeps_caller_prompt_cache_options(self):+ caller_options = {"mode": "explicit", "ttl": "30m"}+ kwargs = {+ "cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT),+ "prompt_cache_options": dict(caller_options),+ }+ _, system = self._inject([{"role": "user", "content": "hi"}], "sys", kwargs)+ assert system[0]["prompt_cache_breakpoint"] == self.EXPLICIT+ assert kwargs["prompt_cache_options"] == caller_options++ def test_v1_messages_no_prompt_cache_options_when_nothing_injected(self):+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ messages, system = self._inject([{"role": "user", "content": "hi"}], None, kwargs)+ assert system is None+ assert "prompt_cache_options" not in kwargs+ assert not _contains_key(messages, "prompt_cache_breakpoint")++ def test_v1_messages_anthropic_target_unchanged(self):+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ _, system = self._inject(+ [{"role": "user", "content": "hi"}],+ "sys",+ kwargs,+ model="anthropic/claude-sonnet-4-5",+ custom_llm_provider="anthropic",+ )+ assert system == [{"type": "text", "text": "sys", "cache_control": {"type": "ephemeral"}}]+ assert kwargs == {}++ def test_v1_messages_older_openai_model_keeps_cache_control(self):+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ _, system = self._inject([{"role": "user", "content": "hi"}], "sys", kwargs, model="openai/gpt-4.1")+ assert system == [{"type": "text", "text": "sys", "cache_control": {"type": "ephemeral"}}]+ assert kwargs == {}++ def test_v1_messages_client_content_breakpoint_makes_configured_points_stand_down(self):+ messages = [{"role": "user", "content": [{"type": "text", "text": "hi", "prompt_cache_breakpoint": self.EXPLICIT}]}]+ kwargs = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ result, system = self._inject(messages, "sys", kwargs)+ assert result == messages+ assert system == "sys"+ assert kwargs == {}++ def test_v1_messages_client_system_breakpoint_makes_configured_points_stand_down(self):+ system = [{"type": "text", "text": "sys", "prompt_cache_breakpoint": self.EXPLICIT}]+ messages = [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]+ kwargs = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ result, result_system = self._inject(messages, system, kwargs)+ assert result == messages+ assert result_system == system+ assert kwargs == {}++ def test_chat_system_string_wrapped_with_block_breakpoint(self):+ params = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ messages = [{"role": "system", "content": "sys"}, {"role": "user", "content": "hi"}]+ _, processed, returned = self._chat(messages, params)+ assert processed[0] == {+ "role": "system",+ "content": [{"type": "text", "text": "sys", "prompt_cache_breakpoint": self.EXPLICIT}],+ }+ assert processed[1] == {"role": "user", "content": "hi"}+ assert returned is params+ assert returned == {"prompt_cache_options": self.EXPLICIT}++ def test_chat_list_content_marks_last_block(self):+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "look"},+ {"type": "image_url", "image_url": {"url": "https://example.com/a.png"}},+ ],+ }+ ]+ params = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ _, processed, _ = self._chat(messages, params)+ assert processed[0]["content"] == [+ {"type": "text", "text": "look"},+ {+ "type": "image_url",+ "image_url": {"url": "https://example.com/a.png"},+ "prompt_cache_breakpoint": self.EXPLICIT,+ },+ ]+ assert params["prompt_cache_options"] == self.EXPLICIT++ def test_chat_unprefixed_model_resolves_to_openai(self):+ params = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ _, processed, _ = self._chat([{"role": "system", "content": "sys"}], params, model="gpt-5.6")+ assert processed[0]["content"] == [{"type": "text", "text": "sys", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert params["prompt_cache_options"] == self.EXPLICIT++ def test_chat_keeps_caller_prompt_cache_options(self):+ params = {+ "cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT),+ "prompt_cache_options": {"mode": "implicit"},+ }+ self._chat([{"role": "system", "content": "sys"}], params)+ assert params["prompt_cache_options"] == {"mode": "implicit"}++ def test_chat_no_prompt_cache_options_when_nothing_injected(self):+ params = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ messages = [{"role": "user", "content": "hi"}]+ _, processed, _ = self._chat(messages, params)+ assert processed == messages+ assert params == {}++ @pytest.mark.parametrize("model", ["openai/gpt-4.1", "anthropic/claude-sonnet-4-5"])+ def test_chat_other_targets_keep_message_level_cache_control(self, model):+ params = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ _, processed, _ = self._chat([{"role": "system", "content": "sys"}], params, model=model)+ assert processed[0] == {"role": "system", "content": "sys", "cache_control": {"type": "ephemeral"}}+ assert params == {}++ def test_chat_client_breakpoint_makes_seeded_points_stand_down(self):+ params = {"cache_control_injection_points": copy.deepcopy(self.SYSTEM_POINT)}+ AnthropicCacheControlHook.maybe_seed_default_injection_points(+ non_default_params=params,+ messages=[+ {"role": "system", "content": "sys"},+ {"role": "user", "content": [{"type": "text", "text": "hi", "prompt_cache_breakpoint": self.EXPLICIT}]},+ ],+ model="openai/gpt-5.6",+ custom_llm_provider="openai",+ )+ assert params == {}++ def test_cap_counts_client_breakpoints_of_both_kinds(self):+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "a", "prompt_cache_breakpoint": self.EXPLICIT}]},+ {"role": "user", "content": [{"type": "text", "text": "b", "cache_control": {"type": "ephemeral"}}]},+ {"role": "user", "content": [{"type": "text", "text": "c", "prompt_cache_breakpoint": self.EXPLICIT}]},+ {"role": "user", "content": "d"},+ {"role": "user", "content": "e"},+ ]+ result = AnthropicCacheControlHook._apply_message_injections(+ points=[{"location": "message", "role": "user"}],+ messages=copy.deepcopy(messages),+ max_blocks=4,+ openai_dialect=True,+ )+ assert result[:3] == messages[:3]+ assert result[3]["content"] == [{"type": "text", "text": "d", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert result[4] == {"role": "user", "content": "e"}+++class TestOpenAIPromptCacheBreakpointPlacementRules:+ """OpenAI dialect only marks blocks OpenAI (and the /v1/messages bridges) can carry (#37509)."""++ EXPLICIT = {"mode": "explicit"}++ def _chat(self, messages, points, model="openai/gpt-5.6"):+ params = {"cache_control_injection_points": copy.deepcopy(points)}+ _, out, params = AnthropicCacheControlHook().get_chat_completion_prompt(+ model=model,+ messages=copy.deepcopy(messages),+ non_default_params=params,+ prompt_id=None,+ prompt_variables=None,+ dynamic_callback_params={},+ )+ return out, params++ def test_assistant_message_is_never_marked_on_chat_path(self):+ messages = [{"role": "user", "content": "q"}, {"role": "assistant", "content": "a"}]+ out, params = self._chat(messages, [{"location": "message", "role": "assistant"}])+ assert out == messages+ assert "prompt_cache_options" not in params++ def test_tool_message_text_is_marked_on_chat_path(self):+ messages = [+ {"role": "user", "content": "weather?"},+ {"role": "assistant", "content": None, "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "w", "arguments": "{}"}}]},+ {"role": "tool", "tool_call_id": "c1", "content": "sunny"},+ ]+ out, params = self._chat(messages, [{"location": "message", "index": -1}])+ assert out[2]["content"] == [{"type": "text", "text": "sunny", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert params["prompt_cache_options"] == self.EXPLICIT++ def test_tool_result_only_turn_is_skipped_on_v1_messages(self):+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "q"}]},+ {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "w", "input": {}}]},+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": "sunny"}]},+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ out, system = AnthropicCacheControlHook.maybe_inject_cache_control(+ copy.deepcopy(messages), None, kwargs, model="openai/gpt-5.6"+ )+ assert out == messages+ assert system is None+ assert "prompt_cache_options" not in kwargs++ def test_assistant_turn_is_skipped_on_v1_messages(self):+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "q"}]},+ {"role": "assistant", "content": [{"type": "text", "text": "a"}]},+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "role": "assistant"}]}+ out, _ = AnthropicCacheControlHook.maybe_inject_cache_control(+ copy.deepcopy(messages), None, kwargs, model="openai/gpt-5.6"+ )+ assert out == messages+ assert "prompt_cache_options" not in kwargs++ def test_text_after_tool_result_is_marked(self):+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "tool_result", "tool_use_id": "t1", "content": "sunny"},+ {"type": "text", "text": "thanks"},+ ],+ }+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ out, _ = AnthropicCacheControlHook.maybe_inject_cache_control(messages, None, kwargs, model="openai/gpt-5.6")+ assert out[0]["content"] == [+ {"type": "tool_result", "tool_use_id": "t1", "content": "sunny"},+ {"type": "text", "text": "thanks", "prompt_cache_breakpoint": self.EXPLICIT},+ ]+ assert kwargs["prompt_cache_options"] == self.EXPLICIT++ def test_marker_walks_back_to_last_eligible_block(self):+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "read this"},+ {"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "doc"}},+ ],+ }+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "index": -1}]}+ out, _ = AnthropicCacheControlHook.maybe_inject_cache_control(messages, None, kwargs, model="openai/gpt-5.6")+ assert out[0]["content"][0] == {"type": "text", "text": "read this", "prompt_cache_breakpoint": self.EXPLICIT}+ assert "prompt_cache_breakpoint" not in out[0]["content"][1]++ def test_skipped_block_does_not_consume_a_slot(self):+ messages = [{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t0", "content": "r"}]}] + [+ {"role": "user", "content": [{"type": "text", "text": f"m{i}"}]} for i in range(4)+ ]+ kwargs = {"cache_control_injection_points": [{"location": "message", "role": "user"}]}+ out, _ = AnthropicCacheControlHook.maybe_inject_cache_control(messages, None, kwargs, model="openai/gpt-5.6")+ assert "prompt_cache_breakpoint" not in out[0]["content"][0]+ assert all(msg["content"][0]["prompt_cache_breakpoint"] == self.EXPLICIT for msg in out[1:])+++class TestChatPathProviderStamp:+ """The chat path learns the caller's custom_llm_provider through the seeded points (#37509)."""++ POINTS = [{"location": "message", "role": "system"}]+ MESSAGES = [{"role": "system", "content": "sys"}, {"role": "user", "content": "hi"}]++ def _seed_and_run(self, model, custom_llm_provider):+ params = {"cache_control_injection_points": copy.deepcopy(self.POINTS)}+ AnthropicCacheControlHook.maybe_seed_default_injection_points(+ non_default_params=params,+ messages=copy.deepcopy(self.MESSAGES),+ model=model,+ custom_llm_provider=custom_llm_provider,+ )+ _, out, params = AnthropicCacheControlHook().get_chat_completion_prompt(+ model=model,+ messages=copy.deepcopy(self.MESSAGES),+ non_default_params=params,+ prompt_id=None,+ prompt_variables=None,+ dynamic_callback_params={},+ )+ return out, params++ def test_openai_compatible_provider_keeps_anthropic_style_markers(self):+ out, params = self._seed_and_run("gpt-5.6", "hosted_vllm")+ assert out[0] == {"role": "system", "content": "sys", "cache_control": {"type": "ephemeral"}}+ assert "prompt_cache_options" not in params++ def test_explicit_openai_provider_uses_openai_dialect(self):+ out, params = self._seed_and_run("gpt-5.6", "openai")+ assert out[0]["content"] == [{"type": "text", "text": "sys", "prompt_cache_breakpoint": {"mode": "explicit"}}]+ assert params["prompt_cache_options"] == {"mode": "explicit"}++ def test_bare_gpt_model_without_provider_resolves_to_openai(self):+ out, params = self._seed_and_run("gpt-5.6", None)+ assert out[0]["content"] == [{"type": "text", "text": "sys", "prompt_cache_breakpoint": {"mode": "explicit"}}]+ assert params["prompt_cache_options"] == {"mode": "explicit"}++ def test_points_keep_identity_for_models_below_gpt_5_6(self):+ points = copy.deepcopy(self.POINTS)+ params = {"cache_control_injection_points": points}+ AnthropicCacheControlHook.maybe_seed_default_injection_points(+ non_default_params=params,+ messages=copy.deepcopy(self.MESSAGES),+ model="anthropic/claude-sonnet-4-5",+ custom_llm_provider="anthropic",+ )+ assert params["cache_control_injection_points"] is points++ def test_provider_lookup_skipped_for_models_below_gpt_5_6(self):+ from unittest.mock import patch++ with patch.object(AnthropicCacheControlHook, "_resolve_provider") as resolve:+ assert AnthropicCacheControlHook._targets_openai_prompt_cache_breakpoint("gpt-4.1", None) is False+ assert AnthropicCacheControlHook._targets_openai_prompt_cache_breakpoint("my-custom-model", None) is False+ resolve.assert_not_called()+++class TestClientBreakpointsCountedOnce:+ def test_client_message_breakpoints_are_not_double_counted(self):+ messages = [{"role": "user", "content": [{"type": "text", "text": "m0", "cache_control": {"type": "ephemeral"}}]}] + [+ {"role": "user", "content": [{"type": "text", "text": f"m{i}"}]} for i in range(1, 4)+ ]+ out, system, _ = AnthropicCacheControlHook.apply_to_anthropic_messages_request(+ messages=messages,+ system="sys",+ injection_points=[+ {"location": "message", "role": "system"},+ {"location": "message", "index": -1},+ {"location": "message", "index": -2},+ {"location": "message", "index": -3},+ ],+ )+ marked = [msg["content"][0].get("cache_control") is not None for msg in out]+ assert marked == [True, False, True, True]+ assert system[0]["cache_control"] == {"type": "ephemeral"}diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.pyindex 1185893d428..abb976c3560 100644--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py@@ -3741,3 +3741,59 @@ def test_tool_result_plain_text_unchanged_by_openai_transform():assert len(tool_messages) == 1assert tool_messages[0]["content"] == "42 files found"assert _image_urls_in_user_messages(result) == []+++def test_translate_anthropic_to_openai_carries_prompt_cache_breakpoint_on_system_and_user_blocks():+ explicit = {"mode": "explicit"}+ openai_request, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(+ anthropic_message_request={+ "model": "gpt-5.6",+ "max_tokens": 64,+ "system": [{"type": "text", "text": "sys", "prompt_cache_breakpoint": explicit}],+ "messages": [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "hi", "prompt_cache_breakpoint": explicit},+ {+ "type": "image",+ "source": {"type": "url", "url": "https://example.com/a.png"},+ "prompt_cache_breakpoint": explicit,+ },+ ],+ }+ ],+ }+ )+ assert openai_request["messages"][0] == {+ "role": "system",+ "content": [{"type": "text", "text": "sys", "prompt_cache_breakpoint": explicit}],+ }+ user_content = openai_request["messages"][1]["content"]+ assert user_content[0] == {"type": "text", "text": "hi", "prompt_cache_breakpoint": explicit}+ assert user_content[1]["type"] == "image_url"+ assert user_content[1]["prompt_cache_breakpoint"] == explicit+++def test_translate_anthropic_to_openai_without_prompt_cache_breakpoint_adds_nothing():+ openai_request, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(+ anthropic_message_request={+ "model": "gpt-5.6",+ "max_tokens": 64,+ "system": [{"type": "text", "text": "sys"}],+ "messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}],+ }+ )+ assert openai_request["messages"][0] == {"role": "system", "content": [{"type": "text", "text": "sys"}]}+ assert openai_request["messages"][1]["content"] == [{"type": "text", "text": "hi"}]+++def test_translate_anthropic_messages_to_openai_carries_midturn_system_prompt_cache_breakpoint():+ explicit = {"mode": "explicit"}+ result = LiteLLMAnthropicMessagesAdapter().translate_anthropic_messages_to_openai(+ messages=[{"role": "system", "content": [{"type": "text", "text": "fix", "prompt_cache_breakpoint": explicit}]}],+ model="gpt-5.6",+ )+ assert result == [+ {"role": "system", "content": [{"type": "text", "text": "fix", "prompt_cache_breakpoint": explicit}]}+ ]diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_handler_output_config_passthrough.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_handler_output_config_passthrough.pyindex 615dc5cfebc..a944afc6152 100644--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_handler_output_config_passthrough.py+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_handler_output_config_passthrough.py@@ -230,3 +230,10 @@ class TestEmptyExtraKwargsPath:# dict-like result.completion_kwargs = result[0] if isinstance(result, tuple) else resultassert isinstance(completion_kwargs, dict)+++class TestPromptCacheOptionsForwarded:+ def test_prompt_cache_options_reaches_completion_kwargs(self):+ result = _call_prepare(extra_kwargs={"prompt_cache_options": {"mode": "explicit"}}, model="gpt-5.6")+ completion_kwargs = result[0] if isinstance(result, tuple) else result+ assert completion_kwargs["prompt_cache_options"] == {"mode": "explicit"}diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.pyindex 876213eda3f..8228d41cf82 100644--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.py+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.py@@ -1413,3 +1413,155 @@ class TestToolResultImages:outputs = [item for item in items if item.get("type") == "function_call_output"]assert outputs[0]["output"] == "screenshot saved"assert self._input_images(items) == []+++def _contains_key(value, key) -> bool:+ if isinstance(value, dict):+ return key in value or any(_contains_key(v, key) for v in value.values())+ if isinstance(value, list):+ return any(_contains_key(v, key) for v in value)+ return False+++class TestPromptCacheBreakpointToResponses:+ """OpenAI `prompt_cache_breakpoint` markers ride through the /v1/messages -> Responses bridge (#37509)."""++ EXPLICIT = {"mode": "explicit"}++ def test_system_with_breakpoint_becomes_leading_developer_message(self):+ request = _make_request(+ model="openai/gpt-5.6",+ system=[+ {"type": "text", "text": "Be concise."},+ {"type": "text", "text": "Be helpful.", "prompt_cache_breakpoint": self.EXPLICIT},+ ],+ )+ kwargs = _ADAPTER.translate_request(request)+ assert "instructions" not in kwargs+ assert kwargs["input"] == [+ {+ "type": "message",+ "role": "developer",+ "content": [+ {"type": "input_text", "text": "Be concise."},+ {"type": "input_text", "text": "Be helpful.", "prompt_cache_breakpoint": self.EXPLICIT},+ ],+ },+ {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hello"}]},+ ]++ def test_system_without_breakpoint_still_becomes_instructions(self):+ request = _make_request(system=[{"type": "text", "text": "Be concise."}, {"type": "text", "text": "Be helpful."}])+ kwargs = _ADAPTER.translate_request(request)+ assert kwargs["instructions"] == "Be concise.\nBe helpful."+ assert kwargs["input"] == [+ {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hello"}]}+ ]++ def test_system_string_still_becomes_instructions(self):+ kwargs = _ADAPTER.translate_request(_make_request(system="Be concise."))+ assert kwargs["instructions"] == "Be concise."+ assert kwargs["input"][0]["role"] == "user"++ def test_system_with_breakpoint_skips_non_text_blocks(self):+ request = _make_request(+ system=[+ {"type": "image", "source": {"type": "url", "url": "https://example.com/a.png"}},+ {"type": "text", "text": "only", "prompt_cache_breakpoint": self.EXPLICIT},+ ]+ )+ kwargs = _ADAPTER.translate_request(request)+ assert kwargs["input"][0] == {+ "type": "message",+ "role": "developer",+ "content": [{"type": "input_text", "text": "only", "prompt_cache_breakpoint": self.EXPLICIT}],+ }++ def test_user_text_and_image_blocks_carry_breakpoint(self):+ items = _ADAPTER.translate_messages_to_responses_input(+ [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "look", "prompt_cache_breakpoint": self.EXPLICIT},+ {+ "type": "image",+ "source": {"type": "url", "url": "https://example.com/a.png"},+ "prompt_cache_breakpoint": self.EXPLICIT,+ },+ ],+ }+ ]+ )+ assert items == [+ {+ "type": "message",+ "role": "user",+ "content": [+ {"type": "input_text", "text": "look", "prompt_cache_breakpoint": self.EXPLICIT},+ {+ "type": "input_image",+ "image_url": "https://example.com/a.png",+ "prompt_cache_breakpoint": self.EXPLICIT,+ },+ ],+ }+ ]++ def test_user_blocks_without_breakpoint_are_unchanged(self):+ items = _ADAPTER.translate_messages_to_responses_input(+ [{"role": "user", "content": [{"type": "text", "text": "look"}]}]+ )+ assert items == [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "look"}]}]++ def test_midturn_system_block_carries_breakpoint(self):+ items = _ADAPTER.translate_messages_to_responses_input(+ [{"role": "system", "content": [{"type": "text", "text": "fix", "prompt_cache_breakpoint": self.EXPLICIT}]}]+ )+ assert items == [+ {+ "type": "message",+ "role": "system",+ "content": [{"type": "input_text", "text": "fix", "prompt_cache_breakpoint": self.EXPLICIT}],+ }+ ]++ def test_assistant_and_tool_result_blocks_drop_breakpoint(self):+ items = _ADAPTER.translate_messages_to_responses_input(+ [+ {"role": "user", "content": [{"type": "text", "text": "q"}]},+ {+ "role": "assistant",+ "content": [+ {"type": "text", "text": "a", "prompt_cache_breakpoint": self.EXPLICIT},+ {"type": "tool_use", "id": "toolu_01", "name": "t", "input": {}},+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "toolu_01",+ "content": "r",+ "prompt_cache_breakpoint": self.EXPLICIT,+ }+ ],+ },+ ]+ )+ assert len(items) == 4+ assert not _contains_key(items, "prompt_cache_breakpoint")++ def test_prompt_cache_options_forwarded_to_responses_kwargs(self):+ from litellm.llms.anthropic.experimental_pass_through.responses_adapters.handler import (+ _build_responses_kwargs,+ )++ kwargs = _build_responses_kwargs(+ max_tokens=16,+ messages=[{"role": "user", "content": "hi"}],+ model="openai/gpt-5.6",+ extra_kwargs={"prompt_cache_options": {"mode": "explicit"}},+ )+ assert kwargs["prompt_cache_options"] == {"mode": "explicit"}diff --git a/tests/test_litellm/llms/openai/chat/test_openai_gpt_transformation.py b/tests/test_litellm/llms/openai/chat/test_openai_gpt_transformation.pyindex 41c2e215c60..45f1bdbfa85 100644--- a/tests/test_litellm/llms/openai/chat/test_openai_gpt_transformation.py+++ b/tests/test_litellm/llms/openai/chat/test_openai_gpt_transformation.py@@ -871,3 +871,48 @@ class TestToolMessageImageHoisting:result = request["messages"]assert [m.get("role") for m in result] == ["user", "assistant", "tool", "user"]assert result[3]["content"] == self.HOISTED_USER_CONTENT+++class TestOpenAIPromptCacheBreakpointChatPath:+ """Chat-path shape for OpenAI explicit prompt caching (#37509)."""++ EXPLICIT = {"mode": "explicit"}++ def test_prompt_cache_options_travels_in_extra_body(self):+ optional_params = litellm.get_optional_params(+ model="gpt-5.6", custom_llm_provider="openai", prompt_cache_options=self.EXPLICIT+ )+ assert optional_params["extra_body"]["prompt_cache_options"] == self.EXPLICIT+ assert "prompt_cache_options" not in optional_params++ def test_prompt_cache_options_is_not_a_supported_chat_param(self):+ assert "prompt_cache_options" not in OpenAIGPT5Config().get_supported_openai_params("gpt-5.6")+ assert "prompt_cache_options" not in OpenAIGPTConfig().get_supported_openai_params("gpt-4.1")++ def test_block_breakpoint_survives_transform_request(self):+ request = OpenAIGPT5Config().transform_request(+ model="gpt-5.6",+ messages=[+ {+ "role": "system",+ "content": [+ {+ "type": "text",+ "text": "sys",+ "prompt_cache_breakpoint": self.EXPLICIT,+ "cache_control": {"type": "ephemeral"},+ }+ ],+ },+ {"role": "user", "content": [{"type": "text", "text": "hi", "prompt_cache_breakpoint": self.EXPLICIT}]},+ ],+ optional_params={"extra_body": {"prompt_cache_options": self.EXPLICIT}},+ litellm_params={},+ headers={},+ )+ assert request["messages"][0]["content"] == [+ {"type": "text", "text": "sys", "prompt_cache_breakpoint": self.EXPLICIT}+ ]+ assert request["messages"][1]["content"] == [{"type": "text", "text": "hi", "prompt_cache_breakpoint": self.EXPLICIT}]+ assert request["extra_body"] == {"prompt_cache_options": self.EXPLICIT}+ assert "prompt_cache_options" not in requestdiff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.pyindex 151c51f1ca0..13b96dc9943 100644--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py@@ -1540,3 +1540,61 @@ class TestPhaseParameter:assert validated[0]["phase"] == "commentary"assert validated[1]["phase"] == "final_answer"assert "phase" not in validated[2]+++class TestPromptCacheOptionsOnResponsesPath:+ """`prompt_cache_options` and block-level `prompt_cache_breakpoint` survive the Responses transformation (#37509)."""++ OPTIONS = {"mode": "explicit", "ttl": "30m"}++ def test_prompt_cache_options_survives_optional_param_filter(self):+ from litellm.responses.utils import ResponsesAPIRequestUtils++ result = ResponsesAPIRequestUtils.get_requested_response_api_optional_param(+ {"prompt_cache_options": dict(self.OPTIONS), "temperature": 0.2, "not_a_responses_param": 1}+ )+ assert result["prompt_cache_options"] == self.OPTIONS+ assert result["temperature"] == 0.2+ assert "not_a_responses_param" not in result++ def test_prompt_cache_options_reaches_transformed_request(self):+ config = OpenAIResponsesAPIConfig()+ mapped = config.map_openai_params(+ response_api_optional_params={"prompt_cache_options": dict(self.OPTIONS)},+ model="gpt-5.6",+ drop_params=False,+ )+ result = config.transform_responses_api_request(+ model="gpt-5.6",+ input="hi",+ response_api_optional_request_params=mapped,+ litellm_params={},+ headers={},+ )+ assert result["prompt_cache_options"] == self.OPTIONS++ def test_prompt_cache_breakpoint_survives_cache_control_strip(self):+ result = OpenAIResponsesAPIConfig().transform_responses_api_request(+ model="gpt-5.6",+ input=[+ {+ "role": "user",+ "content": [+ {+ "type": "input_text",+ "text": "hi",+ "prompt_cache_breakpoint": {"mode": "explicit"},+ "cache_control": {"type": "ephemeral"},+ }+ ],+ }+ ],+ response_api_optional_request_params={},+ litellm_params={},+ headers={},+ )+ assert result["input"][0]["content"][0] == {+ "type": "input_text",+ "text": "hi",+ "prompt_cache_breakpoint": {"mode": "explicit"},+ }