ai · 9 min read

Building Multi-Agent Systems with CrewAI - Orchestrating AI Teams

Master CrewAI to build collaborative multi-agent systems where specialized AI agents work together to solve complex tasks.

Fortan Pireva · 28 November 2024

Imagine having a team of AI specialists - a researcher, a writer, a critic - all working together on your projects. That's the power of CrewAI, a framework designed to orchestrate multiple AI agents collaborating to accomplish complex tasks.

What is CrewAI?

CrewAI is a cutting-edge framework for orchestrating role-playing, autonomous AI agents. Unlike single-agent systems, CrewAI enables you to create "crews" of agents that work together, each with specific roles, goals, and tools.

Why Multi-Agent Systems?

Complex tasks often require diverse expertise. A single AI agent, no matter how capable, can't match the effectiveness of specialized agents working together:

  • Specialization: Each agent focuses on what it does best
  • Collaboration: Agents share information and build on each other's work
  • Quality: Multiple perspectives lead to better outcomes
  • Scalability: Add more agents for more complex tasks

Core Concepts of CrewAI

1. Agents - The Team Members

Agents are autonomous entities with specific roles and capabilities:

from crewai import Agent
from langchain_openai import ChatOpenAI

# Initialize LLM
llm = ChatOpenAI(model="gpt-4")

# Create a researcher agent
researcher = Agent(
    role='Senior Research Analyst',
    goal='Uncover cutting-edge developments in AI and data science',
    backstory="""You are an expert research analyst with a keen eye for
    emerging trends and technologies. You have a PhD in Computer Science
    and years of experience in the AI industry.""",
    verbose=True,
    allow_delegation=False,
    llm=llm,
    max_iter=5
)

# Create a writer agent
writer = Agent(
    role='Tech Content Strategist',
    goal='Craft compelling content on tech advancements',
    backstory="""You are a renowned content creator known for making complex
    technical topics accessible and engaging. Your articles are widely read
    and shared in the tech community.""",
    verbose=True,
    allow_delegation=True,
    llm=llm
)

# Create an editor agent
editor = Agent(
    role='Content Quality Assurance Specialist',
    goal='Ensure all content meets the highest standards',
    backstory="""You are a meticulous editor with an eye for detail and a
    passion for quality. You've edited for top tech publications and know
    what makes content truly exceptional.""",
    verbose=True,
    allow_delegation=False,
    llm=llm
)

2. Tasks - The Work Items

Tasks define what needs to be accomplished:

from crewai import Task

research_task = Task(
    description="""Conduct comprehensive research on the latest trends in
    {topic}. Identify key developments, major players, and potential
    future directions. Your final answer should be a detailed report
    with citations and sources.""",
    agent=researcher,
    expected_output="A detailed research report with at least 10 key findings"
)

writing_task = Task(
    description="""Using the research report, create an engaging blog post
    about {topic}. The post should be approximately 800 words, include
    relevant examples, and be accessible to a general tech audience.""",
    agent=writer,
    expected_output="An 800-word blog post in markdown format",
    context=[research_task]  # Depends on research task
)

editing_task = Task(
    description="""Review the blog post for accuracy, clarity, and
    engagement. Fix any grammatical errors, improve flow, and ensure
    technical accuracy. Provide the final polished version.""",
    agent=editor,
    expected_output="A polished, publication-ready blog post",
    context=[writing_task]  # Depends on writing task
)

3. Crews - The Team Structure

Crews orchestrate how agents work together:

from crewai import Crew, Process

# Create the crew
content_crew = Crew(
    agents=[researcher, writer, editor],
    tasks=[research_task, writing_task, editing_task],
    process=Process.sequential,  # Tasks executed in order
    verbose=True
)

# Execute the crew
result = content_crew.kickoff(inputs={"topic": "quantum computing"})
print(result)

Advanced CrewAI Patterns

Hierarchical Process

In hierarchical mode, a manager agent coordinates other agents:

manager = Agent(
    role='Project Manager',
    goal='Efficiently coordinate the team to produce high-quality content',
    backstory="""You are an experienced project manager who excels at
    coordinating teams and ensuring deadlines are met.""",
    llm=llm,
    allow_delegation=True
)

hierarchical_crew = Crew(
    agents=[researcher, writer, editor],
    tasks=[research_task, writing_task, editing_task],
    process=Process.hierarchical,
    manager_llm=llm,
    verbose=True
)

result = hierarchical_crew.kickoff(inputs={"topic": "AI ethics"})

Custom Tools for Agents

Equip agents with specialized tools:

from langchain.tools import Tool
from langchain_community.tools import DuckDuckGoSearchRun

# Web search tool
search = DuckDuckGoSearchRun()

# Custom database query tool
def query_database(query: str) -> str:
    """Query the internal knowledge base"""
    # Your database logic here
    return f"Database results for: {query}"

