Time: 60 minutes | Difficulty: Advanced
Multi-agent debate systems leverage diverse perspectives and critical thinking by having multiple agents discuss, challenge, and refine ideas. This pattern produces more robust, well-reasoned outputs through collaborative argumentation.
By the end of this tutorial, you'll be able to:
- Implement multi-agent debate protocols
- Design agents with different perspectives
- Build consensus mechanisms
- Handle disagreements and conflicts
- Synthesize insights from debates
- Apply debate to decision-making
- Understand when debate improves outcomes
A debate system featuring:
- Multiple Agents - Different viewpoints and roles
- Debate Protocol - Structured argumentation rounds
- Challenger Agents - Devil's advocate and critics
- Moderator - Manages flow and synthesizes
- Consensus Builder - Finds common ground
- Decision Framework - Converts debate to action
Make sure you have:
- Completed Tutorial 11: Hierarchical Agents
- Understanding of argumentation and logic
- PHP 8.1+ installed
- Claude PHP SDK configured
Multi-agent debate involves multiple AI agents with different roles or perspectives discussing a topic to reach better conclusions.
Single Agent:
Question: Should we adopt technology X?
Agent: Yes, because... [one perspective]
Done.
Multi-Agent Debate:
Question: Should we adopt technology X?
Proponent: Yes, because benefits A, B, C...
Opponent: No, because risks X, Y, Z...
Analyst: Data shows...
Critic: Both sides overlook...
Moderator: Considering all views...
Result: Nuanced, well-reasoned decision
Different agents bring different perspectives:
$roles = [
'proponent' => [
'perspective' => 'Support the proposal',
'system' => 'You advocate for the proposal. Find benefits and opportunities.'
],
'opponent' => [
'perspective' => 'Challenge the proposal',
'system' => 'You oppose the proposal. Identify risks and drawbacks.'
],
'analyst' => [
'perspective' => 'Objective analysis',
'system' => 'You analyze facts objectively. Focus on data and evidence.'
],
'critic' => [
'perspective' => 'Critical thinking',
'system' => 'You identify logical flaws and assumptions in arguments.'
],
'moderator' => [
'perspective' => 'Synthesis and balance',
'system' => 'You synthesize viewpoints and find balanced conclusions.'
]
];Structured rounds ensure comprehensive discussion:
$protocol = [
'round_1' => [
'type' => 'opening_statements',
'agents' => ['proponent', 'opponent'],
'purpose' => 'Present initial positions'
],
'round_2' => [
'type' => 'rebuttals',
'agents' => ['opponent', 'proponent'],
'purpose' => 'Challenge opponent arguments'
],
'round_3' => [
'type' => 'analysis',
'agents' => ['analyst', 'critic'],
'purpose' => 'Objective evaluation'
],
'final' => [
'type' => 'synthesis',
'agents' => ['moderator'],
'purpose' => 'Unified conclusion'
]
];Evaluate argument strength:
$argumentMetrics = [
'logic' => 'Is reasoning sound?',
'evidence' => 'Are claims supported?',
'completeness' => 'Are counterarguments addressed?',
'relevance' => 'Does it address the question?'
];Find common ground:
function findConsensus($debateHistory) {
$agreementPoints = [];
$disagreementPoints = [];
foreach ($debateHistory as $round) {
// Identify where agents agree
$commonalities = extractCommonPoints($round);
$agreementPoints = array_merge($agreementPoints, $commonalities);
// Track persistent disagreements
$conflicts = extractConflicts($round);
$disagreementPoints = array_merge($disagreementPoints, $conflicts);
}
return [
'consensus' => $agreementPoints,
'open_issues' => $disagreementPoints
];
}class DebateSystem {
private $client;
private $agents = [];
private $history = [];
public function __construct($client) {
$this->client = $client;
}
public function addAgent($name, $role, $systemPrompt) {
$this->agents[$name] = [
'role' => $role,
'system' => $systemPrompt
];
}
public function debate($topic, $rounds = 3) {
$context = "Topic: {$topic}\n\n";
for ($round = 1; $round <= $rounds; $round++) {
echo "Round {$round}:\n";
foreach ($this->agents as $name => $config) {
$prompt = $context . "As the {$config['role']}, provide your perspective.";
$response = $this->client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'system' => $config['system'],
'messages' => [['role' => 'user', 'content' => $prompt]]
]);
$statement = extractTextContent($response);
$this->history[] = [
'round' => $round,
'agent' => $name,
'statement' => $statement
];
$context .= "\n{$name}: {$statement}\n";
echo "{$name}: {$statement}\n\n";
}
}
return $this->synthesize($topic);
}
private function synthesize($topic) {
$debateText = "";
foreach ($this->history as $entry) {
$debateText .= "Round {$entry['round']} - {$entry['agent']}:\n";
$debateText .= "{$entry['statement']}\n\n";
}
$prompt = "Topic: {$topic}\n\nDebate transcript:\n{$debateText}\n" .
"Synthesize this debate into a balanced conclusion that:\n" .
"1. Identifies key agreements\n" .
"2. Acknowledges valid concerns\n" .
"3. Provides actionable recommendation";
$response = $this->client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1536,
'system' => 'You synthesize debates into clear, balanced conclusions.',
'messages' => [['role' => 'user', 'content' => $prompt]]
]);
return extractTextContent($response);
}
}class DebateAgent {
private $client;
private $name;
private $perspective;
private $systemPrompt;
public function __construct($client, $name, $perspective, $systemPrompt) {
$this->client = $client;
$this->name = $name;
$this->perspective = $perspective;
$this->systemPrompt = $systemPrompt;
}
public function respond($topic, $context, $roundType) {
$prompt = $this->buildPrompt($topic, $context, $roundType);
$response = $this->client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'system' => $this->systemPrompt,
'messages' => [['role' => 'user', 'content' => $prompt]]
]);
return extractTextContent($response);
}
private function buildPrompt($topic, $context, $roundType) {
$prompts = [
'opening' => "Topic: {$topic}\n\nProvide your opening position.",
'rebuttal' => "Topic: {$topic}\n\nPrevious arguments:\n{$context}\n\n" .
"Rebut the opposing arguments.",
'analysis' => "Topic: {$topic}\n\nDebate so far:\n{$context}\n\n" .
"Provide objective analysis.",
'closing' => "Topic: {$topic}\n\nFull debate:\n{$context}\n\n" .
"Closing statement."
];
return $prompts[$roundType] ?? $prompts['opening'];
}
public function getName() {
return $this->name;
}
}Simple two-sided debate:
$proAgent = new DebateAgent(
$client,
'Proponent',
'support',
'You advocate for the proposal. Present benefits and opportunities.'
);
$conAgent = new DebateAgent(
$client,
'Opponent',
'oppose',
'You challenge the proposal. Identify risks and drawbacks.'
);
$moderator = new DebateAgent(
$client,
'Moderator',
'synthesize',
'You create balanced conclusions from debates.'
);Each agent responds to all others:
foreach ($agents as $speaker) {
$otherStatements = array_filter(
$statements,
fn($s) => $s['agent'] !== $speaker->getName()
);
$context = implode("\n\n", array_map(
fn($s) => "{$s['agent']}: {$s['text']}",
$otherStatements
));
$response = $speaker->respond($topic, $context, 'discussion');
$statements[] = ['agent' => $speaker->getName(), 'text' => $response];
}Questioner challenges assumptions:
$questioner = new DebateAgent(
$client,
'Questioner',
'socratic',
'You ask probing questions to reveal assumptions and test logic. ' .
'Never make statements, only ask clarifying questions.'
);
$responder = new DebateAgent(
$client,
'Responder',
'answer',
'You answer questions thoughtfully and defend your position with evidence.'
);One agent challenges everything:
$devilsAdvocate = new DebateAgent(
$client,
"Devil's Advocate",
'challenge',
'You challenge every assumption. Find flaws, identify risks, ' .
'question evidence. Be skeptical of all claims.'
);Agents vote on final decision:
function weightedVote($agents, $topic, $options) {
$votes = [];
foreach ($agents as $agent) {
$vote = $agent->vote($topic, $options);
$confidence = $agent->getConfidence();
$votes[$vote] = ($votes[$vote] ?? 0) + $confidence;
}
arsort($votes);
return array_key_first($votes);
}Debate outcome feeds back for improvement:
function iterativeDebate($topic, $agents, $iterations = 3) {
$proposal = initialProposal($topic);
for ($i = 0; $i < $iterations; $i++) {
$feedback = debate($proposal, $agents);
$issues = extractIssues($feedback);
$proposal = refine($proposal, $issues);
}
return $proposal;
}Debate continues until agreement reached:
function debateUntilConsensus($topic, $agents, $threshold = 0.8) {
$round = 1;
$maxRounds = 10;
while ($round <= $maxRounds) {
$statements = collectStatements($topic, $agents);
$agreement = measureAgreement($statements);
if ($agreement >= $threshold) {
return synthesize($statements);
}
$round++;
}
return "Consensus not reached";
}Add specialists as needed:
function adaptiveDebate($topic, $baseAgents) {
$debate = initializeDebate($topic, $baseAgents);
// Identify gaps in discussion
$gaps = analyzeGaps($debate);
// Add specialists for gaps
foreach ($gaps as $gap) {
$specialist = createSpecialist($gap);
$debate->addAgent($specialist);
}
return $debate->continue();
}Topic: Choose database technology
Agents: Performance expert, Cost analyst, Operations engineer, Developer
Result: Balanced decision considering all factors
Topic: New feature design
Agents: User advocate, Engineer, Designer, Business analyst
Result: Feature balancing UX, feasibility, and business value
Topic: Project risks
Agents: Optimist, Pessimist, Realist, Risk manager
Result: Comprehensive risk identification and mitigation
Topic: AI policy decision
Agents: Ethicist, Legal expert, Technologist, Public representative
Result: Ethically sound policy
$config = [
'min_rounds' => 2,
'max_rounds' => 5,
'consensus_threshold' => 0.75,
'max_turn_length' => 500, // words
'timeout_seconds' => 300
];Ensure varied perspectives:
$perspectives = [
'technical' => 0.3, // 30% technical focus
'business' => 0.3, // 30% business focus
'user' => 0.2, // 20% user focus
'ethical' => 0.2 // 20% ethical focus
];Track effectiveness:
$metrics = [
'rounds_to_consensus' => 3,
'unique_points_raised' => 15,
'arguments_per_agent' => [
'proponent' => 5,
'opponent' => 6,
'analyst' => 4
],
'agreement_level' => 0.85,
'decision_quality_score' => 8.5
];Before moving on, make sure you understand:
- Multi-agent debate protocols
- Different agent roles and perspectives
- Structured argumentation rounds
- Consensus building mechanisms
- Synthesis of diverse viewpoints
- When debate improves decisions
- Cost-benefit of multi-agent systems
You've mastered Multi-Agent Debate! But what about integrating external knowledge?
Learn how to augment agents with retrieval for knowledge-grounded responses!
Run the complete working example:
php tutorials/12-multi-agent-debate/debate_agent.phpThe script demonstrates:
- ✅ Pro vs Con debate structure
- ✅ Multi-round argumentation
- ✅ Four-agent round table discussion
- ✅ Moderator synthesis
- ✅ Consensus building
- ✅ Structured debate protocols
- Multiple perspectives improve decisions - Diverse views catch blind spots
- Structure prevents chaos - Clear protocols keep debates productive
- Roles create focus - Each agent contributes uniquely
- Synthesis is critical - Convert debate to actionable conclusion
- Balance cost and quality - More agents = better but more expensive
- Consensus takes time - Allow sufficient rounds
- Not always needed - Simple decisions don't require debate
- Measure effectiveness - Track if debate improves outcomes
- Improving Factuality via Multi-Agent Debate - Du et al., 2023
- Debating with More Persuasive LLMs - Khan et al., 2024
- Society of Mind - Minsky, 1986
- Tutorial 10: Reflection - Self-critique
- Tutorial 11: Hierarchical Agents - Agent coordination
- Tutorial 14: Autonomous Agents - Independent operation
Try multi-agent debate for:
- Technology Selection - Agents with different priorities (cost, performance, maintainability)
- Feature Prioritization - User advocate vs business vs engineering
- Risk Analysis - Optimist vs pessimist vs realist
- Design Review - Multiple design philosophies debate best approach
Issue: Agents agree too quickly
- Solution: Strengthen opposing viewpoints, reward finding issues
Issue: Debate never converges
- Solution: Lower consensus threshold, add moderator with tie-breaking
Issue: Repetitive arguments
- Solution: Track previous points, penalize repetition
Issue: One perspective dominates
- Solution: Balance turn lengths, require all agents to participate
Issue: High cost with marginal benefit
- Solution: Reduce rounds, use fewer agents, simpler debate structure