Skip to content

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_agent tool. 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 maxSteps limit replaces Python’s separate max_handoffs/max_iterations.
  • Agent descriptions are embedded in the structured output schema for routing decisions.
  • Exceeding maxSteps throws an exception. Python returns a FAILED result.
const swarm = new Swarm({
nodes: [researcher, writer],
start: 'researcher',
maxSteps: 10,
})
const result = await swarm.invoke('Explain quantum computing')
  • MultiAgentBase
new Swarm(options): Swarm;

Defined in: src/multiagent/swarm.ts:109

ParameterType
optionsSwarmOptions

Swarm

readonly id: string;

Defined in: src/multiagent/swarm.ts:100

Unique identifier for this orchestrator.

MultiAgentBase.id

readonly nodes: ReadonlyMap<string, AgentNode>;

Defined in: src/multiagent/swarm.ts:101


readonly config: Required<SwarmConfig>;

Defined in: src/multiagent/swarm.ts:102

initialize(): Promise<void>;

Defined in: src/multiagent/swarm.ts:133

Initialize the swarm. Invokes the MultiAgentInitializedEvent callback. Called automatically on first invocation.

Promise<void>


addHook<T>(eventType, callback): HookCleanup;

Defined in: src/multiagent/swarm.ts:147

Register a hook callback for a specific swarm event type.

Type Parameter
T extends HookableEvent
ParameterTypeDescription
eventTypeHookableEventConstructor<T>The event class constructor to register the callback for
callbackHookCallback<T>The callback function to invoke when the event occurs

HookCleanup

Cleanup function that removes the callback when invoked

MultiAgentBase.addHook

invoke(input): Promise<MultiAgentResult>;

Defined in: src/multiagent/swarm.ts:157

Invoke swarm and return final result (consumes stream).

ParameterTypeDescription
inputInvokeArgsThe input to pass to the start agent

Promise<MultiAgentResult>

Promise resolving to the final MultiAgentResult

MultiAgentBase.invoke

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.

ParameterTypeDescription
inputInvokeArgsThe input to pass to the start agent

AsyncGenerator<MultiAgentStreamEvent, MultiAgentResult, undefined>

Async generator yielding streaming events and returning a MultiAgentResult

MultiAgentBase.stream