$npx -y skills add AgentWorkforce/relay --skill choosing-swarm-patternsUse when coordinating multiple AI agents with Agent Relay's workflow engine and need to pick the right orchestration pattern - covers the 10 core patterns (fan-out, pipeline, hub-spoke, consensus, mesh, handoff, cascade, dag, debate, hierarchical) plus 14 specialized ones, with d
| 1 | ### Overview |
| 2 | |
| 3 | The Agent Relay workflow engine (`@relayflows/core`) supports 24 swarm patterns via a single `swarm.pattern` field. Patterns are configured declaratively in YAML or programmatically via the `workflow()` fluent builder — there are no standalone `fanOut(...)` / `hubAndSpoke(...)` helpers. Pick the simplest pattern that solves the problem; add complexity only when the system proves it's insufficient. |
| 4 | |
| 5 | ### Two ways to run a pattern |
| 6 | |
| 7 | #### **1. YAML (portable):** |
| 8 | |
| 9 | ```ts |
| 10 | import { runWorkflow } from '@relayflows/core'; |
| 11 | |
| 12 | const run = await runWorkflow('workflows/feature-dev.yaml', { |
| 13 | vars: { task: 'Add OAuth login' }, |
| 14 | }); |
| 15 | ``` |
| 16 | |
| 17 | #### **2. Fluent builder (programmatic):** |
| 18 | |
| 19 | ```ts |
| 20 | import { workflow } from '@relayflows/core'; |
| 21 | |
| 22 | const run = await workflow('feature-dev') |
| 23 | .pattern('hub-spoke') |
| 24 | .channel('swarm-feature-dev') |
| 25 | .agent('lead', { cli: 'claude', role: 'lead' }) |
| 26 | .agent('developer', { cli: 'codex', role: 'worker', interactive: false }) |
| 27 | .step('plan', { agent: 'lead', task: 'Plan {{task}}' }) |
| 28 | .step('implement', { agent: 'developer', task: 'Implement: {{steps.plan.output}}', dependsOn: ['plan'] }) |
| 29 | .run(); |
| 30 | ``` |
| 31 | |
| 32 | Both paths hit the same `WorkflowRunner`. |
| 33 | |
| 34 | ### Quick Decision Framework |
| 35 | |
| 36 | ``` |
| 37 | Is the task independent per agent? |
| 38 | YES → fan-out (parallel workers, hub collects) |
| 39 | |
| 40 | Does each step need the previous step's output? |
| 41 | YES → Is it strictly linear? |
| 42 | YES → pipeline |
| 43 | NO → dag (parallel where possible, `dependsOn` edges) |
| 44 | |
| 45 | Does a coordinator need to stay alive and adapt? |
| 46 | YES → hub-spoke (single-level hub + workers) |
| 47 | hierarchical (structurally identical in current impl; use for naming/intent) |
| 48 | |
| 49 | Is the task about making a decision? |
| 50 | YES → Do agents need to argue opposing sides? |
| 51 | YES → debate (adversarial, full mesh) |
| 52 | NO → consensus (cooperative, full mesh + coordination.consensusStrategy) |
| 53 | |
| 54 | Does the right specialist emerge during processing? |
| 55 | YES → handoff (sequential chain, one active at a time) |
| 56 | |
| 57 | Do all agents need to freely collaborate? |
| 58 | YES → mesh (full peer-to-peer edges) |
| 59 | |
| 60 | Is cost the primary concern? |
| 61 | YES → cascade (chain of increasingly capable agents; each step's prompt |
| 62 | decides whether to pass through or redo the prior output) |
| 63 | ``` |
| 64 | |
| 65 | ### Pattern Reference (Core 10) |
| 66 | |
| 67 | | # | Pattern | Topology (actual edges) | Best For | |
| 68 | | --- | ---------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | |
| 69 | | 1 | **fan-out** | Hub broadcasts to N workers; workers reply to hub only | Independent subtasks (reviews, research, tests) | |
| 70 | | 2 | **pipeline** | Linear chain (agent*i → agent*{i+1}) | Ordered stages (design → implement → test) | |
| 71 | | 3 | **hub-spoke** | Hub ↔ spokes (bidirectional); no spoke-to-spoke | Dynamic coordination, lead reviews/adjusts | |
| 72 | | 4 | **consensus** | Full mesh; decision via `coordination.consensusStrategy` | Architecture decisions, approval gates | |
| 73 | | 5 | **mesh** | Full mesh (every agent ↔ every other) | Brainstorming, collaborative debugging | |
| 74 | | 6 | **handoff** | Chain; passes control forward | Triage, specialist routing | |
| 75 | | 7 | **cascade** | Chain of `dependsOn` steps; all run on success, downstream skipped on upstream failure (no built-in "fall through") | Cost optimization: cheap first, each step's prompt passes through or redoes | |
| 76 | | 8 | **dag** | Edges from step `dependsOn` | Mixed dependencies, parallel where possible | |
| 77 | | 9 | **debate** | Full mesh (same topology as mesh; roles drive behavior) |