Multi-agent FAQ¶
Short answers, with pointers to the code. New questions are added as they come up.
Basics¶
What is a multi-agent workflow, and how is it different from one agent?¶
Several agents, each with one job, joined by code that passes typed results between them.
One agent (../agents) |
A team (workflow/) |
|
|---|---|---|
| Prompt | One prompt for the whole job | One short prompt per job |
| Tools | All of them | Only what each job needs (ScopedTools) |
| Who decides the next step | The model, every turn | Code decides the stage; the model decides inside it |
| Passing results on | The chat history | Typed handoffs (Rfq, QuoteHandoff), checked in code |
| Failure | Somewhere in one long trace | In one stage, with an owner |
| Cost | Fewer calls | More calls, smaller contexts |
When should I split one agent into several?¶
- When a trace shows one agent failing at a specific job (dropping lines, ignoring the delivery city).
- When jobs need different tools or permissions (the writer must not create quotes).
- When jobs need different models, owners or release cycles.
- When parts are independent and can run in parallel (
03). - Not because the diagram looks better. Measure one agent vs the team first (
06_single_vs_multi.py).
What are the main multi-agent patterns?¶
| Pattern | Problem it solves | Lab |
|---|---|---|
| Sequential pipeline | Clear stages with different skills | 01_rfq_pipeline.py |
| Supervisor / router | Many kinds of request in one inbox | 02_supervisor.py |
| Fan-out and merge | Independent sub-tasks; long lists that one agent drops items from | 03_parallel_lines.py |
| Critic / reviewer | Mistakes code can't see | 04_critic.py |
| Human approval across agents | A person must sign off, later | 05_pause_and_resume.py |
Others you'll meet: agent-as-tool (a supervisor calls specialists as tools), handoff (an agent transfers the conversation to another), debate and negotiation. Same building blocks, different wiring.
Is "approval agent" really an agent?¶
No, on purpose. Who must approve is a business rule, so approval_policy() is code: limit, credit, shortfall, unmatched items, special requests. A person decides; no model is involved. Rule of thumb: don't make an agent of what an if-statement does.
Handoffs and control¶
How do the agents talk to each other?¶
- They don't chat. Each agent returns a typed object, and code passes it on (
Rfq->price_with_*->QuoteHandoff->write_reply). - Handoffs are built from tool results where possible: the quote comes from
create_quote's result, not the agent's summary (price_with_agent). - Every handoff has a contract check in code:
check_rfq,match_lines,check_reply. Fail once, the owner gets one fix; fail twice,HandoffErrorand a person takes over. - Shared chat between agents is the common mistake: it is hard to check, grows every turn, and lets one agent's error steer the next.
How should I design a handoff schema?¶
- One field per fact, with a description that says what goes there and what doesn't (
Rfqinworkflow/handoffs.py). - Overlapping fields get double-filed: in the first
01run, "delivery Mumbai" went into bothdelivery_cityandspecial_requests, so a plain ₹3,010 quote went to a manager and the email promised an account manager for nothing. - Enforce it in the contract check, not just the prompt (
check_rfqsends delivery-looking special requests back). - Prefer closed fields (numbers, Literals, IDs) over free-text lists wherever a rule reads the field.
Who decides which agent runs next?¶
| Style | Who decides | Here |
|---|---|---|
| Workflow | Code, fixed order | step() in workflow/stages.py; edges in lg_workflow.py |
| Router | A model picks from a fixed list, once | route() in 02 |
| Supervisor loop | A model picks again after each specialist | Not in the labs (see Going deeper) |
| Autonomous team | Agents hand off to each other freely | Avoid for business processes |
Start with a workflow. Add a router where the input decides the path.
What stops agents from looping or arguing?¶
- One fix per failed check, then a person (
ask_json,price_with_agent, the review stage'sfixes). - One budget for the whole workflow (
WorkflowBudget), on top of each agent's own (Budget). - Each run ends with a named status:
done,waiting_approval,stopped(budget, rejection) orneeds_human(a handoff a person must fix).
How does one budget work when agents run in parallel?¶
Each agent reserves its turns before it starts (ctx.reserved in _run()), so four parallel agents can't each spend the same last ten calls. When an agent finishes, the unused part is released. Without this, a shared budget overshoots by roughly the number of agents running at once.
Does fan-out make it faster with Ollama?¶
Only if Ollama runs requests in parallel. OLLAMA_NUM_PARALLEL sets how many requests one model serves at once; with 1, the line agents queue and fan-out is no faster than one at a time. Compare runs B and C in 03. Measured on a 16 GB Mac with OLLAMA_NUM_PARALLEL=4 (03): 23.4s one at a time vs 19.0s four at a time, because each request slowed from about 4.5s to 14s while sharing the GPU. On a laptop, fan-out's gain is correctness and isolation, not speed; a hosted API runs parallel calls for real.
Quality¶
Can the critic be the same model as the writer?¶
Same model is fine; same prompt and context is not. A model reviewing its own work in its own conversation tends to approve it. review() is a separate call with its own prompt. Measure it on seeded faults first (04, part 1).
Why did the critic pass everything, and how was it fixed?¶
- The first version asked qwen3:8b one big question: "review the RFQ, the quote and the email". It answered ok to all three seeded cases, including a wrong product and an email that dropped Pune (
04, first run). - Small models rubber-stamp big questions. The fix: one narrow question per quote line ("same type and size?"), with the product name in front of the model (
JUDGE_SYSTEM,LineVerdict). whycomes beforesame_productin the schema, so the model reasons before it decides.- Everything code can check is code (
check_replyinsidereview()); the verdict is computed, not asked for.
What can code checks catch, and what needs a critic?¶
- Code: amounts, IDs, counts, quantities, a missing delivery city or unanswered special request, "order placed" claims (
check_reply,match_lines). - Critic: meaning, such as M10 anchor bolts quoted as M8 hex bolts at the right quantity (
04, case B). - Always run code checks first: free, fast, never wrong about arithmetic.
- Wording that commits us counts too: "Delivery to Pune by 20 Nov" while gloves were out of stock read as a promise, though the prompt said "noted, not promised".
promised_dates()inworkflow/reply.pyflags the buyer's date unless the sentence says it was asked for. Same for terms: in lab 07 the note said 45 days and the email offered the buyer's 60;terms_off_note()now catches it.
How do I debug a multi-agent run?¶
Read the trace: every event has an agent name ([intake], [line3], [approval]), and print_summary() shows model calls, tool calls and seconds per agent. Then read the handoffs (wf.rfq, wf.quote): a wrong quote usually starts as a wrong Rfq. Production uses a tracing tool with one span per agent (rnr).
Is multi-agent more expensive?¶
Usually, in model calls: each agent has its own system prompt and turns. It can be cheaper in tokens per call (small contexts), and cheaper overall if it lets you use a small model per job instead of a large one for everything. Measure with 06.
Frameworks and protocols¶
Do I need LangGraph, CrewAI or AutoGen?¶
Not to learn. workflow/ is about 850 lines in 13 small files, most of it prompts, handoff models and checks. LangGraph (07) replaces the plumbing: order as edges, state saved after every node, interrupt() for pauses, history. It doesn't replace the stage functions, checks or policy. Keep those framework-free, so they're testable without it (test_workflow.py).
How is this different from MCP, or from A2A?¶
- MCP (mcp-server) connects an agent to tools and data: one agent, many servers.
- Multi-agent (this topic) connects agents to each other inside one application, through your code.
- A2A (Agent2Agent) is a protocol for agents in different services or companies to call each other, like MCP but agent-to-agent. You need it when the pricing agent belongs to another team's service, not when both live in one process.