Consensus Voting

medium10 minconsensus-swarm.ts

A merge decision should come from several reviewers voting, with a lead breaking ties.

What it shows

  • Build a consensus swarm with the swarm() builder
  • Set threshold, rounds and what happens without consensus
  • Read every vote from result.votes

The code

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

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });
const model = 'google/gemini-3.5-flash-lite';

const reviewer = (name: string, focus: string) =>
  new Agent({
    name,
    model,
    instructions: `You review pull requests with a focus on ${focus}.
Give one short paragraph of reasoning, then end with a line "VOTE: MERGE" or "VOTE: REJECT".`,
    temperature: 0.2,
  });

const reviewBoard = swarm('review-board')
  .strategy('consensus')
  .agents([
    reviewer('security-reviewer', 'security'),
    reviewer('perf-reviewer', 'performance'),
    reviewer('quality-reviewer', 'code quality'),
  ])
  .supervisor(new Agent({ name: 'lead', model, instructions: 'Break ties with a final MERGE or REJECT decision.' }))
  .consensus({
    threshold: 0.66,
    maxRounds: 2,
    resolution: 'majority',
    onNoConsensus: 'supervisor-decides',
  })
  .build(cog);

reviewBoard.on('consensus:vote', (event) => console.log('vote', JSON.stringify(event.data)));

const result = await reviewBoard.run({
  input: `Should we merge this PR? It replaces a hand-written SQL string with a parameterized query
and adds an index on users.email. Tests pass.`,
});

console.log(result.output);
for (const [key, vote] of result.votes ?? []) {
  const { decision } = vote as { decision: string };
  console.log(`${key}: ${decision}`);
}

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/swarms
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx consensus-swarm.ts

Learn more