AI Engineering: Agents and Applications β€Ί AI Agents β€Ί Day 293

Day 293: Multi-Agent Systems

Day 293 of 365 β€” 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.

Course
AI Engineering: Agents and Applications
Category
AI Agents
Reading time
β‰ˆ 35 min
Practical time
β‰ˆ 50 min
Lesson duration
1h 25m
Last verified
2026-08-30

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

  1. 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
  2. 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
  3. 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.
  4. 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:

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.

Multi-Agent Topologies Router vs Debate diagram illustrating Hierarchical Supervisor, Sequential Pipeline, and Multi-Agent Debate patterns

The idea in plain language

Diagram: a single agent with every tool compared with specialised agents

Diagram: the supervisor, pipeline and debate multi-agent topologies

Think of a Multi-Agent System like a professional architectural firm designing a skyscraper:

By dividing labor among specialists with clear interfaces, the team delivers flawless blueprints that no individual could produce alone.

Historical background

Diagram: the progression from classical multi-agent systems to LLM debate

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:

What a Multi-Agent System Is Not:

Diagram: a structured multi-agent system compared with three misconceptions

+-------------------------------------------------------------------------------+
|                       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

![Diagram: how a consensus arbiter combines independent agent reviews](./assets/consensus.svg)

![Diagram: the fields of a structured inter-agent message envelope](./assets/message-envelope.svg)

@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)

Hierarchical Supervisor Subagent Delegation diagram showing task dispatch from Lead Supervisor to Specialized Research, Code, and Review agents

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

Diagram: a surgical team as an analogy for role division in a multi-agent system

Think of a Multi-Agent System like a hospital emergency surgical team:

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

Diagram: broadcast message complexity compared with topic-based publish and subscribe

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

Diagram: the cost and isolation implications of multi-agent systems

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 / PatternTopology TypeOverheadBest Suited For
Hierarchical SupervisorCentralized Hub & SpokeLowEnterprise pipelines, coding assistants
AutoGen ConversableAgentConversational GroupHighComplex open-ended brainstorming
CrewAI CrewsSequential / HierarchicalModerateRole-based content workflows
Multi-Agent DebatePeer-to-Peer DialecticHighVerification of high-stakes claims
+-----------------------+-----------------------+-----------------------+
| 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

Diagram: when a multi-agent system is warranted

Choose Multi-Agent Systems when:

Do NOT use Multi-Agent Systems when:

The AI connection

Knowledge check

  1. 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.
  2. What failure mode does the Multi-Agent Debate pattern mitigate? Single-model confirmation bias and ungrounded hallucinations.
  3. 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:

  1. Construct a structured MessageBus supporting typed agent-to-agent communication.
  2. Build specialized subagents: LeadSupervisor, WebResearcher, PythonCoder, and SecurityCritic.
  3. Implement dynamic task dispatch, output validation, and final answer synthesis.
  4. 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

  1. Message routing KeyError: Verify recipient names match registered agent identifiers.
  2. Infinite loop in supervisor: Ensure completion triggers when the critic emits "PASS".

Common mistakes

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?

  1. The agents consume zero tokens
  2. The server runs out of disk space
  3. The Python interpreter crashes on the first import
  4. 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?

  1. To decompose, delegate, and synthesize results
  2. To execute low-level shell scripts
  3. To write unit tests for the human
  4. 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?

  1. It reduces token spend by 90%
  2. Opposing agents expose each other's weak claims
  3. It eliminates the need for tool calling
  4. 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?

  1. It gives all agents 100 tools simultaneously
  2. It removes all JSON parsing
  3. Each agent gets a minimal, focused toolset
  4. 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?

  1. Broadcasting every message to every agent
  2. Using XML instead of JSON
  3. Disabling all logging
  4. 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


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.