CrewAI Flows Guide
Agentby davila7 · Added 5mo ago
Claude
Install
npx claude-code-templates@latest --agent=cli-tool/components/skills/ai-research/agents-crewai/references --yesAdd to Claude
claude mcp add crewai-flows-guideAbout
CrewAI Flows Guide
Overview
Flows provide event-driven orchestration with precise control over execution paths, state management, and conditional branching. Use Flows when you need more control than Crews provide.
When to Use Flows vs Crews
| Scenario | Use Crews | Use Flows |
|---|---|---|
| Simple multi-agent collaboration | ✅ | |
| Sequential/hierarchical tasks | ✅ | |
| Conditional branching | ✅ | |
| Complex state management | ✅ | |
| Event-driven workflows | ✅ | |
| Hybrid (Crews inside Flow steps) | ✅ |
Flow Basics
Creating a Flow
from crewai.flow.flow import Flow, listen, start, router, or_, and_
from pydantic import BaseModel
# Define state model
class MyState(BaseModel):
counter: int = 0
data: str = ""
results: list = []
# Create flow with typed state
class MyFlow(Flow[MyState]):
@start()
def initialize(self):
"""Entry point - runs first"""
self.state.counter = 1
return {"initialized": True}
@listen(initialize)
def process(self, data):
"""Runs after initialize completes"""
self.state.counter += 1
return f"Processed: {data}"
# Run flow
flow = MyFlow()
result = flow.kickoff()
print(flow.state.counter) # Access final state
Flow Decorators
@start() - Entry Point
@start()
def begin(self):
"""First method(s) to execute"""
return {"status": "started"}
# Multiple start points (run in parallel)
@start()
def start_a(self):
return "A"
@start()
def start_b(self):
return "B"
@listen() - Event Trigger
# Listen to single method
@listen(initialize)
def after_init(self, result):
"""Runs when initialize completes"""
return process(result)
# Listen to string name
@listen("high_confidence")
def handle_high(self):
"""Runs when router returns 'high_confidence'"""
pass
@router() - Conditional Branching
@router(analyze)
def decide_path(self):
Tags
FullStackaitmplclaude-code-templates