Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix: stop an agent that repeats the same tool call (#8164)
Gabe Milanic069653b8351
diff --git a/lib/crewai/src/crewai/agents/step_executor.py b/lib/crewai/src/crewai/agents/step_executor.pyindex a25db4e2f..db2c96a0e 100644--- a/lib/crewai/src/crewai/agents/step_executor.py+++ b/lib/crewai/src/crewai/agents/step_executor.py@@ -34,9 +34,11 @@ from crewai.utilities.agent_utils import (build_text_tool_calling_fallback_message,build_tool_calls_assistant_message,check_native_tool_support,+ check_repeated_tool_call,enforce_rpm_limit,execute_single_native_tool_call,extract_task_section,+ extract_tool_call_info,format_message_for_llm,is_native_tool_calling_unsupported_error,is_tool_call_list,@@ -150,6 +152,7 @@ class StepExecutor:"""start_time = time.monotonic()tool_calls_made: list[str] = []+ tool_call_counts: dict[int, int] = {}try:enforce_rpm_limit(self.request_within_rpm_limit)@@ -160,6 +163,7 @@ class StepExecutor:messages,todo,tool_calls_made,+ tool_call_counts=tool_call_counts,max_step_iterations=max_step_iterations,step_timeout=step_timeout,start_time=start_time,@@ -169,6 +173,7 @@ class StepExecutor:messages,todo,tool_calls_made,+ tool_call_counts=tool_call_counts,max_step_iterations=max_step_iterations,step_timeout=step_timeout,start_time=start_time,@@ -213,6 +218,7 @@ class StepExecutor:messages,todo,tool_calls_made,+ tool_call_counts=tool_call_counts,max_step_iterations=max_step_iterations,step_timeout=step_timeout,start_time=start_time,@@ -334,6 +340,7 @@ class StepExecutor:max_step_iterations: int = 15,step_timeout: int | None = None,start_time: float | None = None,+ tool_call_counts: dict[int, int] | None = None,) -> str:"""Execute step using text-parsed tool calling with a multi-turn loop.@@ -344,6 +351,8 @@ class StepExecutor:"""use_stop_words = self.llm.supports_stop_words() if self.llm else Falselast_tool_result = ""+ if tool_call_counts is None:+ tool_call_counts = {}for _ in range(max_step_iterations):if step_timeout and start_time:@@ -374,6 +383,14 @@ class StepExecutor:if isinstance(formatted, AgentAction):tool_calls_made.append(formatted.tool)tool_result = self._execute_text_tool_with_events(formatted, todo)+ check_repeated_tool_call(+ tool_call_counts,+ tool_name=formatted.tool,+ tool_args=formatted.tool_input,+ result=tool_result,+ agent=self.agent,+ task=self.task,+ )last_tool_result = tool_resultmessages.append({"role": "assistant", "content": answer_str})messages.append(self._build_observation_message(tool_result))@@ -550,6 +567,7 @@ class StepExecutor:max_step_iterations: int = 15,step_timeout: int | None = None,start_time: float | None = None,+ tool_call_counts: dict[int, int] | None = None,) -> str:"""Execute step using native function calling with a multi-turn loop.@@ -559,6 +577,8 @@ class StepExecutor:correct mistakes, and issue follow-up commands — all within one step."""accumulated_results: list[str] = []+ if tool_call_counts is None:+ tool_call_counts = {}for _ in range(max_step_iterations):if step_timeout and start_time:@@ -587,7 +607,7 @@ class StepExecutor:if isinstance(answer, list) and answer and is_tool_call_list(answer):result = self._execute_native_tool_calls(- answer, messages, todo, tool_calls_made+ answer, messages, todo, tool_calls_made, tool_call_counts)accumulated_results.append(result)continue@@ -607,6 +627,7 @@ class StepExecutor:messages: list[LLMMessage],todo: TodoItem,tool_calls_made: list[str],+ tool_call_counts: dict[int, int],) -> str:"""Execute a batch of native tool calls and return their results.@@ -637,6 +658,15 @@ class StepExecutor:if call_result.func_name:tool_calls_made.append(call_result.func_name)+ info = extract_tool_call_info(tool_call)+ check_repeated_tool_call(+ tool_call_counts,+ tool_name=call_result.func_name,+ tool_args=info[2] if info else None,+ result=call_result.result,+ agent=self.agent,+ task=self.task,+ )if call_result.result_as_answer:return str(call_result.result)diff --git a/lib/crewai/src/crewai/experimental/agent_executor.py b/lib/crewai/src/crewai/experimental/agent_executor.pyindex 5fa1a1e69..154b457a4 100644--- a/lib/crewai/src/crewai/experimental/agent_executor.py+++ b/lib/crewai/src/crewai/experimental/agent_executor.py@@ -92,6 +92,7 @@ from crewai.utilities.agent_utils import (_llm_stop_words_applied,build_text_tool_calling_fallback_message,check_native_tool_support,+ check_repeated_tool_call,enforce_rpm_limit,extract_tool_call_info,format_message_for_llm,@@ -234,6 +235,7 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):_console: Console = PrivateAttr(default_factory=Console)_last_parser_error: OutputParserError | None = PrivateAttr(default=None)_last_context_error: Exception | None = PrivateAttr(default=None)+ _tool_call_counts: dict[int, int] = PrivateAttr(default_factory=dict)_execution_lock: threading.Lock = PrivateAttr(default_factory=threading.Lock)_finalize_lock: threading.Lock = PrivateAttr(default_factory=threading.Lock)_finalize_called: bool = PrivateAttr(default=False)@@ -1465,7 +1467,7 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):# todo ↔ result (or exception) mapping.step_results: list[tuple[TodoItem, StepResult]] = []for todo, item in zip(ready, gathered, strict=True):- if isinstance(item, HookAborted):+ if isinstance(item, (HookAborted, ToolExecutionFailedError)):raise itemif isinstance(item, BaseException):error_msg = f"Error: {item!s}"@@ -1877,6 +1879,14 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):return "tool_completed"+ check_repeated_tool_call(+ self._tool_call_counts,+ tool_name=action.tool,+ tool_args=action.tool_input,+ result=tool_result.result,+ agent=self.agent,+ task=self.task,+ )result = self._handle_agent_action(action, tool_result)self.state.current_answer = result@@ -2329,6 +2339,15 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):crew=self.crew,)+ check_repeated_tool_call(+ self._tool_call_counts,+ tool_name=func_name,+ tool_args=args_dict,+ result=result,+ agent=self.agent,+ task=self.task,+ )+return {"call_id": call_id,"func_name": func_name,@@ -3031,6 +3050,9 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):self._finalize_called = Falseself.state.messages.clear()self.state.iterations = 0+ self._tool_call_counts.clear()+ if self.tools_handler:+ self.tools_handler.last_used_tool = Noneself.state.current_answer = Noneself.state.is_finished = Falseself.state.use_native_tools = False@@ -3119,6 +3141,9 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):self._finalize_called = Falseself.state.messages.clear()self.state.iterations = 0+ self._tool_call_counts.clear()+ if self.tools_handler:+ self.tools_handler.last_used_tool = Noneself.state.current_answer = Noneself.state.is_finished = Falseself.state.use_native_tools = False@@ -3462,6 +3487,9 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):self.state.current_answer = Noneself.state.is_finished = Falseself.state.iterations = 0+ self._tool_call_counts.clear()+ if self.tools_handler:+ self.tools_handler.last_used_tool = Noneself.state.use_native_tools = Falseself.state.pending_tool_calls = []self.state.plan = Nonediff --git a/lib/crewai/src/crewai/tools/tool_failure.py b/lib/crewai/src/crewai/tools/tool_failure.pyindex 839a62e92..46c80155e 100644--- a/lib/crewai/src/crewai/tools/tool_failure.py+++ b/lib/crewai/src/crewai/tools/tool_failure.py@@ -53,6 +53,9 @@ class ToolFailureReason(str, Enum):INVALID_INPUT = "invalid_input""""Arguments could not be parsed or validated into the tool's schema."""+ REPEATED_CALL = "repeated_call"+ """The agent kept making the same call and getting the same result."""+class ToolFailurePolicy(str, Enum):"""How an agent reacts when one of its tools reports a failure."""diff --git a/lib/crewai/src/crewai/utilities/agent_utils.py b/lib/crewai/src/crewai/utilities/agent_utils.pyindex a446f5c9d..f84d630b8 100644--- a/lib/crewai/src/crewai/utilities/agent_utils.py+++ b/lib/crewai/src/crewai/utilities/agent_utils.py@@ -32,8 +32,10 @@ from crewai.tools.structured_tool import (strip_composite_description_prefix,)from crewai.tools.tool_failure import (+ ToolExecutionFailedError,ToolFailure,ToolFailureReason,+ ToolFailureRecord,detect_tool_failure,failure_from_exception,handle_tool_failure,@@ -2115,3 +2117,55 @@ def _setup_after_llm_call_hooks(answer = hook_responsereturn answer+++MAX_REPEATED_TOOL_CALLS = 5+++def check_repeated_tool_call(+ counts: dict[int, int],+ *,+ tool_name: str,+ tool_args: dict[str, Any] | str | None,+ result: Any,+ agent: BaseAgent | LiteAgent | None = None,+ task: Task | None = None,+) -> None:+ """Stop a run that keeps repeating an identical tool call.++ Args:+ counts: Per-run tally of seen (tool, args, result) keys. Updated in place.+ tool_name: Name of the tool that was called.+ tool_args: Arguments the tool was called with.+ result: What the tool returned.+ agent: The calling agent, named in the error.+ task: The task in flight, named in the error.++ Raises:+ ToolExecutionFailedError: On the MAX_REPEATED_TOOL_CALLS-th identical call.+ """+ key = hash(+ json.dumps([tool_name, tool_args, str(result)], sort_keys=True, default=str)+ )+ counts[key] = counts.get(key, 0) + 1+ if counts[key] < MAX_REPEATED_TOOL_CALLS:+ return++ role = getattr(agent, "role", None)+ raise ToolExecutionFailedError(+ ToolFailureRecord(+ tool_name=tool_name,+ failure=ToolFailure(+ message=(+ f"Agent '{role}' called '{tool_name}' with the same "+ f"arguments {MAX_REPEATED_TOOL_CALLS} times and got the same "+ "result each time. Stopped because it is not making progress."+ ),+ reason=ToolFailureReason.REPEATED_CALL,+ ),+ tool_args=tool_args,+ agent_role=role,+ task_name=(task.name or task.description) if task else None,+ task_id=str(task.id) if task else None,+ )+ )diff --git a/lib/crewai/tests/tools/test_tool_failure.py b/lib/crewai/tests/tools/test_tool_failure.pyindex f4241e1ed..0997c53ae 100644--- a/lib/crewai/tests/tools/test_tool_failure.py+++ b/lib/crewai/tests/tools/test_tool_failure.py@@ -1775,3 +1775,198 @@ class TestSuccessfulToolsUnaffected:agent.llm = ScriptedLLM(_slack_steps())crew.kickoff()assert len(agent.last_tool_failures) == 1, "records must not accumulate"+++from crewai.utilities.agent_utils import MAX_REPEATED_TOOL_CALLS # noqa: E402+++class LoopingNativeLLM(LLM):+ """Asks for the same native tool call forever, like a stuck agent."""++ def __new__(cls, *args: Any, **kwargs: Any) -> "LoopingNativeLLM":+ return object.__new__(cls)++ def __init__(self) -> None:+ super().__init__(model="gpt-4o")+ self.calls = 0++ def call(self, messages, tools=None, callbacks=None, available_functions=None, **kw): # noqa: ANN001, ANN003+ self.calls += 1+ return [+ {+ "id": f"call_{self.calls}",+ "type": "function",+ "function": {"name": "echo", "arguments": '{"text": "hi"}'},+ }+ ]++ def supports_function_calling(self) -> bool:+ return True+++class TestRepeatedToolCallStopsTheRun:+ """Same call + same result means no progress: stop before the context fills."""++ def test_helper_raises_on_the_last_identical_call(self) -> None:+ from crewai.utilities.agent_utils import check_repeated_tool_call++ agent = Agent(role="Looper", goal="g", backstory="b")+ counts: dict[int, int] = {}+ for _ in range(MAX_REPEATED_TOOL_CALLS - 1):+ check_repeated_tool_call(+ counts, tool_name="echo", tool_args={"text": "hi"}, result="x", agent=agent+ )++ with pytest.raises(ToolExecutionFailedError) as exc_info:+ check_repeated_tool_call(+ counts, tool_name="echo", tool_args={"text": "hi"}, result="x", agent=agent+ )++ record = exc_info.value.record+ assert record.failure.reason is ToolFailureReason.REPEATED_CALL+ assert record.tool_name == "echo"+ assert "Looper" in str(exc_info.value)++ def test_helper_allows_repeats_whose_result_changes(self) -> None:+ from crewai.utilities.agent_utils import check_repeated_tool_call++ counts: dict[int, int] = {}+ for status in ["queued", "running", "running-2", "running-3", "done", "done-2"]:+ check_repeated_tool_call(+ counts, tool_name="poll", tool_args={"id": 1}, result=status+ )++ def test_native_loop_stops_the_crew_early(self) -> None:+ llm = LoopingNativeLLM()+ agent = Agent(+ role="Looper", goal="g", backstory="b", llm=llm, tools=[WorkingTool()]+ )+ task = Task(description="echo", expected_output="x", agent=agent)++ with pytest.raises(ToolExecutionFailedError) as exc_info:+ Crew(agents=[agent], tasks=[task]).kickoff()++ assert exc_info.value.record.failure.reason is ToolFailureReason.REPEATED_CALL+ assert "Looper" in str(exc_info.value)+ # Stopped on the last allowed call, not after max_iter, and not retried.+ assert llm.calls == MAX_REPEATED_TOOL_CALLS++ def test_text_loop_stops_the_crew_early(self) -> None:+ step = 'Thought: again\nAction: echo\nAction Input: {"text": "hi"}'+ llm = ScriptedLLM([step])+ agent = Agent(+ role="Looper", goal="g", backstory="b", llm=llm, tools=[WorkingTool()]+ )+ task = Task(description="echo", expected_output="x", agent=agent)++ with pytest.raises(ToolExecutionFailedError) as exc_info:+ Crew(agents=[agent], tasks=[task]).kickoff()++ assert exc_info.value.record.failure.reason is ToolFailureReason.REPEATED_CALL+ # ToolUsage nudges every other identical call, so the count is not exact.+ assert llm._index < agent.max_iter++ def test_planning_step_loop_raises_instead_of_failing_the_step(self) -> None:+ from crewai.agents.step_executor import StepExecutor+ from crewai.utilities.planning_types import TodoItem+ from crewai.utilities.step_execution_context import StepExecutionContext++ executor = StepExecutor(+ llm=LoopingNativeLLM(),+ tools=[],+ agent=Agent(role="Looper", goal="g", backstory="b"),+ original_tools=[WorkingTool()],+ )++ with pytest.raises(ToolExecutionFailedError) as exc_info:+ executor.execute(+ TodoItem(step_number=1, description="echo"),+ StepExecutionContext(task_description="t", task_goal="g"),+ )++ assert exc_info.value.record.failure.reason is ToolFailureReason.REPEATED_CALL++ def test_helper_raises_on_an_alternating_loop(self) -> None:+ """A, B, A, B... is still a loop; counting only consecutive repeats misses it."""+ from crewai.utilities.agent_utils import check_repeated_tool_call++ counts: dict[int, int] = {}+ with pytest.raises(ToolExecutionFailedError):+ for _ in range(MAX_REPEATED_TOOL_CALLS):+ for text in ("a", "b"):+ check_repeated_tool_call(+ counts, tool_name="echo", tool_args={"text": text}, result=text+ )++ @pytest.mark.asyncio+ async def test_parallel_step_loop_stops_the_agent(self) -> None:+ from crewai.experimental.agent_executor import AgentExecutor+ from crewai.utilities.planning_types import TodoItem++ record = ToolFailureRecord(+ tool_name="echo",+ failure=ToolFailure(message="loop", reason=ToolFailureReason.REPEATED_CALL),+ )++ class LoopingStepExecutor:+ def execute(self, *args: Any, **kwargs: Any) -> Any:+ raise ToolExecutionFailedError(record)++ agent = Agent(role="Looper", goal="g", backstory="b", llm=LoopingNativeLLM())+ executor = AgentExecutor(agent=agent, llm=agent.llm, task=None)+ executor.state.todos.items = [+ TodoItem(step_number=1, description="first"),+ TodoItem(step_number=2, description="second"),+ ]+ step_executor = LoopingStepExecutor()+ object.__setattr__(executor, "_ensure_step_executor", lambda: step_executor)++ with pytest.raises(ToolExecutionFailedError):+ await executor.execute_todos_parallel()++ def test_parallel_steps_keep_separate_tallies(self) -> None:+ """Two steps sharing one StepExecutor must not add up each other's calls."""+ import threading++ from crewai.agents.step_executor import StepExecutor+ from crewai.utilities.planning_types import TodoItem+ from crewai.utilities.step_execution_context import StepExecutionContext++ calls_per_step = MAX_REPEATED_TOOL_CALLS - 1+ # Both steps call the tool in lockstep, so their calls interleave.+ barrier = threading.Barrier(2, timeout=10)++ class LockstepTool(WorkingTool):+ def _run(self, text: str) -> Any:+ barrier.wait()+ return f"echoed: {text}"++ class FewCallsLLM(LoopingNativeLLM):+ def call(self, messages, tools=None, callbacks=None, available_functions=None, **kw): # noqa: ANN001, ANN003+ if sum(m.get("role") == "tool" for m in messages) >= calls_per_step:+ return "done"+ return super().call(messages, tools, callbacks, available_functions, **kw)++ executor = StepExecutor(+ llm=FewCallsLLM(),+ tools=[],+ agent=Agent(role="Looper", goal="g", backstory="b"),+ original_tools=[LockstepTool()],+ )+ context = StepExecutionContext(task_description="t", task_goal="g")+ errors: list[BaseException] = []++ def run(step: int) -> None:+ try:+ executor.execute(TodoItem(step_number=step, description="echo"), context)+ except BaseException as e: # noqa: BLE001+ errors.append(e)+ barrier.abort()++ threads = [threading.Thread(target=run, args=(n,)) for n in (1, 2)]+ for t in threads:+ t.start()+ for t in threads:+ t.join(timeout=30)++ assert errors == []