Debate

medium10 mindebate-swarm.ts

You want a decision weighed from two sides before a moderator concludes.

What it shows

  • Run the debate strategy with roles, rounds and a moderator
  • Read the transcript

The code

debate-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 advocate = new Agent({
  name: 'pro-open-source',
  model,
  instructions: 'You argue that AI code should be open-source. Give 2-3 focused points per round.',
  temperature: 0.7,
  maxIterations: 1,
});

const critic = new Agent({
  name: 'pro-closed-source',
  model,
  instructions: 'You argue that AI code should stay proprietary. Give 2-3 focused points per round.',
  temperature: 0.7,
  maxIterations: 1,
});

const moderator = new Agent({
  name: 'moderator',
  model,
  instructions: 'You are a neutral moderator. Synthesize both sides and give a balanced conclusion.',
  temperature: 0.4,
  maxIterations: 1,
});

const debate = new Swarm(cog, {
  name: 'ai-open-source-debate',
  strategy: 'debate',
  agents: [advocate, critic],
  agentMetadata: {
    'pro-open-source': { role: 'advocate' },
    'pro-closed-source': { role: 'critic' },
  },
  moderator,
  debate: { rounds: 2, format: 'structured' },
});

debate.on('debate:turn', (event) => {
  const { agent } = event.data as { agent: string };
  console.log(`${agent} is speaking...`);
});

const result = await debate.run({
  input: 'Should AI code be open-source? Consider safety, innovation, access and business viability.',
});

for (const message of result.debateTranscript ?? []) {
  console.log(`[round ${String(message.metadata?.round)}] ${message.from}: ${message.content.slice(0, 120)}...`);
}
console.log('\nModerator:', result.output);
console.log('Tokens:', debate.getResourceUsage().totalTokens);

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 debate-swarm.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/swarms/01-debate-swarm.ts

Learn more