Cogitator
Swarms

Distributed Swarms

Scale swarms across multiple machines with Redis-backed coordination, distributed worker nodes, and shared state.

Overview

By default, swarms run in-memory on a single process. For production workloads, you can enable distributed mode to coordinate agents across multiple machines using Redis as the communication backbone.

┌─────────────────────────┐
│     Swarm Coordinator   │
│     (orchestrator node)  │
│                         │
│  Strategy ──▶ Redis     │
│             (pub/sub)   │
└──────────┬──────────────┘
           │ job queue
    ┌──────┴──────┐
    ▼             ▼
┌─────────┐  ┌─────────┐
│ Worker  │  │ Worker  │
│ Node 1  │  │ Node 2  │
│         │  │         │
│ Cogitator│  │ Cogitator│
│ + Agent │  │ + Agent │
└─────────┘  └─────────┘

The orchestrator node runs the strategy and dispatches agent jobs to a Redis queue. Worker nodes pick up jobs, execute the agent, and publish results back through Redis.

Enabling Distributed Mode

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

const model = 'ollama/llama3.2';

const team = swarm('distributed-team')
  .strategy('pipeline')
  .pipeline({
    stages: [
      {
        name: 'draft',
        agent: new Agent({ name: 'writer', model, instructions: 'Draft the text.' }),
      },
      { name: 'review', agent: new Agent({ name: 'editor', model, instructions: 'Review it.' }) },
    ],
  })
  .distributed({
    enabled: true,
    redis: {
      url: process.env.REDIS_URL, // redis://user:password@redis.example.com:6379/0, rediss:// for TLS
      keyPrefix: 'swarm',
    },
    queue: 'swarm-agent-jobs',
    timeout: 300_000,
  })
  .build(cogitator);

await team.run({ input: 'Write the release notes' });
await team.close();

Always call team.close() when done: it closes the Redis connections and schedules the swarm's Redis state to expire after cleanupAfter.

Distributed Configuration

OptionDefaultDescription
enabledfalseEnable distributed mode
redis.urlundefinedredis://user:password@host:port/db, or rediss:// for TLS. Read first, the explicit fields below win over it
redis.host'localhost'Redis server hostname
redis.port6379Redis server port
redis.usernameundefinedRedis ACL user
redis.passwordundefinedRedis authentication password
redis.db0Redis database number
redis.tlsfalseConnect over TLS, implied by a rediss:// url
redis.clusterundefined{ nodes: { host, port }[] }: connect to a Redis Cluster through these nodes
redis.keyPrefix'swarm'Key prefix for all Redis keys
queue'swarm-agent-jobs'Job queue name (the list <keyPrefix>:jobs:{<queue>})
timeout300000Timeout in ms for waiting on agent job results
retryundefined (no retries)Re-dispatch jobs that fail on a worker or time out: maxRetries (3), backoff ('exponential', or 'constant', 'linear'), initialDelay (1000 ms), maxDelay (30000 ms)
cleanupAfter3600000How long the swarm's Redis state (blackboard, messages, events, stored results) is kept after close(), in ms, 0 deletes it at once

retry acts before the swarm's errorHandling sees the failure, and an aborted run is never retried. Every attempt of a turn carries the same job id, and a timed-out attempt still waiting in the queue is taken off it before the next one is pushed, so retries never pile up. The result an earlier attempt left is deleted before the turn is dispatched again. A timed-out job a worker already took may still be running, so a retried turn can run twice, whichever answer arrives first is used. When a turn is given up (the run is aborted or times out, the swarm is closed or reset, or the turn timed out on a worker), its queued copy is removed and its job id goes into the <prefix>:{<swarmId>}:cancelled set: DistributedSwarmWorker skips such a job and aborts it within cancelCheckInterval (default 1000 ms) when it is cancelled while running, without publishing a result. workerConcurrency is deprecated and has no effect: set concurrency where the workers run, with DistributedSwarmWorker's concurrency option.

