Skip to main content

Swarm Multi-Agent Workflow

This tutorial shows how to use Shannon’s SwarmWorkflow to deploy persistent, collaborating agents coordinated by an LLM-powered Lead Agent. Agents work in parallel with inter-agent messaging, a shared workspace, and dynamic task reassignment.

What You’ll Learn

  • How to submit a swarm task via API and Python SDK
  • How the Lead Agent coordinates agents through events
  • How to monitor agent progress with SSE streaming
  • How to configure swarm parameters and budget controls
  • Real-world use cases and best practices

Prerequisites

  • Shannon stack running (Docker Compose)
  • Gateway reachable at http://localhost:8080
  • Swarm enabled in config/features.yaml (enabled by default)
  • Auth defaults:
    • Docker Compose: authentication is disabled by default (GATEWAY_SKIP_AUTH=1).
    • Local builds: authentication is enabled by default. Set GATEWAY_SKIP_AUTH=1 to disable auth, or include an API key header -H "X-API-Key: $API_KEY".

Quick Start

Submit + Stream in One Call

For frontend applications, use the combined submit-and-stream endpoint:
Response:
Then connect to the stream URL for real-time events:

Python SDK

Basic Usage

With Streaming

With Custom Context

How Agents Collaborate

Lead Agent Coordination

The Lead Agent acts as an event-driven coordinator. It does not execute tasks itself, but rather plans, assigns, and reassigns work based on events:
  • When an agent becomes idle, the Lead checks for pending tasks with met dependencies and assigns the next one
  • When an agent completes, the Lead evaluates whether to reassign it, shut it down, or revise the plan
  • On periodic checkpoints (every 120s), the Lead reviews overall progress and can adjust the plan
  • The Lead skips unnecessary LLM calls when there are no idle agents and no actionable pending tasks

Team Roster

Each agent receives a team roster showing all agents and their assignments. This enables agents to know who to contact for specific information:

Publishing Findings

Agents share discoveries via the shared workspace. These appear in every agent’s prompt context:

Sending Direct Messages

Agents can send direct messages to specific teammates:

Requesting Help

When an agent needs additional support, it requests help from the Lead Agent:
The Lead Agent evaluates the request and may spawn a new agent, reassign an existing idle agent, or add the subtask to the pending task queue.

Configuration

features.yaml

Configuration Parameters

Real-World Use Cases

Collaborative Coding

Agents review, implement, and test code collaboratively with sandboxed execution.

Financial Analysis

Bull/bear analysts, sentiment agents, and a portfolio manager synthesize investment insights.

Data Processing

Parallel data pipelines with sandboxed Python execution, JSON querying, and statistical analysis.

Competitive Intelligence

Monitor competitor websites, pricing, and social media with automatic cross-sharing of discoveries.

Example: Collaborative Code Review

The Lead Agent creates tasks for each concern (security audit, code quality, test coverage), assigns agents with the developer role, and creates a final synthesis task that depends on all reviews completing.

Example: Multi-Site Price Monitoring

Understanding the Response Metadata

The swarm workflow returns metadata with per-model execution breakdown and token usage:

Tips and Best Practices

  • Set context.force_swarm: true to route to SwarmWorkflow
  • Start with default configuration and adjust based on results
  • Monitor SSE events to understand Lead Agent decisions and agent behavior
  • Use sessions (session_id) for multi-turn swarm conversations
  • Watch for LEAD_DECISION events to understand coordination logic

Troubleshooting

Common Issues:
  • Swarm not triggering: Ensure force_swarm: true is in the context object and swarm is enabled in features.yaml
  • Agents timing out: Increase agent_timeout_seconds for complex tasks (default is 1800s / 30 minutes)
  • Too many agents: Reduce the number of subtasks by simplifying your query, or lower max_agents
  • High token usage: Lower max_iterations_per_agent, use model_tier: "small", or reduce max_total_tokens
  • Agents stuck in loops: Convergence detection (3 consecutive non-tool iterations) catches this automatically
  • Budget exceeded: Check max_total_llm_calls and max_total_tokens settings; the Lead will attempt graceful shutdown when budget is tight
  • Redundant searches: Knowledge deduplication should catch this; if persisting, check that agents have access to the shared workspace

Fallback Behavior

If the swarm workflow fails (planning error, all agents fail, etc.), Shannon automatically falls back to standard DAG/Supervisor workflow routing. The force_swarm flag is removed from context to prevent recursive failures.

Next Steps

Swarm Concepts

Understand swarm architecture in depth

Deep Research

Multi-stage research with citations

API Reference

Full API documentation