Design stateful, multi-step Agent workflows as graphs with LangGraph. Conditional branching, loops, and Human-in-the-loop approval β build the complex Agent logic that plain chains struggle with. Rather than listing concepts, this guide is organized so you can follow the content in the order you have to make decisions on a real project.
Key perspective
AI / LLM systems
Look beyond the model and the prompt: connect the data flow, evaluation, and post-deployment operating metrics in one view.
Stateful Agent workflowsConditional branching and iterative reasoningHuman-in-the-loop approvalMulti-step pipelines
Architecture diagrams
To make what you read stick, it helps to capture the flow as a picture first. The two diagrams below are reference maps you can keep coming back to while learning LangGraph.
Learning flow
Rendering diagramβ¦
Architecture view
Rendering diagramβ¦
Why LangGraph
When you first open LangGraph, the big picture matters more than individual commands. This section starts with the problems the concepts ahead were created to solve.
A LangChain chain is a linear structure that runs only in a fixed order such as AβBβC. A real Agent, though, has to decide for itself "whether to use a tool" and "how many more times to think". LangGraph expresses that flow as a graph of nodes (tasks) and edges (transition rules), which makes workflows with branching, loops, and pauses come naturally.
Aspect
LangChain chain
LangGraph
Execution flow
A fixed linear order
A graph that branches and loops on conditions
State management
Passed implicitly between chain calls
Shared through an explicit State schema
Pause/resume
Difficult
Can stop at a specific point and resume with a checkpoint
Good fit
Simple sequential pipelines
ReAct loops, multi-agent, workflows that need approval
StateGraph basics
Here you look at StateGraph basics alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
The heart of LangGraph is the StateGraph. You define a State schema shared by the whole graph, and each node is a function that reads and updates that state.
basic_graph.pyPYTHON
from typing import TypedDict, Annotatedfrom langgraph.graph import StateGraph, START, ENDimport operatorclass AgentState(TypedDict): messages: Annotated[list, operator.add] # each node appends to the list step_count: intdef call_model(state: AgentState) -> dict: # In reality an LLM call β only the concept is shown here return {"messages": ["model response"], "step_count": state["step_count"] + 1}def call_tool(state: AgentState) -> dict: return {"messages": ["tool result"], "step_count": state["step_count"] + 1}graph = StateGraph(AgentState)graph.add_node("model", call_model)graph.add_node("tool", call_tool)graph.add_edge(START, "model")graph.add_edge("model", "tool")graph.add_edge("tool", END)app = graph.compile()result = app.invoke({"messages": [], "step_count": 0})print(result["step_count"])
Conditional branching
Here you look at Conditional branching alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
add_conditional_edges lets you choose the next node dynamically from the current state. This is how the pattern is implemented where the Agent decides for itself "whether to use another tool or give the final answer".
conditional_routing.pyPYTHON
def should_continue(state: AgentState) -> str: last_message = state["messages"][-1] if "final_answer" in last_message: return "end" if state["step_count"] >= 5: return "end" # upper bound to prevent an infinite loop return "continue"graph.add_conditional_edges( "model", should_continue, {"continue": "tool", "end": END},)
Loops (ReAct)
Here you look at Loops (ReAct) alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Creating a cycle of model node β tool node β back to the model node expresses the ReAct pattern, which repeats "thought β action β observation", as a graph.
PYTHON
graph.add_edge("tool", "model") # back to the model after the tool runs β forms the cycle# model --(more needed)--> tool --> model --(done)--> END# β___________________________|
Human-in-the-loop
Here you look at Human-in-the-loop alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Actions that are hard to undo, such as payments, deployments, and deleting data, should go through human approval. With interrupt_before/interrupt_after, LangGraph can pause the graph before or after a specific node runs.
PYTHON
app = graph.compile(interrupt_before=["tool"]) # always stop before running a toolstate = app.invoke({"messages": [], "step_count": 0})# The graph stops here β it resumes once a person reviews and approvesif input("Approve the tool run? (y/n) ") == "y": result = app.invoke(None) # continue from where it stopped
Persisting state
This section covers Persisting state from a practical perspective. Instead of memorizing the concept, focus on the situations in which you would reach for it.
Attaching a Checkpointer saves the state of the graph to a DB, so even if the conversation is cut off it can continue from the same point later β essential for long-running Agents and multi-turn conversations.
LangGraph: frequently asked questions
What is LangGraph and when do I need it?
LangGraph is a library that defines an agent's execution flow as a stateful graph. You need it for flows that branch on conditions, repeat a step, or save intermediate state and resume later. A simple chain that runs in one direction does not need it.
What is State in LangGraph?
State is the shared data every node in the graph reads and updates. Each node receives the current state and returns only the part it changes, and the graph merges that and passes it to the next node. Conversation messages, intermediate results, and the values that decide the next path all live there.
How do I keep an agent from looping forever?
Set a maximum number of iterations on the graph run and add a node that checks a clear termination condition on every loop. Detecting repeated calls to the same tool with the same arguments and stopping is also effective. Adding cost and time limits bounds the damage when an unexpected loop does happen.