Lab 02 — CrewAI Hierarchical Process Engine

Phase 19 · Lab 02 · Phase README · Warmup

The problem

The sequential process (lab-01) runs pre-assigned tasks in a fixed order — no LLM decides control flow. The hierarchical process does the opposite: you do not assign tasks to agents. You hand the crew a pool of specialist workers and a manager (in real CrewAI, a manager_llm or a manager_agent), and the manager, at runtime:

  1. delegates — for each task, decides which worker is best suited to it, and
  2. synthesizes — reconciles the workers' outputs into one final answer.

That flexibility is not free, and the interview question is always some version of "what does the manager cost you?" A hierarchical crew of N tasks makes N + 1 manager LLM calls (one delegation per task, plus one final synthesis) on top of the N worker calls — and the manager is a single point of judgment and failure. This lab makes that cost visible (CrewOutput.manager_calls) instead of leaving it as folklore.

What you build

PieceWhat it doesThe lesson
Manager (allocator, synthesizer)the two injected manager policiesdelegation and synthesis are separate jobs
Agent (worker, addressed by role)executes exactly the task delegated to itthe role string is the delegation address
Crew.kickoff(inputs) (hierarchical)delegate → execute → accumulate → synthesizedynamic routing + fan-in reconciliation
Delegationwho got what, and what came backthe audit trail for a surprising answer
CrewOutput.manager_callsN + 1the manager is not free

File map

FileRole
lab.pyyour implementation (fill the # TODOs)
solution.pyreference + worked example (python solution.py)
test_lab.py21 tests: delegation, worker isolation, context accumulation, synthesis, cost, errors, determinism
requirements.txtpytest only

Run it

pytest test_lab.py -v
LAB_MODULE=solution pytest test_lab.py -v
python solution.py

Success criteria

  • You can contrast sequential vs hierarchical on order, who decides, LLM-call count, and determinism without notes.
  • You can state why manager_calls == len(tasks) + 1 and why that is the headline cost of the hierarchical process.
  • You can explain why a delegation to an unknown worker must be a hard error (a manager LLM can hallucinate a role that does not exist).
  • A worker only ever sees the task delegated to it; the manager sees all outputs to synthesize.
  • All 21 tests pass under both lab and solution.

How this maps to the real stack

  • This is CrewAI's Process.hierarchical. In real CrewAI you set Crew(..., process=Process.hierarchical, manager_llm=<model>) (or manager_agent=<Agent>), do not put agent= on the tasks, and the auto-created manager plans, delegates via the built-in Delegate work / Ask question coworker tools, and produces the final answer. Our allocator is the delegation decision; our synthesizer is the manager's final write-up.
  • manager_calls is the real cost lever behind "hierarchical is more expensive": every task incurs manager reasoning to route it, and there is a final synthesis pass. In a token budget review (Phase 14) this is exactly the term you point at.
  • The Delegation trail is what CrewAI's verbose logs / AgentOps / Langtrace traces show you: which agent the manager handed each task to. When a hierarchical crew misbehaves, this is the first thing you read.

Limits of the miniature. Real CrewAI's manager can re-delegate, ask a worker a clarifying question, loop, and reject a worker's output — its coordination is itself an agent loop, not a single allocate-then-synthesize pass. We model the essential shape (delegate each task, run the worker, synthesize) that makes the cost and control-flow tradeoff legible; the recursive coordinator is the Extensions build.

Extensions (your own machine)

  • Give the manager a re-delegation loop: after a worker returns, let the synthesizer emit REVISE to send the task back (bounded by a step budget — Phase 00's max_steps).
  • Add an allow_delegation worker tool: let a worker itself ask the manager to delegate a sub-task to a peer, and measure how the manager-call count explodes.
  • Instrument per-role token estimates and produce the sequential-vs-hierarchical cost table for the same task list — the artifact you would bring to a design review.

Interview / resume signal

"Built CrewAI's hierarchical process from scratch — a manager that delegates each task to the best-suited worker by role and synthesizes their outputs — and surfaced its cost as N+1 manager LLM calls, making the sequential-vs-hierarchical tradeoff a number instead of a vibe."