LangGraph is a Python framework for building agents as state graphs. Instead of a linear chain of calls, you define nodes, edges and conditions. This allows loops, branching and state recovery, which are essential for agents that need to iterate and self-correct.

LangGraph in practice: how to structure cyclic graphs and checkpoints for agents
LangGraph is a Python framework for agents as state graphs. See how to build graphs and use checkpoints.
Marcelo Santiago
Criador do bridgecode
Get AI articles in your inbox
Technical curation on models, agents and coding tools. No spam, unsubscribe anytime.
If you have already built an agent that repeats the same call without progress or that loses context when it hits an error, LangGraph tackles that problem. It treats the agent as a state machine, with checkpoints that allow you to pause, inspect and resume.
The langchain-ai/langgraph repository has more than 44 thousand stars, MIT license and was updated on November 8, 2026. It is part of the LangChain ecosystem, but can be used independently.
In this article you will see:
- What is a state graph and why it helps agents
- How to install and create a simple graph
- How to use checkpoints to recover state
- When to use LangGraph and when a linear chain is enough
- Limits and best practices
What is LangGraph and how does the state graph work
An agent based on a linear chain executes steps in fixed order. If a step fails, the chain breaks. If the agent needs to go back and try again, you need to reimplement retry logic manually.
LangGraph models the agent as a graph. Each node is a function that receives the current state and returns an update. Edges define the execution order, including conditions. A node can lead back to a previous node, creating a controlled cycle.
The state is a shared object, usually a TypedDict or a Pydantic class. Each node reads and writes fields of that state. This makes the flow explicit and inspectable.
This approach is useful for agents that need to plan, execute, verify and replan. The plan-execute-verify cycle is natural in a graph, but awkward in a linear chain.
How to install LangGraph and create a simple graph
Installation is straightforward. The main package is langgraph.
pip install langgraphThen, define the state and the nodes.
from typing import TypedDict
from langgraph.graph import StateGraph, END
class Estado(TypedDict):
entrada: str
saida: str
def processar(estado: Estado) -> Estado:
return {"saida": estado["entrada"].upper()}
grafo = StateGraph(Estado)
grafo.add_node("processar", processar)
grafo.set_entry_point("processar")
grafo.add_edge("processar", END)
app = grafo.compile()
resultado = app.invoke({"entrada": "olá"})
print(resultado)The compile method generates an executable application. The invoke method runs the graph with the initial state. You can use stream to follow each step, which helps with debugging.
If you already use LangChain, you can reuse models and tools. LangGraph does not require LangChain, but the integration is natural.
How to use checkpoints to recover state
Checkpoints are LangGraph's differentiator. At each step, the framework can save the state to a store, such as SQLite or PostgreSQL. If the process crashes, you resume from the last checkpoint instead of starting over.
This is essential for long-running agents, such as those that process large documents or run background tasks. It also facilitates auditing, because you can inspect the state at any point.
To enable checkpoints, pass a checkpointer when compiling the graph.
from langgraph.checkpoint.sqlite import SqliteSaver
memoria = SqliteSaver.from_conn_string("checkpoints.db")
app = grafo.compile(checkpointer=memoria)
config = {"configurable": {"thread_id": "1"}}
resultado = app.invoke({"entrada": "olá"}, config=config)The thread_id identifies the execution. With it, you can resume later or inspect the state history.
When to use LangGraph and when a linear chain is enough
Not every agent needs a graph. If the task is a fixed sequence of calls, such as summarizing a text and then translating it, a linear chain is simpler and faster to implement.
Use LangGraph when the agent needs to iterate, decide different paths or recover from failures. Typical cases include research agents that refine search, code agents that test and fix, and flows with human approval in the middle.
If you need persistent memory between executions, combine LangGraph with a memory layer. See Mem0: how to give persistent memory to AI agents. If the task involves a browser, see browser-use: how to connect AI agents to the browser.
Limits and best practices
Graphs add complexity. A poorly designed graph can enter an infinite loop or produce inconsistent states. Set iteration limits and validate the state at each node.
Another point is debugging. Graphs are harder to debug than linear chains. Use stream and logging to track each transition. The official documentation includes visualization tools that help.
Best practices that apply to any graph based agent:
- Keep the state small and focused
- Validate inputs and outputs at each node
- Set a maximum iteration limit
- Use checkpoints in production, not just in development
- Test each node in isolation before composing the graph
If you coordinate multiple agents, each one can be a subgraph with its own state. On bridgecode you assemble this squad and follow each step, from planning to delivery.
Frequently asked questions about LangGraph
Is LangGraph free?
Yes. It is open source under an MIT license. Cost comes from the model provider and checkpoint infrastructure.
Do I need to use LangChain to use LangGraph?
No. LangGraph can be used independently. Integration with LangChain is optional and makes it easier to reuse models and tools.
Does LangGraph replace LangChain?
No. They complement each other. LangChain provides components and integrations. LangGraph provides graph based orchestration. You can use both together.
How to avoid infinite loops in a graph?
Set an iteration limit in the state and check it at each node. LangGraph also lets you configure recursion limits when invoking the graph.
Does it work with local models?
Yes, as long as you use a compatible model. Ollama and vLLM work when integrated via LangChain or directly.
Frequently asked questions about LangGraph
Is LangGraph free?
Yes. It is open source with an MIT license. Costs come from the model provider and checkpoint infrastructure.
Do I need to use LangChain to use LangGraph?
No. LangGraph can be used independently. Integration with LangChain is optional and makes it easier to reuse models and tools.
Does LangGraph replace LangChain?
No. They complement each other. LangChain provides components and integrations. LangGraph provides graph based orchestration. You can use both together.
How to avoid infinite loops in a graph?
Set an iteration limit in the state and check it in each node. LangGraph also lets you configure recursion limits when invoking the graph.
Does it work with local models?
Yes, as long as you use a compatible model. Ollama and vLLM work when integrated via LangChain or directly.
Conclusion
Agents that iterate and recover from failures need more than a linear chain. LangGraph delivers this capability with a clear API and a permissive license. Start with a two node graph, add conditionals and enable checkpoints when moving to production.
If you coordinate multiple agents, each one can be a subgraph. On bridgecode you assemble this squad and follow each step, from planning to delivery.
Source: langchain-ai/langgraph
Related articles

Pydantic AI: How to Build Type-Safe Agents in Python
Pydantic AI is a Python framework for AI agents with strong typing. See how to install and validate results.

browser-use: how to connect AI agents to the browser
browser-use is a Python library that lets AI agents control a real browser. See how to install and use it.

Mem0: How to Give Persistent Memory to AI Agents
Mem0 is an open source memory layer for AI agents. See how to install it, save facts and retrieve context in Python.
