Chapter 16
15 min read
Section 98 of 175

CrewAI Architecture

CrewAI Framework

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

FeatureDescription
Role-Based AgentsDefine agents by their roles, goals, and backstories
Task ManagementStructured task definitions with expected outputs
Process TypesSequential or hierarchical execution patterns
Tool IntegrationEasy tool sharing and agent-specific tools
DelegationAgents can delegate tasks to other agents
MemoryBuilt-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 dynamics

Core 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 expert

Architecture 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-community

Basic 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 point

Key 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.