Course Content
LangGraph Agents
7 sections · 49 lessons
How do you model roles (planner, researcher, writer, critic) as nodes/agents in LangGraph?
What you need to know
Supervisor as code
Python
1from typing import Literal2from langgraph.types import Command34def supervisor(state) -> Command[Literal["researcher", "writer", "critic", "__end__"]]:5 if not state.get("findings"):6 return Command(goto="researcher")7 if not state.get("draft"):8 return Command(goto="writer")9 if state.get("approved") or state["revisions"] >= 2:10 return Command(goto="__end__")11 return Command(goto="critic")1213builder.add_node("supervisor", supervisor)14builder.add_node("researcher", research_agent) # create_agent with search tools15builder.add_node("writer", writer_node) # strong model, no tools16builder.add_node("critic", critic_node) # cheap model, rubric, structured output17for role in ("researcher", "writer", "critic"):18 builder.add_edge(role, "supervisor")19builder.add_edge(START, "supervisor")Supervisor as an LLM with subagents as tools
Python
1from langchain.agents import create_agent2from langchain.tools import tool34researcher = create_agent("openai:gpt-4.1-mini", tools=[web_search],5 system_prompt="Find facts with sources. Return bullet points.")67@tool8def research(question: str) -> str:9 """Research one question and return findings with sources."""10 out = researcher.invoke({"messages": [{"role": "user", "content": question}]})11 return out["messages"][-1].content1213supervisor = create_agent("anthropic:claude-sonnet-4-5", tools=[research, write_section])The supervisor sees only the researcher's final answer, not its 12 search steps, which keeps its context small.
Design rules
- Narrow shared state —
plan,findings,draft,critique,revisions. - Tools per role — a writer with a search tool will search instead of writing.
- Structured critic —
{"approved": bool, "issues": [...]}. - Deterministic routing where possible — an LLM supervisor runs on every hop and is often the most expensive part.
A real-life example
A consulting firm's "sector brief" system has four roles. The planner turns "EV two-wheeler market in India" into five questions; the researcher (a create_agent with search and a filings tool) answers each; the writer drafts 1,200 words from the findings only; the critic checks every number has a source. The supervisor is 12 lines of code. An early version used an LLM supervisor, which sometimes sent the draft back to research for no reason; switching to code cut average cost from Rs 42 to Rs 27 per brief with no quality change.
Follow-up questions to expect
- "Code supervisor or LLM supervisor?" — Code when the order of roles follows clear rules; an LLM when the next step depends on understanding the request.
- "Should each role be a subgraph?" — Only roles with their own loop or tools. A single model call is just a node.
- "How do roles share memory?" — Through typed state keys; each role reads only what it needs, formatted into its own prompt.