Home

Chapter 2 · Lesson 0051

CrewAI — role-based multi-agent coordination

30–40 min read · Hands-on coding

Motivation

A customer-support pipeline that triages a ticket, researches the account history, drafts a reply, and has a second agent check tone before sending — that’s not one agent calling tools, it’s a small team with distinct jobs. CrewAI is the framework purpose-built for that shape of problem: agents with roles, the way a real team has roles, collaborating on a shared goal. It’s increasingly common in interview conversations and production stacks alongside LangChain and LangGraph, and you’ll close this lesson — and this three-lesson arc — knowing exactly when to reach for it over the other two.

Prerequisites

  • Lesson 50: LangGraph — you’ll contrast its graph model against CrewAI’s role-based model directly
  • Lesson 17: LangChain — CrewAI sits at the same framework tier, specialised for multi-agent teams
  • Install: uv add crewai crewai-tools

CrewAI — modeling a workflow as a team

CrewAI is a framework — the same tier as LangChain (Lesson 17), not the runtime tier LangGraph (Lesson 50) occupies. Where LangGraph models your workflow as a graph of states and transitions, CrewAI models it as a team of people with jobs to do. Both can build a multi-agent system; they just start from a different mental model, and that difference shapes which one fits a given problem more naturally.

Agent, Task, and Crew

Every CrewAI workflow is built from three pieces:

  1. Agent — a configured LLM with a role, a goal, and a backstory. The backstory shapes how the agent approaches problems — think of it as a system prompt that CrewAI writes and manages for you.
  2. Task — a specific piece of work with a description and an expected output format, assigned to an agent.
  3. Crew — the team: a list of agents, a list of tasks, and a process defining how they collaborate.
from crewai import Agent, Task, Crew, Process

# ── Agents ────────────────────────────────────────────────────────────
researcher = Agent(
    role="Research Analyst",
    goal="Gather and synthesise accurate information on any topic",
    backstory=(
        "You are a meticulous analyst who structures findings clearly "
        "and always flags gaps in the available information."
    ),
    llm="gpt-4o-mini",
    verbose=False,
)

writer = Agent(
    role="Technical Writer",
    goal="Transform research findings into clear, developer-friendly summaries",
    backstory="You write concise summaries that a developer can act on in two minutes.",
    llm="gpt-4o-mini",
    verbose=False,
)

# ── Tasks ─────────────────────────────────────────────────────────────
research_task = Task(
    description=(
        "Research vector databases in 2026: leading options, their trade-offs, "
        "and when to choose each one."
    ),
    expected_output="A structured list of 4–5 vector databases with pros, cons, and ideal use cases.",
    agent=researcher,
)

write_task = Task(
    description="Using the research, write a concise developer-friendly recommendation.",
    expected_output="A 200-word summary with a clear top recommendation and reasoning.",
    agent=writer,
    context=[research_task],   # writer receives researcher's output automatically
)

# ── Crew ──────────────────────────────────────────────────────────────
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential,
)

result = crew.kickoff()
print(result.raw)

The context=[research_task] parameter is how agents share results — the writer automatically receives everything the researcher produced, without you writing any state-passing code. This is CrewAI’s signature convenience: agent-to-agent handoff is handled by the framework, not by you.

Sequential vs hierarchical process

A Crew’s process decides how tasks move between agents:

manager = Agent(
    role="Project Manager",
    goal="Coordinate the team and ensure the final report meets the brief",
    backstory="An experienced manager skilled at delegation and quality control.",
    allow_delegation=True,
)

crew = Crew(
    agents=[manager, researcher, writer],
    tasks=[research_task, write_task],
    process=Process.hierarchical,
    manager_llm="gpt-4o-mini",
)

Use sequential when the order of work is known in advance — research, then write, then review. Reach for hierarchical when the right agent for a given task should be decided dynamically, not hard-coded into task order.

CrewAI vs LangGraph for multi-agent systems

Both frameworks can coordinate multiple agents — the choice comes down to which mental model fits your problem, and how much control you need over the mechanics of collaboration.

Choose CrewAI when you have distinct specialist roles that naturally hand off work — a researcher, a critic, a writer, a planner. The role-based model maps directly onto real team structures, and context=[...] removes the need to write state-passing logic by hand.

Choose LangGraph when you need precise control over branching, retries, and state at the level of individual transitions. CrewAI is harder to customise at the loop level — you’re working within its process abstraction. LangGraph gives full visibility into every node and edge, at the cost of writing more of the coordination logic yourself.

