AI Study Online
AI Tutorials

Mastering Multi-Agent Collaboration: A Practical Guide to 5 Core Patterns with Hands-On Code

5 min read

Why Multi-Agent Collaboration Matters

In the realm of AI development, single agents often hit bottlenecks in handling complex, dynamic tasks. This is where multi-agent collaboration shines. Unlike rigid function-based workflows, multi-agent systems empower each agent with independent LLM reasoning contexts and decision-making autonomy, enabling them to coordinate dynamically. This article dives into five core collaboration patterns — Supervisor, Hierarchical, Swarm, Sequential Chain, and Network — complete with practical code examples using LangGraph and AutoGen. We will also explore communication protocols, security measures, and when to choose multi-agent vs. single-agent architectures.

Before diving into patterns, let us clarify why multi-agent systems are worth the effort. Unlike traditional function chaining (where a central controller rigidly calls functions A to B to C), multi-agent setups offer:

  • Independent Reasoning: Each agent makes decisions autonomously, no need for a central controller to predefine all branches.
  • Dynamic Collaboration: Agents adapt their interactions based on real-time context, not fixed workflows.
  • Focused Contexts: Each agent's prompts stay concise, boosting reasoning quality by avoiding irrelevant noise.

Multi-agent is ideal when:

  • Tasks require cross-disciplinary roles (e.g., a software team with product managers, developers, and testers).
  • Workflows are unpredictable (no pre-defined step-by-step process).
  • Parallel processing or context window limitations (single agents cannot hold all necessary info) demand scaling.

Five Core Multi-Agent Collaboration Patterns

1. Supervisor Pattern: Centralized Task Orchestration

Think of this as a "project manager" agent that delegates tasks to specialized sub-agents and consolidates results.

Use Case

Building a content pipeline with a researcher (finds data), analyst (interprets data), and writer (creates content).

Code Implementation (LangGraph)

from langgraph.supervisor import create_react_agent, create_supervisor

# Step 1: Create specialized agents
researcher = create_react_agent({
    "model": "gpt-4o",
    "name": "researcher",
    "prompt": "Your role is to research and gather factual information."
})

analyst = create_react_agent({
    "model": "gpt-4o",
    "name": "analyst",
    "prompt": "Your role is to analyze data and identify insights."
})

writer = create_react_agent({
    "model": "gpt-4o",
    "name": "writer",
    "prompt": "Your role is to turn insights into engaging content."
})

# Step 2: Assemble the Supervisor
supervisor = create_supervisor({
    "model": "gpt-4o",
    "agents": [researcher, analyst, writer],
    "prompt": "You are a project manager. Delegate tasks and compile results."
})

# Step 3: Run the workflow
result = supervisor.invoke("Create a blog post about AI agent collaboration trends in 2026")
print(result)

2. Hierarchical Pattern: Nested Multi-Level Orchestration

This is a scaled-up Supervisor — think "CEO to Department Heads to Team Members" — ideal for large, complex systems.

Use Case

A software development team with a CEO supervisor, technical/design department heads, and individual contributors (frontend, backend, testers).

Code Implementation (LangGraph)

from langgraph.supervisor import create_supervisor

# Step 1: Create individual contributors
frontend_dev = create_react_agent({
    "model": "gpt-4o",
    "name": "frontend_dev",
    "prompt": "Specialized in React/TypeScript development."
})

backend_dev = create_react_agent({
    "model": "gpt-4o",
    "name": "backend_dev",
    "prompt": "Specialized in Node.js/Express backend development."
})

# Step 2: Create department supervisors
tech_supervisor = create_supervisor({
    "model": "gpt-4o",
    "agents": [frontend_dev, backend_dev],
    "prompt": "Manage the technical team: delegate coding tasks and review work."
})

design_supervisor = create_react_agent({
    "model": "gpt-4o",
    "name": "design_supervisor",
    "prompt": "Oversee UI/UX design and ensure alignment with product goals."
})

# Step 3: Create CEO-level supervisor
ceo_supervisor = create_supervisor({
    "model": "gpt-4o",
    "agents": [tech_supervisor, design_supervisor],
    "prompt": "Oversee the entire project: coordinate tech and design teams."
})

