LangGraph

If you have built applications using standard Large Language Model (LLM) pipelines, you have likely encountered a hard limit. Traditional chains are linear: Input Prompt LLM Output.

But what if the LLM realizes halfway through a task that it needs more information? What if it makes a mistake and needs to try again? Real intelligence is not a straight line; it is cyclical. It involves reasoning, observing, failing, and looping back.

LangGraph is a framework built by the LangChain ecosystem specifically to handle these complex, cyclical workflows. It allows developers to define AI workflows as directed graphs, enabling the creation of stateful, multi-actor applications that can loop, branch, and pause for human intervention.

Why Graphs?

In computer science, a graph is a data structure consisting of Nodes (vertices) connected by Edges (lines).

When you ask an autonomous agent to "Research quantum computing and write a summary," the agent might need to search the web, read a page, realize the page is unhelpful, search again, read a new page, and finally write the summary.

This requires a while loop. Standard LLM chains (Directed Acyclic Graphs, or DAGs) cannot natively loop. LangGraph solves this by treating the entire agent architecture as a cyclical graph where state is constantly preserved and updated as execution flows back and forth between nodes.

The Core Primitives of LangGraph

To understand LangGraph, you must understand its five foundational primitives. Everything built in LangGraph is a combination of these elements.

Primitive 1: State

In LangGraph, State is the single source of truth. It is a shared data structure (usually a Python TypedDict or a Pydantic model) that is passed continuously throughout the entire graph.

  • Every node reads the current state.
  • Every node executes its logic.
  • Every node returns a partial update to the state.

Reducers: State fields use "reducers" to define how updates are handled. If a node outputs a new value, does it overwrite the old value (like updating a final_answer string), or does it append to it (like adding a new message to a chat_history list)? LangGraph manages this merging process automatically.

Primitive 2: Nodes

Nodes are the "workers" in your graph. A node is simply a Python function (or a LangChain object) that receives the current State, performs an action (like calling an LLM or querying a database), and returns an updated piece of the State.

  • Agent Nodes: Nodes that use an LLM to make decisions.
  • Tool Nodes: Nodes that execute functions (like running Python code or calling a weather API).

Primitive 3: Edges

Edges are the roads connecting the nodes. They define the execution flow.

  • Normal Edges: Unconditional transitions. (e.g., START always goes to the Planner node).
  • Conditional Edges: The decision-makers. A conditional edge uses a routing function to look at the current State and decide where to go next.
    • Example: If the state says tool_call_required = True, the edge routes to the Tool Node. If it says False, the edge routes to the Final Answer Node. This is how loops are formed.

Primitive 4: StateGraph

The StateGraph is the builder class. It is the canvas where you register your State schema, add your Nodes, draw your Edges, and compile them into a running application.

Primitive 5: Checkpointers (Persistence and Memory)

By default, graphs execute in memory. Checkpointers allow you to save the graph's state to a database (like PostgreSQL or SQLite) after every single node execution.

This unlocks enterprise-grade features:

  • Fault Tolerance: If a server crashes mid-execution, the graph can resume exactly where it left off.
  • Time Travel: Because every state change is saved, you can rewind the graph to a previous step, change a variable, and fork the execution from that point.
  • Cross-Session Memory: The graph remembers conversations from days or weeks ago.

Advanced Pattern: Human-in-the-Loop (HITL)

One of LangGraph's most powerful enterprise use cases is the ability to pause execution. You rarely want an autonomous agent executing a SQL DROP TABLE command or sending a mass email to clients without oversight.

Because of Checkpointers, you can set a breakpoint before a specific node.

  1. The graph executes until it hits the Send_Email node.
  2. The graph interrupts itself, saving its current State to the database, and goes to sleep.
  3. A human reviews the drafted email in a UI.
  4. The human clicks "Approve."
  5. An API call resumes the graph, picking up the exact state and completing the task.

LangChain vs. LangGraph

While both frameworks are developed by the same team and work together seamlessly, they are built for fundamentally different application architectures.

To choose the right tool, you must understand how they handle control flow, state, and execution paths.

Core Differences at a Glance

  • LangChain (Linear Chains): Designed for Directed Acyclic Graphs (DAGs). Data flows in one direction. Even if an agent makes a decision, the framework is optimized for a sequence of steps that moves forward toward a conclusion.

  • LangGraph (Cyclic Graphs): Designed for Cyclic Graphs. Data can flow in loops. A node can route execution back to a previous node indefinitely until a specific condition is met, making it a true state machine.

In-Depth Comparison Matrix

Feature LangChain (Chains / LCEL) LangGraph
Primary Architecture Linear pipelines (Step A Step B Step C) Network of nodes with loops and cycles
State Management Passed implicitly from step to step; memory is usually a list appended to the prompt. Centralized, explicit State object updated via Reducers by any node.
Control Flow Hardcoded or managed by basic conditional chains. Dynamic routing via Conditional Edges based on the current state.
Persistence & Fault Tolerance Requires custom implementation to save intermediate states. Built-in Checkpointers save state at every node automatically.
Human-in-the-Loop Difficult to pause and resume mid-chain. Native support for Breakpoints to pause, inspect, edit, and resume.
Multi-Agent Systems Requires complex custom orchestration logic. Native; each agent can be represented as an independent node.

LangChain Flow

LangGraph Flow

Architectural Guideline: When to Use Which?

Use LangChain when:

  1. You are building a standard Retrieval-Augmented Generation (RAG) pipeline (Fetch docs Augment prompt Generate answer).
  2. Your workflow follows a strict, predictable sequence of data transformation steps.
  3. You want to quickly wrap an LLM API with simple prompt formatting and output parsing.

Use LangGraph when:

  1. Your agent needs to perform iterative correction (e.g., Code generation Execute tests If failed, pass errors back to coder Repeat).
  2. You are building a complex Multi-Agent Team where an execution planner passes tasks to a researcher, who passes findings to a writer, who can send it back to the researcher for revisions.
  3. You need enterprise-grade Human-in-the-Loop checkpoints for manual approval gates.