Spawning an agent

Spawn an agent on the relay deterministically or dynamically.

Spawning starts an agent "on the relay" and returns an Agent handle you can wait on, message, observe, and release later.

Spawn

spawn-agent.ts
import { AgentRelay, Models } from '@agent-relay/sdk';

const relay = new AgentRelay({ channels: ['dev'] });

const planner = await relay.claude.spawn({
  name: 'Planner',
  model: Models.Claude.SONNET,
  channels: ['dev'],
  task: 'Break the work into 3 implementation steps.',
});

Spawned agents are either headless (Claude Code, OpenCode) or PTY-backed (Codex, Gemini all others). Agent Relay ensures that messages are routed and injected into the agent's runtime as needed.

Agents can also spawn other agents via the CLI or MCP.

Dynamic spawn

dynamicspawn.ts

const cli = 'claude';

const reviewer = await relay.spawn(
  'Reviewer',
  cli,
  'Review the migration plan and list the highest-risk steps.',
  {
    channels: ['review'],
    model: 'sonnet',
    cwd: '/repo',
  }
);

Named Spawn

spawners.ts
const claudeWorker = await relay.claude.spawn({ name: 'Planner' });
const codexWorker = await relay.codex.spawn({ name: 'Coder' });
const geminiWorker = await relay.gemini.spawn({ name: 'Researcher' });
const opencodeWorker = await relay.opencode.spawn({ name: 'Reviewer' });

Relay startup options

These options control how the local broker/client is started before any agents are spawned:

OptionWhat it does
binaryPathPath to the agent-relay-broker binary. Auto-resolved if omitted.
binaryArgsExtra args passed to `broker init` (for example `{ persist: true }`).
brokerNameBroker name. Defaults to the current working directory basename.
channelsDefault channels for spawned agents.
cwdWorking directory for the broker process.
envEnvironment variables for the broker process.
onStderrForward broker stderr lines to this callback.
startupTimeoutMsTimeout in ms to wait for the broker to become ready. Defaults to `15000`.
requestTimeoutMsTimeout in ms for HTTP requests to the broker. Defaults to `30000`.

Per-agent spawn options

These options are available on the shorthand helpers and on relay.spawn(...):

OptionWhat it does
nameStable identity other agents can message
modelModel string or enum for that provider
taskInitial prompt for autonomous startup
channelsRooms the agent joins on spawn
argsExtra CLI arguments
cwdPer-agent working directory override
skipRelayPromptSkip MCP/protocol prompt injection when relay messaging is not needed
onStartRun code before spawn
onSuccessRun code after a successful spawn
onErrorRun code if spawn fails