# Step 4: Execute
result = ceo_supervisor.invoke("Build a task management app with React and Node.js")
print(result)

3. Swarm Pattern: Peer-to-Peer Autonomous Handoffs

Agents act as equals, passing control dynamically via a handoff mechanism — like a relay race where each agent decides who takes the baton next.

Use Case

A content creation workflow where a researcher, critic, and writer pass work between themselves based on need.

Code Implementation (LangGraph)

from langgraph.swarm import create_handoff, create_swarm

# Step 1: Create a handoff tool
handoff_tool = create_handoff()

# Step 2: Create agents with handoff capabilities
researcher = create_react_agent({
    "model": "gpt-4o",
    "name": "researcher",
    "tools": [handoff_tool],
    "prompt": "Research topics and hand off to the critic when done."
})

critic = create_react_agent({
    "model": "gpt-4o",
    "name": "critic",
    "tools": [handoff_tool],
    "prompt": "Critique research and hand off to the writer (or back to researcher if incomplete)."
})

writer = create_react_agent({
    "model": "gpt-4o",
    "name": "writer",
    "tools": [handoff_tool],
    "prompt": "Turn critiqued research into a blog post."
})

# Step 3: Assemble the Swarm
swarm = create_swarm({
    "agents": [researcher, critic, writer],
    "default_active_agent": "researcher"
})

# Step 4: Run
result = swarm.invoke("Write a post about multi-agent security best practices")
print(result)

Note: In AutoGen, use SelectorGroupChat for similar behavior (vs. RoundRobinGroupChat, which is rigidly sequential).

4. Sequential Chain Pattern: Fixed Pipeline Workflows

A rigid, linear pipeline — great for tasks with strictly defined steps (e.g., research, outline, draft, edit).

Use Case

Content creation with strict sequential steps.

Code Implementation (LangGraph)

from langgraph.graph import StateGraph, START, END

# Define the state schema (data passed between steps)
class ContentState:
    research: str
    outline: str
    draft: str
    edited: str
    final_content: str

# Build the graph
graph = StateGraph(ContentState)

# Define each step as a node
def research(state):
    state.research = "AI agent collaboration trends include multi-protocol support and security advancements."
    return state

def outline(state):
    state.outline = "1. Introduction
2. Key Patterns
3. Protocols
4. Security
5. Conclusion"
    return state

def draft(state):
    state.draft = f"Based on research: {state.research}
Outline: {state.outline}
[Draft content here]"
    return state

def edit(state):
    state.edited = state.draft.replace("[Draft content here]", "Polished content...")
    return state

def finalize(state):
    state.final_content = state.edited + "

---
Published on 2026-06-10"
    return state

# Add nodes and define the fixed sequence
graph.add_node("research", research)
graph.add_node("outline", outline)
graph.add_node("draft", draft)
graph.add_node("edit", edit)
graph.add_node("finalize", finalize)

graph.add_edge(START, "research")
graph.add_edge("research", "outline")
graph.add_edge("outline", "draft")
graph.add_edge("draft", "edit")
graph.add_edge("edit", "finalize")
graph.add_edge("finalize", END)

# Compile and run
app = graph.compile()
result = app.invoke({})
print(result.final_content)

5. Network Pattern: Free-Form Routing

Agents connect freely, with a "router" agent deciding the next step using an LLM — perfect for exploratory, unstructured tasks.

Use Case

A dynamic problem-solving workflow where a planner, executor, and reviewer collaborate flexibly.

Code Implementation (LangGraph)

from langgraph.graph import StateGraph, START, END
from langchain_openai import ChatOpenAI

