05. Modern Stack: CrewAI
Overview
Manual string passing between agents represents the "From Scratch" approach. CrewAI provides a structured framework to define Agents (with Roles and Goals), Tasks (with Descriptions and Expected Outputs), and Crews (Processes).
Key Concepts
| Concept | Description |
|---|---|
| Role Playing | Giving agents rich backstories to improve performance |
| Agent | Autonomous unit with role, goal, and backstory |
| Task | Work item with description, expected output, and assigned agent |
| Crew | Team of agents with a process (sequential/hierarchical) |
| Process.sequential | Tasks executed one after another |
| Process.hierarchical | Manager agent delegates to specialists |
CrewAI Agent Definition
from crewai import Agent, Task, Crew, Process
researcher = Agent(
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI and data science",
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting
actionable insights.""",
verbose=True,
allow_delegation=False
)
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.""",
verbose=True,
allow_delegation=True
)Task Definition
research_task = Task(
description="""Conduct a comprehensive analysis of the latest
advancements in AI in 2024. Identify key trends, breakthrough
technologies, and potential industry impacts.""",
expected_output="Full analysis report in bullet points",
agent=researcher
)
write_task = Task(
description="""Using the research insights, develop an engaging
blog post that highlights the most significant AI advancements.""",
expected_output="Full blog post of at least 4 paragraphs",
agent=writer
)Crew Assembly
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential, # or Process.hierarchical
verbose=True
)
result = crew.kickoff()Sequential vs Hierarchical
Why CrewAI?
Role Playing: Agents with rich personas produce better results than generic instructions
| Feature | Benefit |
|---|---|
| Rich Backstories | Better context understanding |
| Delegation | Agents can ask each other for help |
| Memory | Agents remember previous interactions |
| Tools | Easy integration with custom tools |
Hands-on Practice
Define Your Agents
Create agents with roles, goals, and backstories
Create Tasks
Define what each agent should accomplish
Assemble the Crew
Choose process type and run the workflow
Analyze Results
Compare sequential vs hierarchical outputs