Swarm
Defined in: src/multiagent/swarm.ts:99
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:109
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:100
Unique identifier for this orchestrator.
Implementation of
Section titled “Implementation of”MultiAgentBase.idreadonly nodes: ReadonlyMap<string, AgentNode>;Defined in: src/multiagent/swarm.ts:101
config
Section titled “config”readonly config: Required<SwarmConfig>;Defined in: src/multiagent/swarm.ts:102
Methods
Section titled “Methods”initialize()
Section titled “initialize()”initialize(): Promise<void>;Defined in: src/multiagent/swarm.ts:133
Initialize the swarm. Invokes the MultiAgentInitializedEvent callback. Called automatically on first invocation.
Returns
Section titled “Returns”Promise<void>
addHook()
Section titled “addHook()”addHook<T>(eventType, callback): HookCleanup;Defined in: src/multiagent/swarm.ts:147
Register a hook callback for a specific swarm event type.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends HookableEvent |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
eventType | HookableEventConstructor<T> | The event class constructor to register the callback for |
callback | HookCallback<T> | The callback function to invoke when the event occurs |
Returns
Section titled “Returns”HookCleanup
Cleanup function that removes the callback when invoked
Implementation of
Section titled “Implementation of”MultiAgentBase.addHookinvoke()
Section titled “invoke()”invoke(input): Promise<MultiAgentResult>;Defined in: src/multiagent/swarm.ts:157
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:173
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