A useful rule of thumb from production teams: in a LangGraph workflow, a routing decision can be a pure Python function costing zero extra tokens. In CrewAI, delegation between agents typically triggers an LLM call. That makes CrewAI faster to build and reason about, but LangGraph cheaper and more predictable to run at scale.

Built-in tools

CrewAI ships a library of prebuilt tools through the separate crewai_tools package, so common integrations don’t need to be written from scratch:

from crewai_tools import SerperDevTool, FileReadTool, ScrapeWebsiteTool

search_tool = SerperDevTool()                        # web search (requires a Serper API key)
file_tool = FileReadTool(file_path="./data/report.txt")  # read a local file
scrape_tool = ScrapeWebsiteTool()                     # scrape and return a URL's text content

researcher = Agent(
    role="Researcher",
    goal="Find accurate information on the given topic",
    tools=[search_tool, scrape_tool],
    llm="gpt-4o-mini",
)

Tools are assigned to individual agents, not the crew as a whole — each agent only sees the tools relevant to its role, which keeps its decision-making focused.

Composio — connectors across frameworks

Composio is a connector framework that gives agents access to real external services — GitHub, Notion, Slack, Gmail, Linear, and hundreds more — through a single, authentication-managed interface. Instead of writing OAuth flows and API wrappers yourself, you install a connector and hand it to your agent as a tool:

from composio_langchain import ComposioToolSet, Action

toolset = ComposioToolSet()
github_tools = toolset.get_tools(actions=[
    Action.GITHUB_CREATE_ISSUE,
    Action.GITHUB_LIST_PULL_REQUESTS,
])

# These tools work as ordinary tools in either framework — CrewAI or LangChain
agent = Agent(role="Release Manager", goal="Triage open PRs", tools=github_tools, llm="gpt-4o-mini")

Composio handles authentication once (via composio login), then every agent in your system — built with CrewAI, LangChain, or both — can use those connectors without storing tokens in your code.

When Composio earns its place: an assistant that takes real actions across SaaS products — creating a GitHub issue, sending a Slack message, updating a Notion page. For pure data-retrieval tools like web search, rolling your own with ddgs (Lesson 12) is simpler and has no extra dependency.

Choosing your orchestration stack

You now know five ways to build an LLM-powered system, in increasing order of how much structure they hand you:

Situation Recommendation
Need total control, or maximum debuggability Raw API (Lesson 15)
Strict typed input/output validation matters most PydanticAI (Lesson 16)
Need a working tool-using agent fast, with room to grow LangChain (Lesson 17)
Need branching, durable execution, or fine-grained state LangGraph (Lesson 50)
Multi-agent team with distinct, naturally-handing-off roles CrewAI (this lesson)

These aren’t mutually exclusive in a real system — a production app might use LangChain’s create_agent for one component, drop to LangGraph for a step that needs durable execution, and use CrewAI for a research sub-team, all behind the same API.

Career note: CrewAI now appears in roughly a third of LangGraph-adjacent job listings — stronger co-occurrence than LangChain alone — which signals teams increasingly pair a runtime (LangGraph) with a role-based framework (CrewAI) rather than picking one. Knowing all three, and being able to justify the choice for a given problem, is what 2026 AI Engineering interviews actually probe for.

Assignment

  1. Read: CrewAI: A Practical Guide to Role-Based Agent Orchestration by DigitalOcean Community Tutorials — about 15 minutes. A practitioner-written crash course that goes from 'hello world' to a working multi-agent crew, with the same hands-on framing as this lesson — worth reading because it shows the role-based mental model applied to a different example than the one below.
  2. Do: Build a two-agent CrewAI crew that researches and writes a short brief on any topic.
    1. Define a Researcher agent (role, goal, backstory) and a Writer agent
    2. Define a research_task assigned to the researcher and a write_task assigned to the writer, with context=[research_task] so the writer receives the researcher’s output
    3. Assemble a Crew with Process.sequential and call kickoff()
    4. Run it once, then add verbose=True to both agents and run again — read the intermediate reasoning each agent produces before its final output

    Expected output: A ~150-word brief produced by the writer, visibly built on top of the researcher’s output, plus a transcript of each agent’s intermediate reasoning when verbose. Watching the verbose trace is the fastest way to see what 'role-based coordination' actually looks like at runtime — each agent reasoning inside its own role, not just outputting a final answer.

Knowledge Check

Answer each question from memory, then click to reveal.

Additional Resources