← Back to Tutorials
Tutorials intermediate

How to Build a Multi-Agent AI System with CrewAI 2026 — Step by Step Guide

How to Build a Multi-Agent AI System with CrewAI 2026 — Step by Step Guide

Overview

Multi-agent AI systems are the most powerful paradigm in 2026 for building complex AI workflows. Instead of one LLM doing everything, multiple specialized AI agents collaborate — a researcher gathers data, an analyst processes it, and a writer produces the final output. CrewAI has emerged as the leading Python framework for orchestrating these multi-agent teams.

In this tutorial, you’ll build a three-agent content research crew that researches, analyzes, and writes comprehensive reports on any topic. You’ll learn every concept through real, working code.

What You’ll Build

A multi-agent “Content Research Crew” consisting of:

  • Research Agent — Searches the web and gathers information
  • Analysis Agent — Processes data, identifies patterns, and creates insights
  • Writing Agent — Produces a polished report based on the analysis

Prerequisites

  • Python 3.11+
  • OpenAI API key (or Claude/Gemini — CrewAI supports multiple providers)
  • Basic familiarity with Python and AI prompts
  • pip (Python package manager)

Step 1: Set Up Your Environment (5 minutes)

Create a new project and install CrewAI:

mkdir content-research-crew
cd content-research-crew
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

Install CrewAI and the tools you’ll need:

pip install crewai crewai-tools python-dotenv

Create a .env file for your API keys:

OPENAI_API_KEY=sk-your-key-here
# Optional: use Claude instead
# ANTHROPIC_API_KEY=sk-ant-your-key-here

Note: CrewAI 2026 supports GPT-4, Claude 4 Sonnet, Gemini 2.5, and DeepSeek as LLM backends. Set the appropriate env var for your chosen model.


Step 2: Understand CrewAI Concepts (5 minutes)

Before writing code, understand the four core concepts:

ConceptDescriptionAnalogy
AgentAn AI worker with a role, goal, and backstoryA team member with a specific job
TaskA specific unit of work assigned to an agentAn assignment or deliverable
CrewThe team of agents working together on tasksThe project team
ToolExternal capability an agent can use (web search, calculator, API)Equipment or software access

Agents communicate through a structured process:

  1. Each agent receives a role, goal, and backstory that defines its personality and constraints
  2. Tasks are assigned to specific agents with clear expected_output
  3. The Crew orchestrates execution — agents work sequentially or in parallel
  4. Results from one task are passed as context to subsequent tasks

Step 3: Create Your First Agent (5 minutes)

Create agents.py:

import os
from crewai import Agent
from dotenv import load_dotenv

load_dotenv()

# --- AGENT 1: Research Agent ---
researcher = Agent(
    role="Senior Research Analyst",
    goal="Uncover cutting-edge developments in {topic} by searching the web and synthesizing findings.",
    backstory=(
        "You work at a leading think tank. "
        "You're known for your ability to find the most relevant, timely, and surprising information "
        "from a vast array of sources. You're meticulous, thorough, and always cite your sources."
    ),
    allow_delegation=False,
    verbose=True,
)

# --- AGENT 2: Analysis Agent ---
analyst = Agent(
    role="Data Analyst & Pattern Identifier",
    goal="Analyze research data to identify key trends, patterns, and actionable insights about {topic}.",
    backstory=(
        "You're a senior data analyst at a strategy consulting firm. "
        "You excel at taking raw research and transforming it into structured analysis. "
        "You always identify at least 3-5 key trends and support each with evidence."
    ),
    allow_delegation=False,
    verbose=True,
)

# --- AGENT 3: Writing Agent ---
writer = Agent(
    role="Senior Content Writer",
    goal="Craft comprehensive, engaging, and well-structured reports on {topic} based on research and analysis.",
    backstory=(
        "You're a renowned technology journalist who writes for major publications. "
        "You transform complex analysis into clear, compelling narratives. "
        "Your reports are known for being thorough yet readable. "
        "You always include an executive summary, key findings, and actionable recommendations."
    ),
    allow_delegation=False,
    verbose=True,
)

Each agent has three defining attributes:

  • role: The job title — this shapes how the LLM behaves
  • goal: What the agent aims to achieve — includes {topic} as a dynamic variable
  • backstory: Context that helps the LLM adopt the right persona

Step 4: Define Tasks (5 minutes)

Create tasks.py:

from crewai import Task

# --- TASK 1: Research ---
research_task = Task(
    description=(
        "1. Search for the latest developments in {topic} (last 12 months).\n"
        "2. Identify at least 5-7 key sources including news articles, research papers, and expert analyses.\n"
        "3. For each source, extract: the main finding, the source URL, publication date, and key statistics.\n"
        "4. Focus on concrete data and specific examples — avoid vague summaries."
    ),
    expected_output=(
        "A comprehensive research brief with 5-7 sources. "
        "Each source entry includes: finding, URL, date, and 2-3 supporting statistics. "
        "Total: 800-1200 words."
    ),
    agent=researcher,
)

