| name | crewai-developer |
| description | Comprehensive CrewAI framework guide for building collaborative AI agent teams and structured workflows. Use when developing multi-agent systems with CrewAI, creating autonomous AI crews, orchestrating flows, implementing agents with roles and tools, or building production-ready AI automation. Essential for developers building intelligent agent systems, task automation, and complex AI workflows. |
CrewAI Developer Guide
Overview
CrewAI is a lean, lightning-fast Python framework for building collaborative AI agent teams and structured workflows. It empowers developers to create autonomous AI agents with specific roles, tools, and goals that work together to tackle complex tasks. This skill covers Crews (autonomous collaboration), Flows (structured orchestration), agents, tasks, and enterprise deployment.
Core Concepts
Agents: Specialized Team Members
Agents are autonomous AI units with specific roles, goals, and capabilities.
from crewai import Agent
researcher = Agent(
role='Senior Research Analyst',
goal='Uncover cutting-edge developments in AI and data science',
backstory="""You are an expert at a leading tech think tank.
Your expertise lies in identifying emerging trends and technologies in AI,
data science, and machine learning.""",
verbose=True,
allow_delegation=False,
tools=[search_tool, scrape_tool]
)
writer = Agent(
role='Tech Content Strategist',
goal='Craft compelling content on tech advancements',
backstory="""You are a renowned content strategist, known for
your insightful and engaging articles on technology and innovation.
You transform complex concepts into compelling narratives.""",
verbose=True,
allow_delegation=True,
tools=[write_tool]
)
Agent Key Properties
agent = Agent(
role='Role Name',
goal='Specific objective',
backstory='Background story',
verbose=True,
allow_delegation=False,
tools=[tool1, tool2],
llm=custom_llm,
max_iter=15,
max_rpm=10,
memory=True,
cache=True,
system_template="template",
prompt_template="template",
response_template="template"
)
Tasks: Individual Assignments
Tasks define specific work to be completed by agents.
from crewai import Task
research_task = Task(
description="""Conduct a comprehensive analysis of the latest advancements in AI.
Identify key trends, breakthrough technologies, and potential industry impacts.
Compile your findings in a detailed report.""",
expected_output='A comprehensive 3-paragraph report on AI advancements',
agent=researcher,
tools=[search_tool],
output_file='research_report.md'
)
write_task = Task(
description="""Using the research analyst's report, develop an engaging blog post
highlighting the most significant AI advancements.
Make it accessible and engaging for a general audience.""",
expected_output='A 4-paragraph blog post about AI advancements',
agent=writer,
context=[research_task],
output_file='blog_post.md'
)
Task Key Properties
task = Task(
description='Detailed task description',
expected_output='Clear output format',
agent=agent_instance,
tools=[tool1, tool2],
context=[previous_task],
async_execution=False,
output_json=OutputClass,
output_pydantic=OutputClass,
output_file='result.txt',
callback=callback_function,
human_input=False
)
Crews: Organizing Agent Teams
Crews orchestrate agents working together toward a common goal.
from crewai import Crew, Process
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential,
verbose=True,
memory=True,
cache=True,
max_rpm=10,
share_crew=False
)
result = crew.kickoff()
print(result)
result = crew.kickoff(inputs={
'topic': 'Artificial Intelligence',
'audience': 'developers'
})
Process Types
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2],
process=Process.sequential
)
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2],
process=Process.hierarchical,
manager_llm='gpt-4'
)
Flows: Structured Workflow Orchestration
Flows provide event-driven, deterministic control over execution paths.
from crewai.flow.flow import Flow, listen, start
class BlogPostFlow(Flow):
@start()
def fetch_topic(self):
"""Entry point - fetch the topic to write about"""
print("Starting blog post generation")
return "AI advancements in 2024"
@listen(fetch_topic)
def research_topic(self, topic):
"""Research the topic"""
print(f"Researching: {topic}")
research_crew = Crew(
agents=[researcher],
tasks=[research_task]
)
result = research_crew.kickoff(inputs={'topic': topic})
return result
@listen(research_topic)
def write_blog_post(self, research_data):
"""Write the blog post"""
print("Writing blog post...")
write_crew = Crew(
agents=[writer],
tasks=[write_task]
)
result = write_crew.kickoff(inputs={'research': research_data})
return result
@listen(write_blog_post)
def finalize(self, blog_post):
"""Finalize and save"""
print("Blog post completed!")
return blog_post
flow = BlogPostFlow()
result = flow.kickoff()
Flow State Management
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel
class ArticleState(BaseModel):
topic: str = ""
research: str = ""
draft: str = ""
final: str = ""
class ArticleFlow(Flow[ArticleState]):
@start()
def set_topic(self):
self.state.topic = "AI Ethics"
return self.state.topic
@listen(set_topic)
def research(self, topic):
self.state.research = "Research findings..."
return self.state.research
@listen(research)
def write_draft(self, research):
self.state.draft = "Draft content..."
return self.state.draft
flow = ArticleFlow()
flow.kickoff()
print(flow.state.topic)
print(flow.state.research)
Router Pattern
from crewai.flow.flow import Flow, listen, start, router
class ContentFlow(Flow):
@start()
def categorize_content(self):
return "technical"
@router(categorize_content)
def route_content(self, category):
if category == "technical":
return "write_technical"
elif category == "marketing":
return "write_marketing"
else:
return "write_blog"
@listen("write_technical")
def write_technical_doc(self):
return "Technical documentation..."
@listen("write_marketing")
def write_marketing_copy(self):
return "Marketing content..."
@listen("write_blog")
def write_blog_post(self):
return "Blog post..."
Tools: Extending Agent Capabilities
Built-in Tools
from crewai_tools import (
SerperDevTool,
ScrapeWebsiteTool,
FileReadTool,
DirectoryReadTool,
CodeDocsSearchTool,
CSVSearchTool,
JSONSearchTool,
MDXSearchTool,
PDFSearchTool,
TXTSearchTool,
WebsiteSearchTool,
SeleniumScrapingTool,
YoutubeChannelSearchTool,
YoutubeVideoSearchTool
)
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
file_tool = FileReadTool()
agent = Agent(
role='Researcher',
tools=[search_tool, scrape_tool, file_tool]
)
Custom Tools
from crewai_tools import BaseTool
class MyCustomTool(BaseTool):
name: str = "Custom Tool Name"
description: str = "Clear description of what the tool does"
def _run(self, argument: str) -> str:
result = perform_operation(argument)
return result
custom_tool = MyCustomTool()
agent = Agent(
role='Specialist',
tools=[custom_tool]
)
Function as Tool
from crewai import Agent
def calculate_sum(a: int, b: int) -> int:
"""Calculate the sum of two numbers"""
return a + b
agent = Agent(
role='Calculator',
tools=[calculate_sum]
)
Memory: Learning from Past Interactions
from crewai import Crew, Agent, Task
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2],
memory=True,
verbose=True
)
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2],
memory=True,
memory_config={
'short_term': True,
'long_term': True,
'entity': True
}
)
Knowledge: RAG Integration
from crewai import Agent, Crew, Task, knowledge
docs_knowledge = knowledge.StringKnowledgeSource(
content="Company policies and procedures...",
metadata={"source": "policy_docs"}
)
agent = Agent(
role='Policy Expert',
goal='Answer questions about company policies',
backstory='Expert in company policies',
knowledge_sources=[docs_knowledge]
)
pdf_knowledge = knowledge.PDFKnowledgeSource(
file_path='./documents/handbook.pdf'
)
txt_knowledge = knowledge.TextKnowledgeSource(
file_path='./documents/faq.txt'
)
agent = Agent(
role='Support Agent',
knowledge_sources=[pdf_knowledge, txt_knowledge]
)
Structured Outputs with Pydantic
from pydantic import BaseModel
from crewai import Task, Agent
class BlogPost(BaseModel):
title: str
content: str
tags: list[str]
word_count: int
write_task = Task(
description='Write a blog post about AI',
expected_output='Blog post with title, content, tags, and word count',
agent=writer,
output_pydantic=BlogPost
)
result = crew.kickoff()
blog_post: BlogPost = write_task.output.pydantic
print(blog_post.title)
print(blog_post.tags)
Training: Improving Performance
from crewai import Crew
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2]
)
crew.train(
n_iterations=10,
inputs={'topic': 'AI'},
filename='trained_crew.pkl'
)
trained_crew = Crew.load('trained_crew.pkl')
Human-in-the-Loop
from crewai import Task
review_task = Task(
description='Review the draft and provide feedback',
expected_output='Approved draft or feedback for revision',
agent=editor,
human_input=True
)
task = Task(
description='Generate report',
expected_output='Final report',
agent=analyst,
callback=lambda output: validate_output(output),
human_input=True if needs_review else False
)
Testing Crews
from crewai import Crew
import pytest
def test_research_crew():
crew = Crew(
agents=[researcher],
tasks=[research_task]
)
result = crew.kickoff(inputs={'topic': 'AI'})
assert result is not None
assert 'AI' in result
assert len(result) > 100
def test_crew_with_mock():
mock_agent = Agent(
role='Mock Agent',
goal='Return test data',
backstory='Test agent'
)
mock_task = Task(
description='Test task',
expected_output='Test output',
agent=mock_agent
)
crew = Crew(agents=[mock_agent], tasks=[mock_task])
result = crew.kickoff()
assert result == 'Test output'
Custom LLMs
from langchain_openai import ChatOpenAI
from crewai import Agent, Crew
custom_llm = ChatOpenAI(
model='gpt-4-turbo-preview',
temperature=0.7,
max_tokens=2000
)
agent = Agent(
role='Writer',
llm=custom_llm
)
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2],
manager_llm=custom_llm
)
Async Execution
from crewai import Crew
crew = Crew(
agents=[agent1, agent2],
tasks=[task1, task2]
)
async def run_crew():
result = await crew.kickoff_async(inputs={'topic': 'AI'})
return result
inputs_list = [
{'topic': 'AI'},
{'topic': 'ML'},
{'topic': 'Data Science'}
]
results = crew.kickoff_for_each(inputs=inputs_list)
Callbacks and Event Listeners
from crewai import Task, Agent
def on_task_complete(output):
print(f"Task completed with output: {output}")
def on_task_error(error):
print(f"Task failed with error: {error}")
task = Task(
description='Analyze data',
expected_output='Analysis report',
agent=analyst,
callback=on_task_complete
)
agent = Agent(
role='Analyst',
step_callback=lambda step: print(f"Agent step: {step}"),
task_callback=on_task_complete
)
Enterprise Deployment
Environment Configuration
import os
os.environ['OPENAI_API_KEY'] = 'your-key'
os.environ['SERPER_API_KEY'] = 'your-key'
os.environ['CREWAI_API_KEY'] = 'your-enterprise-key'
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_API_KEY'] = 'your-langchain-key'
Project Structure
my_crew_project/
āāā src/
ā āāā my_crew_project/
ā āāā __init__.py
ā āāā main.py
ā āāā crew.py
ā āāā config/
ā ā āāā agents.yaml
ā ā āāā tasks.yaml
ā āāā tools/
ā āāā custom_tool.py
āāā tests/
ā āāā test_crew.py
āāā pyproject.toml
āāā README.md
YAML Configuration
agents.yaml
researcher:
role: >
Senior Research Analyst
goal: >
Uncover cutting-edge developments in {topic}
backstory: >
You are an expert researcher with deep knowledge in {topic}
writer:
role: >
Content Writer
goal: >
Create engaging content about {topic}
backstory: >
You are a skilled writer who makes complex topics accessible
tasks.yaml
research_task:
description: >
Conduct comprehensive research on {topic}
expected_output: >
A detailed research report with key findings
agent: researcher
writing_task:
description: >
Write an article based on the research
expected_output: >
A well-structured article
agent: writer
context:
- research_task
Best Practices
Agent Design
ā
Good Practices:
- Give agents clear, specific roles
- Provide detailed backstories for context
- Limit tools to what's necessary
- Enable delegation for managers
- Use verbose mode during development
ā Avoid:
- Vague or overlapping roles
- Too many tools (causes confusion)
- Missing backstories
- Overly complex goals
Task Design
ā
Good Practices:
- Write clear, actionable descriptions
- Specify expected output format
- Set up proper task dependencies
- Use context for task chaining
- Enable human input for critical decisions
ā Avoid:
- Ambiguous descriptions
- Missing expected output
- Circular dependencies
- Overly complex single tasks
Crew Organization
ā
Good Practices:
- Start with sequential process
- Use hierarchical for complex coordination
- Enable memory for context retention
- Set reasonable rate limits
- Test with small datasets first
ā Avoid:
- Too many agents (3-5 is optimal)
- Complex hierarchies without testing
- Disabled memory in multi-step flows
- No rate limiting
Common Patterns
Research and Write Pipeline
researcher = Agent(role='Researcher', ...)
analyst = Agent(role='Analyst', ...)
writer = Agent(role='Writer', ...)
editor = Agent(role='Editor', ...)
research = Task(agent=researcher, ...)
analysis = Task(agent=analyst, context=[research], ...)
draft = Task(agent=writer, context=[analysis], ...)
final = Task(agent=editor, context=[draft], ...)
crew = Crew(
agents=[researcher, analyst, writer, editor],
tasks=[research, analysis, draft, final],
process=Process.sequential
)
Multi-Stage Approval Flow
class ApprovalFlow(Flow):
@start()
def create_draft(self):
return draft_content
@listen(create_draft)
def request_review(self, draft):
return review_request
@router(request_review)
def check_approval(self, review):
if review.approved:
return "finalize"
else:
return "revise"
@listen("revise")
def revise_draft(self):
return revised_draft
@listen("finalize")
def finalize_content(self):
return final_content
Quick Reference
Installation
uv pip install crewai crewai-tools
pip install crewai crewai-tools
pip install 'crewai[all]'
CLI Commands
crewai create crew my_project
crewai create flow my_flow
crewai install
crewai run
crewai train
crewai replay <task_id>
crewai test
Essential Imports
from crewai import Agent, Task, Crew, Process
from crewai.flow.flow import Flow, listen, start, router
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
from pydantic import BaseModel
Resources
For advanced patterns, integration examples, and troubleshooting:
Extended Reference
See references/advanced_patterns.md for:
- MCP (Model Context Protocol) integration
- Observability and tracing setup
- Production deployment strategies
- Advanced flow patterns
- Performance optimization