Collaboration and Planning
Collaboration and Planning
Section titled “Collaboration and Planning”A group of isolated geniuses does not make a team. The power of CrewAI lies in how agents interact, share context, and plan their execution. In this chapter, we delve into the social dynamics of your AI crew.
Collaboration: How Agents Talk
Section titled “Collaboration: How Agents Talk”By default, agents in CrewAI are aware of their peers. When allow_delegation is set to True (default for many configurations, but explicit control is recommended), agents can pass tasks to one another or ask specific questions to experts within the crew.
For example, a ‘Researcher’ agent might realize they lack specific legal context. If a ‘Legal Advisor’ agent exists in the crew, the Researcher can query the Advisor directly without human intervention.
researcher = Agent( role="Researcher", goal="Find data", backstory="...", allow_delegation=True # Enables asking other agents for help)
writer = Agent( role="Writer", goal="Write report", backstory="...", allow_delegation=False # This agent performs tasks personally)The Planning Phase
Section titled “The Planning Phase”Before rushing into execution, it is often beneficial to measure twice and cut once. CrewAI introduces a Planning feature that adds a special step at the beginning of a Crew’s lifecycle.
When planning=True is enabled in the Crew configuration, CrewAI uses an LLM to digest all task descriptions and agent roles. It then formulates a step-by-step plan, which is added to the context of every agent. This ensures all agents are aligned on the global strategy before they start their individual tasks.
my_crew = Crew( agents=[researcher, writer], tasks=[task1, task2], planning=True, # Enables the pre-execution strategy phase planning_llm=ChatOpenAI(model="gpt-4o") # Optional: specify a planner model)Reasoning: Peeking into the Black Box
Section titled “Reasoning: Peeking into the Black Box”Understanding why an agent made a decision is crucial for debugging and trust. CrewAI provides powerful reasoning capabilities through its ReAct (Reasoning and Acting) implementation.
To inspect this process, you utilize the verbose flag. This outputs the agent’s internal monologue: the Thought (what it wants to do), the Action (tool usage), and the Observation (result of the tool).
Logging Reasoning to File
Section titled “Logging Reasoning to File”For long-running processes, watching the console is impractical. You can persist this reasoning to a file for later audit.
my_crew = Crew( agents=[...], tasks=[...], verbose=True, output_log_file="crew_execution_logs.txt")Reviewing these logs allows you to spot loops where an agent tries the same failed tool input repeatedly, or instances where an agent hallucinates a tool capability, helping you refine your backstory or tool definitions.