LangGraph Agents

Course Content

LangGraph Agents

7 sections · 49 lessons

How do you model roles (planner, researcher, writer, critic) as nodes/agents in LangGraph?


A code supervisor for sector briefssupervisorplanner: five questionsresearcher:create_agent, searchwriter: strongmodel, no toolscritic: rubric,approved flagstop: approvedor 2 revisions
Replacing the LLM supervisor with twelve lines of code cut cost per brief from Rs 42 to Rs 27 with no quality change.

What you need to know

Supervisor as code

Python
from typing import Literalfrom langgraph.types import Commanddef supervisor(state) -> Command[Literal["researcher", "writer", "critic", "__end__"]]:    if not state.get("findings"):        return Command(goto="researcher")    if not state.get("draft"):        return Command(goto="writer")    if state.get("approved") or state["revisions"] >= 2:        return Command(goto="__end__")    return Command(goto="critic")builder.add_node("supervisor", supervisor)builder.add_node("researcher", research_agent)   # create_agent with search toolsbuilder.add_node("writer", writer_node)          # strong model, no toolsbuilder.add_node("critic", critic_node)          # cheap model, rubric, structured outputfor role in ("researcher", "writer", "critic"):    builder.add_edge(role, "supervisor")builder.add_edge(START, "supervisor")

Supervisor as an LLM with subagents as tools

Python
from langchain.agents import create_agentfrom langchain.tools import toolresearcher = create_agent("openai:gpt-4.1-mini", tools=[web_search],                          system_prompt="Find facts with sources. Return bullet points.")@tooldef research(question: str) -> str:    """Research one question and return findings with sources."""    out = researcher.invoke({"messages": [{"role": "user", "content": question}]})    return out["messages"][-1].contentsupervisor = 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.