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:
- delegates — for each task, decides which worker is best suited to it, and
- 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
| Piece | What it does | The lesson |
|---|---|---|
Manager (allocator, synthesizer) | the two injected manager policies | delegation and synthesis are separate jobs |
Agent (worker, addressed by role) | executes exactly the task delegated to it | the role string is the delegation address |
Crew.kickoff(inputs) (hierarchical) | delegate → execute → accumulate → synthesize | dynamic routing + fan-in reconciliation |
Delegation | who got what, and what came back | the audit trail for a surprising answer |
CrewOutput.manager_calls | N + 1 | the manager is not free |
File map
| File | Role |
|---|---|
lab.py | your implementation (fill the # TODOs) |
solution.py | reference + worked example (python solution.py) |
test_lab.py | 21 tests: delegation, worker isolation, context accumulation, synthesis, cost, errors, determinism |
requirements.txt | pytest 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) + 1and 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
labandsolution.
How this maps to the real stack
- This is CrewAI's
Process.hierarchical. In real CrewAI you setCrew(..., process=Process.hierarchical, manager_llm=<model>)(ormanager_agent=<Agent>), do not putagent=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. Ourallocatoris the delegation decision; oursynthesizeris the manager's final write-up. manager_callsis 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
Delegationtrail 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
REVISEto send the task back (bounded by a step budget — Phase 00'smax_steps). - Add an
allow_delegationworker 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."