05. Multi-Agent with CrewAI
Overview
In this session, we replace manual multi-agent orchestration with CrewAI—a high-level framework that handles role-playing, task delegation, and workflow management out of the box.
What is CrewAI?
CrewAI is a framework for orchestrating role-playing autonomous AI agents. Think of it as assembling a virtual team where each agent has a specific role, goal, and backstory.
Why CrewAI?
| Manual Orchestration | CrewAI |
|---|---|
| Pass strings between agents manually | Automatic message passing |
| Build your own supervisor logic | Built-in Sequential/Hierarchical processes |
| No persona management | Rich role, goal, backstory system |
| DIY tool sharing | Seamless tool delegation |
Core Concepts
1. Agents
Agents are the workers in your crew. Each has a distinct personality and purpose.
from crewai import Agent
researcher = Agent(
role='Senior Researcher',
goal='Uncover groundbreaking technologies in AI',
backstory="""You are a leading tech researcher.
You're expert at finding latest news and papers.""",
verbose=True,
allow_delegation=False # Can this agent delegate to others?
)
writer = Agent(
role='Tech Content Strategist',
goal='Craft compelling content on tech advancements',
backstory="""You are a renowned Content Strategist,
known for simplifying complex topics.""",
verbose=True,
allow_delegation=False
)Pro Tip: The backstory field is crucial! It shapes how the agent "thinks" and responds. A well-crafted backstory leads to more consistent, in-character outputs.
2. Tasks
Tasks are specific assignments given to agents.
from crewai import Task
task1 = Task(
description="""Analyze the latest trends in 'AI Agents' for 2024.
Identify key pros and cons.""",
expected_output="A bullet list summary of top 3 trends.",
agent=researcher # Assign to specific agent
)
task2 = Task(
description="""Using the insights provided, write a short LinkedIn post.
Keep it punchy and professional.""",
expected_output="A LinkedIn post with hashtags.",
agent=writer
)3. Crew
The Crew orchestrates everything—managing workflow and communication.
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=True,
process=Process.sequential # or Process.hierarchical
)
# One line to run the entire team!
result = crew.kickoff()Process Types
Sequential Process: Tasks run one after another. Output from Task 1 becomes input for Task 2.
Best for: Linear workflows where each step depends on the previous one.
Adding Tools to Agents
CrewAI agents can use tools just like our ReAct agents:
from crewai_tools import SerperDevTool, WebsiteSearchTool
search_tool = SerperDevTool()
web_tool = WebsiteSearchTool()
researcher = Agent(
role='Research Analyst',
goal='Find accurate information',
backstory='Expert researcher with access to the web',
tools=[search_tool, web_tool], # Attach tools here
verbose=True
)Hands-on Practice
In the notebook, you will:
Install CrewAI
pip install crewai crewai-toolsDefine Your Agents
Create specialized agents with unique roles and backstories
Create Tasks
Assign specific objectives to each agent
Form the Crew
Combine agents and tasks with a process type
Kickoff!
Run the crew and observe the collaboration
Real-World Applications
| Use Case | Crew Setup |
|---|---|
| Content Pipeline | Researcher → Writer → Editor → Publisher |
| Code Review | Developer → Reviewer → Security Analyst |
| Customer Support | Classifier → Specialist → QA Agent |
| Market Research | Data Collector → Analyst → Report Writer |
CrewAI vs LangGraph
| Aspect | CrewAI | LangGraph |
|---|---|---|
| Abstraction Level | High (role-based) | Low (graph-based) |
| Learning Curve | Gentle | Steeper |
| Flexibility | Opinionated | Highly flexible |
| Best For | Team simulations | Custom workflows |
| State Management | Automatic | Manual control |
When to use what?
- Use CrewAI when you want quick multi-agent setups with role-playing
- Use LangGraph when you need fine-grained control over state and transitions
References & Further Reading
Academic Papers
-
"Communicative Agents for Software Development" (ChatDev) - Chen et al., 2023
- arXiv:2307.07924 (opens in a new tab)
- Multi-agent collaboration for code generation
-
"AutoGen: Enabling Next-Gen LLM Applications" - Microsoft, 2023
- arXiv:2308.08155 (opens in a new tab)
- Framework comparison and multi-agent patterns
-
"AgentVerse: Facilitating Multi-Agent Collaboration" - Chen et al., 2023
- arXiv:2308.10848 (opens in a new tab)
- Dynamics of multi-agent systems
Next Steps
In the Weekend Project, you'll build a Virtual Startup Team where a CEO, CTO, and CMO collaborate to create a business pitch!