/swarm:building-on-swarmfrom swarm
Guides building a custom app on Swarm using @swarm-agent/sdk — covers onboarding, streaming chat, permissions, sessions, projects, custom tools, workers, and monitoring.
Ships in swarm. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Your app is a small Node server plus a web page. The server talks to Swarm with
@swarm-agent/sdk; the page talks only to your server. Never give the page a
Swarm token, key or the Swarm socket.
npm install @swarm-agent/sdk # or a local tarball; it depends on `ws` from npm
import { SwarmClient } from '@swarm-agent/sdk';
// The owner socket: no token; the server runs as the same OS user as Swarm.
const client = new SwarmClient({
socketPath: process.env.SWARM_SOCKET_PATH || '/var/lib/swarmd/local-transport/api.sock',
});
Serve node_modules/@swarm-agent/sdk/dist/ at /sdk/ and import the browser
client as a plain ES module: import { SwarmBrowserChat, describePermission, answerPermission } from '/sdk/browser.js'. Bind your server to 127.0.0.1
unless you add your own authentication. Loopback alone does not stop other web
pages in the person's browser from calling your server: on every request that
changes something (POST/PUT/DELETE, and the WebSocket upgrade), refuse an
Origin that is not your own page, and accept only Content-Type: application/json bodies.
Decide what is missing from Swarm's state, not from
config.desktop_onboarding_complete (it can be true on a fresh Swarm):
| Step | Done when | Do |
|---|---|---|
| Name | (await client.onboarding.get()).identity.bootstrapped | ask name + Swarm name, client.onboarding.ensureBootstrapped({ username, swarm_name }) |
| Model access | heuristics.credential_count > 0 | API key or ChatGPT sign-in (below) |
| Models | (await client.settings.agentModels()) has a swarm action model | usually already done: Swarm sets recommended models when the first key or sign-in is saved; otherwise client.settings.applyProviderFleet(provider) |
| Workspace | your app's workspaces are registered (client.workspaces.list()) | ask which existing folders or repositories the work is in and register each with client.workspaces.add({ path, context_only: true, require_existing_content: true, make_current: false }) (refuses empty, system and nested folders; changes nothing in the folder). Only for a new project from scratch: client.workspaces.create({ parent_path, name }) (an empty repository; name: letters, digits, - and _, at most 63). Store each path and find it again by that path |
| Finish | all above | client.onboarding.update({ desktop_onboarding_complete: true }) |
Re-check onboarding.get() every few seconds while onboarding is open: a key
or sign-in can be added elsewhere (another app, Claude, the CLI), and the page
must move on without a reload.
Before the name step, almost every other call fails ("product identity has not
been bootstrapped"): call only onboarding.get until then. Before any model is
set, agentModels() returns every role unset (empty provider and model,
updated_at 0): treat that as "no models yet", not as an error to show. Older
Swarms answer a 404 "agent model settings not found" instead; treat it the same.
client.settings.providers() (after the name step) returns
{ id, ready, reason, auth_methods: [{ id, label, credential_type }] } per
provider. Model providers have a default_model (others, such as exa for
web search, do not: leave them out of model onboarding).
credential_type: 'api' takes an API key; the codex provider
(credential_type: 'oauth') is the ChatGPT sign-in. ready is true once a
working credential exists. Show reason only as a short status, and never
tell people to use Swarm's own settings screens or commands.client.onboarding.credential({ provider, type: 'api', api_key, active: true }); later ones client.auth.credentials.save(...).
The key goes form → your server → Swarm only: never log, store or return it;
redact it from any error you show. Swarm verifies it: show its error.client.auth.codex.loginDevice({ onCode }) on the
server; show verification_url and user_code; the page polls your server.
It resolves when the sign-in succeeded and throws on failure or expiry. It
can take minutes and cannot be cancelled in Swarm (abandoning it does not
revoke a granted sign-in).client.auth.credentials.list()
(redacted records; the active one's provider), then the Models step.Use a project with your workspace; its conversations are orchestrator sessions and support clearing context. Without a workspace the orchestrator cannot run commands or hand out work.
Creating a project makes Swarm write its project.md (the orchestrator's
context) from the workspaces' AGENTS.md, README and file tree. Open
conversations once project.context_generation.status is ready: re-read with
client.projects.get(id) (or follow realtime project updates); on failed,
client.projects.retryContext(id, project.context_generation.attempt).
const project = await client.projects.ensureProject('My app', { workspacePath });
const list = await client.projects.listConversations(project.id, { limit: 100 });
const created = await client.projects.createConversation(project.id, {
title: 'New conversation', clientRequestId: crypto.randomUUID(),
});
Swarm titles a conversation from its first message; show title from
listConversations. Rename with
client.projects.renameConversation(id, title) (or
client.sessions.rename(id, title)). Do not keep your own labels.
Agent replies can contain <copy label="…">…</copy> snippet tags: pass reply
text through copyTagsToMarkdown(text) (from /sdk/browser.js) before
rendering it as Markdown.
client.chat.attachWebSocket(server, { path: '/ws', autoApprovePermissions: false });
const chat = new SwarmBrowserChat(); // same-origin /ws
chat.on('state', s => render(s.messages, s.live)); // the rendering authority
chat.on('tool_start', t => toolCard(t.id, t.name, t.arguments));
chat.on('tool_delta', (t, chunk) => toolOutput(t.id, chunk));
chat.on('tool_done', t => toolDone(t.id));
chat.on('permissions', p => renderPermissions(p.map(describePermission)));
chat.on('permission_removed', id => removePermission(id));
chat.on('error', showError);
await chat.connect();
await chat.subscribe(conversationId); // before sending
chat.sendMessage(text);
state.messages) and the live reply (state.live) on each
state; never append fullText to history. Reconnects resume without loss
or repeats; a send while disconnected throws: show it, never resend silently.
Send errors are not tied to a message: attach them to your latest
unacknowledged send.state.snapshot.events_by_session[id] (newest 1000) whose event_type is
session.tool.started, session.tool.completed or session.tool.failed.
Their payload has call_id (match the live callbacks' callId on it),
tool_name, arguments (a JSON string), status, and on completion
output or error; ignore payload.metadata. Merge with the live
callbacks; hide tool role messages.created_at on messages, ts_unix_ms on events) are
milliseconds since the epoch.state.snapshot.current_run_state_by_session[id] has active
(true while a run is going, including while it waits for a permission) and
status (completed, failed or cancelled once it ended). Decide busy
from active, not from status names.await chat.subscribe(otherId). Selecting the
conversation that is already open must do nothing: never clear the thread
before the new subscription has delivered its state.state.historyOmitted is
true and only the newest part is in state.messages: say older history is
not shown.client.sessions.stopRun(id, { run_id })
with the run_id from current_run_state_by_session[id]. A stopped plan
pauses; the next message continues it.describePermission(p) gives kind (tool or ask-user), toolName,
title, context, rawArguments, questions and actions. Lead each card
with title and context in plain words; put rawArguments behind a
"details" toggle, never as the main content. Render as text
(never HTML) and resolve only on the person's click:
await chat.resolvePermission(p.id, 'allow_once',
describePermission(p).kind === 'ask-user' ? answerPermission(p, input) : {});
await chat.resolvePermission(p.id, 'deny');
input for answerPermission: a string when describePermission(p).structured
is false (one free question), else a map { [question.id]: answer }. An
option with custom: true is not an answer itself: show a text box for it.
The permissions event repeats the same requests often: key cards by id,
keep unsent answers across re-renders, remove a card only on
permission_removed. Some destructive commands are refused by Swarm policy and
never become requests.
await client.sessions.clearContext(id, {
expectedLastEventSeq: state.snapshot.projections_by_session[id].last_event_seq,
});
Allowed only while the conversation is idle (a conflict error otherwise: show it; retry after the run). History stays visible; the model starts fresh.
Give an agent exactly the tools your app answers, nothing else (no shell, files or web; Swarm enforces it on every call):
await client.agents.defineClientTool({
name: 'lookup_order', description: 'Find an order by id',
input_schema: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'] },
effect: 'read', // 'write' = external effect: confirm with a person first
});
await client.agents.defineSealedAgent({ name: 'frontdesk', prompt: 'Help customers with orders.', tools: ['lookup_order'] });
const session = await client.sessions.create({ agent_name: 'frontdesk', title: 'Visitor', workspace_path: workspacePath }); // your app's workspace
const turn = await client.agents.converse(session.id, 'Where is order 42?', {
lookup_order: async ({ id }) => ({ id, status: 'shipped' }),
});
// turn.reply is the answer; for a long-lived session use agents.serve(sessionId, handlers, { signal }).
Throw ClientToolError('message') to send that message to the model.
client.projects.createTask(projectId, { agent: 'swarm', title, description }),
getTask, listTasks. Tasks the orchestrator makes wait in
pending_approval; show them and approve only on the person's click:
client.projects.approveTask(projectId, taskId, guards), with guards from
the task you showed (plan_binding.session_id, plan_id,
definition_revision, else session_id), so a task that changed since is
not approved.task.session_id), and its agent can
ask for permissions or questions there: show them on the task
(client.permissions.listSessionPending(sessionId), resolve with
client.permissions.resolve(sessionId, id, action, …) on the person's
click), or the task waits forever.needs_review. Its result is in getTask(…).task:
attempts[].summary and delivery_assessment (changed files, commits),
plus the last assistant message of the task's session. Then, on the
person's click: integrateTask(projectId, taskId) (it reads the task and fills in the
session and branches) merges the work into the
workspace's branch, completeTask accepts it without merging,
rejectTask sends it back.client.apps.put,
createConversation, send, watchResults({ onChange }) for live task and
worker results.client.workers (create, trigger, listRuns, getRun; a
succeeded run's review says whether its work awaits review).client.agents.spendToday(): today's cost, the daily cap and whether it is hit.client.usage: usage scope and budgets; sign-in (subscription) providers
show no metered cost: say so in your UI.client.notifications.list() / summary(): Swarm's notifications. Each has
title, body, category, severity, status and read_at (unread while
read_at is empty); notifications.update(id, { read: true }) marks one read.Onboarding that only asks for what is missing · streaming chat with tool cards · permission approve, deny and questions · new, list, switch conversations · clear context · model and provider status · errors shown, never hidden · keys never logged or returned · loopback by default · own-origin checks on every write.
title/context.100dvh, keep the composer in normal flow at the bottom of the scroll area
(or pad by the tab bar's height plus env(safe-area-inset-bottom)), and
check it at 390×844 with the keyboard closed and open.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub swarm-agent/swarm --plugin swarm3 skills in this plugin
/setupInstall Swarm (a box the person deploys AI workers to) on their Linux machine, connect their AI to it locally, get a model provider ready, create a project with their folders, and prove a first job. Use when the person asks to set up Swarm, connect their existing Swarm, or get started with the plugin. Help with Tailscale or a relay only if they want to reach Swarm from other machines./using-swarmGet work done by the person's Swarm through its MCP tools: delegate goals to Swarm or give jobs straight to its workers, follow and review them, deliver results, request media, and set up workers, schedules, workflows and environments when the person wants them. Use when the person asks for help with Swarm or work by its agents.Other plugins' skills for the same job