# --- TASK 2: Analysis ---
analysis_task = Task(
    description=(
        "Using the research brief provided:\n"
        "1. Identify 3-5 major trends or patterns across the research data.\n"
        "2. For each trend, explain: what's happening, why it matters, and supporting evidence.\n"
        "3. Create a comparative analysis if multiple viewpoints exist.\n"
        "4. Identify any gaps or contradictions in the research.\n"
        "5. Provide 2-3 actionable insights based on the analysis."
    ),
    expected_output=(
        "A structured analysis document with 3-5 identified trends, "
        "each with explanation, evidence, and significance. "
        "Plus 2-3 actionable insights. Total: 600-1000 words."
    ),
    agent=analyst,
)

# --- TASK 3: Writing ---
writing_task = Task(
    description=(
        "Using the research brief and analysis:\n"
        "1. Write a comprehensive report on {topic} following this structure:\n"
        "   - Executive Summary (2-3 paragraphs)\n"
        "   - Key Findings (3-5 points with evidence)\n"
        "   - Detailed Analysis (with sub-sections for each trend)\n"
        "   - Actionable Recommendations (2-3 items)\n"
        "   - References (all cited sources)\n"
        "2. Use clear, engaging language suitable for business professionals.\n"
        "3. Include specific statistics, quotes, and examples throughout."
    ),
    expected_output=(
        "A polished report of 1500-2500 words with all required sections. "
        "Professional formatting, cited sources, and actionable content."
    ),
    agent=writer,
)

Key points about tasks:

  • description includes {topic} as a dynamic input — passed at runtime
  • expected_output tells the agent exactly what to produce — specific format and word count
  • Tasks are assigned to specific agents via the agent parameter
  • Task results flow: Task 1 → context for Task 2 → context for Task 3

Step 5: Create the Crew and Add Tools (10 minutes)

Create crew.py with web search capability:

import os
from crewai import Crew, Process
from crewai_tools import SerperDevTool
from agents import researcher, analyst, writer
from tasks import research_task, analysis_task, writing_task

# Initialize search tool
# Sign up for a free Serper API key at https://serper.dev
search_tool = SerperDevTool(
    api_key=os.getenv("SERPER_API_KEY"),
    n_results=10,  # Number of search results per query
)

# Give the research agent access to the search tool
researcher.tools = [search_tool]

# Create the crew
content_crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[research_task, analysis_task, writing_task],
    process=Process.sequential,  # Tasks run one after another
    verbose=True,
)

Understanding Crew Processes

CrewAI supports three process types:

ProcessBehaviorBest For
Process.sequentialTasks run in order, each agent waits for the previousClear linear workflows (our use case)
Process.hierarchicalA manager agent delegates tasks to worker agentsComplex projects needing oversight
Process.consensuAgents debate and reach consensusResearch and decision-making tasks

Step 6: Run Your Multi-Agent Crew (5 minutes)

Create main.py:

from crew import content_crew

# The topic to research — change this to anything
topic = "AI-powered climate change solutions in 2026"

# Kick off the crew
result = content_crew.kickoff(inputs={"topic": topic})

# Print the result
print("\n" + "="*80)
print("FINAL REPORT")
print("="*80)
print(result)

Run it:

# First get a Serper API key from https://serper.dev
export SERPER_API_KEY="your-serper-key"
python main.py

What Happens When You Run

  1. Research Agent uses the SerperDevTool to search for the latest news on “{topic}”
  2. Research Agent synthesizes findings into a structured research brief (800-1200 words)
  3. Analysis Agent receives the research brief as context
  4. Analysis Agent identifies trends, patterns, and insights (600-1000 words)
  5. Writing Agent receives both the research brief and analysis as context
  6. Writing Agent produces the final report (1500-2500 words)

Expected runtime: 2-5 minutes depending on the LLM model and complexity of the topic.


Step 7: Make It Interactive with CLI (5 minutes)

Create a CLI version that accepts any topic:

# main_cli.py
import sys
from crew import content_crew

def main():
    if len(sys.argv) < 2:
        print("Usage: python main_cli.py '<your topic>'")
        print("Example: python main_cli.py 'Quantum computing breakthroughs 2026'")
        sys.exit(1)
    
    topic = " ".join(sys.argv[1:])
    print(f"\n🎯 Starting research crew for: {topic}\n")
    
    result = content_crew.kickoff(inputs={"topic": topic})
    
    # Save output to file
    filename = topic.lower().replace(" ", "-")[:50] + ".md"
    with open(filename, "w") as f:
        f.write(str(result))
    
    print(f"\n✅ Report saved to: {filename}")

