Docs / LangGraph

LangGraph

LangGraph is a library for building multi-step, agentic applications, modeling the execution flow as a state graph instead of a simple linear chain.

State Graph Cycles By LangChain

What Is LangGraph?

LangGraph is built on the same LangChain components (model, prompt, tool), but instead of composing them linearly with |, it lets you define the execution flow as a graph of nodes and edges — including loops, conditional branches, and returning to previous steps. This is exactly what's needed to actually implement the ReAct pattern.

Why a Graph Instead of a Linear Chain?

A linear chain assumes step A always goes to B and then to C. But a real agent's behavior isn't like that: after calling a tool, the result might be insufficient and the agent needs to go back to the "decide" step (a loop), or depending on the type of user request, an entirely different path might be taken (a conditional branch). A graph models these patterns naturally.

Core Concepts

  • State — a shared object that's passed between and updated by all nodes (like the graph's working memory)
  • Node — a processing unit (a Python function or a LangChain chain) that reads the State and returns an updated version
  • Edge — the path between two nodes; it can be fixed or conditional (deciding the next node based on the State)

Diagram of a Sample Graph

START Reason Decide next step Needs tool Tool Call MCP / UCP Return to Reason with new result Final answer ready END

Code Sample

from langgraph.graph import StateGraph, END

def reason(state):
    # The model decides: final answer or call a tool
    return {"next": "tool" if state["needs_tool"] else END}

def call_tool(state):
    result = tools[state["tool_name"]](**state["tool_args"])
    return {"last_result": result, "needs_tool": False}

graph = StateGraph(dict)
graph.add_node("reason", reason)
graph.add_node("tool", call_tool)
graph.add_conditional_edges("reason", lambda s: s["next"])
graph.add_edge("tool", "reason")
graph.set_entry_point("reason")

app = graph.compile()
app.invoke({"needs_tool": True, "tool_name": "check_inventory", "tool_args": {"sku": "123"}})

Persistence, Checkpointing & Human-in-the-loop

One of LangGraph's distinguishing features is automatically saving the State after every node runs (a Checkpoint). This means a graph's execution can be paused midway and later resumed — even after a full application restart — from exactly that same point, because the entire State is stored in a database (like Postgres or Redis), not just in application memory.

This mechanism has two important uses:

  • Human-in-the-loop — the graph can deliberately pause before a sensitive action (like a final payment), wait for human approval, and resume from exactly that point after approval — without losing any of the previous steps.
  • Error recovery — if the server crashes in the middle of a long-running execution, once it's back up, execution resumes from the last successful checkpoint, not from zero.

LangGraph vs. Plain LangChain

LangChain (LCEL)LangGraph
Flow structureLinear (Pipe)Graph (Node/Edge)
Loops & returnsLimited supportFull support, a core design goal
Best suited forSimple pipelines and standard RAGMulti-step and multi-agent agents

FAQ

Do I need to learn LangChain first to understand LangGraph?

Knowing the basic concepts (Model, Prompt) helps, but LangGraph can also be learned independently.

Is LangGraph also used for multi-agent systems?

Yes, that's exactly one of its most common uses — each agent is a subgraph or node, and one orchestrator node defines the path between them. Details in Agent Orchestration.