swarm('team')
  .strategy('pipeline')
  .pipeline({ stages })
  .distributed({
    enabled: true,
    redis: { host: 'localhost', port: 6379 },
    retry: { maxRetries: 2, backoff: 'exponential', initialDelay: 500 },
    cleanupAfter: 600_000,
  });

Redis Connection

distributed.redis is a RedisConnectionConfig from @cogitator-ai/types plus keyPrefix, and DistributedSwarmWorker and the worker's JobQueue take the same RedisConnectionConfig. The url is read first (redis:// or rediss://, user:password@, a /db path), then the explicit fields apply and win over it. TLS is on with a rediss:// url or tls: true. With cluster the swarm connects to a Redis Cluster through the listed nodes, and the url and fields give only the credentials and TLS (a cluster has no database):

.distributed({
  enabled: true,
  redis: {
    password: process.env.REDIS_PASSWORD, // used for every node
    tls: true,
    cluster: {
      nodes: [
        { host: 'redis-1.example.com', port: 6379 },
        { host: 'redis-2.example.com', port: 6379 },
      ],
    },
  },
});

Workers take the same redis config. Redis 6.2 or newer is required, as workers move turns with BLMOVE.

How It Works

When distributed mode is enabled, the Swarm creates a DistributedSwarmCoordinator instead of the local SwarmCoordinator. The distributed coordinator:

  1. Uses Redis-backed primitives — RedisMessageBus, RedisBlackboard, and RedisSwarmEventEmitter replace their in-memory counterparts. They are available right after construction and connect lazily on the first run.
  2. Dispatches jobs — when a strategy runs an agent, the coordinator serializes the agent (model, instructions, limits and tool schemas), the full turn context and run options, and pushes a SwarmAgentJobPayload onto the Redis list <keyPrefix>:jobs:{<queue>} (see swarmJobQueueKey()).
  3. Waits for results — the worker keeps each result under stateKeys.result (<keyPrefix>:{<swarmId>}:result:<jobId>) for the job's resultTtl (the larger of timeout and cleanupAfter), then publishes it on the <keyPrefix>:{<swarmId>}:results pub/sub channel, where it is matched by job id. The coordinator reads the stored result when a turn times out, whenever either of its connections reconnects and every few seconds (a quarter of timeout, at most 5 s), so a result published while it reconnected, or while a cluster moved its subscriber to another node, is not lost. A result the worker cannot keep under its key is still published. Turns time out after timeout ms and are cancelled when the swarm is aborted or the run times out.
  4. Applies the same policies as local swarms — retry, failover, skip, budgets, circuit breaking and events behave exactly like in-process execution.
  5. Synchronizes state — blackboard writes and messages are persisted in Redis, so all nodes see the same state.

The worker runs each turn with the whole agent: the payload carries it in the agent wire format of @cogitator-ai/core (toAgentWire), the one worker queue jobs use, so response format, reasoning effort, topP, stop sequences, onIterationLimit and timeout arrive too, and a JSON schema agent returns structured as in-process. The result comes back with the usage the worker measured (cost and duration included) and the truncated, blocked and iterationLimitReached flags, so resources.costLimit and the debate retry of a turn a reasoning model spent thinking work as they do locally. The model is the agent's own (or the swarm's resolveModel default), prefixed with the agent's own provider when it sets one, and the worker routes it with its own cogitator: a model like openrouter/deepseek/deepseek-v4-pro runs on the worker's openrouter backend. Give the worker the same llm.backends, plugins and provider keys as the process that runs the swarm. A provider it cannot route to fails the turn.

Strategy tools that call back into the coordinator (hierarchical delegation, voting, negotiation tools) only exist in-process, so the hierarchical and negotiation strategies are rejected when distributed.enabled is set (a supervisor without delegate_task would answer by itself, negotiators could not make offers). Consensus voters vote with a VOTE: line. agentTools is rejected for distributed swarms too. Use pipeline, round-robin, consensus, debate or auction for distributed swarms. Agent tools are executed on the worker from its own tool registry.

