CrewAI Tutorial: Role-Based Multi-Agent Collaboration (2026)
Build a full CrewAI system with Writer, Researcher, and Editor agents. Complete Python code with Agent, Task, and Crew setup, plus honest comparison with AutoGen.
Get more content like this on Telegram!
Daily AI tips, notes & resources — free
The first time I used CrewAI, I was skeptical. The API looked almost too clean. Surely something had to be wrong with a framework where you could describe a multi-agent workflow in 30 lines and have it actually work.
Turns out I was wrong about that. CrewAI is genuinely well-designed. The role-based abstraction maps naturally onto how most people think about team workflows, and the defaults are sensible enough that you don't have to fight the framework to get something working.
This tutorial builds a complete three-agent content production system — Researcher, Writer, Editor — from scratch. Along the way, I'll explain every design decision and show you where CrewAI's model shines and where it has limits.
If you want to compare approaches before committing to a framework, multi-agent frameworks comparison 2026 has a full side-by-side breakdown.
CrewAI's Core Concepts
CrewAI has three main primitives:
Agent — An AI crew member with a role, goal, and backstory. The role and goal shape how the agent approaches tasks. The backstory adds context that influences tone and perspective.
Task — A unit of work with a description, expected output, and an assigned agent. Tasks can depend on each other and share context.
Crew — The team that coordinates agents and tasks. The Crew defines the execution process (sequential or hierarchical) and manages the overall workflow.
The relationship is clean: Agents are the who, Tasks are the what, Crew is the how they coordinate.
This differs from AutoGen's conversational model where agents negotiate tasks through a chat thread. CrewAI's model is more deterministic — you define up front who does what, and the framework executes it. Less flexible, but much more predictable.
For a deeper comparison of these approaches, multi-agent vs single agent covers the architectural tradeoffs.
Setup
pip install crewai crewai-tools langchain-openai python-dotenv
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool, FileReadTool
from langchain_openai import ChatOpenAI
load_dotenv()
# You can configure the LLM globally or per-agent
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
# Optional: configure a search tool for the Researcher
search_tool = SerperDevTool()
Defining the Agents
Each agent needs a role, goal, and backstory. These three fields are more important than they might seem — they're injected into the agent's system prompt and meaningfully affect how the agent approaches tasks.
# Agent 1: Researcher
researcher = Agent(
role="Senior Research Analyst",
goal="""Find accurate, current, and relevant information on the given topic.
Prioritize primary sources, recent publications, and authoritative data.
Always verify claims across multiple sources before including them.""",
backstory="""You spent 10 years as an academic researcher before joining an AI-driven
content studio. You have a deep intolerance for unsourced claims and a habit of
fact-checking everything twice. You know how to find information that isn't on the
first page of Google.""",
tools=[search_tool],
verbose=True,
allow_delegation=False, # This agent focuses on its own task
llm=ChatOpenAI(model="gpt-4o", temperature=0.1)
)
# Agent 2: Writer
writer = Agent(
role="Content Writer",
goal="""Transform research findings into engaging, well-structured articles.
Write with clarity and authority. Use concrete examples. Vary sentence length.
Target a technically informed but non-specialist audience.""",
backstory="""You've written for major tech publications and know how to explain
complex topics without talking down to readers. You hate jargon for jargon's sake
and always ask yourself whether the sentence earns its place in the piece.""",
tools=[],
verbose=True,
allow_delegation=False,
llm=ChatOpenAI(model="gpt-4o", temperature=0.4) # Slightly warmer for creative output
)
# Agent 3: Editor
editor = Agent(
role="Senior Editor",
goal="""Review and improve written content for accuracy, clarity, and quality.
Check facts against research findings. Improve structure where needed.
Ensure the piece meets publication standards and serves the reader's needs.""",
backstory="""You've edited technology content for 15 years. You have a sharp eye
for when an article buries the lede, relies on unsupported claims, or uses five
words where two would do. You improve without rewriting — you make the writer's
voice better, not different.""",
tools=[],
verbose=True,
allow_delegation=False,
llm=ChatOpenAI(model="gpt-4o", temperature=0.1)
)
A few design notes:
allow_delegation=Falseprevents agents from delegating tasks to each other unprompted. In a tightly orchestrated pipeline, you usually want to control delegation explicitly.- Different temperature settings for the writer (0.4) vs researcher and editor (0.1) gives creative flexibility where it's valuable while keeping structured agents consistent.
- The backstory is real work, not decoration. Agents with rich, specific backstories produce noticeably more role-appropriate responses.
Defining the Tasks
Tasks are where you specify what each agent needs to produce and how outputs should be formatted.
research_task = Task(
description="""Research the following topic thoroughly: {topic}
Your research should cover:
1. Current state of the field — what's true as of 2026
2. Key statistics and data points (with sources)
3. Notable examples, case studies, or implementations
4. Common misconceptions or areas of debate
5. Recent developments (past 12 months)
Provide well-organized findings that a writer can immediately use.
Include source URLs for all major claims.""",
expected_output="""A structured research document with:
- Executive Summary (2-3 sentences)
- Key Facts & Statistics (bulleted, with sources)
- Notable Examples (3-5 concrete cases)
- Controversies or Open Questions
- Source List (URLs with brief description)
Minimum 600 words of findings.""",
agent=researcher,
output_file="research_findings.md" # Optional: save to file
)
writing_task = Task(
description="""Using the research findings, write a comprehensive article on: {topic}
Article requirements:
- 1500-2000 words
- Clear introduction that hooks the reader
- 4-5 main sections with descriptive H2 headers
- At least one concrete example or case study per section
- Cite key statistics with source attribution [Source: name/URL]
- Conclusion with clear takeaways and next steps
- Tone: informative but not academic; expert but accessible
The article should be ready for editorial review.""",
expected_output="""A complete article in Markdown format with:
- Compelling title
- All H2 and H3 headers in place
- All statistics cited inline
- Word count between 1500-2000 words
- No placeholder text or TODO markers""",
agent=writer,
context=[research_task], # This task gets researcher's output as context
)
editing_task = Task(
description="""Edit and finalize the article on: {topic}
Editorial review checklist:
1. Fact accuracy: verify key claims against the research findings
2. Structure: does the flow make sense? Is the lede buried?
3. Clarity: identify any confusing passages and improve them
4. Conciseness: cut redundant sentences or sections
5. Tone consistency: ensure voice is consistent throughout
6. Citation completeness: ensure all statistics are attributed
Make specific improvements. Track your changes and reasoning.""",
expected_output="""The final edited article with:
- All issues from review resolved
- Editorial notes appended as a separate section titled '## Editorial Notes'
- Final word count
- Quality rating: Excellent / Good / Needs Revision""",
agent=editor,
context=[research_task, writing_task], # Editor sees both research and draft
output_file="final_article.md"
)
The context parameter is one of CrewAI's most important features. When you set context=[research_task] on the writing_task, the writer receives the researcher's output automatically. No manual passing of strings required.
Creating the Crew
content_crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.sequential, # Tasks execute in order
verbose=True,
memory=True, # Enable memory for context retention
max_rpm=10, # Rate limit: 10 requests per minute (prevents API throttling)
share_crew=False
)
Process options:
Process.sequential— Tasks run in the order they're defined. Output from each task is available to subsequent tasks. This is what most people want.Process.hierarchical— A manager agent (automatically created or specified) coordinates agents and assigns tasks dynamically. More flexible, uses more tokens.
Running the Crew
# Run with a specific topic
result = content_crew.kickoff(inputs={
"topic": "How multi-agent AI systems are being used in enterprise software development in 2026"
})
print("\n" + "="*60)
print("CREW EXECUTION COMPLETE")
print("="*60)
print(result.raw) # Final task output
print(f"\nToken usage: {result.token_usage}")
The inputs dict populates the {topic} placeholders in all task descriptions. This makes the crew reusable across different topics without modifying task definitions.
Adding Memory and Context Persistence
CrewAI supports multiple memory types:
from crewai.memory import ShortTermMemory, LongTermMemory, EntityMemory
content_crew_with_memory = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.sequential,
memory=True, # Enables all memory types
verbose=True,
)
# For persistent long-term memory across crew runs:
from crewai.memory.storage.ltm_sqlite_storage import LTMSQLiteStorage
long_term_crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.sequential,
memory=True,
long_term_memory=LongTermMemory(
storage=LTMSQLiteStorage(db_path="./crew_memory.db")
),
)
With long-term memory enabled, the crew accumulates knowledge across runs. The researcher doesn't re-learn things it found in previous sessions, the writer adapts to feedback patterns over time, and the editor's quality judgments become more consistent.
For deeper discussion of agent memory systems, AI agent memory and planning covers the different memory types and when to use each.
Hierarchical Process Example
For more complex workflows where you want a manager to dynamically coordinate:
# Add a manager agent
crew_manager = Agent(
role="Content Production Manager",
goal="Coordinate the research, writing, and editing team to produce high-quality content efficiently.",
backstory="You manage a content team and are excellent at knowing when work meets standards and when it needs revision.",
llm=ChatOpenAI(model="gpt-4o", temperature=0),
allow_delegation=True # Manager CAN delegate
)
hierarchical_crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.hierarchical,
manager_agent=crew_manager, # Specify the manager
verbose=True,
)
In hierarchical mode, the manager decides task order and can re-assign tasks if quality criteria aren't met. More powerful but uses more tokens and is harder to predict.
Full Example in One File
import os
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()
def create_content_crew(topic: str) -> str:
search_tool = SerperDevTool()
llm_precise = ChatOpenAI(model="gpt-4o", temperature=0.1)
llm_creative = ChatOpenAI(model="gpt-4o", temperature=0.4)
researcher = Agent(
role="Senior Research Analyst",
goal="Find accurate, current information with verifiable sources.",
backstory="Rigorous academic researcher who never includes an unverified claim.",
tools=[search_tool], verbose=True, llm=llm_precise
)
writer = Agent(
role="Content Writer",
goal="Transform research into engaging, well-structured articles.",
backstory="Tech journalist who makes complex topics accessible.",
verbose=True, llm=llm_creative
)
editor = Agent(
role="Senior Editor",
goal="Review for accuracy, clarity, and quality.",
backstory="15-year veteran editor with a sharp eye for unclear writing.",
verbose=True, llm=llm_precise
)
research = Task(
description=f"Research this topic thoroughly: {topic}\nFind key facts, examples, and recent developments.",
expected_output="Structured research with bullet points, statistics, and source URLs.",
agent=researcher
)
draft = Task(
description=f"Write a 1500-word article on: {topic}",
expected_output="Complete Markdown article with headers and inline citations.",
agent=writer, context=[research]
)
final_edit = Task(
description=f"Edit and finalize the article on: {topic}",
expected_output="Final article with editorial notes. Quality rating included.",
agent=editor, context=[research, draft]
)
crew = Crew(
agents=[researcher, writer, editor],
tasks=[research, draft, final_edit],
process=Process.sequential,
verbose=True
)
result = crew.kickoff()
return result.raw
# Run it
output = create_content_crew("The impact of AI agents on software engineering teams in 2026")
print(output)
CrewAI vs AutoGen: Honest Comparison
| Aspect | CrewAI | AutoGen |
|---|---|---|
| API style | Role + Task + Crew | Agent + GroupChat + Manager |
| Ease of start | Very easy | Moderate |
| Coordination model | Predefined task flow | Conversational negotiation |
| Flexibility | Medium | High |
| Debugging | Straightforward | Complex (chat trace) |
| Memory support | Built-in (multiple types) | Via custom memory tools |
| Tool integration | CrewAI Tools library | Tool functions + UserProxy executor |
| Cost control | max_rpm, process choice | max_round, agent configs |
| Community | Large, fast-growing | Large, Microsoft-backed |
| Best for | Structured workflows | Dynamic, conversational systems |
CrewAI wins when you have a clear workflow and want to move fast. AutoGen wins when you need dynamic coordination or your workflow isn't fully predictable in advance.
The AutoGen multi-agent group chat tutorial shows AutoGen's approach to the same content production problem if you want a direct comparison.
Common Mistakes and Fixes
Vague task descriptions. "Write an article about AI" produces generic output. "Write a 1500-word article explaining transformer self-attention to software engineers with basic ML knowledge, using 2-3 concrete code examples" produces something useful. Specificity in task descriptions is the single biggest lever for output quality.
Ignoring expected_output. This field tells the agent what "done" looks like. Without it, agents may under-deliver or over-elaborate. Define the format, length, and content requirements explicitly.
All agents at high temperature. Writers benefit from temperature 0.3-0.5. Fact-focused agents (researchers, editors, data analysts) should be at 0.0-0.1. High temperature on fact-dependent tasks produces creative lies.
Not using context. If your writing task doesn't have context=[research_task], the writer doesn't see the researcher's output. The pipeline becomes three independent tasks rather than a coordinated workflow.
For broader context on how these patterns fit into AI agent research builds, that article covers more advanced patterns including self-evaluation loops.
According to CrewAI's 2025 annual usage report, the most common production use cases are content generation (38%), data analysis pipelines (24%), and customer support workflows (18%). (source: crewai.com/research)
Conclusion
CrewAI earns its reputation as the most accessible multi-agent framework. The Agent-Task-Crew model maps onto real team structures in a way that makes agent system design feel intuitive rather than technical. You define the roles, the work, and the coordination — and the framework handles the rest.
For your first multi-agent project, CrewAI is the best starting point. The build AI agent with LangChain guide complements this well if you want to understand the lower-level primitives that CrewAI builds on.
Build the three-agent content crew from this tutorial, run it on a few topics, and then experiment with the hierarchical process for more dynamic workflows. The skills transfer directly to more complex architectures.
Frequently Asked Questions
What is CrewAI and how does it work? CrewAI is a Python framework for building role-based multi-agent systems. You define Agents with roles and goals, Tasks that describe what needs to be done, and a Crew that coordinates execution. Agents work sequentially or hierarchically, with each agent contributing to the shared goal.
How is CrewAI different from AutoGen? CrewAI uses a role-based task-assignment model where agents are assigned specific Tasks and execute them with a defined process (sequential or hierarchical). AutoGen uses a conversational model where agents chat and self-coordinate. CrewAI is more structured and beginner-friendly; AutoGen is more flexible for dynamic coordination.
What Python version and dependencies does CrewAI require? CrewAI requires Python 3.10 or higher and installs with pip install crewai crewai-tools. The main dependencies are langchain, langchain-openai, and pydantic. You also need an OPENAI_API_KEY environment variable or configure a different LLM provider.
Frequently Asked Questions
AiTechWorlds Team
✓ Verified WriterThe AiTechWorlds team is passionate about AI, technology, and education. We create high-quality, research-backed content to help you learn, grow, and succeed in the modern digital world.
Related Articles
AutoGen vs CrewAI: Which Multi-Agent Framework is Easier?
A head-to-head AutoGen vs CrewAI comparison with the same task built in both frameworks, a detailed comparison table, and an honest opinion on which is friendlier to use.
AutoGen vs CrewAI: Multi-Agent Role Assignment Compared
Compare AutoGen and CrewAI side-by-side for multi-agent role assignment. Same task, two frameworks — see which conversation-based vs role-based approach wins.
10 Multi-Agent Frameworks Compared (AutoGen, CrewAI, LangGraph, MetaGPT)
AutoGen, CrewAI, LangGraph, MetaGPT — compare all 10 major multi-agent frameworks on GitHub stars, ease of use, and real strengths. Pick the right one for your project.
10 AI Automation Ideas for Small Business (Save 20 Hours a Week)
Discover 10 actionable AI automation ideas for small business that can save you 20+ hours weekly with practical tools and real cost breakdowns.