Day 16/30: Supervisor Pattern A developer implemented the Supervisor pattern using LangGraph and MCP to fix a support bot that was failing in production due to context confusion. The pattern uses a manager agent to delegate tasks to specialized worker agents for orders, shipments, and returns, improving accuracy and reducing errors. 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.