LangGraph
Build Complex AI Agents That Actually Work in Production
LangGraph is a framework for building stateful, multi-step agent applications as graphs. Unlike simple chain-based agents, LangGraph gives you explicit control over state, loops, branching, and human-in-the-loop workflows.
What is LangGraph?
The Missing Piece for Production AI Agents
Why LangGraph Exists:
LangChain agents are great for simple tool-calling. But real production agents need persistent state, conditional branching, loops with exit conditions, human approval steps, and error recovery. LangGraph models agent workflows as state machines represented as directed graphs.
LangGraph is built by the LangChain team but is a separate package. Think of it as "LangChain for complex agents" - where LangChain chains are linear, LangGraph allows cycles, branches, and state persistence.
Real-World Analogy - IRCTC Booking:
Think of booking a train ticket on IRCTC as a LangGraph workflow:
- State: Your booking details (name, train, class, payment status) persisted throughout
- Nodes: Search trains, select seat, fill passenger details, payment gateway
- Conditional Edges: If payment fails -> retry or cancel. If waitlisted -> check alternatives.
- Human-in-the-Loop: "Confirm booking?" - pauses for user approval before payment
- Loops: Payment retry loop (max 3 attempts)
A simple LangChain chain cannot model this. LangGraph can.
Core Concepts:
| Concept | What It Is | Example |
|---|---|---|
| State | Shared data that persists across nodes | Messages, tool results, user preferences |
| Node | A function that reads/writes state | LLM call, tool execution, validation |
| Edge | Connection between nodes | Normal flow or conditional routing |
| Conditional Edge | Edge that routes based on state | If tool_needed -> tool_node, else -> end |
Note: LangGraph is to LangChain agents what React is to vanilla JavaScript DOM manipulation - it gives you a structured way to manage complex state and workflows.
State Management - The Heart of LangGraph
How LangGraph Manages Information Across Steps
What is State?
State in LangGraph is a typed dictionary (defined as a TypedDict or Pydantic model) that flows through the graph. Every node can read from and write to this state. Unlike LangChain where data flows linearly through a chain, LangGraph state is shared and persistent.
State Reducers:
When multiple nodes write to the same state key, LangGraph uses reducers to combine values. The most common reducer is add for message lists - instead of overwriting, new messages are appended.
Example: messages field uses add_messages reducer. Node A adds message 1. Node B adds message 2. Final state has both messages - no data lost.
Checkpointing - Persistent State:
LangGraph can save state at every step using checkpointers. This enables:
- Time Travel: Go back to any previous state and re-run from there
- Human-in-the-Loop: Pause execution, wait for human input, resume
- Crash Recovery: If the process crashes, resume from the last checkpoint
- Conversation Persistence: Save conversations across sessions using thread_id
Checkpointer backends: MemorySaver (dev), SqliteSaver, PostgresSaver (production).
State Best Practices:
- Keep state minimal - only store what nodes actually need
- Use Pydantic models for type validation
- Always use reducers for list fields (especially messages)
- Use PostgresSaver in production for durability
Note: State management is what makes LangGraph fundamentally different from LangChain. It is what enables loops, branching, and human-in-the-loop - features essential for production agents.
Nodes, Edges, and Graph Construction
Building the Agent Workflow Graph
Nodes - The Processing Units:
A node is simply a Python function that takes state as input and returns state updates. Nodes can do anything: call an LLM, execute a tool, validate data, query a database, or even pause for human input.
Edge Types:
- Normal Edge: Always goes from Node A to Node B. Like a straight road.
- Conditional Edge: Routes to different nodes based on current state. Like a traffic signal - green goes forward, red goes to waiting. A function inspects the state and returns the next node name.
- Entry Edge: Defines which node starts the graph (START -> first_node).
- End Edge: Defines which node ends the graph (last_node -> END).
Common Graph Patterns:
- ReAct Agent: LLM node -> conditional (tool_needed?) -> Tool node -> back to LLM node (loop)
- Supervisor: Supervisor node decides which worker node to call next. Workers report back to supervisor.
- Plan-and-Execute: Planner node creates plan -> Executor node runs each step -> Replanner checks if done
- Human-in-the-Loop: Agent node -> interrupt_before tool_node -> wait for human approval -> tool_node
Graph Construction Steps:
- Define your State (TypedDict with annotations)
- Create a StateGraph with that state type
- Add nodes (add_node)
- Add edges and conditional edges (add_edge, add_conditional_edges)
- Set entry point (set_entry_point)
- Compile the graph (graph.compile())
- Run with graph.invoke(initial_state)
Note: The power of LangGraph is in conditional edges - they let you create loops, branches, and decision points that simple chains cannot express.
Human-in-the-Loop and Interrupt Patterns
Pausing Agents for Human Approval
Why Human-in-the-Loop?
Production agents often need human approval before taking high-stakes actions - sending emails, making purchases, modifying databases. LangGraph provides interrupt_before and interrupt_after to pause execution at specific nodes and wait for human input.
How Interrupts Work:
- Graph executes until it reaches a node marked with interrupt_before
- State is saved to the checkpointer (everything persisted)
- Execution pauses and returns to the caller
- Human reviews the planned action and approves/rejects/modifies
- Graph resumes from the interrupted point with (optionally modified) state
Real-World Examples:
- Email Agent: Drafts email -> interrupt -> Human reviews/edits -> Sends
- Trading Bot: Analyzes market -> Suggests trade -> interrupt -> Human approves -> Executes trade
- Customer Support: Suggests refund -> interrupt -> Manager approves -> Processes refund
- Code Agent: Generates code -> interrupt -> Developer reviews -> Commits to repo
Time Travel Debugging:
Because LangGraph checkpoints every state, you can "time travel" to any previous step, modify the state, and re-run from there. This is incredibly powerful for debugging agent behavior - you can see exactly what the agent was thinking at each step and change its course.
Note: Human-in-the-loop is the feature that makes LangGraph production-ready. Never deploy an agent that takes irreversible actions without a human approval step.
LangGraph vs Alternatives
Choosing the Right Tool for Agent Workflows
When to Use LangGraph:
- Complex agent workflows with loops, branches, and conditional logic
- Human-in-the-loop requirements for high-stakes actions
- Multi-agent systems where agents coordinate and hand off tasks
- Conversation persistence across sessions
- Crash recovery for long-running agent tasks
LangGraph vs Alternatives:
| Framework | Best For | State Management |
|---|---|---|
| LangGraph | Complex stateful agents | Built-in, persistent |
| LangChain Agents | Simple tool-calling | Basic (memory) |
| CrewAI | Role-based multi-agent | Shared context |
| AutoGen | Conversational agents | Chat-based |
When NOT to Use LangGraph:
- Simple RAG: Overkill. Use LangChain or Haystack instead.
- Linear workflows: If there are no loops or branches, a simple chain suffices.
- Quick prototypes: LangGraph has a learning curve. For demos, LangChain agents are faster.
Note: LangGraph adds complexity. Only use it when you genuinely need stateful workflows, loops, or human-in-the-loop. For simple tool-calling agents, LangChain is sufficient.
Interview Questions
Q: How is LangGraph different from LangChain agents?
LangChain agents are linear tool-calling loops - the LLM decides tools in a simple think-act-observe cycle. LangGraph models workflows as directed graphs with explicit state management. Key differences: (1) LangGraph supports cycles/loops with exit conditions. (2) State is typed and persistent via checkpointers. (3) Conditional edges allow complex branching. (4) Human-in-the-loop via interrupt_before/after. (5) Time travel debugging via state checkpoints. Use LangChain agents for simple cases, LangGraph for production complexity.
Q: How does human-in-the-loop work in LangGraph?
LangGraph provides interrupt_before and interrupt_after on specific nodes. When execution reaches an interrupted node: (1) State is saved to the checkpointer. (2) Execution pauses and returns to the caller. (3) A human reviews the planned action. (4) The graph resumes from the interrupted point with optionally modified state. This requires a checkpointer backend (Sqlite/Postgres) for state persistence. Essential for high-stakes actions like sending emails, making payments, or modifying databases.
Q: What are state reducers in LangGraph and why are they needed?
State reducers define how state updates are combined when multiple nodes write to the same key. Without reducers, the last write wins and earlier data is lost. The most common reducer is add_messages for message lists - it appends new messages instead of overwriting. Example: Node A adds user message, Node B adds AI response. With add reducer, both are preserved. Without it, only the last write survives. Reducers are essential for any state field that accumulates data over the graph execution.
Frequently Asked Questions
What is LangGraph?
LangGraph is a framework for building stateful, multi-step agent applications as graphs. Unlike simple chain-based agents, LangGraph gives you explicit control over state, loops, branching, and human-in-the-loop workflows.
How does LangGraph work?
The Missing Piece for Production AI Agents Why LangGraph Exists: LangChain agents are great for simple tool-calling. But real production agents need persistent state , conditional branching , loops with exit conditions , human approval steps , and error recovery .
Related topics
Practice this on DevInterviewMaster
Read the full LangGraph breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.