Introduction
CrewAI is a framework designed for orchestrating role-playing autonomous AI agents. Unlike LangGraph's graph-based approach, CrewAI takes a role-based approach where agents are defined by their roles, goals, and backstories, making it intuitive to design collaborative AI teams.
Chapter Overview: We'll explore CrewAI's architecture, learn to define agents with roles and personas, understand task orchestration, and build complete multi-agent applications.
What is CrewAI
CrewAI enables you to create AI agents that work together like a team of specialists, each with their own role, expertise, and tools. This approach mirrors how real teams collaborate on complex projects.
Key Features
| Feature | Description |
|---|---|
| Role-Based Agents | Define agents by their roles, goals, and backstories |
| Task Management | Structured task definitions with expected outputs |
| Process Types | Sequential or hierarchical execution patterns |
| Tool Integration | Easy tool sharing and agent-specific tools |
| Delegation | Agents can delegate tasks to other agents |
| Memory | Built-in short-term, long-term, and entity memory |
CrewAI vs Other Frameworks
πpython
1"""
2CrewAI vs LangGraph vs AutoGPT
3
4CrewAI:
5- Role-based agent design (personas, backstories)
6- High-level abstraction
7- Built-in collaboration patterns
8- Great for: Team-based workflows, role-playing scenarios
9
10LangGraph:
11- Graph-based state machines
12- Fine-grained control over flow
13- Explicit state management
14- Great for: Complex workflows, precise control
15
16AutoGPT:
17- Fully autonomous goal pursuit
18- Self-prompting loop
19- Minimal human intervention
20- Great for: Open-ended exploration tasks
21"""
22
23# CrewAI focuses on making agent collaboration intuitive
24# by modeling real-world team dynamicsCore Philosophy
The Crew Metaphor
CrewAI uses the metaphor of a crew or team to organize agents. Just like a film production crew has a director, cinematographer, and editor, a CrewAI crew has specialized agents that work together toward a common goal.
πpython
1"""
2The Crew Metaphor in CrewAI
3
4ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5β CREW β
6β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
7β β PROCESS β β
8β β β β
9β β βββββββββββ βββββββββββ βββββββββββ β β
10β β β Agent 1 βββββΆβ Agent 2 βββββΆβ Agent 3 β β β
11β β β Role β β Role β β Role β β β
12β β β Tools β β Tools β β Tools β β β
13β β βββββββββββ βββββββββββ βββββββββββ β β
14β β β β β β β
15β β βΌ βΌ βΌ β β
16β β βββββββββββ βββββββββββ βββββββββββ β β
17β β β Task 1 β β Task 2 β β Task 3 β β β
18β β βββββββββββ βββββββββββ βββββββββββ β β
19β β β β
20β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
21β β β
22β βΌ β
23β ββββββββββββββ β
24β β OUTPUT β β
25β ββββββββββββββ β
26ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
27"""
28
29# Key components:
30# - Agent: An autonomous unit with role, goal, backstory, and tools
31# - Task: A specific piece of work with description and expected output
32# - Crew: A collection of agents working on tasks
33# - Process: How tasks are executed (sequential or hierarchical)Role-Playing AI
πpython
1"""
2Role-Playing in CrewAI
3
4Each agent has:
51. Role: Their job title (e.g., "Senior Researcher")
62. Goal: What they're trying to achieve
73. Backstory: Context that shapes their behavior
84. Tools: Capabilities they have access to
9
10This persona-based approach leads to more coherent,
11specialized behavior from agents.
12"""
13
14from crewai import Agent
15
16# Example: A researcher agent with clear persona
17researcher = Agent(
18 role="Senior Research Analyst",
19 goal="Uncover cutting-edge developments in AI technology",
20 backstory="""You are a veteran research analyst with 15 years
21 of experience in technology research. You have a PhD in Computer
22 Science and have published numerous papers on AI. You're known
23 for your thorough research and ability to synthesize complex
24 information into actionable insights.""",
25 verbose=True,
26 allow_delegation=False
27)
28
29# The backstory helps the LLM maintain consistent behavior
30# and approach problems from the perspective of an expertArchitecture Overview
Core Components
πpython
1"""
2CrewAI Core Architecture
3
4 βββββββββββββββββββββ
5 β CrewAI Core β
6 βββββββββββ¬ββββββββββ
7 β
8 βββββββββββββββββββββΌββββββββββββββββββββ
9 β β β
10 βΌ βΌ βΌ
11 βββββββββββββ βββββββββββββ βββββββββββββ
12 β Agent β β Task β β Crew β
13 βββββββ¬ββββββ βββββββ¬ββββββ βββββββ¬ββββββ
14 β β β
15 βββββββ΄ββββββ βββββββ΄ββββββ βββββββ΄ββββββ
16 β - role β β - desc β β - agents β
17 β - goal β β - agent β β - tasks β
18 β - back β β - output β β - processβ
19 β - tools β β - contextβ β - memory β
20 β - llm β β β β β
21 βββββββββββββ βββββββββββββ βββββββββββββ
22
23Memory System:
24βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
25β Memory β
26β ββββββββββββ ββββββββββββ ββββββββββββββββββββ β
27β βShort-termβ βLong-term β βEntity Memory β β
28β β(context) β β(learning)β β(relationships) β β
29β ββββββββββββ ββββββββββββ ββββββββββββββββββββ β
30βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
31"""
32
33from crewai import Agent, Task, Crew, Process
34from langchain_openai import ChatOpenAI
35
36# LLM Configuration
37llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
38
39# Agent with full configuration
40agent = Agent(
41 role="Technical Writer",
42 goal="Create clear and comprehensive documentation",
43 backstory="Expert technical writer with 10 years experience",
44 llm=llm,
45 tools=[], # List of tools
46 verbose=True,
47 allow_delegation=True,
48 max_iter=15, # Maximum iterations
49 max_rpm=None, # Rate limiting
50 memory=True # Enable memory
51)Execution Flow
πpython
1"""
2CrewAI Execution Flow
3
41. Crew Initialization
5 βββΆ Load agents, tasks, and configuration
6
72. Task Assignment
8 βββΆ Match tasks to agents based on process type
9
103. For each task:
11 βββΆ Agent receives task description
12 βββΆ Agent reasons about approach
13 βββΆ Agent uses tools if needed
14 βββΆ Agent may delegate to other agents
15 βββΆ Agent produces output
16 βββΆ Output becomes context for next task
17
184. Crew produces final output
19 βββΆ Combined results from all tasks
20
21"""
22
23# Example flow with three agents
24from crewai import Crew, Process
25
26# Define crew with sequential process
27crew = Crew(
28 agents=[researcher, analyst, writer],
29 tasks=[research_task, analysis_task, writing_task],
30 process=Process.sequential, # Tasks run in order
31 verbose=True
32)
33
34# Execute the crew
35result = crew.kickoff()
36
37# Tasks execute in order:
38# 1. researcher does research_task
39# 2. analyst does analysis_task (uses research output)
40# 3. writer does writing_task (uses analysis output)Installation and Setup
β‘bash
1# Install CrewAI
2pip install crewai
3
4# Install with extra tools
5pip install 'crewai[tools]'
6
7# Optional: Install LangChain integrations
8pip install langchain-openai langchain-communityBasic Configuration
πpython
1import os
2from crewai import Agent, Task, Crew, Process
3from langchain_openai import ChatOpenAI
4
5# Set up API keys
6os.environ["OPENAI_API_KEY"] = "your-api-key"
7
8# Configure LLM
9llm = ChatOpenAI(
10 model="gpt-4o",
11 temperature=0.7,
12 max_tokens=4000
13)
14
15# Minimal working example
16researcher = Agent(
17 role="Researcher",
18 goal="Find accurate information on given topics",
19 backstory="You are an experienced researcher",
20 llm=llm,
21 verbose=True
22)
23
24research_task = Task(
25 description="Research the current state of AI agents in 2025",
26 expected_output="A comprehensive report on AI agent trends",
27 agent=researcher
28)
29
30crew = Crew(
31 agents=[researcher],
32 tasks=[research_task],
33 process=Process.sequential,
34 verbose=True
35)
36
37# Run the crew
38result = crew.kickoff()
39print(result)Project Structure
β‘bash
1# Recommended CrewAI project structure
2my_crew_project/
3βββ src/
4β βββ __init__.py
5β βββ crew.py # Main crew definition
6β βββ agents.py # Agent definitions
7β βββ tasks.py # Task definitions
8β βββ tools/
9β βββ __init__.py
10β βββ custom_tools.py
11βββ config/
12β βββ agents.yaml # Agent configurations
13β βββ tasks.yaml # Task configurations
14βββ tests/
15β βββ test_crew.py
16βββ requirements.txt
17βββ main.py # Entry pointKey Takeaways
- CrewAI uses role-based design - Agents are defined by roles, goals, and backstories, making collaboration intuitive.
- The crew metaphor organizes agents as a team working together on shared objectives.
- Core components are Agent, Task, and Crew - Simple abstractions that compose into powerful workflows.
- Built-in memory system enables agents to learn and maintain context across interactions.
- Process types (sequential, hierarchical) control how tasks are executed.
Next Section Preview: We'll dive deeper into Agents, Tasks, and Crews - the core building blocks of CrewAI applications.