Swarm
Defined in: src/multiagent/swarm.ts:97
Swarm multi-agent orchestration pattern.
Agents execute sequentially, each deciding whether to hand off to another agent or
produce a final response. Routing is driven by structured output: each agent receives
a Zod schema with agentId, message, and optional context fields. When agentId
is present, the swarm hands off to that agent with message as input. When omitted,
message becomes the final response.
Key design choices vs the Python SDK:
- Handoffs use structured output rather than an injected
handoff_to_agenttool. Routing logic stays in the orchestrator, not inside tool callbacks. - Context is passed as serialized JSON text blocks rather than a mutable SharedContext.
- A single
maxStepslimit replaces Python’s separatemax_handoffs/max_iterations. - Agent descriptions are embedded in the structured output schema for routing decisions.
- Exceeding
maxStepsthrows an exception. Python returns a FAILED result.
Example
Section titled “Example”const swarm = new Swarm({ nodes: [researcher, writer], start: 'researcher', maxSteps: 10,})
const result = await swarm.invoke('Explain quantum computing')Implements
Section titled “Implements”MultiAgentBase
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Swarm(options): Swarm;Defined in: src/multiagent/swarm.ts:106
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options | SwarmOptions |
Returns
Section titled “Returns”Swarm
Properties
Section titled “Properties”readonly id: string;Defined in: src/multiagent/swarm.ts:98
Unique identifier for this orchestrator.
Implementation of
Section titled “Implementation of”MultiAgentBase.idconfig
Section titled “config”readonly config: Required<SwarmConfig>;Defined in: src/multiagent/swarm.ts:99
readonly hooks: HookRegistry;Defined in: src/multiagent/swarm.ts:100
Methods
Section titled “Methods”initialize()
Section titled “initialize()”initialize(): Promise<void>;Defined in: src/multiagent/swarm.ts:131
Initialize the swarm. Invokes the MultiAgentInitializedEvent callback. Called automatically on first invocation.
Returns
Section titled “Returns”Promise<void>
invoke()
Section titled “invoke()”invoke(input): Promise<MultiAgentResult>;Defined in: src/multiagent/swarm.ts:143
Invoke swarm and return final result (consumes stream).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | InvokeArgs | The input to pass to the start agent |
Returns
Section titled “Returns”Promise<MultiAgentResult>
Promise resolving to the final MultiAgentResult
Implementation of
Section titled “Implementation of”MultiAgentBase.invokestream()
Section titled “stream()”stream(input): AsyncGenerator<MultiAgentStreamEvent, MultiAgentResult, undefined>;Defined in: src/multiagent/swarm.ts:159
Stream swarm execution, yielding events as agents execute. Invokes hook callbacks for each event before yielding.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | InvokeArgs | The input to pass to the start agent |
Returns
Section titled “Returns”AsyncGenerator<MultiAgentStreamEvent, MultiAgentResult, undefined>
Async generator yielding streaming events and returning a MultiAgentResult
Implementation of
Section titled “Implementation of”MultiAgentBase.stream