CrewAI
Multi-agent orchestration with role-based design. Agents collaborate on tasks via sequential or hierarchical processes.
Core Concepts
from crewai import Agent, Task, Crew, Process
researcher = Agent(
role="Senior Researcher",
goal="Find comprehensive information on {topic}",
backstory="Expert researcher with 10 years experience.",
tools=[search_tool, web_scraper],
verbose=True,
allow_delegation=True,
)
writer = Agent(
role="Technical Writer",
goal="Write clear, engaging content based on research",
backstory="Experienced technical writer specializing in developer docs.",
verbose=True,
)
research_task = Task(
description="Research {topic} thoroughly. Cover architecture, best practices, pitfalls.",
expected_output="Comprehensive research report in markdown",
agent=researcher,
)
writing_task = Task(
description="Write a technical guide based on the research.",
expected_output="Well-structured technical guide",
agent=writer,
context=[research_task], # Receives output from research_task
)
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential, # or Process.hierarchical
verbose=True,
)
result = crew.kickoff(inputs={"topic": "MCP Protocol"})
Process Types
| Process | Description |
|---|---|
sequential |
Tasks execute in order, output chains to next |
hierarchical |
Manager agent delegates to team members |
Hierarchical with Manager
crew = Crew(
agents=[researcher, writer, reviewer],
tasks=[research_task, writing_task, review_task],
process=Process.hierarchical,
manager_llm=ChatOpenAI(model="gpt-4o"),
)
YAML Configuration
agents.yaml
researcher:
role: Senior Researcher
goal: Find comprehensive information on {topic}
backstory: Expert researcher with deep domain knowledge.
writer:
role: Technical Writer
goal: Write clear content based on research findings
backstory: Experienced writer specializing in tech docs.
tasks.yaml
research_task:
description: Research {topic} covering architecture, patterns, and pitfalls.
expected_output: Detailed research report in markdown.
agent: researcher
writing_task:
description: Create a technical guide from research findings.
expected_output: Publication-ready technical guide.
agent: writer
context:
- research_task
Memory System
crew = Crew(
agents=[...],
tasks=[...],
memory=True, # Enable memory
embedder={
"provider": "openai",
"config": {"model": "text-embedding-3-small"}
}
)
Three types: short-term (conversation), long-term (cross-session), entity (knowledge about entities).
Delegation
agent = Agent(
role="Project Manager",
allow_delegation=True, # Can delegate subtasks to other agents
tools=[...]
)
When enabled, agent can ask other crew members to help with subtasks.