AI Engineering: Agents and Applications βΊ AI Agents βΊ Day 293
Day 293: Multi-Agent Systems
Master multi-agent coordination architectures: design Hierarchical Supervisors, Sequential Pipelines, and Multi-Agent Debate protocols, implement structured message passing, and prevent infinite chatter loops.
Hands-on lab for this lesson
Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/ai-engineering/day-293-multi-agent-systems
- Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git cd ai-roadmap-365.github.io - Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path β section / subsection / week / day:
cd labs/sections/ai-engineering/day-293-multi-agent-systems - Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work β read it before running anything.
- Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
bash tests/run_tests.sh # or the test command named in the lab README
You can also open the lab as a local page (works offline, shows the file tree and expected output).
Learning objectives
By the end of this lesson you will be able to:
- Architect multi-agent coordination topologies: Hierarchical Supervisor, Sequential Pipeline, and Debate.
- Implement a structured asynchronous Message Bus for agent-to-agent communication.
- Mitigate multi-agent failure modes: infinite chatter loops, groupthink, and delegation ping-pong.
- Enforce role-based privilege isolation and tool separation across specialized agents.
- Benchmark multi-agent system cost, latency, and accuracy trade-offs against single-agent baselines.
Prerequisites
Why this matters
While a single autonomous agent equipped with a comprehensive toolset can solve many multi-step objectives, single-agent architectures encounter severe cognitive degradation as task complexity expands. When a single agent is tasked with researching competitive market data, writing complex backend SQL, auditing security compliance, and drafting executive slide summaries, its prompt scratchpad becomes overwhelmed with conflicting instructions, bloated schemas, and thousands of lines of intermediate tool observations.
A Multi-Agent System (MAS) resolves this bottleneck through the classical engineering principle of separation of concerns. Instead of forcing a single generalist model to juggle 50 tools, the system decomposes the problem across a team of specialized subagentsβsuch as a Researcher, a SoftwareEngineer, a SecurityAuditor, and a Supervisor. Each agent operates with a focused system prompt, a minimal curated toolset, and an isolated context window.
However, coordinating multiple autonomous LLMs introduces distributed systems complexity: managing message routing, preventing infinite chatter loops, establishing consensus protocols, and controlling exponential token cost expansion. Mastering multi-agent topologies is essential for architecting enterprise-scale autonomous software engineering, deep research, and automated auditing teams.
The idea in plain language
Think of a Multi-Agent System like a professional architectural firm designing a skyscraper:
- The Single-Agent Anti-Pattern (The Overwhelmed Solo Architect): One person attempts to calculate structural beam loads, design interior plumbing, negotiate city zoning permits, draw 3D artistic renderings, and install electrical junction boxes. They make critical mistakes because their brain is overloaded with contradictory details.
- The Multi-Agent System (The Specialized Firm):
- Lead Partner (The Supervisor): Ingests the clientβs vision, defines the budget, and sets milestones.
- Structural Engineer Agent: Uses specialized physics calculation tools to verify beam integrity.
- Zoning & Legal Agent: Searches municipal legal databases for building height limits.
- Interior Design Agent: Drafts floor plans and lighting layouts.
- Peer Review & Audit (Debate): The structural engineer and interior designer debate column placement until an optimal compromise is reached, which the Lead Partner approves.
By dividing labor among specialists with clear interfaces, the team delivers flawless blueprints that no individual could produce alone.
Historical background
The field of Distributed Artificial Intelligence and Multi-Agent Systems dates back to the 1980s:
1. Classical Multi-Agent Systems (1980sβ2000s)
In classical AI, researchers like Michael Wooldridge and Carl Hewitt developed the Actor Model and BDI (Belief-Desire-Intention) architectures. Agents communicated via formal protocols (such as FIPA-ACL and KQML) using deterministic symbolic logic. However, these systems were rigid and could not handle ambiguous natural language.
2. The Multi-Agent LLM Debate Breakthrough (2023)
In May 2023, Yilun Du et al. published Improving Factuality and Reasoning in Language Models through Multiagent Debate. The paper proved that when multiple instances of a language model present competing arguments and critique each other across multiple rounds, mathematical and factual reasoning accuracy improved dramatically over single-agent Chain-of-Thought.
3. Open-Source Multi-Agent Frameworks (2023βPresent)
Frameworks like Microsoft AutoGen and CrewAI demonstrated the power of conversational agent collaboration. In 2024, research by Li et al. (More Agents Is All You Need) formalized the scaling laws of multi-agent ensembles, showing that aggregating outputs from multiple specialized small models frequently outperformed a single massive frontier model on complex benchmarks.
What it is β and what it is not
Let us define the operational boundaries of Multi-Agent Systems:
What a Multi-Agent System Is:
- A network of discrete, specialized language model instances that communicate via structured message passing or shared state memory.
- An orchestrated topology (Supervisor, Pipeline, or Debate) with explicit termination conditions.
- A division of labor where each subagent maintains an independent prompt, role definition, and restricted tool inventory.
What a Multi-Agent System Is Not:
- An uncontrolled group chat where models exchange endless informal pleasantries without solving the task.
- A silver bullet for simple tasks (a single agent is faster, cheaper, and more reliable for straightforward queries).
- A monolithic system sharing one giant context window.
+-------------------------------------------------------------------------------+
| MULTI-AGENT TOPOLOGY TRADEOFFS |
+---------------------+-----------------------+---------------------------------+
| Topology | Latency & Token Cost | Best Suited Use Case |
+---------------------+-----------------------+---------------------------------+
| Hierarchical Lead | Moderate (Linear) | Open-ended tasks with sub-goals |
| Sequential Pipeline | Low (Predictable) | Linear data processing / CI/CD |
| Peer-to-Peer Chat | High (Exponential) | Creative brainstorming |
| Multi-Agent Debate | High (N-rounds x M) | High-stakes fact checking & eval|
+---------------------+-----------------------+---------------------------------+
Why it was created and what problems it solves
Multi-agent architectures resolve four fundamental limitations of single-agent systems:
1. Context Window Contamination & Tool Confusion
When an agent is given 40 tools, its tool-selection accuracy degrades due to semantic overlap. In a multi-agent system, the Researcher agent only sees 3 search tools, while the Database agent only sees 2 SQL tools, guaranteeing 100% tool-selection precision.
2. Overcoming Single-Model Blind Spots (Adversarial Robustness)
A single model evaluating its own generated text tends to confirm its initial biases. Pitting an independent Critic agent with a skeptical prompt against the Generator agent catches logical fallacies and subtle bugs before deployment.
3. Role-Based Prompt Specialization
A system prompt instructs a model to adopt a specific perspective (e.g. βYou are a paranoid security auditor reviewing code for OWASP Top 10 vulnerabilitiesβ). Specialized prompts produce vastly superior domain-specific critique compared to generic instructions.
4. Parallel Task Execution
Independent sub-tasks (e.g. researching three different competitor products simultaneously) can be dispatched concurrently across three worker agents, reducing total wall-clock latency.
How it works
Let us dissect the Hierarchical Supervisor and Message Bus architecture:
[User Goal / Task Request]
β
βΌ
[Supervisor / Lead Agent]
β
βββββββββββββββββΌββββββββββββββββ
βΌ βΌ βΌ
[Task 1: Search] [Task 2: Code] [Task 3: Audit]
β β β
βΌ βΌ βΌ
[Researcher] [Coder] [Critic]
- Tool: search - Tool: sandbox - Tool: linter
β β β
βββββββββββββββββΌββββββββββββββββ
β
βΌ
[Structured Message Bus]
β
βΌ
[Supervisor Review & Synthesis]
β
βΌ
[Final Answer Output]
1. The Standard Message Envelope
Agents communicate using standardized JSON envelopes:
from dataclasses import dataclass, field
import time


