CW

Multi-Agent Systems

LLM AgentsAgent CoordinationFree Lesson

Advertisement

LLM Agents

Multi-Agent Systems — Teams of Specialized Agents

Single agents struggle with complex tasks that require diverse expertise. Multi-agent systems coordinate multiple specialized agents, each focused on a specific aspect of the problem, to achieve results no single agent could.

  • Specialized Roles — Each agent excels at a specific task type
  • Coordination Patterns — Hierarchical, peer-to-peer, and debate-based
  • Emergent Intelligence — The team outperforms any individual agent

The whole is greater than the sum of its parts.

Multi-Agent Systems

Multi-agent systems use multiple LLM-based agents working together to solve complex tasks. Each agent has a specialized role, specific tools, and a focused context, enabling the system to handle tasks that require diverse expertise.

DfMulti-Agent System

A multi-agent system is a collection of autonomous agents that communicate and coordinate to solve problems that are too complex for any single agent. Each agent has specialized capabilities, and the system's overall performance exceeds that of individual agents.

Agent Architectures

Hierarchical Architecture

DfHierarchical Multi-Agent

A hierarchical system has a supervisor agent that delegates tasks to worker agents. The supervisor plans the work, assigns subtasks, and aggregates results. Workers focus on their specialized tasks.

class HierarchicalMultiAgent:
    def __init__(self, supervisor, workers):
        self.supervisor = supervisor
        self.workers = workers  # name -> agent mapping
    
    def solve(self, task):
        # Supervisor plans the work
        plan = self.supervisor.plan(task)
        
        # Execute each subtask
        results = {}
        for subtask in plan["subtasks"]:
            worker_name = subtask["assigned_to"]
            worker = self.workers[worker_name]
            result = worker.execute(subtask["description"], context=results)
            results[subtask["id"]] = result
        
        # Supervisor synthesizes results
        final_answer = self.supervisor.synthesize(task, results)
        return final_answer
Architecture Diagram
           [Supervisor Agent]
          /        |        \
         v         v         v
    [Research] [Analysis] [Writing]
     Agent      Agent      Agent

Peer-to-Peer Architecture

DfPeer-to-Peer Multi-Agent

In a peer-to-peer system, agents communicate directly with each other without a central coordinator. They share information, negotiate, and reach consensus through discussion.

class PeerToPeerMultiAgent:
    def __init__(self, agents):
        self.agents = agents
    
    def solve(self, task, max_rounds=5):
        # Initialize with task
        context = {"task": task, "messages": []}
        
        for round_num in range(max_rounds):
            # Each agent contributes
            for agent in self.agents:
                contribution = agent.contribute(context)
                context["messages"].append({
                    "agent": agent.name,
                    "content": contribution
                })
            
            # Check for consensus
            if self.check_consensus(context):
                break
        
        return self.final_answer(context)

Debate-Based Architecture

DfMulti-Agent Debate

Multi-agent debate has agents argue different positions on a question. Through structured debate, the system arrives at a more balanced and well-reasoned answer than any single agent could produce.

def multi_agent_debate(question, agents, rounds=3):
    """Conduct a structured debate among agents."""
    positions = {}
    
    # Initial positions
    for agent in agents:
        positions[agent.name] = agent.initial_position(question)
    
    # Debate rounds
    for round_num in range(rounds):
        new_positions = {}
        for agent in agents:
            # Agent sees other positions and argues
            other_positions = {k: v for k, v in positions.items() if k != agent.name}
            response = agent.debates(question, other_positions)
            new_positions[agent.name] = response
        positions = new_positions
    
    # Synthesize final answer
    final_answer = synthesize_debate(question, positions)
    return final_answer

Coordination Patterns

Message Passing

class AgentMessage:
    def __init__(self, sender, receiver, content, msg_type="info"):
        self.sender = sender
        self.receiver = receiver
        self.content = content
        self.msg_type = msg_type  # info, request, response, feedback