Redis Key Structure

The braces in the keys are literal: they are Redis Cluster hash tags. A swarm's state keys carry its id as the tag (<keyPrefix>:{<swarmId>}:...) and the queue keys carry the queue name (<keyPrefix>:jobs:{<queue>}...), so in a cluster each group sits in one slot. With the default keyPrefix and queue:

Key PatternTypePurpose
swarm:{<id>}:messagesListMessage bus message history
swarm:{<id>}:channel:<target>Pub/SubReal-time message delivery
swarm:{<id>}:blackboard:<section>String (JSON)Blackboard section data
swarm:{<id>}:blackboard:<section>:historyListBlackboard write history
swarm:{<id>}:blackboard:changesPub/SubBlackboard change notifications
swarm:{<id>}:eventsListEvent history
swarm:{<id>}:events:livePub/SubLive event streaming
swarm:{<id>}:resultsPub/SubAgent job results
swarm:{<id>}:result:<jobId>String (JSON)A job's result, kept for resultTtl ms
swarm:{<id>}:cancelledSetIds of the jobs the coordinator gave up on
swarm:jobs:{swarm-agent-jobs}ListJob queue for worker nodes
swarm:jobs:{swarm-agent-jobs}:processing:<wid>ListThe turns a worker moved off the queue and runs
swarm:jobs:{swarm-agent-jobs}:heartbeat:<wid>StringA worker's heartbeat, expires after 3 x heartbeatInterval
swarm:jobs:{swarm-agent-jobs}:workersSetIds of the workers that took turns from the queue

swarmQueueKeys(keyPrefix?, queue?) gives the queue keys (queue, workers, processing(workerId), heartbeat(workerId)). Earlier versions used <keyPrefix>:jobs:<queue> and <keyPrefix>:<swarmId>:... without the braces, so upgrade coordinators and workers together: a coordinator and a worker on different versions do not see the same queue.

RedisMessageBus

Drop-in replacement for InMemoryMessageBus. Messages are stored in a Redis list and delivered in real-time via pub/sub pattern subscriptions.

import { RedisMessageBus } from '@cogitator-ai/swarms';
import Redis from 'ioredis';

const bus = new RedisMessageBus(
  { enabled: true },
  { redis: new Redis(), swarmId: 'swarm_abc123', keyPrefix: 'swarm' }
);

await bus.initialize();

await bus.send({
  swarmId: 'swarm_abc123',
  from: 'agent-a',
  to: 'agent-b',
  type: 'request',
  content: 'Need your analysis',
});

await bus.syncFromRedis();

await bus.close();

The bus maintains a local cache for fast reads and subscribes to Redis pub/sub channels using pattern matching (swarm:{<id>}:channel:*) for real-time delivery. Like RedisBlackboard and RedisSwarmEventEmitter, it takes an ioredis Redis or Cluster as redis (SwarmRedis) and subscribes on a duplicate of it.

RedisBlackboard

Shared state synchronized across all nodes. Each section is stored as a JSON string in Redis, with change notifications broadcast via pub/sub.

import { RedisBlackboard } from '@cogitator-ai/swarms';
import Redis from 'ioredis';

const bb = new RedisBlackboard(
  { enabled: true, sections: { tasks: [] }, trackHistory: true },
  { redis: new Redis(), swarmId: 'swarm_abc123', keyPrefix: 'swarm' }
);

await bb.initialize();

bb.write('tasks', [{ id: 1, title: 'Implement auth' }], 'lead');

const tasks = bb.read('tasks');

bb.subscribe('tasks', (data, agentName) => {
  console.log(`Tasks updated by ${agentName}`);
});

await bb.syncFromRedis();
await bb.close();

On initialization, existing sections are loaded from Redis. The local cache provides instant reads, while writes are persisted to Redis and broadcast to all subscribers.

RedisSwarmEventEmitter

Events are persisted in a Redis list and streamed live to all connected nodes via pub/sub.

