CrewAI — Build Multi-Agent Systems

Sanjeev SharmaSanjeev Sharma
2 min read

Advertisement

Introduction

CrewAI is a framework for building agent crews that work collaboratively. Perfect for complex tasks requiring multiple specialized agents.

Installation

pip install crewai

Basic Agent Setup

from crewai import Agent, Task, Crew

researcher = Agent(
    role='Researcher',
    goal='Find information and analyze topics',
    backstory='Expert researcher with deep knowledge',
    verbose=True
)

writer = Agent(
    role='Content Writer',
    goal='Write engaging content',
    backstory='Award-winning writer',
    verbose=True
)

Creating Tasks

research_task = Task(
    description='Research machine learning',
    agent=researcher,
    expected_output='Detailed research findings'
)

writing_task = Task(
    description='Write an article based on research',
    agent=writer,
    expected_output='Well-written article'
)

Crew Execution

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    verbose=True
)

result = crew.kickoff()
print(result)

Multi-Agent Workflow

from crewai import Agent, Task, Crew

# Define agents
data_scientist = Agent(
    role='Data Scientist',
    goal='Analyze data and build models',
    verbose=True
)

engineer = Agent(
    role='Engineer',
    goal='Implement models in production',
    verbose=True
)

# Define tasks
analysis_task = Task(
    description='Analyze sales data',
    agent=data_scientist
)

implementation_task = Task(
    description='Implement ML model',
    agent=engineer
)

# Create crew
crew = Crew(
    agents=[data_scientist, engineer],
    tasks=[analysis_task, implementation_task]
)

crew.kickoff()

Agent Tools

from crewai_tools import FileReadTool, WebsiteSearchTool

researcher = Agent(
    role='Researcher',
    tools=[
        FileReadTool(),
        WebsiteSearchTool()
    ]
)

Parallel Task Execution

crew = Crew(
    agents=[agent1, agent2],
    tasks=[task1, task2],
    process="sequential"  # or "hierarchical"
)

Conclusion

CrewAI makes building multi-agent systems straightforward. Ideal for complex workflows and collaborative tasks.

FAQ

Q: When should I use CrewAI vs AutoGen? A: CrewAI is simpler and more flexible; AutoGen is more structured.

Q: Can agents in CrewAI communicate? A: Yes, through shared context and sequential task execution.

Q: Is CrewAI good for production? A: Yes, increasingly used in production systems.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro