Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/crewai/src/crewai/agents/crew_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)))
Expand Down
36 changes: 22 additions & 14 deletions lib/crewai/src/crewai/experimental/agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down