🧑🤝🧑 Crews: Teams of Specialist Agents
Definition: A Crew is a group of autonomous agents working in parallel toward a shared goal. Each agent has a distinct role and can communicate or share context with its teammates.
When to Use Crews:
-
Tasks that benefit from division of labor (e.g., one agent classifies intent, another retrieves context, a third drafts a response).
-
Collaborative problem‑solving, where agents peer‑review or refine each other’s outputs.
-
Scenarios requiring dynamic delegation, where a “dispatcher” agent routes subtasks to specialists.
🛠️ Crew Example
from crewai import Crew, Agent
class Classifier(Agent):
def run(self, text):
# Detect intent
return {"intent": "summarize"}
class Summarizer(Agent):
def run(self, text):
# Perform summarization
return text[:100] + "…"
class Formatter(Agent):
def run(self, summary):
return f"**Summary:** {summary}"
# Build and run a Crew
crew = Crew([Classifier(), Summarizer(), Formatter()])
result = crew.run("Long article text here…")
print(result)
# [
# {"intent": "summarize"},
# "Long article text here… (first 100 chars)…",
# "**Summary:** Long article text here…"
# ]
🔄 Flows: Event‑Driven Pipelines
Definition: A Flow is a linear or branching sequence of steps, each typically handled by one agent. Flows trigger agents in order, passing outputs from one step to the next.
When to Use Flows:
-
Data pipelines (e.g., ingest → validate → transform → store).
-
Conditional logic, where different branches execute based on intermediate results.
-
Simpler orchestration where multi‑step tasks follow a fixed progression.
🛠️ Flow Example
from crewai import Flow, Step
# Define steps
flow = Flow()
flow.add_step("ingest", lambda data: data.splitlines())
flow.add_step("validate", lambda lines: [line for line in lines if len(line) > 10])
flow.add_step("analyze", lambda valid: {"count": len(valid)})
flow.add_step("report", lambda stats: f"Valid lines: {stats['count']}")
# Run the Flow
result = flow.run("Short\nThis is a valid line.\nAnother one.")
print(result) # Output: "Valid lines: 2"
![GitHub - awslabs/multi-agent-orchestrator: Flexible and powerful ...]()
⚖️ Choosing Between Crews and Flows
-
Use Crews when you need parallelism, specialization, and inter‑agent communication.
-
Use Flows for linear or branching processes where tasks proceed one after another.
-
You can nest Crews inside Flows (or vice versa) for hybrid patterns: e.g., a Flow step invokes a Crew for part of the pipeline.
CrewAI’s flexible architecture means you’re never forced into one pattern—pick the abstraction that best matches your use case, or combine both to architect powerful, maintainable AI systems.