← Master Index
Vol. 15 Module 15.4 Lecture

LangGraph

Agent Frameworks (cross-ref Vol. 14.3)

How This Lesson Fits the Module & Volume

Vol. 14.3 LangGraph introduced graphs for RAG critique loops (retrieve → generate → revise). This lecture is the agent control plane: typed state as working memory, tool nodes, conditional routing, checkpointers, and HITL interrupts. Read 14.3 for graph ABCs; use this page for production agent patterns.

LangChain still supplies tools and models. CrewAI / AutoGen hide topology behind roles or chats—LangGraph makes topology explicit.

Learning Objectives

By the end of this lesson, students should be able to:

  • Model an agent as a state graph: nodes, edges, reducers, cycles.
  • Implement a tool-calling node with routing back to the model or END.
  • Use interrupts + checkpointers for HITL pause/resume.
  • Distinguish graph state (WM / in-flight) from organizational LTM.
  • Contrast LangGraph vs LangChain executors vs multi-agent chat frameworks.
  • Design small, named subgraphs instead of one giant mega-graph.
Definition

LangGraph (agent view) is a library for durable, stateful multi-actor LLM apps. You declare a shared state schema and a graph of steps—including cycles—so the agent loop is visible, interruptible, and resumable rather than buried in an executor.

From Hidden Loop to Explicit Graph

ConcernLangChain agent executorLangGraph agent
Control flowOpaque while-loopNodes + conditional edges
StateMessage list soupTyped fields + reducers
HITLDIY callbacksinterrupt / resume + checkpoint
Failure recoveryRestart the whole runResume from last checkpoint
Multi-actorAwkwardMultiple nodes / subgraphs

Agent Graph Pattern

agent (model)

  • Reads state.messages
  • May emit tool_calls
  • Or returns final text

tools

  • Executes calls (MCP OK)
  • Writes observations
  • Edges back to agent

human

  • interrupt() before writes
  • Approval in state
  • Resume with checkpoint

Tool Loop + HITL Sketch

Vol. 14.3 sketched retrieve → generate. Here the cycle is model ↔ tools, with an optional human gate on dangerous tools—the Volume 15 pattern.

from typing import Annotated, Literal, TypedDict from langgraph.graph import StateGraph, START, END from langgraph.checkpoint.memory import MemorySaver from langgraph.types import interrupt import operator class AgentState(TypedDict): messages: Annotated[list, operator.add] pending_write: dict | None approved: bool DANGEROUS = {"create_jira_comment", "run_sql"} def agent(state: AgentState) -> dict: ai = llm.invoke(state["messages"]) # model bound to tools return {"messages": [ai], "pending_write": None} def route(state: AgentState) -> Literal["tools", "human", END]: last = state["messages"][-1] calls = getattr(last, "tool_calls", None) or [] if not calls: return END if any(c["name"] in DANGEROUS for c in calls): return "human" return "tools" def human(state: AgentState) -> dict: last = state["messages"][-1] decision = interrupt({"tool_calls": last.tool_calls, "ask": "Approve writes?"}) return {"approved": bool(decision.get("approved")), "pending_write": last.tool_calls} def tools(state: AgentState) -> dict: last = state["messages"][-1] if last.tool_calls and any(c["name"] in DANGEROUS for c in last.tool_calls): if not state.get("approved"): return {"messages": [{"role": "tool", "content": "Write blocked by human."}]} observations = [run_tool(c) for c in last.tool_calls] return {"messages": observations, "approved": False} graph = StateGraph(AgentState) graph.add_node("agent", agent) graph.add_node("tools", tools) graph.add_node("human", human) graph.add_edge(START, "agent") graph.add_conditional_edges("agent", route) graph.add_edge("human", "tools") graph.add_edge("tools", "agent") app = graph.compile(checkpointer=MemorySaver()) # thread_id scopes working memory snapshots (not org LTM) config = {"configurable": {"thread_id": "ticket-881"}} # app.invoke({...}, config); app.invoke(Command(resume={"approved": True}), config)

State vs Long-Term Memory

Checkpoints snapshot in-flight working memory so HITL can resume. They are not the company wiki. After success, commit recaps to episodic LTM and keep policies in the vector store. Mixing checkpoint blobs into semantic search is a design smell.

Pick LangGraph when

  • HITL, retries, or long-running jobs
  • You need inspectable topology
  • Shared state across specialist nodes
  • Compliance requires replay

Tradeoffs

  • More upfront design than LC agents
  • Easy to over-graph simple Q&A
  • Reducer bugs = silent state corruption
  • Not a role-play UX (CrewAI) or chat UX (AutoGen)
Common Misconception

“If I compile a graph, I automatically have multi-agent collaboration.” Multiple nodes can still be one logical agent. Multi-agent means distinct roles, permissions, and (often) subgraphs—see multi-agent systems. A giant node named do_all is still a single-agent loop with extra ceremony.

Knowledge Check

  1. Short Answer: What does LangGraph make explicit that LC agent executors hide? Answer: The loop as nodes/edges plus typed durable state.
  2. True/False: Vol. 14.3 LangGraph was mainly a RAG critique-loop intro. Answer: True—this lecture is the agent/HITL control plane.
  3. Multiple Choice: interrupt() is for: (a) CNN dropout, (b) HITL pause/resume, (c) MCP stdio only. Answer: (b).
  4. Short Answer: Is a checkpointer organizational LTM? Answer: No—it snapshots in-flight working memory for a thread.
  5. True/False: Dangerous tools should route through a human node. Answer: True.
  6. Multiple Choice: After tools run, the edge typically returns to: (a) agent, (b) PCA, (c) volume 07. Answer: (a).
  7. Short Answer: Name one reducer use in AgentState. Answer: messages: Annotated[list, operator.add] to append rather than overwrite.
  8. True/False: LangGraph replaces the need for MCP servers. Answer: False—tool nodes can call MCP; graphs do not implement I/O catalogs.
  9. Multiple Choice: Prefer a plain LCEL RAG chain when: (a) simple Q&A, (b) multi-day HITL ops, (c) group chat debate. Answer: (a).
  10. Short Answer: Which next framework specializes in data/index agents? Answer: LlamaIndex.

Key Takeaways

  • LangGraph is the inspectable, durable agent loop—not just a RAG graph toy.
  • Typed state = working memory; checkpoints ≠ semantic LTM.
  • HITL interrupts belong on write paths; tool nodes can wrap MCP.
  • Use graphs when topology matters; keep Vol. 14.3 chains for linear RAG.
  • Continue with LlamaIndex.
Trainer’s Guide

Lab: Implement agent ↔ tools with a fake interrupt on create_jira_comment; resume approved vs denied and compare traces.

Whiteboard: Draw Vol. 14.3 critique graph vs this HITL tool graph. Mark thread_id vs episode LTM write after END.

Recap: LangGraph is the agent control plane. Continue with LlamaIndex.