db_tool = Tool(
    name="DatabaseQuery",
    func=query_database,
    description="Query the internal knowledge base for company information"
)

# Agent with tools
researcher_with_tools = Agent(
    role='Senior Research Analyst',
    goal='Conduct thorough research using all available resources',
    backstory="""Expert researcher with access to multiple data sources.""",
    tools=[search, db_tool],
    llm=llm,
    verbose=True
)

Memory and Context Sharing

Enable agents to remember and learn:

from crewai import Agent, Task, Crew
from crewai.memory import EntityMemory, LongTermMemory

# Create agents with memory
analyst = Agent(
    role='Data Analyst',
    goal='Analyze data and identify trends',
    memory=True,  # Enable memory
    llm=llm
)

# Crew with memory capabilities
memory_crew = Crew(
    agents=[analyst, researcher, writer],
    tasks=[analysis_task, research_task, writing_task],
    memory=True,  # Enable crew-level memory
    verbose=True
)

Production-Ready CrewAI Applications

1. Customer Support Crew

# Define specialized support agents
triage_agent = Agent(
    role='Support Ticket Triage Specialist',
    goal='Quickly categorize and prioritize customer issues',
    backstory="""You excel at understanding customer problems and
    routing them to the right team.""",
    llm=llm
)

technical_agent = Agent(
    role='Senior Technical Support Engineer',
    goal='Resolve technical issues efficiently',
    backstory="""You have deep technical knowledge and can solve
    complex problems.""",
    tools=[kb_search_tool, ticket_system_tool],
    llm=llm
)

communication_agent = Agent(
    role='Customer Communication Specialist',
    goal='Provide clear, empathetic responses to customers',
    backstory="""You excel at translating technical solutions into
    customer-friendly language.""",
    llm=llm
)

# Define support workflow
triage_task = Task(
    description="Analyze the customer issue: {issue} and categorize it",
    agent=triage_agent,
    expected_output="Issue category and priority level"
)

resolution_task = Task(
    description="Develop a solution for the categorized issue",
    agent=technical_agent,
    context=[triage_task],
    expected_output="Technical solution steps"
)

response_task = Task(
    description="Draft a customer-friendly response with the solution",
    agent=communication_agent,
    context=[resolution_task],
    expected_output="Customer response email"
)

support_crew = Crew(
    agents=[triage_agent, technical_agent, communication_agent],
    tasks=[triage_task, resolution_task, response_task],
    process=Process.sequential
)

# Handle a support ticket
response = support_crew.kickoff(inputs={
    "issue": "Customer cannot log in after password reset"
})

2. Content Creation Pipeline

# SEO and topic research
seo_specialist = Agent(
    role='SEO Specialist',
    goal='Identify high-value topics and keywords',
    tools=[keyword_research_tool, competitor_analysis_tool],
    llm=llm
)

# Content creation
content_creator = Agent(
    role='Content Creator',
    goal='Create engaging, SEO-optimized content',
    llm=llm
)

# Visual design
designer = Agent(
    role='Visual Designer',
    goal='Create compelling visuals for content',
    tools=[image_generation_tool, design_tool],
    llm=llm
)

# Distribution
marketing_agent = Agent(
    role='Marketing Specialist',
    goal='Optimize content distribution across channels',
    tools=[social_media_tool, email_tool],
    llm=llm
)

# Content pipeline tasks
seo_task = Task(
    description="Research and identify top 5 topics for {niche}",
    agent=seo_specialist,
    expected_output="List of 5 topics with keyword analysis"
)

content_task = Task(
    description="Create comprehensive articles for identified topics",
    agent=content_creator,
    context=[seo_task],
    expected_output="5 complete articles in markdown"
)

design_task = Task(
    description="Create featured images and graphics for articles",
    agent=designer,
    context=[content_task],
    expected_output="Image URLs and alt text for each article"
)

distribution_task = Task(
    description="Create distribution plan and schedule posts",
    agent=marketing_agent,
    context=[content_task, design_task],
    expected_output="Distribution schedule and social media posts"
)

content_pipeline = Crew(
    agents=[seo_specialist, content_creator, designer, marketing_agent],
    tasks=[seo_task, content_task, design_task, distribution_task],
    process=Process.sequential,
    verbose=True
)

result = content_pipeline.kickoff(inputs={"niche": "AI development"})

3. Code Review and Quality Assurance

code_reviewer = Agent(
    role='Senior Code Reviewer',
    goal='Identify bugs, security issues, and improvement opportunities',
    tools=[static_analysis_tool, security_scanner],
    llm=llm
)

test_engineer = Agent(
    role='Test Engineer',
    goal='Design comprehensive test cases',
    tools=[test_framework_tool],
    llm=llm
)

documentation_writer = Agent(
    role='Technical Documentation Writer',
    goal='Create clear documentation for code changes',
    llm=llm
)

