CrewAI is a revolutionary framework for creating teams of AI agents that can collaborate on complex tasks. In this tutorial, you’ll learn how to create your own agents and make them efficiently collaborate on solving problems.
Introduction to CrewAI: Multi-Agent Systems in Practice¶
CrewAI is a Python framework for creating coordinated teams of AI agents that can collaborate on complex tasks. Unlike single-agent approaches, CrewAI enables creating specialized agents with different roles and skills that can effectively communicate and delegate tasks among themselves.
The framework uses the concept of crews (teams), agents, and tasks. Each agent has a defined role, goal, and working method, while tasks specify concrete actions to be performed.
Installation and Basic Setup¶
Let’s start by installing CrewAI and necessary dependencies:
pip install crewai
pip install 'crewai[tools]' # For advanced tools
CrewAI requires an API key for an LLM provider. The simplest is to use OpenAI:
import os
from crewai import Agent, Task, Crew
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
# Setting up API keys
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["SERPER_API_KEY"] = "your-serper-api-key" # For web search
Creating Your First Agent¶
An agent in CrewAI is defined using role, goal, backstory, and tools. Let’s show creating a research agent:
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
researcher = Agent(
role='Senior Research Analyst',
goal='Find and analyze the latest trends in AI and machine learning',
backstory="""You are an experienced research analyst with 10 years
of experience in AI. You specialize in identifying emerging technologies
and their potential business impact.""",
verbose=True,
allow_delegation=False,
tools=[search_tool, scrape_tool],
max_iter=3,
memory=True
)
The verbose=True parameter allows monitoring the agent’s thought processes, allow_delegation determines whether the agent can delegate tasks to other agents, and max_iter limits the number of iterations for solving a task.
Defining Tasks¶
A Task specifies concrete work that an agent should perform. It contains a description, expected output, and assigned agent:
research_task = Task(
description="""Conduct comprehensive research on current trends
in Large Language Models. Focus on:
1. Latest models released in the last 6 months
2. Key innovations in architecture
3. Practical applications in enterprise environments
4. Future development predictions""",
expected_output="""Structured report with the following sections:
- Executive Summary (2-3 paragraphs)
- Analysis of new models (list with descriptions)
- Technical innovations (key points)
- Business applications (concrete use cases)
- Future trends (12-month predictions)""",
agent=researcher,
tools=[search_tool, scrape_tool]
)
Advanced Multi-Agent System¶
The power of CrewAI shows when creating a team of specialized agents. Let’s create a complete workflow for content marketing:
# Content Strategist
strategist = Agent(
role='Content Marketing Strategist',
goal='Create effective content strategy based on research data',
backstory="""You are a content marketing expert with deep understanding
of SEO and audience engagement. You can transform research data
into actionable content strategy.""",
verbose=True,
allow_delegation=True,
tools=[search_tool]
)
# Content Writer
writer = Agent(
role='Senior Technical Writer',
goal='Write engaging and technically accurate content',
backstory="""You are an experienced technical writer specializing in AI
and software development. You can present complex technical concepts
understandably for various audiences.""",
verbose=True,
allow_delegation=False
)
# Editor
editor = Agent(
role='Content Editor',
goal='Ensure highest quality and consistency of content',
backstory="""You are a detail-oriented editor with an eye for quality.
You specialize in tech content and ensure every piece
meets editorial standards.""",
verbose=True,
allow_delegation=False
)
Sequential Workflow with Task Dependencies¶
CrewAI allows creating sequential workflows where the output of one task serves as input for the next:
# 1. Research task
research_task = Task(
description="Conduct research on CrewAI framework and its competitive landscape",
expected_output="Detailed research report with competitive analysis",
agent=researcher
)
# 2. Strategy task (dependent on research)
strategy_task = Task(
description="""Based on research data, create content strategy for
CrewAI tutorial series. Define:
- Target audience segments
- Content topics and priorities
- Distribution channels
- Success metrics""",
expected_output="Comprehensive content strategy document",
agent=strategist,
context=[research_task] # Dependency on research task
)
# 3. Writing task
writing_task = Task(
description="""Write an introductory tutorial article about CrewAI according to
content strategy. The article should be:
- 1200-1500 words
- Practically focused with code examples
- SEO optimized""",
expected_output="Finished tutorial article in markdown format",
agent=writer,
context=[research_task, strategy_task]
)
# 4. Editing task
editing_task = Task(
description="""Perform final edit of the article. Check:
- Grammar and style
- Technical accuracy
- SEO optimization
- Structure and flow""",
expected_output="Final, publication-ready article",
agent=editor,
context=[writing_task]
)
Running the Crew and Monitoring¶
Now let’s create the crew and run the entire workflow:
# Creating crew
content_crew = Crew(
agents=[researcher, strategist, writer, editor],
tasks=[research_task, strategy_task, writing_task, editing_task],
verbose=2, # Level of detailed logging
process=Process.sequential, # Sequential processing
memory=True, # Enable memory between tasks
max_rpm=10, # Rate limiting
share_crew=False
)
# Running workflow
try:
result = content_crew.kickoff()
print("Workflow completed successfully!")
print(f"Final result: {result}")
except Exception as e:
print(f"Error during execution: {e}")
# Logging and error handling
Advanced Features and Optimization¶
CrewAI offers several advanced features for performance optimization:
Custom Tools¶
We can create custom tools for specific needs:
from crewai_tools import BaseTool
class DatabaseQueryTool(BaseTool):
name: str = "Database Query Tool"
description: str = "Allows querying the company database"
def _run(self, query: str) -> str:
# Database query implementation
# connection = get_db_connection()
# result = connection.execute(query)
return f"Query result for: {query}"
# Using custom tool
db_tool = DatabaseQueryTool()
analyst = Agent(
role='Data Analyst',
goal='Analyze business data',
tools=[db_tool],
# ... other parameters
)
Hierarchical Process¶
For more complex workflows, we can use hierarchical process with a manager agent:
manager = Agent(
role='Project Manager',
goal='Coordinate team work and ensure deadline compliance',
backstory='Experienced PM with expertise in AI projects',
allow_delegation=True,
verbose=True
)
hierarchical_crew = Crew(
agents=[manager, researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.hierarchical,
manager_llm="gpt-4", # Specialized LLM for manager
verbose=2
)
Summary¶
CrewAI represents a paradigm shift in approach to AI applications. Instead of relying on one universal agent, it enables creating specialized teams where each agent excels in their domain. The framework is ideal for complex workflows like content creation, research & analysis, or business process automation. The key to success is proper role definition, clear task specifications, and efficient orchestration between agents. With growing complexity of AI systems, CrewAI’s multi-agent approach becomes an indispensable tool for enterprise AI development.