AI Hints
Home›LangGraph Tutorial›Command and Multi-Agent
LangGraph · Lesson 69 of 120

A Supervisor

One agent routes.

⏱ 2 min read▶ Video at 1:13:37🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

A supervisor reads each request, and decides which specialist should handle it. At Riverside Books, a books specialist knows about stock, prices, and authors. An orders specialist tracks orders.

A Supervisor
From the video: A Supervisor
  • Reads each request
  • Picks a specialist
  • Books and orders

Supervisor and specialists

The two specialists are agents made with create agent, in a helper file, each with its own tools and prompt. The supervisor uses structured output to pick one, and a command to go there. The specialists are added as nodes, directly, because they share the messages key. Run it. Each question reaches the right specialist, which uses its own tools to answer.

supervisor.pypython
from typing import Literal
from pydantic import BaseModel
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.types import Command
from specialists import books, orders

class Pick(BaseModel):
    agent: Literal["books", "orders"]
router = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0).with_structured_output(Pick)

def supervisor(s: MessagesState) -> Command[Literal["books", "orders"]]:
    return Command(goto=router.invoke(s["messages"]).agent)

b = StateGraph(MessagesState)
b.add_node(supervisor); b.add_node("books", books); b.add_node("orders", orders)
b.add_edge(START, "supervisor"); team = b.compile()
for q in ["Who wrote Circe?", "Where is order RB-1234?"]:
    print(q, "->", team.invoke({"messages": [{"role": "user", "content": q}]})["messages"][-1].text)
Output
Who wrote Circe? -> Madeline Miller wrote Circe.
Where is order RB-1234? -> Order RB-1234 has shipped and is arriving Thursday.
Also uses shop_tools.py
from langchain.tools import tool

BOOKS = {
    "The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
    "Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
    "Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}

@tool
def check_stock(title: str) -> int:
    """Copies of a book in stock at Riverside Books."""
    return BOOKS.get(title, {}).get("stock", 0)

@tool
def get_price(title: str) -> float:
    """Price of a book in pounds."""
    return BOOKS.get(title, {}).get("price", 0.0)

@tool
def get_author(title: str) -> str:
    """Author of a book."""
    return BOOKS.get(title, {}).get("author", "unknown")
Also uses specialists.py
from langchain.agents import create_agent
from langchain.tools import tool
from shop_tools import check_stock, get_price, get_author

@tool
def order_status(order_id: str) -> str:
    """Status of an order. Order ids look like RB-1234."""
    return {"RB-1234": "shipped, arriving Thursday", "RB-5678": "being packed"}.get(order_id, "not found")

MODEL = "google_genai:gemini-flash-lite-latest"
books = create_agent(MODEL, tools=[check_stock, get_price, get_author], name="books",
                     system_prompt="You are the books specialist at Riverside Books. Be brief.")
orders = create_agent(MODEL, tools=[order_status], name="orders",
                      system_prompt="You are the orders specialist at Riverside Books. Be brief.")

Supervisor tips

Keep the supervisor's job small. It only routes. Give it clear descriptions of each specialist. Use a small, fast model for it. And let it route back for another specialist, when a request covers two areas.

keyvalue
Only routessmall job
Describeeach specialist
Modelsmall and fast
Multi-partroute again

Summary

A supervisor routes each request to a specialist agent, added as a node. Next, letting agents hand off to each other directly.

Key takeaways
  • Structured choice
  • Command(goto=...)
  • Agents as nodes
Next up: Handoffs — passing control