if __name__ == "__main__":
    main()

Usage:

python main_cli.py "Rust vs Zig programming languages 2026 comparison"

Step 8: Advanced Configuration (10 minutes)

Customize the LLM

Override the default model per-agent or globally:

from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic

# Create a custom LLM
claude_llm = ChatAnthropic(
    model="claude-sonnet-4-20260515",
    temperature=0.3,
)

# Assign to a specific agent
advanced_analyst = Agent(
    role="Senior Data Analyst & Pattern Identifier",
    goal="Analyze research data to identify key trends...",
    backstory="...",
    llm=claude_llm,  # Use Claude instead of default GPT-4
    verbose=True,
)

Add Memory for Long-Running Crews

content_crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[research_task, analysis_task, writing_task],
    process=Process.sequential,
    memory=True,  # Enable short-term and long-term memory
    embedder={
        "provider": "openai",
        "model": "text-embedding-3-small",
    },
    verbose=True,
)

Memory enables agents to recall information from previous task runs — useful for multi-session projects.

Add Custom Tools

Beyond the search tool, CrewAI supports:

from crewai_tools import (
    ScrapeWebsiteTool,    # Scrape a specific URL
    DOCXSearchTool,       # Search within DOCX files
    FileReadTool,         # Read local files
    JSONSearchTool,       # Search JSON data
    CalculatorTool,       # Perform calculations
)

# Example: Add website scraping to the analyst
analyst.tools = [
    ScrapeWebsiteTool(),
    CalculatorTool(),
]

Best Practices

  1. Be Specific in Task Descriptions. The more precise your description and expected_output, the better the results. Include format requirements, word counts, and structural guidance.

  2. Use Dynamic Inputs. Use {variable_name} in agent goals and task descriptions. Pass values via kickoff(inputs={...}).

  3. Set allow_delegation=False by default. Only enable delegation when you explicitly want agents to assign subtasks to each other. This prevents unpredictable behavior.

  4. Use Process.sequential for clear pipelines. Only use hierarchical or consensu when you have complex decision-making needs.

  5. Monitor Token Usage. Multi-agent systems consume tokens from all agents. A single report generation can use 20,000-50,000 tokens. Consider using cheaper models (GPT-4o-mini, DeepSeek V4 Flash) for research and analysis, and premium models (Claude Sonnet 4) for final writing.

  6. Add Error Handling. CrewAI agents can fail or produce unexpected output. Wrap in try/except blocks and implement retry logic for production use.


Troubleshooting

ProblemSolution
API key errorsCheck .env file is in the correct directory and load_dotenv() is called
Search tool not workingVerify your Serper API key is valid and has credits
Agents not delegatingSet allow_delegation=True explicitly on the agent
Output too shortIncrease word count targets in expected_output
Agents timeoutReduce task complexity, set shorter max_iter, or upgrade your LLM tier
High token costsUse a cheaper model for research/analysis tasks; reserve premium model for writing
Memory not savingEnsure memory=True and a valid embedder configuration

FAQ

What is CrewAI?

CrewAI is a Python framework for orchestrating multiple AI agents that work together on complex tasks. Each agent has a specific role, goal, and set of tools, and agents collaborate through a structured process to produce results.

Can I use Claude or Gemini instead of GPT-4?

Yes. CrewAI supports OpenAI, Anthropic, Google, and DeepSeek models. Pass a different LLM instance to your agents as shown in Step 8.

How much does it cost to run a multi-agent crew?

A typical run (3 agents, 3 tasks) with GPT-4o consumes about 20,000-50,000 tokens. At current pricing (~$5/M input tokens), that’s $0.10-0.25 per run. Using GPT-4o-mini costs ~$0.01-0.03 per run.

Can agents use custom APIs or databases?

Yes. Create custom tools by extending CrewAI’s BaseTool class. Any Python function can be wrapped as a tool.

Is CrewAI production-ready for 2026?

Absolutely. CrewAI has matured significantly with v3.x. It’s used in production by companies for automated research, content generation, customer support triage, and data pipeline management.

Can I run agents in parallel?

CrewAI’s Process.sequential runs one task at a time. For parallel execution, split your crew into multiple sub-crews and manage them from a parent orchestration layer.


Conclusion

You’ve built a fully functional multi-agent AI system with CrewAI in under an hour. The three-agent content research crew demonstrates the core pattern: specialized agents collaborating through structured tasks to produce results that no single LLM call could match.

From here, you can:

  • Add more agents (editor, fact-checker, SEO specialist)
  • Create custom tools (database queries, API integrations, Slack notifications)
  • Build hierarchical crews with a manager agent
  • Deploy as a web API using FastAPI

Multi-agent systems represent the frontier of practical AI in 2026. CrewAI makes them accessible with clear abstractions, robust tooling, and a growing ecosystem.