Supervisor Pattern
Instead of letting agents talk to whoever they want, you create a rigid hierarchy. A 'Manager' agent receives the task, delegates sub-tasks to 'Worker' agents, reviews their work, and compiles the final answer.
Why Does This Exist?
When building Multi-Agent Systems, the default approach is often a "Group Chat." You put the User, the Coder Agent, and the Reviewer Agent in a virtual room and tell them to figure it out.
Group Chats are highly creative, but highly unreliable. The Coder might ignore the Reviewer. The agents might get stuck in an infinite argument. They might hallucinate that the task is finished before it actually is. In production applications (like banking or healthcare), you cannot afford this chaos.
The Supervisor Pattern enforces strict control flow. You structure your AI exactly like a corporate hierarchy. Worker agents do not talk to each other; they only talk to their Manager (the Supervisor).
Think of It Like This
Building a House
Group Chat Pattern: You put a plumber, an electrician, and a carpenter in an empty lot. You yell "Build a house!" and walk away. They will likely argue over who goes first, step on each other's toes, and build a mess.
Supervisor Pattern: You hire a General Contractor (Supervisor). The Contractor tells the carpenter to build the frame. The carpenter finishes and reports to the Contractor. The Contractor then tells the electrician to wire the frame. The workers never talk to each other directly; the Contractor orchestrates the entire project plan.
How It Actually Works
The Supervisor is usually a fast, highly capable LLM (like GPT-4o). It has no tools of its own (it cannot search the web or write code). Its only job is to Route, Delegate, and Evaluate.
The Execution Loop
- The Request: The user asks: "Research Apple's Q3 earnings and write a python script to graph them."
- The Plan: The Supervisor reads the request and breaks it into steps.
- Delegation 1: The Supervisor creates a Sub-Agent Handoff to the
Researcher Agent: "Find Apple's Q3 earnings." - Execution 1: The
Researcher Agentuses its web tools, finds the data, and reports back to the Supervisor. - Delegation 2: The Supervisor routes to the
Coder Agent: "Here is the data: [Data]. Write a graphing script." - Execution 2: The
Coder Agentwrites the script and reports back. - Finalization: The Supervisor compiles the final response and returns it to the user.
Show Me the Code
Frameworks like LangGraph excel at the Supervisor Pattern because they allow you to define the communication pathways as a rigid state graph.
# Conceptual LangGraph Supervisor Implementationfrom langgraph.graph import StateGraph, ENDimport openai
def supervisor_node(state): """The Supervisor decides who should act next.""" messages = state["messages"] prompt = f""" You are a Supervisor managing a Researcher and a Coder. Review the conversation so far. If the task is entirely complete, output "FINISH". If we need data, output "RESEARCHER". If we need code, output "CODER". """ # ... call LLM ... decision = llm_output.strip() return {"next": decision}
def researcher_node(state): """Worker 1: Uses web tools""" print("-> Researcher is working...") # ... call LLM with search tools ... return {"messages": ["Researcher found the Q3 data: $100M"]}
def coder_node(state): """Worker 2: Uses code tools""" print("-> Coder is working...") # ... call LLM with python tools ... return {"messages": ["Coder wrote the script."]}
# --- Build the Graph ---workflow = StateGraph(AgentState)
# Add the nodes (The Agents)workflow.add_node("Supervisor", supervisor_node)workflow.add_node("Researcher", researcher_node)workflow.add_node("Coder", coder_node)
# Define the rigid hierarchy (Edges)# Workers ALWAYS report back to the Supervisor. They never talk to each other.workflow.add_edge("Researcher", "Supervisor") workflow.add_edge("Coder", "Supervisor")
# The Supervisor routes dynamically based on its decisionworkflow.add_conditional_edges("Supervisor", lambda x: x["next"], { "RESEARCHER": "Researcher", "CODER": "Coder", "FINISH": END})
# Set entry pointworkflow.set_entry_point("Supervisor")app = workflow.compile()Watch Out For
Supervisor Bottleneck
Because every single message must pass through the Supervisor, you will be calling the Supervisor LLM very frequently. If you use an expensive, slow model (like GPT-4) for the Supervisor, your system will be slow and costly. Conversely, if you use a model that is too cheap (like GPT-4o-mini), it might lack the reasoning capabilities to accurately evaluate the workers' output, leading to terrible routing decisions. You must carefully balance the intelligence and cost of your Supervisor node.
The Quick Version
- Peer-to-peer agent networks (Group Chats) are creative but chaotic and unreliable for production systems.
- The Supervisor Pattern imposes a strict hierarchy.
- A central Manager agent plans the workflow and routes tasks to specialized Worker agents.
- Worker agents never communicate with each other; they only report their results back to the Manager.
- This pattern is highly deterministic and prevents infinite looping and off-topic hallucinations.
What to Read Next
- Read Multi-Agent Systems to understand the broader ecosystem this pattern lives in.
- Read Sub-Agents and Handoffs to see the exact mechanism the Supervisor uses to spawn a worker.