CrewAI Multi-Agents

Course Content

CrewAI Multi-Agents

9 sections · 53 lessons

How do you implement manager-worker patterns using CrewAI?


What you need to know

Option A: hierarchical crew

Python
manager = Agent(    role="Research Director",    goal="Get a correct, sourced answer by assigning the right specialist",    backstory="You delegate every piece of work and reject answers without sources.",    llm="openai/gpt-4o",)pricing = Agent(role="Pricing Analyst", goal="...", backstory="...",                tools=[price_search], allow_delegation=False, max_iter=6)regulation = Agent(role="Regulation Analyst", goal="...", backstory="...",                   tools=[law_search], allow_delegation=False, max_iter=6)crew = Crew(agents=[pricing, regulation],            tasks=[Task(description="{question}",                        expected_output="An answer with sources")],            process=Process.hierarchical,            manager_agent=manager)

The task has no agent, so the manager chooses. The manager has no tools; CrewAI adds the delegation tools itself.

Option B: explicit plan and dispatch

Python
from typing import Literalfrom pydantic import BaseModel, Fieldfrom crewai.flow.flow import Flow, start, listenclass Step(BaseModel):    worker: Literal["pricing", "regulation"]    question: strclass Plan(BaseModel):    steps: list[Step] = Field(max_length=4)class ResearchFlow(Flow[ResearchState]):    @start()    def plan(self):        out = PlannerCrew().crew().kickoff(inputs={"question": self.state.question})        self.state.plan = out.pydantic            # a Plan    @listen(plan)    def run_steps(self):        crews = {"pricing": PricingCrew, "regulation": RegulationCrew}        for step in self.state.plan.steps:            out = crews[step.worker]().crew().kickoff(inputs={"q": step.question})            self.state.answers.append(out.raw)

The planner still uses an LLM, but its output is a typed list with at most 4 steps, and Python does the dispatching. Each step can be retried, logged and costed on its own. (ResearchState is a Pydantic model with question, plan and answers fields; the crews are your own.)

Hierarchical crew

  • Manager adapts during the run
  • Fast to build
  • Cost varies with the manager's choices
  • Harder to test and replay

Plan plus Flow

  • Plan is fixed once written
  • More code to write
  • Cost bounded by the plan length
  • Each step testable and retryable

A real-life example

A consumer-goods company's strategy team asks open questions like "should we launch our energy drink in Kerala?". They use a hierarchical crew: a Research Director manager and four specialists (market size, pricing, regulation, distribution). Question shapes vary a lot, so run-time routing is worth the cost. A run costs between Rs 30 and Rs 110 and takes 2 to 6 minutes, which the team accepts.

The same company's weekly retailer price report had also started as a hierarchical crew. Its steps never change, and the variable cost annoyed finance. It became plan-plus-Flow with a fixed three-step plan — effectively sequential — costing a steady Rs 20 per report. Same framework, two patterns, chosen by how predictable the work is.

Follow-up questions to expect

  • "Why must workers have delegation off?" — Otherwise workers can delegate back to each other or to the manager's other workers, creating loops and duplicated work.
  • "Can the manager use tools?" — No; CrewAI raises an error if a custom manager has tools. Give tools to workers.
  • "How do you cap cost in hierarchical mode?" — Low max_iter on manager and workers, max_rpm on the crew, max_execution_time, and monitoring token_usage per run.