English
Week 3: Multi-Agent
05. Modern Stack (CrewAI)

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

ConceptDescription
Role PlayingGiving agents rich backstories to improve performance
AgentAutonomous unit with role, goal, and backstory
TaskWork item with description, expected output, and assigned agent
CrewTeam of agents with a process (sequential/hierarchical)
Process.sequentialTasks executed one after another
Process.hierarchicalManager 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

FeatureBenefit
Rich BackstoriesBetter context understanding
DelegationAgents can ask each other for help
MemoryAgents remember previous interactions
ToolsEasy 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

References