How to Build AI Agents With LangGraph: A Practical Guide
LangGraph turns an AI agent into a graph of steps with shared state, so you can control decisions, retry failures and pause for review. Here is how to build one.
On this page
Short answer
A LangGraph agent is an AI agent built as a graph, where each node does one job, edges decide which node runs next, and a shared state object carries the working data between steps. That structure gives you control over multi-step work that a single prompt cannot handle.
What is LangGraph?
LangGraph is an open-source framework from the makers of LangChain for building agents as graphs of steps, where each node is a piece of work and each edge is a decision about what happens next. A shared state object travels through the graph, so every node can read what happened before and add its own result.
The graph idea matters because agents do multi-step work, and multi-step work needs control. A plain chain runs the same steps in the same order every time, which breaks when a tool call fails, a result needs checking, or a human should approve a step. A graph lets the agent branch, loop back, retry and stop, while the state keeps the full picture.
LangGraph works with models from several providers, and it runs in Python and JavaScript. It is the same framework behind many production agents, because teams can inspect the state at any node and replay a run from a saved point. For a team that needs to see exactly what an agent did, that visibility is the main reason to pick it. See LangChain for language models for how the wider framework fits together.
How does a LangGraph agent work?
A LangGraph agent works by moving a state object through a graph: a node runs, updates the state, and an edge picks the next node based on the new state. The loop repeats until the graph reaches an end node, which returns the final answer.
Four parts make up most agents. The table below shows what each part does.
| Part | What it does | Example |
|---|---|---|
| State | Holds the working data | Messages, facts, flags |
| Node | Does one job | Call a model, run a tool |
| Edge | Chooses the next node | Retry, approve, finish |
| Checkpointer | Saves state after each step | Resume after a failure |
The checkpointer is the part teams notice most in production. Because state is saved after every node, a run can pause for a human decision and resume later, or restart from the last good step after a crash instead of from the beginning. That makes long-running agents practical: a research agent that takes twenty minutes can survive an interruption without losing its work.
What do you need before you start?
Before you start, you need a clear task, a model with an API key, and the tools the agent will call. The task should be narrow: one job with a visible result, such as triaging support requests or drafting a weekly report from your own data.
Pick the model first, then the tools. The model needs reliable tool calling, because the agent will call tools many times in one run. The tools should be narrow and safe: a search tool, a database lookup, a draft writer. Each tool needs a plain description of what it does and what it returns, because the model reads that description to decide when to use it.
You also need a way to watch runs. LangGraph can log each step and its state, and that log is how you debug an agent that goes wrong. Set up logging before the first real run, not after a failure.
A small test set of real requests with known good answers is also worth preparing, so you can check each change against the same cases. The multi-agent systems guide covers how several agents share work when one agent is not enough.
How do you build a LangGraph agent, step by step?
You build a LangGraph agent by defining the state, adding one node at a time, wiring the edges, and testing each step. Small, working steps beat a large graph drawn up front.
- Define the state. List the fields the agent needs: messages so far, facts found, and a flag for whether the work is done. Keep the state small.
- Build the first node. Start with a single model call that reads the state and writes a draft answer. Test it on real requests before adding anything else.
- Add one tool node. Give the agent one tool, such as a search over your own documents, and a node that runs it. Check that the agent calls it only when the task needs it.
- Add the routing edge. Write the rule that picks the next step: if the model asked for a tool, run the tool node; if it gave a final answer, end the run.
- Add a checkpointer. Save state after each node so runs can pause, resume and be replayed for debugging.
- Add a human review step. Route risky actions, such as sending a message or changing a record, through a node that waits for approval.
- Test on the full set. Run the test cases, read the traces, and fix the worst failure before adding more nodes.
Resist the urge to add every feature in the first version. A graph with three nodes that works is worth more than one with twelve nodes that nobody can debug.
How do you add tools, memory and human review?
You add tools by writing small functions with clear descriptions, add memory by keeping state in the checkpointer, and add human review by routing a step through a pause node. Each of these is a separate concern, and keeping them separate makes the agent easier to change.
Tools should do one thing and report errors plainly. A tool that searches your help centre should return the matching articles or say that nothing matched, not a long error message the model must interpret. Give each tool a name and description the model can understand, and keep the number of tools small: an agent with four good tools beats one with twenty overlapping ones.
Memory in LangGraph comes in two forms. Short-term memory is the state itself, which travels with the run. Longer memory, such as facts about a customer, lives in a store the agent reads through a tool.
Human review is an interrupt: the graph pauses before a chosen node, a person approves or edits the pending action, and the run resumes. That pattern is how teams ship agents that take real actions without giving the model unchecked access. For the business case, AI agent development covers costs and team roles.
Example (hypothetical): a support triage agent
Example (hypothetical): a software company receives 200 support requests a day, and a support lead spends about 8 minutes reading and routing each one. That is 200 times 8 minutes, or 1,600 minutes, which is about 27 hours of work a day across the team.
The team builds a LangGraph agent with four nodes: read the request, search the help centre, draft a reply, and route it to the right queue. The agent handles 150 of the 200 requests, and each of those needs 2 minutes of human review, which is 300 minutes.
The other 50 requests go straight to a person at 8 minutes each, which is 400 minutes. The total is 700 minutes, or about 12 hours, against 27 hours before.
The saving is about 15 hours a day, but the team keeps a person in the loop for every draft the agent writes. The checkpointer lets a reviewer pause any run, and the graph routes billing and account-deletion requests to a senior agent instead of drafting a reply. The point is not to remove people, but to spend their time on the requests that need judgement.
What are the common mistakes?
The most common LangGraph mistakes are building too many nodes too early, letting state grow without limit, and skipping the human review step for actions that matter.
- Too many nodes. A graph with a node for every tiny step is hard to follow. Combine small steps into one node and split only when a step needs its own retry or review.
- Unbounded state. Storing every message and tool result in state makes each step slower and more expensive. Keep only what later nodes need.
- No loop limits. An agent that retries forever can burn time and money. Set a maximum number of steps per run and a clear stop rule.
- Skipping human review. Letting an agent send messages or change records without approval is the fastest way to lose trust in the system.
- No evaluation set. Without fixed test cases, every change to the graph is a guess, and regressions slip in quietly.
- Vague tool descriptions. If the model cannot tell when to use a tool, it will use the wrong one or none at all.
Key takeaways
- LangGraph builds agents as graphs, so each step is visible and testable
- State, nodes, edges and a checkpointer are the four parts most agents need
- Start with one task, one node and one tool, then grow the graph
- Human review belongs on any step that sends a message or changes a record
- Loop limits and an evaluation set keep an agent from drifting
Frequently asked questions
What is the difference between LangGraph and LangChain?
LangChain is the wider framework for building applications on language models, with chains, prompts, retrieval and tools. LangGraph is the part of that family for building agents as graphs, with explicit control over state, branching and retries. Use LangChain for a fixed pipeline and LangGraph when the agent must decide its own path.
Do I need to know LangChain to use LangGraph?
It helps, because LangGraph shares ideas and tools with LangChain, such as models, prompts and tool definitions. You can learn LangGraph on its own, but a basic grasp of LangChain makes the first graph faster to build, since the model and tool setup is the same.
Can a LangGraph agent run for a long time?
Yes. The checkpointer saves state after each node, so a run can pause for hours while it waits for a human decision or an external event, then resume from the saved state. Long runs should still have a step limit and a timeout, so a stuck run does not run forever.
How many tools should an agent have?
Start with one to four tools that cover the task. Each extra tool makes the model's choice harder and the agent slower to reason about. Add a tool only when a real request shows the gap, and remove tools that the agent never calls.
When should I use LangGraph instead of a simpler agent framework?
Use LangGraph when the work has branches, retries or human approval steps that must be explicit, such as a support agent that routes, drafts and escalates. For a single agent with a few tools and no branching, a lighter framework is faster to build and easier to maintain.
How do I debug a LangGraph agent that goes wrong?
Read the trace of the run: which nodes ran, what the state held at each step, and what each tool returned. Then replay the run from the last good state with a fix in place. Most failures trace back to a vague tool description, a missing loop limit or state that grew too large.