Cogitator
Swarms

Swarms

Multi-agent coordination for complex tasks that require collaboration between specialized agents.

What Are Swarms?

A swarm is a group of agents working together under a coordination strategy. Instead of a single agent handling everything, swarms let you decompose complex tasks across multiple specialized agents that communicate, delegate, and synthesize results.

import { Cogitator, Agent } from '@cogitator-ai/core';
import { swarm } from '@cogitator-ai/swarms';

const cogitator = new Cogitator({
  /* ... */
});

const devTeam = swarm('dev-team')
  .strategy('hierarchical')
  .supervisor(
    new Agent({
      name: 'tech-lead',
      model: 'openai/gpt-4o',
      instructions: 'You are a tech lead. Break down tasks and delegate to your team.',
    })
  )
  .workers([
    new Agent({ name: 'frontend-dev', instructions: 'Build React components.' }),
    new Agent({ name: 'backend-dev', instructions: 'Build APIs and services.' }),
  ])
  .build(cogitator);

const result = await devTeam.run({ input: 'Build a user authentication system' });
console.log(result.output);

Core Concepts

Every swarm has three fundamental pieces:

ConceptDescription
AgentsThe individual LLM agents that form the swarm. Each has a name, instructions, model, and optional tools.
StrategyThe coordination pattern that determines how agents interact: who runs when, how results flow, and how decisions are made.
CommunicationThe infrastructure for inter-agent messaging: blackboard (shared state), message bus (direct messaging), and event emitter (coordination signals).

Available Strategies

Cogitator ships with 8 built-in coordination strategies:

StrategyUse Case
hierarchicalSupervisor delegates to workers, coordinates results
round-robinRotate tasks across agents for load balancing
consensusAgents vote and reach agreement on decisions
auctionAgents bid on tasks based on capability
pipelineSequential stages with optional quality gates
debateAgents argue opposing positions, moderator synthesizes
negotiationMulti-party negotiation with offers, counteroffers, and agreements
distributedRedis-backed coordination across multiple machines

Swarms vs Workflows

Both swarms and workflows coordinate multiple steps, but they solve different problems:

SwarmsWorkflows
CoordinationAgent-to-agent, strategy-drivenDAG-based, node-to-node
CommunicationAgents message each other via blackboard and message busData flows through edges between nodes
Decision makingDynamic: agents vote, bid, debate, negotiateStatic: predefined graph with conditional branches
Best forTasks requiring collaboration, deliberation, or emergent behaviorTasks with clear sequential/parallel steps and deterministic flow

Use swarms when agents need to discuss, delegate, or reach consensus. Use workflows when the execution path is well-defined upfront.

The Swarm Lifecycle

1. Build     → Configure agents, strategy, communication
2. Assess    → (Optional) Auto-assign optimal models via SwarmAssessor
3. Execute   → Strategy coordinates agent runs
4. Observe   → Events stream progress in real-time
5. Result    → Aggregated output with per-agent results

When a swarm runs, the SwarmCoordinator manages agent execution, tracks resource usage, and handles errors according to your configuration. Each strategy implements its own execution flow on top of the coordinator.

Resource Management

Swarms support built-in resource tracking and circuit breakers:

const team = swarm('team')
  .strategy('hierarchical')
  .supervisor(supervisor)
  .workers(workers)
  .resources({
    maxConcurrency: 5,
    tokenBudget: 100_000,
    costLimit: 1.0,
    timeout: 300_000,
  })
  .errorHandling({
    onAgentFailure: 'retry',
    retry: { maxRetries: 3, backoff: 'exponential' },
    circuitBreaker: { enabled: true, threshold: 5, resetTimeout: 60_000 },
  })
  .build(cogitator);

Events

Every swarm emits structured events you can subscribe to:

const team = swarm('team')
  .strategy('pipeline')
  .pipeline({
    stages: [
      /* ... */
    ],
  })
  .build(cogitator);

team.on('agent:start', (event) => {
  console.log(`Agent ${event.agentName} started`);
});

team.on('agent:complete', (event) => {
  console.log(`Agent ${event.agentName} finished`);
});

team.on('swarm:complete', (event) => {
  console.log(`Swarm done, ${event.data.agentCount} agents ran`);
});

Use the wildcard '*' to capture all events for logging or observability.

Next Steps

On this page