LangGraph: How to Build an Agent (ReAct + Tools)
Updated 2026-11-10
LangGraph is a graph-based runtime for agents: you define state, nodes, and edges, and the framework runs a tool-calling loop with checkpointing when you add it later. This guide builds the fastest path, a ReAct-style agent with create_react_agent, so you can compare it to the raw loop in Build your first AI agent in Python. LangChain’s newer create_agent API sits on LangGraph too; we start with the LangGraph prebuilt named in the keyword. Last tested 2026-11-10.
Prerequisites
- Python 3.10+
- An OpenAI API key or another chat model supported by LangChain
Docs used:
Steps
1. Install LangGraph and a chat model package
python -m venv .venv && source .venv/bin/activate
pip install langgraph langchain-openai
export OPENAI_API_KEY="sk-..."
langgraph bundles langgraph-prebuilt; do not install langgraph-prebuilt separately.
2. Define a tool as a plain Python function
# langgraph_agent.py
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
def get_weather(city: str) -> str:
"""Return a fake forecast for demo purposes."""
return f"It's sunny and 72°F in {city}."
tools = [get_weather]
model = ChatOpenAI(model="gpt-4.1-mini")
Typed arguments and docstrings become the tool schema, matching the pattern in the prebuilt README.
3. Build the agent graph
app = create_react_agent(model, tools)
create_react_agent compiles a graph that alternates between model and tool nodes until the model answers without tool calls.
4. Invoke with messages
result = app.invoke(
{"messages": [{"role": "user", "content": "What's the weather in Austin?"}]},
config={"recursion_limit": 12},
)
print(result["messages"][-1].content)
recursion_limit caps graph steps. If you hit it, read Fix: LangGraph recursion limit.
5. Stream tokens (optional)
for event in app.stream(
{"messages": [{"role": "user", "content": "Weather in Oslo?"}]},
config={"recursion_limit": 12},
):
print(event)
Streaming shows each node update, which helps debug tool loops.
6. Add memory or deploy later
- Memory / checkpoints: follow LangGraph persistence docs and pair with Fix: LangGraph recursion limit when loops grow.
- Hosted tools: wrap MCP with Connect an AI agent to an MCP server.
- Deploy: containerize with How to deploy an AI agent with Docker.
LangGraph v1 notes that create_react_agent is deprecated in favor of LangChain’s create_agent. When you migrate, change the import to from langchain.agents import create_agent and rename prompt= to system_prompt= per the migration guide.
Common errors
ModuleNotFoundError: langgraph: Install langgraph in the same virtualenv you run from.
GraphRecursionError / recursion limit: Raise recursion_limit slightly or reduce tools. See Fix: LangGraph recursion limit.
Model never calls tools. The user question may not require them, or the docstring is vague. Add a system message via LangGraph’s prompt / state_modifier options, or switch to create_agent with an explicit system_prompt.
OpenAI auth errors. Export OPENAI_API_KEY or configure the model provider package you chose.
Deprecation warnings for create_react_agent. Expected on LangGraph v1. Plan a move to langchain.agents.create_agent when you adopt LangChain v1 across the project.
FAQ
How do I build an agent in LangGraph?
Install langgraph, define Python tools, call create_react_agent(model, tools), and invoke with a messages list. Cap steps with recursion_limit in the config.
LangGraph vs the hand-written Python loop?
The hand-written loop in Build your first AI agent in Python shows every step. LangGraph compiles the same pattern into a graph you can checkpoint, stream, and extend.
Should I use create_react_agent or create_agent?
New LangChain v1 projects should use create_agent. This guide uses create_react_agent because it is the LangGraph-native API named in many existing tutorials and matches the keyword “langgraph how to build an agent.”
Where does human-in-the-loop fit?
Add LangGraph interrupts after the basic graph works. See LangGraph: human in the loop for interrupt() and Command(resume=...).