|
| 1 | +# Swarm Tool (`swarm`) |
| 2 | + |
| 3 | +Use `swarm` to run many independent, simple tasks through ephemeral worker |
| 4 | +agents and return a structured aggregate result to the parent agent. |
| 5 | + |
| 6 | +Swarm is intended for map-reduce style work: |
| 7 | + |
| 8 | +- analyzing many files independently |
| 9 | +- processing chunks of a large data file |
| 10 | +- running independent searches where the first successful result is enough |
| 11 | +- collecting per-item summaries, counts, or validation results |
| 12 | + |
| 13 | +For a few complex role-based tasks, use the [`task`](./task.md) tool instead. |
| 14 | +For model comparison on the same task, use Agent Arena. |
| 15 | + |
| 16 | +## Arguments |
| 17 | + |
| 18 | +- `description` (string, required): Short description of the overall swarm job. |
| 19 | +- `tasks` (array, required): Independent tasks. Each task becomes one worker. |
| 20 | + - `id` (string, optional): Stable identifier returned in results. |
| 21 | + - `description` (string, required): Short per-worker description. |
| 22 | + - `prompt` (string, required): Complete instructions for the worker. |
| 23 | +- `mode` (`wait_all` or `first_success`, optional): Defaults to `wait_all`. |
| 24 | +- `max_concurrency` (number, optional): Maximum workers to run at once. |
| 25 | +- `max_turns` (number, optional): Maximum model/tool turns per worker. |
| 26 | + Defaults to `8`. |
| 27 | +- `timeout_seconds` (number, optional): Per-worker wall-clock timeout. |
| 28 | +- `worker_system_prompt` (string, optional): Shared worker system prompt. |
| 29 | +- `allowed_tools` (string array, optional): Tool allowlist for workers. |
| 30 | +- `disallowed_tools` (string array, optional): Tools removed from workers. |
| 31 | + |
| 32 | +If `max_concurrency` is omitted, Qwen Code uses |
| 33 | +`QWEN_CODE_MAX_SWARM_CONCURRENCY`, then `QWEN_CODE_MAX_TOOL_CONCURRENCY`, then |
| 34 | +`10`. |
| 35 | + |
| 36 | +## Result |
| 37 | + |
| 38 | +The tool returns JSON to the parent agent with: |
| 39 | + |
| 40 | +- `summary.total` |
| 41 | +- `summary.completed` |
| 42 | +- `summary.failed` |
| 43 | +- `summary.cancelled` |
| 44 | +- `summary.notStarted` |
| 45 | +- `results[]` with one entry per task, including `taskId`, `status`, `output` |
| 46 | + or `error`, duration, and execution stats when available |
| 47 | + |
| 48 | +Individual worker failures do not abort the whole swarm. The parent agent is |
| 49 | +responsible for reading the aggregate result and presenting the final answer. |
| 50 | + |
| 51 | +## Examples |
| 52 | + |
| 53 | +Analyze files in parallel: |
| 54 | + |
| 55 | +```text |
| 56 | +swarm( |
| 57 | + description="Extract function names", |
| 58 | + tasks=[ |
| 59 | + { |
| 60 | + id="src/a.ts", |
| 61 | + description="Analyze src/a.ts", |
| 62 | + prompt="Read /repo/src/a.ts and return the exported function names." |
| 63 | + }, |
| 64 | + { |
| 65 | + id="src/b.ts", |
| 66 | + description="Analyze src/b.ts", |
| 67 | + prompt="Read /repo/src/b.ts and return the exported function names." |
| 68 | + } |
| 69 | + ], |
| 70 | + max_concurrency=10 |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +Use first successful result: |
| 75 | + |
| 76 | +```text |
| 77 | +swarm( |
| 78 | + description="Find API route definition", |
| 79 | + mode="first_success", |
| 80 | + tasks=[ |
| 81 | + { |
| 82 | + description="Search routes directory", |
| 83 | + prompt="Search /repo/src/routes for the user creation route." |
| 84 | + }, |
| 85 | + { |
| 86 | + description="Search controllers directory", |
| 87 | + prompt="Search /repo/src/controllers for the user creation route." |
| 88 | + } |
| 89 | + ] |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | +## Notes |
| 94 | + |
| 95 | +Workers are lightweight and ephemeral: they are spawned, execute one task, |
| 96 | +return a result, and are cleaned up. Workers cannot spawn further subagents or |
| 97 | +cron jobs. |
| 98 | + |
| 99 | +Swarm workers run concurrently, so interactive permission prompts are avoided. |
| 100 | +Permission hooks can still approve actions, and permissive approval modes still |
| 101 | +apply where configured. Prefer read-only or disjoint file scopes for swarm |
| 102 | +tasks. |
0 commit comments