Skip to main content

Lesson 12: LangGraph - Stateful Workflows

Topics Covered
  • Beyond AgentExecutor: Why LangChain agents aren't enough for complex flows.
  • Graph-Based Design: Nodes, edges, and state as first-class concepts.
  • Conditional Branching: Dynamic routing based on agent decisions.
  • Cycles and Loops: Iterative refinement patterns.
  • State Persistence: Checkpointing and resuming workflows.
  • Human-in-the-Loop: Pausing for approval and intervention.

LangChain's AgentExecutor handles simple tool-calling loops, but real-world workflows demand more: conditional branches, iterative refinement, human approval gates, and the ability to pause and resume. LangGraph extends LangChain with graph-based orchestration—think of it as a state machine for agents. In this lesson, you'll build workflows that couldn't exist with AgentExecutor alone.

Synopsis

1. The Limitations of Linear Agents

  • AgentExecutor: input → tools → output (linear)
  • Real workflows need: branches, loops, parallel paths, human gates
  • Examples of workflows AgentExecutor can't handle
  • The state machine mental model
  • Why graphs are the right abstraction

2. LangGraph Fundamentals

  • Installing LangGraph
  • Core concepts: State, Nodes, Edges, Graph
  • StateGraph: the main class
  • Defining state with TypedDict
  • Compiling graphs into runnable workflows

3. Nodes: The Building Blocks

  • What is a node (a function that transforms state)
  • Node input/output contracts
  • LLM nodes vs tool nodes vs logic nodes
  • Adding nodes to the graph
  • The special START and END nodes

4. Edges: Controlling Flow

  • Simple edges: node A → node B
  • Conditional edges: route based on state
  • The add_conditional_edges() method
  • Edge functions that return next node names
  • Handling edge cases and defaults

5. Building a ReAct Agent from Scratch

  • The ReAct loop as a graph
  • Agent node: LLM decides action
  • Tool node: executes selected tool
  • Conditional edge: continue or finish?
  • Comparing to LangChain's built-in ReAct

6. Cycles and Iterative Refinement

  • Why cycles matter (self-correction, multi-step reasoning)
  • Implementing loops in graphs
  • Breaking out of loops: termination conditions
  • Example: iterative document improvement
  • Avoiding infinite loops

7. State Persistence and Checkpointing

  • The problem: long workflows fail midway
  • Checkpointers: saving state at each step
  • MemorySaver for development
  • SQLite and Postgres checkpointers for production
  • Resuming from checkpoints

8. Human-in-the-Loop Patterns

  • When to pause for human input
  • The interrupt_before and interrupt_after options
  • Collecting human feedback
  • Resuming after approval
  • Building approval workflows

9. Parallel Execution

  • Running multiple nodes concurrently
  • Fan-out and fan-in patterns
  • Aggregating parallel results
  • Error handling in parallel branches
  • Performance considerations

10. Subgraphs: Composing Complex Systems

  • Breaking large graphs into subgraphs
  • Subgraph as a node in parent graph
  • State mapping between graphs
  • Reusable workflow components
  • Testing subgraphs in isolation

11. Debugging LangGraph Workflows

  • Visualizing graphs with Mermaid
  • Step-by-step execution tracing
  • State inspection at each node
  • Common debugging patterns
  • Integration with LangSmith

Additional Resources