Exercise 2: Multi-Agent Role Allocation (CrewAI)¶
Pairs with Stage 4 — Workflow Graphs & Agent Frameworks Exercise 2.
🎓 How to use this: First run the provided
starter.py(python starter.py), then change exactly one small thing and run the existing test again:python test.py. If the test fails, undo or fix that one change and try again. You do not need to rename the file or rewrite the whole solution. Seedocs/HOW_TO_USE.mdfor the full method.📚 Want the chapter-length version? The starter in this folder is an illustrative build focused on the core pattern plus two SDK paths — it is not in-depth teaching material. Recommended for depth: -
datawhalechina/hello-agents⭐ the most complete Chinese-language course out there — chapter by chapter, plus 16 production capabilities. This exercise maps to hello-agents' multi-agent roles / Crew chapter - CrewAI Examples repo (official sequential / hierarchical templates; ⚠️ archived 2026-04, still worth reading as reference) - Full references in Stage 4 Curated Projects
Task¶
Three agents each own one step, collaborating to produce a blog intro:
Researcher → Writer → Critic
(find facts) (write) (verify, PASS/ISSUES)
This is a role-based pipeline: describe each role, goal, and task, then CrewAI passes the result along in order.
How to run — two paths¶
⚠️ Give each exercise its own Python 3.11
.venv. Do not mix the five Stage 4requirements.txtfiles.
Path A (default, free, local)¶
py -3.11 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
ollama pull qwen2.5:3b
ollama serve
.\.venv\Scripts\python.exe starter.py
Budget: the model API costs $0. Runtime depends on your CPU, memory, model, and prompt, so measure it on your machine.
Path B (Anthropic, compare a cloud result)¶
$env:ANTHROPIC_API_KEY="sk-ant-..."
.\.venv\Scripts\python.exe starter_anthropic.py
Pinned default: anthropic/claude-haiku-4-5-20251001. One model request with 2,000 input + 1,000 output tokens costs 2,000 / 1,000,000 × $1 + 1,000 / 1,000,000 × $5 = $0.007. Three roles may each call the model or retry, so set a provider spend limit of $0.10.
macOS/Linux commands and verification information
python3.11 -m venv .venv
./.venv/bin/python -m pip install -r requirements.txt
export ANTHROPIC_API_KEY="sk-ant-..."
./.venv/bin/python test.py
Official sources: CrewAI docs | LiteLLM docs | Anthropic pricing
Packages, model IDs, prices, and official links verified: 2026-08-28 UTC.
Validate the logic¶
.\.venv\Scripts\python.exe test.py # roles, tasks, handoff, and stop condition
.\.venv\Scripts\python.exe test_anthropic.py # Anthropic-path behavior
The offline tests do not call a real model, but they check three agents, three tasks, sequential process, context dependencies, handoff output, and an observable stop condition. Test model quality separately.
Core CrewAI multi-agent concepts¶
Agent¶
researcher = Agent(
role="Researcher",
goal="...", # one line: what does "success" look like
backstory="...", # persona context, shapes the prompt
tools=[search],
llm=MODEL,
)
Key: role and goal dramatically affect prompt quality. Don't write "Agent" — write "Researcher who finds factual data".
Task¶
research_task = Task(
description="Search for X and report findings.",
expected_output="A 1-2 sentence factual entry.",
agent=researcher,
)
Key: expected_output is the passing example the LLM sees. "A two-sentence intro in active voice" is clearer than "Some text"; measure the improvement on your own task.
Context dependency¶
write_task = Task(..., context=[research_task]) # writer sees researcher's output
critic_task = Task(..., context=[research_task, write_task]) # critic sees both
Key: context is CrewAI's dataflow mechanism. critic_task.context=[a, b] means the critic sees the output of tasks a and b.
Sequential vs hierarchical process¶
Crew(..., process=Process.sequential) # linear walk-through
Crew(..., process=Process.hierarchical) # manager + workers, needs manager_llm
This exercise uses sequential because its order is easy to see. Hierarchical lets a manager dispatch work; use it when you need dynamic assignment and already have evaluations and stop conditions.
Observation across both paths¶
| Observation | Anthropic Claude haiku | Ollama qwen2.5:3b |
|---|---|---|
| Researcher calls the tool | Verify in logs and output | Verify in logs and output |
| Writer uses the research | Check with the same test cases | Check with the same test cases |
| Critic catches an error | Do not assume success | Do not assume success |
| Speed | Measure on your network and task | Measure on your hardware and model |
| Model API cost | Calculate from tokens and calls | $0 |
Punchline: multi-agent adds handoff points. If one role drops information, the mistake can travel forward. Model size is not the only answer; evaluate role prompts, tool results, handoffs, and stop conditions.
Common pitfalls¶
expected_outputtoo generic: "Some output" has no clear success condition. Change it to "A two-sentence blog intro in active voice," then compare with test cases- Missing
context: writer withoutcontext=[research_task]doesn't see researcher's output — it'll hallucinate - Small model + 3 agents: it may run slowly or miss a step. Inspect the log first, then compare
qwen2.5:7bor Claude if needed allow_delegation=Trueuse cautiously: enables agents to call others, easy to loop. DefaultFalsefor prototypes
Want smarter answers?¶
$env:MODEL="anthropic/claude-sonnet-5"; .\.venv\Scripts\python.exe starter_anthropic.py
$env:MODEL="ollama/qwen2.5:7b"; .\.venv\Scripts\python.exe starter.py
Extensions¶
- Add a manager:
process=Process.hierarchical+manager_llm=...for dynamic delegation - Add memory: CrewAI has
memory=Truefor cross-task context - Batch or async execution:
crew.kickoff_for_each(...)handles a list of inputs;crew.kickoff_async(...)runs asynchronously. Neither one means streaming - Streaming: construct
Crew(..., stream=True), then callcrew.kickoff() - Human-in-the-loop: Exercise 3 demonstrates LangGraph; CrewAI also provides its own human-feedback triggers