import { RedisSwarmEventEmitter } from '@cogitator-ai/swarms';
import Redis from 'ioredis';

const events = new RedisSwarmEventEmitter({
  redis: new Redis(),
  swarmId: 'swarm_abc123',
  keyPrefix: 'swarm',
  maxEvents: 1000,
});

await events.initialize();

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

await events.emitAsync('agent:start', { agentName: 'worker-a' });

const history = await events.getEventsAsync();

await events.close();

The emitter auto-trims the event list in Redis to maxEvents entries to prevent unbounded growth.

Running Worker Nodes

DistributedSwarmWorker from @cogitator-ai/worker consumes the job queue, runs each agent turn with its own Cogitator and tools, and hands the result back to the coordinator:

import { Cogitator } from '@cogitator-ai/core';
import { DistributedSwarmWorker } from '@cogitator-ai/worker';

const worker = new DistributedSwarmWorker(
  {
    redis: { url: process.env.REDIS_URL }, // the same Redis as distributed.redis
    keyPrefix: 'swarm', // must match distributed.redis.keyPrefix
    queue: 'swarm-agent-jobs', // must match distributed.queue
    concurrency: 4,
    cogitator: new Cogitator({ llm: { defaultModel: 'ollama/llama3.2' } }),
    tools: [searchTool, calculatorTool],
  },
  {
    onJobFailed: (job, error) => console.error(job.agentName, error.message),
    onJobsRecovered: (workerId, count) => console.warn(`${count} turns of ${workerId} requeued`),
  }
);

await worker.start();
process.on('SIGTERM', () => void worker.stop());

Agents reference tools by name, and a turn that needs a tool missing from the worker's tools fails with a clear error instead of running with a fake implementation.

Worker Failures

A worker takes a turn with BLMOVE, which moves it from the queue to the worker's own list <keyPrefix>:jobs:{<queue>}:processing:<workerId> in one step (Redis 6.2 or newer), so a turn is never only in the memory of a worker. The turn leaves that list once it is done. Every heartbeatInterval ms (default 5000) the worker sets its heartbeat key <keyPrefix>:jobs:{<queue>}:heartbeat:<workerId>, which expires after three intervals, and stays in the set <keyPrefix>:jobs:{<queue>}:workers. On start() and on every beat, a worker looks for workers in the set whose heartbeat is gone, moves their turns back to the head of the queue, drops them from the set and reports onJobsRecovered(workerId, count). A worker that crashed or lost its connection therefore loses no turns, another one runs them. A worker also checks its own list on every beat: a turn on it that the worker does not run at two beats in a row, such as one a BLMOVE moved while a dropped connection lost its reply, goes back to the head of the queue and is reported with the worker's own id.

A worker silent for three intervals counts as gone even when it is only stalled, so a turn that runs past that on a stalled worker can run twice. Before it runs a turn, a worker checks the job's stored result and skips a job that has one, for example a turn a dead worker answered before it took the turn off its list. Whichever answer reaches the coordinator first is used.

stop() stops taking new jobs, waits for in-flight turns, hands whatever is still on its list back to the queue, leaves the set and deletes its heartbeat. getWorkerId() gives the worker's id in the set, new on every start().

Scaling Considerations

Horizontal scaling — spin up multiple worker nodes consuming from the same job queue. Redis BLMOVE hands each job to one worker, and a turn of a worker that died goes back to the queue (see Worker Failures). concurrency controls parallel turns per node.

State consistency — the blackboard and message bus use Redis as the source of truth. Local caches provide fast reads; writes are immediately persisted and broadcast.

Connection management — a distributed swarm uses a command connection plus subscriber connections for messages, blackboard, events and results. Call close() when done to release them.

Timeouts — configure the timeout option based on your expected agent execution time. Jobs that exceed the timeout will cause the coordinator to throw an error.

Key cleanup — close() sets every key of the swarm to expire after cleanupAfter ms (default one hour), and a stored result expires after its resultTtl on its own. The queue keys belong to the queue and its workers, not to a swarm.

On this page