@dataclass
class AgentMessage:
sender: str
recipient: str # Specific agent name or "BROADCAST"
content: str
message_type: str # "TASK_DISPATCH", "TASK_RESULT", "CRITIQUE", "FINAL"
timestamp: float = field(default_factory=time.time)
In addition to hierarchical supervisor delegation, multi-agent systems employ Consensus Protocols and Majority Voting Ensembles. When evaluating critical security patches, architectural designs, or complex mathematical proofs, the system spawns three independent model instances running diverse system prompts (e.g., an optimist proposer, a conservative security auditor, and an efficiency architect). A dedicated Consensus Arbiter compiles their arguments, measures semantic agreement across independent reviews, and accepts the proposal only when a strict supermajority consensus ($\ge 67%$) is achieved.
2. Preventing Infinite Chatter Loops
To prevent agents from getting stuck in polite conversational loops (βThank you! Youβre welcome! Glad to help!β), the message bus tracks round counts ($R \le 4$) and drops non-actionable conversational filler.
An everyday analogy
Think of a Multi-Agent System like a hospital emergency surgical team:
- The Lead Surgeon (Supervisor) makes the incision and dictates overall strategy.
- The Anesthesiologist continuously monitors vital signs and adjusts medication.
- The Scrub Nurse manages surgical instruments, handing the exact scalpel or clamp required.
- The Radiologist interprets MRI scans and reports bone fracture coordinates.
No single person performs all roles; the lives of patients depend on strict role division and clear, concise verbal protocols.
Deep Dive: Scalable Pub/Sub Channels & Deadlock Mitigation
When architecting multi-agent systems with dozens of cooperating agents, naive peer-to-peer broadcast communication collapses due to quadratic message complexity ($O(N^2)$). If 10 agents broadcast every observation to every other agent, a single 5-turn task generates hundreds of redundant message exchanges, saturating context windows and exploding token billing.
To maintain scalability, enterprise multi-agent topologies implement Topic-Based Publish/Subscribe Message Channels (Pub/Sub) paired with Semantic Filtering Gateways.
+-------------------------------------------------------------------------------+
| MULTI-AGENT PUB/SUB CHANNEL SCHEMA |
+-------------------+-----------------------+--------------------+--------------+
| Channel Name | Authorized Publishers | Subscribed Agents | Message Scope|
+-------------------+-----------------------+--------------------+--------------+
| /system/tasks | Lead Supervisor | All Workers | Task Tickets |
| /data/research | Researcher Agents | Coders, Analysts | Raw Citations|
| /code/review | Python Coder | Security Critic | Code Diffs |
| /audit/verdicts | Security Critic | Lead Supervisor | Pass / Fail |
+-------------------+-----------------------+--------------------+--------------+
Furthermore, multi-agent systems deploy Deadlock Detection Algorithms. If Agent A is waiting for Agent B to complete a database query while Agent B is simultaneously waiting for Agent A to provide schema definitions, the runtime detects the cyclic dependency graph and injects an automated supervisor intervention to break the deadlock.
Examples in practice
Let us inspect a Python implementation of a Hierarchical Multi-Agent System featuring a Supervisor, Researcher, and Critic communicating via an in-memory Message Bus:
from typing import Dict, Any, List, Optional
import json
class AgentMessage:
def __init__(self, sender: str, recipient: str, content: str, msg_type: str):
self.sender = sender
self.recipient = recipient
self.content = content
self.msg_type = msg_type
class MessageBus:
def __init__(self):
self.history: List[AgentMessage] = []
def send(self, msg: AgentMessage):
self.history.append(msg)
def get_messages_for(self, recipient: str) -> List[AgentMessage]:
return [m for m in self.history if m.recipient in (recipient, "BROADCAST")]
class MultiAgentSupervisorSystem:
def __init__(self, max_rounds: int = 5):
self.bus = MessageBus()
self.max_rounds = max_rounds
self.agents = {
"supervisor": self._supervisor_turn,
"researcher": self._researcher_turn,
"critic": self._critic_turn
}
def _researcher_turn(self, task: str) -> str:
if "revenue" in task.lower():
return "Q3 Revenue reached $12.4 Billion (+14% YoY)."
return "Researched records: data verified."
def _critic_turn(self, draft: str) -> str:
if "12.4 Billion" in draft:
return "PASS: Facts are verified and grounded."
return "REVISE: Missing specific revenue numbers."
def _supervisor_turn(self, goal: str, round_num: int) -> Dict[str, Any]:
if round_num == 1:
# Delegate to researcher
self.bus.send(AgentMessage("supervisor", "researcher", f"Find financial data for: {goal}", "TASK_DISPATCH"))
return {"action": "DELEGATE", "target": "researcher"}
elif round_num == 2:
# Gather researcher output and send to critic
res_msgs = self.bus.get_messages_for("supervisor")
latest_res = res_msgs[-1].content if res_msgs else ""
self.bus.send(AgentMessage("supervisor", "critic", latest_res, "AUDIT_REQUEST"))
return {"action": "DELEGATE", "target": "critic"}
elif round_num == 3:
# Evaluate critique and emit final answer
critic_msgs = [m for m in self.bus.history if m.sender == "critic"]
critique = critic_msgs[-1].content if critic_msgs else ""
if "PASS" in critique:
final_text = "Executive Summary: Q3 Revenue reached $12.4 Billion (+14% YoY) with 100% verified compliance."
self.bus.send(AgentMessage("supervisor", "USER", final_text, "FINAL_ANSWER"))
return {"action": "COMPLETE", "final_answer": final_text}
else:
return {"action": "RETRY", "reason": critique}
return {"action": "COMPLETE", "final_answer": "Task finalized."}
def run(self, goal: str) -> Dict[str, Any]:
for r in range(1, self.max_rounds + 1):
decision = self._supervisor_turn(goal, r)
if decision["action"] == "DELEGATE":
target = decision["target"]
if target == "researcher":
msgs = self.bus.get_messages_for("researcher")
res = self._researcher_turn(msgs[-1].content)
self.bus.send(AgentMessage("researcher", "supervisor", res, "TASK_RESULT"))
elif target == "critic":
msgs = [m for m in self.bus.history if m.recipient == "critic"]
crit = self._critic_turn(msgs[-1].content)
self.bus.send(AgentMessage("critic", "supervisor", crit, "AUDIT_RESULT"))
elif decision["action"] == "COMPLETE":
return {
"status": "SUCCESS",
"rounds": r,
"final_answer": decision["final_answer"],
"message_count": len(self.bus.history)
}
return {"status": "MAX_ROUNDS_EXCEEDED", "rounds": self.max_rounds}
Implications: security, privacy, performance, scalability, and cost
1. Multi-Agent Token Cost Explosion
In a multi-agent debate with 3 agents running 4 rounds of critique, each agent processes all previous messages. The token consumption scales super-linearly:
\text(Tokens) = O(N_(\text(agents))^2 \times R_(\text(rounds)) \times \text(Context Length))
Production systems must enforce hard message truncation, summarization, and per-round token limits.
2. Privilege Isolation
Subagents should never inherit universal permissions. The Researcher agent has read-only web access but zero database write access. The Database agent has database access but zero internet access, preventing prompt injection exfiltration attacks.
Alternatives: free, open source, and commercial
| Framework / Pattern | Topology Type | Overhead | Best Suited For |
|---|---|---|---|
| Hierarchical Supervisor | Centralized Hub & Spoke | Low | Enterprise pipelines, coding assistants |
| AutoGen ConversableAgent | Conversational Group | High | Complex open-ended brainstorming |
| CrewAI Crews | Sequential / Hierarchical | Moderate | Role-based content workflows |
| Multi-Agent Debate | Peer-to-Peer Dialectic | High | Verification of high-stakes claims |
Comparison with related concepts
+-----------------------+-----------------------+-----------------------+
| Feature | Single-Agent ReAct | Multi-Agent System |
+-----------------------+-----------------------+-----------------------+
| Tool Allocation | All tools in 1 prompt | Specialized per agent |
| Context Management | Single scratchpad | Isolated per agent |
| Error Recovery | Self-Correction only | Independent Critic |
| Token Efficiency | High | Lower (Higher quality)|
| Architecture | Simple Loop | Distributed Topology |
+-----------------------+-----------------------+-----------------------+
When to use it β and when not to
Choose Multi-Agent Systems when:
- Tasks require specialized, mutually exclusive skills (e.g. deep research + code execution + security auditing).
- High-stakes factual accuracy requires independent adversarial review (Debate/Critic pattern).
- You want to parallelize independent sub-tasks across concurrent models.
Do NOT use Multi-Agent Systems when:
- The task can be solved by a single agent with 2β3 tools (multi-agent adds unnecessary latency and cost).
- You need sub-second response times.
The AI connection
-
Tool-selection accuracy degrades with tool count, which is the concrete reason to split an agent rather than an organisational metaphor.
-
A model reviewing its own output confirms its own biases. An independent critic with a skeptical prompt is the cheapest robustness gain available.
-
Debate measurably improves factual accuracy β the 2023 result that made multi-agent systems more than a staffing analogy.
-
Token cost multiplies with agents and rounds. Naive broadcast is quadratic, which is why real topologies use a supervisor or a pub/sub channel.
-
Isolation is a security boundary too. A researcher agent with three search tools cannot be persuaded into a database write it has no tool for.
Knowledge check
- Why does separating tools across specialized agents improve tool selection accuracy? Because it eliminates semantic overlap between tools, keeping individual system prompts clean and focused.
- What failure mode does the Multi-Agent Debate pattern mitigate? Single-model confirmation bias and ungrounded hallucinations.
- How does a Message Bus coordinate agents? By routing typed message envelopes between senders and recipients, tracking conversation history, and enforcing communication bounds.
Hands-on exercise
In this lab, you will implement an Autonomous Multi-Agent Supervisor System with an In-Memory Message Bus in Python. Your implementation will:
- Construct a structured
MessageBussupporting typed agent-to-agent communication. - Build specialized subagents:
LeadSupervisor,WebResearcher,PythonCoder, andSecurityCritic. - Implement dynamic task dispatch, output validation, and final answer synthesis.
- Verify multi-agent coordination across unit tests.
Expected output
============================= test session starts ==============================
collected 5 items
tests/test_multi_agent_system.py ..... [100%]
============================== 5 passed in 0.04s ===============================
[MULTI-AGENT SYSTEM] Goal: "Analyze Q3 financial revenue and audit numbers"
[ROUND 1] SUPERVISOR -> RESEARCHER: "Find Q3 revenue"
[ROUND 2] RESEARCHER -> SUPERVISOR: "$12.4 Billion (+14% YoY)"
[ROUND 3] SUPERVISOR -> CRITIC: "Audit financial draft"
[ROUND 4] CRITIC -> SUPERVISOR: "PASS: Grounded and verified."
[FINAL ANSWER] "Executive Summary: Q3 Revenue reached $12.4 Billion with verified compliance."
Validate your work
Run the pytest test suite:
pytest tests/run_tests.sh
Troubleshooting
- Message routing KeyError: Verify recipient names match registered agent identifiers.
- Infinite loop in supervisor: Ensure completion triggers when the critic emits
"PASS".
Common mistakes
- Broadcasting all messages to all agents, causing context pollution and token inflation.
- Failing to include a round limit in the execution loop.
Practice assignment
Add a Sequential Pipeline Mode where Agent A automatically feeds its output directly to Agent B without routing back through the supervisor.
Extension challenge
Implement an Async Consensus Voting Protocol where three independent reviewer agents vote on a proposed artifact, accepting it only when a majority ($\ge 2/3$) approves.
Advanced Multi-Agent Topology Topologies and Routing
When architecting large-scale agent ecosystems, specialized routing subagents dynamically inspect task complexity and select between Hierarchical Supervisor delegation, Sequential Pipeline execution, and Multi-Agent Debate. For low-ambiguity deterministic tasks, the router selects the sequential pipeline to minimize token expenditure and latency. For high-stakes analytical tasks containing subjective or regulatory nuances, the router triggers a multi-round debate protocol with an impartial judge. This dynamic topology selection ensures optimal balancing of latency, compute cost, and decision accuracy.
Quiz
Q1. What is the primary operational risk when deploying an unsupervised Peer-to-Peer multi-agent chat?
- The agents consume zero tokens
- The server runs out of disk space
- The Python interpreter crashes on the first import
- They can loop forever, agreeing with each other
Show answer
Answer: D. They can loop forever, agreeing with each other
Without a supervisor or explicit termination predicate, peer-to-peer agents frequently engage in repetitive conversational loops.
Q2. In a Hierarchical Supervisor multi-agent topology, what is the role of the lead supervisor agent?
- To decompose, delegate, and synthesize results
- To execute low-level shell scripts
- To write unit tests for the human
- To delete the database
Show answer
Answer: A. To decompose, delegate, and synthesize results
The supervisor serves as the strategic orchestrator, routing tasks to domain specialists and aggregating their returns.
Q3. Why does a Multi-Agent Debate pattern improve reasoning quality on complex analytical questions?
- It reduces token spend by 90%
- Opposing agents expose each other's weak claims
- It eliminates the need for tool calling
- It compresses prompt text
Show answer
Answer: B. Opposing agents expose each other's weak claims
Debate exposes confirmation bias and ungrounded assumptions by pitting competing models against each other.
Q4. How does role specialization in multi-agent systems improve tool execution accuracy?
- It gives all agents 100 tools simultaneously
- It removes all JSON parsing
- Each agent gets a minimal, focused toolset
- It speeds up network I/O
Show answer
Answer: C. Each agent gets a minimal, focused toolset
Curating small, role-specific tool inventories prevents tool-selection ambiguity and context pollution.
Q5. What communication protocol prevents quadratic message explosion in large multi-agent networks?
- Broadcasting every message to every agent
- Using XML instead of JSON
- Disabling all logging
- A pub/sub bus with hard round bounds
Show answer
Answer: D. A pub/sub bus with hard round bounds
Pub/Sub channels and bounded message routing ensure messages are only delivered to relevant subscriber agents.
Glossary
- Multi-Agent System (MAS)
- A distributed system comprising multiple autonomous AI agents that communicate and cooperate to solve complex objectives.
- Hierarchical Supervisor
- An orchestration topology where a central lead agent delegates sub-tasks to specialized subagents and reviews deliverables.
- Message Bus
- A structured communication channel enabling agents to publish, subscribe, and route messages using standardized envelopes.
- Multi-Agent Debate
- A verification topology where multiple agents argue competing hypotheses before an impartial judge synthesizes the final conclusion.
- Groupthink Drift
- A failure mode in peer-to-peer multi-agent systems where agents reinforce and amplify each other's hallucinations.
- Role Specialization
- Restricting an individual agent's system prompt and tool inventory to a single domain of expertise.
- Delegation Ping-Pong
- A failure mode where two agents repeatedly delegate a task back and forth without executing real work.
- Consensus Arbiter
- An impartial agent that aggregates votes and arguments across an ensemble to determine the final output.
Sources and further reading
- Improving Factuality and Reasoning in Language Models through Multiagent Debate (Du et al., 2023) β arXiv (accessed 2026-08-30)
- AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation (Wu et al., 2023) β arXiv (accessed 2026-08-30)
- More Agents Is All You Need (Li et al., 2024) β arXiv (accessed 2026-08-30)
Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.