swarm-scripts
Swarm scripts execute bulk SDK operations and data processing tasks outside the main context window, returning only final results. Use scripts when handling 10+ items, performing heavy fetch-parse-transform work across many records, or repeating SDK calls that would consume excessive context, keeping agent reasoning focused on high-level decisions rather than data wrangling details.
git clone --depth 1 https://github.com/desplega-ai/agent-swarm /tmp/swarm-scripts && cp -r /tmp/swarm-scripts/templates/skills/swarm-scripts ~/.claude/skills/swarm-scriptsSKILL.md
# Swarm Scripts
Use swarm scripts when direct tool calls would create repetitive work, flood the context window, or require deterministic data processing across many records. Scripts run out-of-process with a typed Swarm SDK and return only the final result to your context.
## Decision Rubric
| Situation | Use |
|---|---|
| 1-10 SDK calls, result fits in context | Direct tool call |
| 10+ items or bulk fan-out SDK operations | Script |
| Heavy fetch, parse, transform, or aggregation | Script or context-mode |
| Single expensive web fetch | `ctx_fetch_and_index` |
| Multi-agent parallel work | Workflow |
| Logic needed across sessions or agents | Named script |
## Loading Script Tools
The script tools are deferred. Before authoring or running a script, load the relevant tools with ToolSearch:
```text
script-query-types
script-upsert
script-run
script-search
script-delete
```
Use `script-query-types` before non-trivial work so the script matches the live `swarm-sdk.d.ts` and stdlib signatures.
## Inline Script Pattern
Use `script-run` with inline source for one-off work:
```typescript
export default async function main(args: { status: string; limit: number }, ctx) {
const { swarm, logger } = ctx;
const result = await swarm.task_list({ status: args.status, limit: args.limit });
logger.info(`Fetched ${result.tasks.length} tasks`);
return {
total: result.tasks.length,
tasks: result.tasks.map((task) => ({
id: task.id,
status: task.status,
title: task.task.slice(0, 120),
})),
};
}
```
Keep logs useful but compact. The value returned from `main` is what comes back to your context.
## Named Script Pattern
Use `script-upsert` when the same logic is likely to be reused by another task or agent. Give the script a searchable name, a concrete description, and an intent that explains when to choose it.
Good named scripts:
- Aggregate failures by agent, schedule, or error family.
- Fetch and normalize a third-party API response.
- Fan out over many swarm tasks, memories, repos, or schedules.
- Convert noisy JSON or HTML into a compact summary.
## SDK And Context Gotchas
- `agentId` is propagated to scripts via the `X-Agent-ID` header, so SDK calls run as the invoking agent.
- `taskId` is not ambient. If a script needs to call `ctx.swarm.task_storeProgress`, pass `taskId` explicitly in `args`.
- Scripts invoked from a workflow script node may run with a workflow identity rather than a human or worker agent identity.
- Return compact structured data. Do not return raw logs, full HTML, huge JSON arrays, or large file contents.
- For a single large web fetch, prefer context-mode `ctx_fetch_and_index`; for repeated fetch/parse/aggregate work, prefer a script.
## Progress Updates From Scripts
Thread task identity explicitly:
```typescript
export default async function main(args: { taskId: string; items: string[] }, ctx) {
const { swarm } = ctx;
await swarm.task_storeProgress({
taskId: args.taskId,
progress: `Processing ${args.items.length} items with a script`,
});
return { processed: args.items.length };
}
```
Do not assume the runtime can infer the current task.Code search agent for exploring any codebase. Use for finding code by intent, locating implementations, understanding how something works, or discovering related code. Prefer over Grep/Glob/Read for any semantic or exploratory question.
Guide for running local E2E tests with API server, Docker lead/worker containers, task creation, log verification, UI dashboard, and cleanup
Close a GitHub or GitLab issue with a summary comment
Create a pull request (GitHub) or merge request (GitLab) from the current branch
Implement a GitHub issue or GitLab issue and create a PR/MR
Investigate and triage a Sentry error issue
Respond to a GitHub issue/PR or GitLab issue/MR
Review a task that has been offered to you and decide whether to accept or reject it