The debate pattern assigns one agent to generate and another to critique — then iterates until the output meets a quality bar. This produces significantly better results for high-stakes content.
Single-agent outputs have a consistent blind spot: the agent does not know what it does not know. A critic agent catches logical errors, unsupported claims, and missing considerations that the original generator missed.
import Anthropic from '@anthropic-ai/sdk';
import * as dotenv from 'dotenv';
dotenv.config();
const client = new Anthropic();
async function callAgent(system, message) {
const response = await client.messages.create({
model: 'claude-opus-4-5',
max_tokens: 2048,
system,
messages: [{ role: 'user', content: message }]
});
return response.content[0].text;
}
const GENERATOR = 'You write thorough, well-structured analysis. Be specific and concrete.';
const CRITIC = `You are a rigorous critic. Review the work and identify:
1. Factual errors or unsupported claims
2. Missing important considerations
3. Logical weaknesses
4. Sections that are vague or generic
Be specific about each issue. Rate severity: [HIGH/MEDIUM/LOW].
End with: PASS (if quality is acceptable) or REVISE (if it needs work).`;
const REVISER = 'You improve drafts based on critique feedback. Address each HIGH and MEDIUM issue specifically.';
async function debateLoop(task, maxRounds = 3) {
let currentOutput = await callAgent(GENERATOR, task);
console.log('Initial draft generated.');
for (let round = 1; round <= maxRounds; round++) {
console.log(`
Critique round ${round}...`);
const critique = await callAgent(
CRITIC,
`Review this work:
${currentOutput}`
);
console.log('Critique summary:', critique.slice(0, 200) + '...');
// Check if critic approves
if (critique.includes('PASS')) {
console.log('Quality check passed!');
break;
}
if (round < maxRounds) {
console.log('Revising...');
currentOutput = await callAgent(
REVISER,
`Original task: ${task}
Current version:
${currentOutput}
Critique:
${critique}
Revise to address the HIGH and MEDIUM issues.`
);
}
}
return currentOutput;
}
// Example: write a persuasive argument
const result = await debateLoop(
'Write a persuasive case for why small businesses should adopt AI tools in 2026'
);
console.log('
FINAL OUTPUT:
', result);