review_task = Task(
    description="Review the code changes in PR #{pr_number}",
    agent=code_reviewer,
    expected_output="Detailed code review with findings"
)

test_task = Task(
    description="Create test cases for the reviewed code",
    agent=test_engineer,
    context=[review_task],
    expected_output="Comprehensive test suite"
)

docs_task = Task(
    description="Update documentation based on code changes",
    agent=documentation_writer,
    context=[review_task],
    expected_output="Updated documentation in markdown"
)

qa_crew = Crew(
    agents=[code_reviewer, test_engineer, documentation_writer],
    tasks=[review_task, test_task, docs_task],
    process=Process.sequential
)

Monitoring and Optimization

Performance Tracking

from crewai.telemetry import Telemetry

class CrewMonitor:
    def __init__(self):
        self.metrics = {
            'task_completion_times': [],
            'agent_interactions': [],
            'errors': []
        }

    def track_crew_execution(self, crew: Crew, inputs: dict):
        start_time = time.time()

        try:
            result = crew.kickoff(inputs=inputs)
            execution_time = time.time() - start_time

            self.metrics['task_completion_times'].append({
                'crew': crew.__class__.__name__,
                'time': execution_time,
                'success': True
            })

            return result
        except Exception as e:
            self.metrics['errors'].append({
                'crew': crew.__class__.__name__,
                'error': str(e),
                'timestamp': time.time()
            })
            raise

monitor = CrewMonitor()
result = monitor.track_crew_execution(content_crew, {"topic": "AI"})

Cost Optimization

from langchain.callbacks import get_openai_callback

def optimize_crew_costs(crew: Crew, inputs: dict):
    with get_openai_callback() as cb:
        result = crew.kickoff(inputs=inputs)

        print(f"Total Tokens: {cb.total_tokens}")
        print(f"Total Cost: ${cb.total_cost:.4f}")

        # Analyze and optimize
        if cb.total_cost > 1.0:  # $1 threshold
            print("Warning: High cost detected. Consider:")
            print("- Using smaller models for simple tasks")
            print("- Reducing max_iter for agents")
            print("- Implementing caching")

        return result

Best Practices

1. Clear Role Definition

Each agent should have a distinct, well-defined role:

# Good: Specific and clear
data_analyst = Agent(
    role='Financial Data Analyst',
    goal='Analyze quarterly financial data and identify trends',
    backstory='Expert in financial analysis with 10 years experience'
)

# Avoid: Too broad
generic_agent = Agent(
    role='AI Assistant',
    goal='Help with various tasks',
    backstory='General AI helper'
)

2. Task Dependencies

Structure tasks to build on each other:

# Tasks with clear dependencies
tasks = [
    research_task,           # Step 1: Research
    analysis_task,           # Step 2: Analyze (needs research)
    writing_task,            # Step 3: Write (needs analysis)
    review_task              # Step 4: Review (needs writing)
]

# Each task specifies its context
analysis_task.context = [research_task]
writing_task.context = [analysis_task]
review_task.context = [writing_task]

3. Error Handling

Implement robust error handling:

def execute_crew_safely(crew: Crew, inputs: dict, max_retries: int = 3):
    for attempt in range(max_retries):
        try:
            return crew.kickoff(inputs=inputs)
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            print(f"Attempt {attempt + 1} failed: {e}. Retrying...")
            time.sleep(2 ** attempt)  # Exponential backoff

Real-World Success Stories

E-commerce Product Research

  • Researcher: Analyzes market trends
  • Competitor Analyst: Studies competitor products
  • Content Creator: Writes product descriptions
  • Result: Automated product research and listing creation

Financial Analysis

  • Data Collector: Gathers financial data
  • Analyst: Performs quantitative analysis
  • Report Writer: Creates investor reports
  • Result: Automated financial reporting pipeline

Marketing Campaigns

  • Market Researcher: Identifies target audiences
  • Creative Writer: Develops campaign copy
  • Designer: Creates visual assets
  • Campaign Manager: Optimizes distribution
  • Result: End-to-end campaign creation

Conclusion

CrewAI revolutionizes how we build AI applications by enabling true collaboration between specialized agents. By orchestrating teams of AI agents, you can:

  • Tackle complex, multi-faceted problems
  • Achieve higher quality outputs through specialization
  • Build scalable, maintainable AI systems
  • Automate entire workflows end-to-end

The key to success with CrewAI is thoughtful design - clearly define roles, establish task dependencies, and let your AI crew work together to achieve remarkable results.

As multi-agent systems become more sophisticated, frameworks like CrewAI will be essential for building the next generation of AI applications. Start small, experiment with different crew configurations, and scale up as you learn.


Ready to build your AI crew? Start with a simple three-agent team, define clear roles and tasks, and watch your AI agents collaborate to solve complex problems.