Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 39 ยท Multi-agent Systems

Multi-Agent Patterns

Learn when to use several AI agents instead of one, and the main patterns: pipeline, router, supervisor, debate and swarm, with runnable Python stand-ins.

One agent with a good prompt and a handful of tools can solve a surprising amount. But as tasks grow, a single agent starts to struggle: its prompt becomes a wall of instructions, its tool list is too long to choose from, and its context window fills with unrelated detail. A multi-agent system splits the work among several agents, each with a narrow role, its own instructions and its own tools.

This lesson explains when splitting helps (and when it only adds cost), then walks through the five patterns you will meet most: pipeline, router, supervisor, debate and swarm. Each has a small runnable stand-in.

No real model is called here. Each "agent" is a plain Python function that stands in for a model call with a role prompt, so you can see how the pieces connect.

Why split a single agent?

  • Focus. A short, specific prompt ("you only review SQL") is followed more reliably than a long one.
  • Tool clarity. Five tools per agent are easier to choose between than thirty.
  • Context hygiene. A sub-agent can read ten web pages and return one paragraph, keeping the main window small.
  • Parallelism. Independent sub-tasks can run at the same time.
  • Independent checking. A reviewer agent that did not write the draft catches different mistakes.

The costs are real: more model calls, more latency, harder debugging and new failure modes such as agents misunderstanding each other. Start with one agent. Add more only when you can name the problem a second agent solves.

Five multi-agent patterns

Pipeline A then B then C, in order
Router Pick one specialist per request
1 / 2

Pattern 1: pipeline

Each agent does one stage and passes its output to the next, like an assembly line: researcher, then writer, then editor. It is simple, predictable and easy to test stage by stage.

Python A three-stage pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weakness: errors flow downstream. If stage one is wrong, nobody notices. Add validation between stages.

Pattern 2: router

A router reads the request and sends it to exactly one specialist (billing, technical, sales). The router can be a small model, or even plain rules.

Python Route a request to a specialist

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Always include a general fallback. In production the routing step is often an LLM call that returns just a label, which you validate against the known set.

Pattern 3: supervisor (orchestrator)

A supervisor agent receives the goal, breaks it into tasks, hands each to a worker, and combines the results. It is the most common pattern for complex jobs and has its own lesson: orchestrator-worker pattern.

Pattern 4: debate and review

Two agents take opposing roles, or a critic reviews a maker's output. The maker revises until the critic approves or a round limit is reached. This improves quality on tasks where mistakes are easy to spot but hard to avoid, such as code and reasoning.

Python Maker and critic loop with a round limit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The round limit is essential. Two models can disagree forever, and each round costs money.

Pattern 5: swarm and handoffs

In a swarm, there is no boss. Agents hand the conversation to each other as needed: the triage agent hands to billing, billing hands to a human. Each agent knows only who it may hand off to. This is flexible but harder to reason about; see handoffs and shared state.

Choosing a pattern

Need Pattern
Fixed steps in order Pipeline
Many topics, one answer each Router
Open-ended task with parts Supervisor
Higher quality on checkable work Debate or review
Conversation passes between roles Swarm

Common failure modes

  • Chatty agents. Without a cap, agents trade pleasantries and burn tokens. Limit rounds and message length.
  • Lost context. A worker only knows what you send it. Pass the goal and constraints explicitly.
  • Conflicting actions. Two agents editing the same file need locking or a single owner.
  • Cascading errors. One wrong output becomes the next agent's trusted input. Validate at the boundaries.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When should you add a second agent?

    Extra agents add cost and complexity, so each one needs a reason.
  2. 2. Which pattern sends each request to exactly one specialist?

    A router classifies the request and picks one agent to handle it.
  3. 3. Why does a maker-critic loop need a round limit?

    A cap guarantees the loop ends.
  4. 4. What is a key weakness of a pipeline?

    Later stages trust earlier output, so add validation between stages.

Summary

  • Multi-agent systems split work by role, tools and context, at the price of more calls and complexity.
  • Pipeline, router, supervisor, debate and swarm cover most designs.
  • Always cap rounds, include a fallback and validate output at each boundary.
  • Start with one agent and grow only when there is a clear reason.

Next: The Orchestrator-Worker Pattern