def router(state):
    llm = ChatOpenAI(model="gpt-4o")
    response = llm.invoke(f"Current state: {state}
Choose next agent: planner, executor, reviewer, or end?")
    return {"next_agent": response.content.strip().lower()}

def planner(state):
    state.plan = "Develop a multi-agent security framework with three layers."
    return state

def executor(state):
    state.code = "def secure_multi_agent(): ..."
    return state

def reviewer(state):
    state.feedback = "Code looks solid, but add input validation."
    return state

# Build the graph
graph = StateGraph(dict)
graph.add_node("router", router)
graph.add_node("planner", planner)
graph.add_node("executor", executor)
graph.add_node("reviewer", reviewer)

graph.add_edge(START, "router")
graph.add_conditional_edges(
    "router",
    lambda s: s["next_agent"],
    {
        "planner": "planner",
        "executor": "executor",
        "reviewer": "reviewer",
        "end": END
    }
)
# Loop back to router after each agent
graph.add_edge("planner", "router")
graph.add_edge("executor", "router")
graph.add_edge("reviewer", "router")

app = graph.compile()
result = app.invoke({})
print(result)

Key Communication Protocols

Multi-agent systems rely on protocols to ensure interoperability:

1. Agent-to-Agent (A2A) Protocol

  • Purpose: Enables agents from different frameworks/vendors to communicate.
  • Core Concept: AgentCard — a JSON object describing an agent's capabilities, endpoints, and authentication methods.

Example Structure

{
  "name": "researcher-agent",
  "capabilities": ["web-search", "data-synthesis"],
  "endpoint": "https://agent-service.com/researcher",
  "auth": {
    "type": "api-key",
    "key": "secure_123"
  }
}

2. Model Context Protocol (MCP)

  • Purpose: Manages how agents interact with external tools and data sources.
  • Capabilities:
    • Tools: Access to APIs (e.g., weather, databases).
    • Resources: Access to documents/files.
    • Prompts: Predefined prompt templates.
  • Architecture: LLM apps connect via MCP Client to an MCP Server that exposes these resources.

Security in Multi-Agent Systems

Multi-agent setups introduce unique security risks — here is how to mitigate them:

  1. Structured Messaging: Enforce JSON or structured data between agents (no raw text) to prevent prompt injection.
def validate_message(message):
    if not isinstance(message, dict):
        raise SecurityError("Only structured messages allowed")
    return message
  1. Tool Whitelists: Restrict agents to pre-approved tools.
ALLOWED_TOOLS = ["web-search", "calculator"]
def check_tool(tool_name):
    if tool_name not in ALLOWED_TOOLS:
        raise SecurityError(f"Tool {tool_name} not authorized")
    return tool_name
  1. Token Budgeting: Limit token usage per agent to prevent infinite loops.
def check_token_usage(usage):
    if usage > 10000:
        raise ResourceError("Token limit exceeded")
    return usage

When to Use Multi-Agent vs. Single-Agent

Task CharacteristicSingle-AgentMulti-Agent
Steps are fixed/unchangingRecommendedOverkill
Requires cross-disciplinary rolesContext chaosRecommended
Workflow is unpredictableCannot handle branchesRecommended
Single interaction sufficesRecommendedOverhead
Cost is a priorityLower token usage2-5x higher cost

Frequently Asked Questions

Q: What is the difference between Supervisor and Hierarchical patterns?

The Supervisor pattern uses a single manager overseeing multiple workers. The Hierarchical pattern extends this with multiple layers of management — think CEO, then department heads, then individual contributors. Use Hierarchical when you need to scale beyond 5-7 agents.

Q: Can I use multiple patterns together?

Yes, real-world systems often combine patterns. For example, use a Supervisor pattern at the top level with a Swarm pattern inside one of the sub-groups. LangGraph supports nesting graphs to achieve this.

Q: Which framework is better for beginners — LangGraph or AutoGen?

LangGraph has a gentler learning curve with its graph-based API and clear documentation. AutoGen offers more flexibility but requires deeper understanding of agent communication. Start with LangGraph for prototyping and switch to AutoGen for production needs.

Share this article

Related Articles

AI TutorialsBeginner

How to Write Prompts That Actually Work: The 5-Point Framework

Vague prompts get mediocre answers. Master the 5-Point Prompt Framework — Role, Context, Task, Format, Constraints — and get dramatically better results from any AI tool.

5 min read
PromptsPrompt EngineeringFramework