Advanced CrewAI
Complex multi-agent patterns, hierarchical crews, and advanced workflows
Advanced CrewAI Patterns
πΆ Explained like I'm 5
You know how to build simple crews with a few agents. But what if you need something more complex? Like a company with managers, teams, and workers all working together?
Advanced CrewAI is like building a whole organization of AI agents - some manage others, some work in parallel, some review work, and they all coordinate together!
Building on Basics
This module assumes you understand basic CrewAI. If not, review the Building with CrewAI module first.
β Why we need this
Simple crews are great, but real-world problems need:
- Hierarchical structures: Managers coordinating teams
- Parallel processing: Multiple agents working simultaneously
- Review loops: Quality assurance through multiple passes
- Dynamic workflows: Agents that adapt based on results
- Error recovery: Handling failures gracefully
Advanced patterns make crews more powerful and reliable!
π§ How it works
Pattern 1: Hierarchical Crews
Managers coordinate specialized teams:
βββββββββββββββββββ
β Manager Agent β
ββββββββββ¬βββββββββ
β
ββββββ΄βββββ
β β
βββββΌββββ ββββΌβββββ
β Team Aβ βTeam B β
β Agent1β βAgent1 β
β Agent2β βAgent2 β
βββββββββ βββββββββ
Code Example:
from crewai import Agent, Task, Crew
# Manager agent
manager = Agent(
role='Project Manager',
goal='Coordinate teams and ensure quality',
backstory='You are an experienced project manager',
verbose=True
)
# Team A agents
researcher_a = Agent(role='Researcher A', goal='Research topic A')
writer_a = Agent(role='Writer A', goal='Write about topic A')
# Team B agents
researcher_b = Agent(role='Researcher B', goal='Research topic B')
writer_b = Agent(role='Writer B', goal='Write about topic B')
# Manager coordinates both teams
coordination_task = Task(
description='Coordinate research and writing teams',
agent=manager
)
crew = Crew(
agents=[manager, researcher_a, writer_a, researcher_b, writer_b],
tasks=[coordination_task],
process='hierarchical',
manager_llm=manager.llm
)
Pattern 2: Review Loops
Content goes through multiple review cycles:
# Create β Review β Revise β Final Review
writer = Agent(role='Writer', goal='Create content')
reviewer = Agent(role='Reviewer', goal='Improve content')
final_reviewer = Agent(role='Final Reviewer', goal='Approve content')
write_task = Task(description='Write article', agent=writer)
review_task = Task(
description='Review and suggest improvements',
agent=reviewer,
context=[write_task]
)
revise_task = Task(
description='Revise based on feedback',
agent=writer,
context=[review_task]
)
final_task = Task(
description='Final review and approval',
agent=final_reviewer,
context=[revise_task]
)
crew = Crew(
agents=[writer, reviewer, final_reviewer],
tasks=[write_task, review_task, revise_task, final_task]
)
Pattern 3: Parallel Processing with Synthesis
Multiple agents work simultaneously, then results are combined:
# Parallel research β Synthesis
market_researcher = Agent(role='Market Researcher', goal='Analyze market')
tech_researcher = Agent(role='Tech Researcher', goal='Research technology')
competitor_researcher = Agent(role='Competitor Analyst', goal='Study competitors')
synthesizer = Agent(role='Synthesizer', goal='Combine findings')
# Parallel tasks (no dependencies)
market_task = Task(description='Research market', agent=market_researcher)
tech_task = Task(description='Research tech', agent=tech_researcher)
competitor_task = Task(description='Analyze competitors', agent=competitor_researcher)
# Synthesis task (depends on all three)
synthesis_task = Task(
description='Synthesize all research into comprehensive report',
agent=synthesizer,
context=[market_task, tech_task, competitor_task]
)
crew = Crew(
agents=[market_researcher, tech_researcher, competitor_researcher, synthesizer],
tasks=[market_task, tech_task, competitor_task, synthesis_task]
)
π§ͺ Advanced Examples
Example: Multi-Stage Content Creation
from crewai import Agent, Task, Crew
# Stage 1: Research
researcher = Agent(
role='Researcher',
goal='Gather comprehensive information',
tools=[DuckDuckGoSearchRun()],
verbose=True
)
# Stage 2: Outline
outliner = Agent(
role='Content Outliner',
goal='Create structured outline',
verbose=True
)
# Stage 3: Writing
writer = Agent(
role='Writer',
goal='Write engaging content',
verbose=True
)
# Stage 4: Fact-checking
fact_checker = Agent(
role='Fact Checker',
goal='Verify all facts',
tools=[DuckDuckGoSearchRun()],
verbose=True
)
# Stage 5: Editing
editor = Agent(
role='Editor',
goal='Polish content',
verbose=True
)
# Tasks with dependencies
research_task = Task(description='Research topic', agent=researcher)
outline_task = Task(description='Create outline', agent=outliner, context=[research_task])
write_task = Task(description='Write content', agent=writer, context=[outline_task])
fact_check_task = Task(description='Verify facts', agent=fact_checker, context=[write_task, research_task])
edit_task = Task(description='Edit and polish', agent=editor, context=[fact_check_task])
crew = Crew(
agents=[researcher, outliner, writer, fact_checker, editor],
tasks=[research_task, outline_task, write_task, fact_check_task, edit_task],
verbose=True
)
result = crew.kickoff()
π― Real-World Case Studies
Enterprise Content Production System
π Scenario
A company needs to produce hundreds of articles monthly with consistent quality, fact-checking, and SEO optimization.
π‘ Solution
Built hierarchical crew: Manager coordinates β Research Team (3 agents) β Writing Team (2 agents) β Fact-Checking Team (2 agents) β SEO Team (1 agent) β Quality Review Team (2 agents). Manager ensures workflow, teams work in parallel where possible, sequential where dependencies exist.
β Outcome
Production increased from 50 to 500 articles/month. Quality improved. Consistency achieved. Human editors now focus on strategy, not production.
π Key Lessons
- Hierarchical structures scale well
- Parallel processing increases throughput
- Review loops ensure quality
- Managers coordinate complex workflows
π Hands-on Task
Design an advanced crew for a complex problem:
- Choose Complex Problem: Something requiring multiple stages
- Design Hierarchy: Manager and teams
- Identify Parallel Tasks: What can run simultaneously?
- Plan Dependencies: What must happen in order?
- Add Review Loops: Where is quality critical?
- Error Handling: What if something fails?
β Checklist
Understand advanced patterns:
π¨ Common Pitfalls & Solutions
Pitfall 1: Over-Complex Hierarchies
Problem: Too many management layers slow things down.
Solution: Keep hierarchies flat. Only add managers when truly needed.
Pitfall 2: Ignoring Parallel Opportunities
Problem: Running everything sequentially wastes time.
Solution: Identify independent tasks and run them in parallel.
Pitfall 3: Missing Error Handling
Problem: One failure stops entire crew.
Solution: Add error handling and retry logic.
Complexity Warning
Start simple. Add complexity only when needed. Over-engineering slows development.
π‘ Best Practices
- Start Simple: Begin with basic crew, add complexity gradually
- Identify Parallelism: Look for independent tasks
- Use Hierarchies Sparingly: Only when coordination is needed
- Add Review Loops: For quality-critical outputs
- Handle Errors: Plan for failures
- Monitor Performance: Track what works
- Document Patterns: Help others understand your design
π Additional Resources
CrewAI Advanced Concepts
Advanced patterns and orchestration processes for multi-agent systems
π Challenge for GitHub
Build an advanced crew with:
- Hierarchical structure
- Parallel processing
- Review loops
- Error handling
- Complete documentation
Share your code and explain your design decisions!
π Next Steps
Continue learning:
- Explore: Memory & State Management - Persistent agent memory
- Learn: Testing & Debugging - Ensure crew reliability
- Review: Building with CrewAI - Refresh basics
Advanced Skills!
You now understand advanced CrewAI patterns. You can build complex, production-ready multi-agent systems!