From cb6b748609ef0266862e50ca3ff1116d6ffbd398 Mon Sep 17 00:00:00 2001 From: fedyaahmed135-star Date: Fri, 29 May 2026 10:42:57 +0400 Subject: [PATCH] fix: guard cache_breakpoint marking behind is_anthropic check --- .../src/crewai/agents/crew_agent_executor.py | 19 +++++++--- .../src/crewai/experimental/agent_executor.py | 36 +++++++++++-------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/crewai/src/crewai/agents/crew_agent_executor.py b/lib/crewai/src/crewai/agents/crew_agent_executor.py index f985da83c..b51d5cb11 100644 --- a/lib/crewai/src/crewai/agents/crew_agent_executor.py +++ b/lib/crewai/src/crewai/agents/crew_agent_executor.py @@ -174,7 +174,18 @@ def _setup_messages(self, inputs: dict[str, Any]) -> None: if provider.setup_messages(cast(ExecutorContext, cast(object, self))): return - from crewai.llms.cache import mark_cache_breakpoint + use_cache_breakpoint = ( + hasattr(self, "llm") + and self.llm is not None + and getattr(self.llm, "is_anthropic", False) + ) + if use_cache_breakpoint: + from crewai.llms.cache import mark_cache_breakpoint + def _maybe_mark(msg): + return mark_cache_breakpoint(msg) + else: + def _maybe_mark(msg): + return msg if self.prompt is not None and "system" in self.prompt: system_prompt = self._format_prompt( @@ -187,17 +198,17 @@ def _setup_messages(self, inputs: dict[str, Any]) -> None: # prefix; end-of-user caches the per-task stable prefix across # ReAct-loop iterations. self.messages.append( - mark_cache_breakpoint( + _maybe_mark( format_message_for_llm(system_prompt, role="system") ) ) self.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) elif self.prompt is not None: user_prompt = self._format_prompt(self.prompt.get("prompt", ""), inputs) self.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) provider.post_setup_messages(cast(ExecutorContext, cast(object, self))) diff --git a/lib/crewai/src/crewai/experimental/agent_executor.py b/lib/crewai/src/crewai/experimental/agent_executor.py index 81b431a24..7fe820723 100644 --- a/lib/crewai/src/crewai/experimental/agent_executor.py +++ b/lib/crewai/src/crewai/experimental/agent_executor.py @@ -2643,26 +2643,30 @@ def invoke( "AgentExecutor.llm or .prompt is unset; the executor was " "not fully restored or initialized before execution." ) - if "system" in self.prompt: + use_cache_breakpoint = getattr(self.llm, "is_anthropic", False) + if use_cache_breakpoint: from crewai.llms.cache import mark_cache_breakpoint - + def _maybe_mark(msg): + return mark_cache_breakpoint(msg) + else: + def _maybe_mark(msg): + return msg + if "system" in self.prompt: prompt = cast("SystemPromptResult", self.prompt) system_prompt = self._format_prompt(prompt["system"], inputs) user_prompt = self._format_prompt(prompt["user"], inputs) self.state.messages.append( - mark_cache_breakpoint( + _maybe_mark( format_message_for_llm(system_prompt, role="system") ) ) self.state.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) else: - from crewai.llms.cache import mark_cache_breakpoint - user_prompt = self._format_prompt(self.prompt["prompt"], inputs) self.state.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) self._inject_files_from_inputs(inputs) @@ -2749,26 +2753,30 @@ async def invoke_async(self, inputs: dict[str, Any]) -> dict[str, Any]: "AgentExecutor.llm or .prompt is unset; the executor was " "not fully restored or initialized before execution." ) - if "system" in self.prompt: + use_cache_breakpoint = getattr(self.llm, "is_anthropic", False) + if use_cache_breakpoint: from crewai.llms.cache import mark_cache_breakpoint - + def _maybe_mark(msg): + return mark_cache_breakpoint(msg) + else: + def _maybe_mark(msg): + return msg + if "system" in self.prompt: prompt = cast("SystemPromptResult", self.prompt) system_prompt = self._format_prompt(prompt["system"], inputs) user_prompt = self._format_prompt(prompt["user"], inputs) self.state.messages.append( - mark_cache_breakpoint( + _maybe_mark( format_message_for_llm(system_prompt, role="system") ) ) self.state.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) else: - from crewai.llms.cache import mark_cache_breakpoint - user_prompt = self._format_prompt(self.prompt["prompt"], inputs) self.state.messages.append( - mark_cache_breakpoint(format_message_for_llm(user_prompt)) + _maybe_mark(format_message_for_llm(user_prompt)) ) self._inject_files_from_inputs(inputs)