# Day 16/30: Supervisor Pattern

> Source: <https://dev.to/yashwanth_kasi/day-1630-supervisor-pattern-3ghc>
> Published: 2026-07-26 05:51:16+00:00

I recently spent hours debugging a support bot that was supposed to handle customer inquiries about orders, shipments, and returns. The bot was built using LangGraph and MCP, and it worked great in isolation, but when we deployed it to production, it started to fail in unexpected ways. Sometimes it would answer questions about orders when the customer was asking about returns, or vice versa. It was as if the bot had forgotten what context it was in, and was just making random guesses.

After digging through the code, I realized that the problem was that the bot was trying to do too much itself. It was a single agent that was responsible for understanding the customer's question, retrieving the relevant information from the database, and generating a response. This was causing the bot to get overwhelmed and lose track of what it was doing.

That's when I decided to try out the Supervisor pattern. The idea is to create a manager agent that delegates tasks to specialized worker agents. In this case, I created a supervisor agent that would receive the customer's question and then delegate it to one of three worker agents: an orders agent, a shipments agent, or a returns agent. Each worker agent was responsible for a specific task, such as retrieving information from the database or generating a response.

Here's an example of how I implemented the Supervisor pattern using LangGraph and MCP:

``` python
import langgraph as lg
from mcp import tools

# Define the supervisor agent
supervisor = lg.StateGraph()
supervisor.add_node("start", tools.prompt("What is your question?"))
supervisor.add_node("orders", tools.prompt("What is your order number?"))
supervisor.add_node("shipments", tools.prompt("What is your shipment tracking number?"))
supervisor.add_node("returns", tools.prompt("What is your return reason?"))

# Define the worker agents
orders_agent = lg.StateGraph()
orders_agent.add_node("start", tools.prompt("Order details:"))
orders_agent.add_node("response", tools.response("Your order will be shipped soon."))

shipments_agent = lg.StateGraph()
shipments_agent.add_node("start", tools.prompt("Shipment details:"))
shipments_agent.add_node("response", tools.response("Your shipment is on its way."))

returns_agent = lg.StateGraph()
returns_agent.add_node("start", tools.prompt("Return details:"))
returns_agent.add_node("response", tools.response("Your return has been processed."))

# Define the conditional edges between the supervisor and worker agents
supervisor.add_conditional_edges(
    "start",
    {
        "orders": lambda x: x.contains("order"),
        "shipments": lambda x: x.contains("shipment"),
        "returns": lambda x: x.contains("return")
    }
)

# Define the conditional edges between the worker agents and the supervisor
orders_agent.add_conditional_edges(
    "start",
    {
        "response": lambda x: True
    }
)

shipments_agent.add_conditional_edges(
    "start",
    {
        "response": lambda x: True
    }
)

returns_agent.add_conditional_edges(
    "start",
    {
        "response": lambda x: True
    }
)

# Run the supervisor agent
supervisor.run()
```

This code defines a supervisor agent that delegates tasks to three worker agents based on the customer's question. Each worker agent is responsible for a specific task, such as retrieving information from the database or generating a response.

One practical gotcha to watch out for when using the Supervisor pattern is that it can be easy to create a situation where the supervisor agent is delegating tasks to worker agents that are not equipped to handle them. For example, if the orders agent is not designed to handle questions about shipments, it may not be able to generate a correct response. To avoid this, it's essential to carefully define the conditional edges between the supervisor and worker agents, and to ensure that each worker agent is designed to handle the tasks that are delegated to it.

As we continue to build more complex agentic AI systems, we'll need to develop new patterns and techniques for managing the interactions between agents. Tomorrow, we'll explore another key concept that will help us build more robust and scalable AI systems.