class MessageBus:
    def __init__(self):
        self.messages = []
        self.handlers = {}
    
    def register(self, agent_name, handler):
        self.handlers[agent_name] = handler
    
    def send(self, message):
        self.messages.append(message)
        if message.receiver in self.handlers:
            self.handlers[message.receiver](message)

Shared Memory

DfShared Memory

Shared memory enables agents to read and write to a common state store. This allows agents to build on each other's work without direct communication.

class SharedMemory:
    def __init__(self):
        self.store = {}
        self.lock = threading.Lock()
    
    def read(self, key):
        with self.lock:
            return self.store.get(key)
    
    def write(self, key, value):
        with self.lock:
            self.store[key] = value
    
    def append(self, key, value):
        with self.lock:
            if key not in self.store:
                self.store[key] = []
            self.store[key].append(value)

Specialized Agent Roles

Common Agent Types

Agent RoleResponsibilityTools
ResearcherFind and summarize informationWeb search, document retrieval
AnalystAnalyze data and identify patternsCalculator, data analysis
CoderWrite and debug codeCode execution, testing
ReviewerCheck work for errors and qualityValidation, fact-checking
WriterProduce final outputText generation, formatting
CoordinatorPlan and delegate workPlanning, task assignment

Example: Code Review System

code_review_system = {
    "planner": PlannerAgent(
        role="Break down code review into manageable tasks"
    ),
    "security_reviewer": CodeReviewerAgent(
        role="Review code for security vulnerabilities",
        tools=["static_analysis", "vulnerability_scanner"]
    ),
    "performance_reviewer": CodeReviewerAgent(
        role="Review code for performance issues",
        tools=["profiler", "complexity_analyzer"]
    ),
    "style_reviewer": CodeReviewerAgent(
        role="Review code for style and best practices",
        tools=["linter", "style_checker"]
    ),
    "synthesizer": SynthesizerAgent(
        role="Combine all review feedback into a coherent report"
    )
}

Scalability Considerations

Multi-Agent Communication Cost

Ccomm=O(N2×M)C_{\text{comm}} = O(N^2 \times M)

Here,

  • NN=Number of agents
  • MM=Average message size

Communication overhead grows quadratically with the number of agents. For large systems (10+ agents), use hierarchical structures to limit direct communication to log(N) levels.

Practice Exercises

  1. Hierarchical System: Build a hierarchical multi-agent system with a supervisor and 3 specialized workers. Test on a task requiring research, analysis, and writing.

  2. Debate System: Implement a 3-agent debate system where agents argue different positions. Compare the debate outcome to individual agent responses.

  3. Coordination Patterns: Compare message passing vs shared memory for coordinating 5 agents. Which is more efficient for different task types?

  4. Scalability Test: Scale your multi-agent system from 2 to 10 agents. At what point does communication overhead become a bottleneck?

Key Takeaways

Summary: Multi-Agent Systems

  • Specialized agents each excel at different task types
  • Hierarchical systems use a supervisor to coordinate workers
  • Peer-to-peer systems enable direct agent-to-agent communication
  • Debate systems arrive at balanced answers through argumentation
  • Shared memory enables agents to build on each other's work
  • Message passing provides structured communication channels
  • Communication overhead grows quadratically with agent count
  • Emergent intelligence — teams outperform individuals

What to Learn Next

-> LLM Agent Frameworks Building autonomous agents with LLMs.

-> Tool Use and Function Calling Teaching LLMs to use external tools.

-> Planning and Reasoning in Agents How agents plan and execute multi-step tasks.

-> Memory Systems for Agents Long-term and short-term memory for agents.

-> Agent Evaluation and Safety Measuring and ensuring agent safety.

-> Reinforcement Learning Training agents through reward signals.

Advertisement

Need Expert LLM Help?

Get personalized tutoring, RAG system design, or production LLM consulting.

Advertisement