Sign in
All templates
Template· Starters

LangGraph supervisor template

Python starter for a supervisor + workers graph.

Sign in to save
#langgraph#python
python
"""
LangGraph supervisor pattern: one router, N specialist workers.
Reference: https://langchain-ai.github.io/langgraph/tutorials/multi_agent/agent_supervisor/
"""
from typing import Literal
from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4.1")

researcher = create_react_agent(model, tools=[], prompt="You research topics on the web.")
writer     = create_react_agent(model, tools=[], prompt="You turn research notes into a brief.")

def supervisor(state) -> Literal["researcher", "writer", "__end__"]:
    # Ask the model which worker should act next, or END.
    decision = model.invoke([
        {"role": "system", "content": "Route to 'researcher', 'writer', or 'end'."},
        {"role": "user",   "content": state["messages"][-1].content},
    ]).content.strip().lower()
    if "research" in decision: return "researcher"
    if "writ"    in decision: return "writer"
    return "__end__"

graph = StateGraph(dict)
graph.add_node("researcher", researcher)
graph.add_node("writer",     writer)
graph.add_conditional_edges(START, supervisor)
graph.add_edge("researcher", START)
graph.add_edge("writer",     END)
app = graph.compile()