scalable/ai/langgraph agent teams/lesson 05 cph / /
lesson 05 / 08 · 12 min · updated ·

swarms — peer-to-peer handoffs

no router: agents hand off directly and the active agent is part of the state. when a swarm beats a supervisor, and when it dissolves into chaos.

tl;dr. a swarm removes the router: agents hand off to each other directly via handoff tools, and "who is active" is just another state field. fewer hops, better momentum on specialist-to-specialist work — paid for with control flow that's distributed across every agent's prompt. use it for peer collaboration; keep the supervisor for dispatch.

delete the hub

two team topologies, one difference: where routing lives
//  supervisor (lesson 4):          swarm (this lesson):
//
//        ┌──────┐                 ┌─────────┐
//        │ hub  │                 │ billing │ ◄──┐
//       ╱   │   ╲                 └────┬────┘    │
//      ▼    ▼    ▼                     ▼         │
//   ┌────┐ ┌───┐ ┌────┐           ┌─────────┐    │
//   │ a  │ │ b │ │ c  │           │  tech   │ ───┘
//   └────┘ └───┘ └────┘           └────┬────┘
//   every hop through the              ▼
//   hub: route, work,             ┌─────────┐
//   report, route again           │ orders  │
//                                 └─────────┘
//                                 agents hand off directly;
//                                 "who's active" lives in state

the supervisor's cost is structural: every exchange is worker → hub → worker, twice the model calls and a full re-route even when the right next step is obvious to the agent doing the work. billing knows this ticket became a tech problem halfway through the second tool call. a swarm lets billing act on that knowledge — hand the conversation sideways, with context, without asking permission from a router.

a handoff is a tool

transfer_to_tech — a Command wrapped in a tool
import { tool } from "@langchain/core/tools";
import { Command } from "@langchain/langgraph";
import { z } from "zod";

// a handoff is a tool the agent can call — transfer as an action
// the model chooses, not a decision a router makes about it.
function makeHandoff(target: string, when: string) {
  return tool(
    async ({ context }, config) => {
      return new Command({
        goto: target,                     // jump to the peer...
        graph: Command.PARENT,            // ...in the parent graph
        update: {
          activeAgent: target,
          messages: [toolMsg(config, `→ ${target}: ${context}`)],
        },
      });
    },
    {
      name: `transfer_to_${target}`,
      description: `hand this conversation to ${target}. ${when}`,
      schema: z.object({
        context: z.string()
          .describe("one paragraph: what's done, what's needed next"),
      }),
    },
  );
}

const billing = createAgent({
  model: fast,
  tools: [
    ...billingTools,
    makeHandoff("tech", "use when the issue is a product bug, not an invoice."),
  ],
  systemPrompt: "you handle invoices. hand off anything that isn't one.",
});
the packaged version, three lines
import { createSwarm, createHandoffTool } from "@langchain/langgraph-swarm";

// the packaged version: agents + a default entry point.
// activeAgent is tracked in state, so a resumed thread
// continues with whoever was talking — not back at triage.
const swarm = createSwarm({
  agents: [billing, tech, orders],
  defaultActiveAgent: "billing",
}).compile({ checkpointer });

picking between the two

after building both a few times, my dividing line: supervisors dispatch, swarms collaborate. when work arrives as discrete units that need classifying and assigning — tickets, issues, queue items — central routing is a feature: one place to read the policy, one place to fix it. when the work is one continuous thing passing between specialists — a support conversation drifting from invoice to bug, a document moving between writer and fact-checker — the hub is pure overhead, and the swarm's momentum wins. the sdlc pipeline in lesson eight uses both: supervisor-style dispatch at the top, swarm-style peer flow inside a team. they compose; you don't have to convert.

naming note. "swarm" gets used for two different things: this pattern (peer handoffs, one active agent at a time — openai's swarm library popularized it) and "many agents working in parallel," which in this course is fan-out, lesson six. when someone says swarm, ask which one they mean before you agree with them.

when it breaks

next: subgraphs and fan-out — teams as black boxes, and the other kind of swarm: many workers at once.

scalable labs·cvr 30091604·github·linkedin·hello@scalable.dk