From a0fad289c5a10a941c0a3a15e5f6fea9c8894dc1 Mon Sep 17 00:00:00 2001 From: lorenzejay Date: Thu, 22 Jan 2026 11:42:52 -0800 Subject: [PATCH] Enhance tool handling and delegation tracking in agent executors - Implemented immediate return for tools with result_as_answer=True in crew_agent_executor.py. - Added delegation tracking functionality in agent_utils.py to increment delegations when specific tools are used. - Updated tool usage logic to handle caching more effectively in tool_usage.py. - Enhanced test cases to validate new delegation features and tool caching behavior. This update improves the efficiency of tool execution and enhances the delegation capabilities of agents. --- .../src/crewai/experimental/agent_executor.py | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/crewai/src/crewai/experimental/agent_executor.py b/lib/crewai/src/crewai/experimental/agent_executor.py index a1ffc8cfe..3afd770d5 100644 --- a/lib/crewai/src/crewai/experimental/agent_executor.py +++ b/lib/crewai/src/crewai/experimental/agent_executor.py @@ -608,9 +608,15 @@ class AgentExecutor(Flow[AgentReActState], CrewAgentExecutorMixin): from_cache = False input_str = json.dumps(args_dict) if args_dict else "" if self.tools_handler and self.tools_handler.cache: - cached_result = self.tools_handler.cache.read(tool=func_name, input=input_str) + cached_result = self.tools_handler.cache.read( + tool=func_name, input=input_str + ) if cached_result is not None: - result = str(cached_result) if not isinstance(cached_result, str) else cached_result + result = ( + str(cached_result) + if not isinstance(cached_result, str) + else cached_result + ) from_cache = True # Emit tool usage started event @@ -644,14 +650,20 @@ class AgentExecutor(Flow[AgentReActState], CrewAgentExecutorMixin): and hasattr(original_tool, "cache_function") and original_tool.cache_function ): - should_cache = original_tool.cache_function(args_dict, raw_result) + should_cache = original_tool.cache_function( + args_dict, raw_result + ) if should_cache: self.tools_handler.cache.add( tool=func_name, input=input_str, output=raw_result ) # Convert to string for message - result = str(raw_result) if not isinstance(raw_result, str) else raw_result + result = ( + str(raw_result) + if not isinstance(raw_result, str) + else raw_result + ) except Exception as e: result = f"Error executing tool: {e}" if self.task: