Build a LangGraph ReAct agent with LangChain tools
LangChain’s agents now lean on LangGraph, a lightweight orchestration layer that compiles your agent into a graph. This starter walks through the modern stack—using `create_react_agent`, LangChain tools, and the new agent state patterns—then lets you run the finished graph directly from the page.
What’s new in LangChain agents (LangGraph era)
- Graph-first orchestration: `create_react_agent` returns a compiled LangGraph you can run locally, host remotely, or extend with supervisor nodes.
- ToolNode integration: Tools plug into the graph via `ToolNode` so argument validation and interruptions happen before execution.
- Structured state: `AgentState` keeps messages, structured responses, and interrupts consistent across runs.
Prerequisites
- Python 3.10+
- `pip install langchain==0.2.11 langchain-openai==0.1.14 langchain-community==0.2.9 langgraph==0.2.32`
- `export OPENAI_API_KEY=sk-…`
Starter project: Market expansion advisor
We’ll build a ReAct-style agent that recommends market launch plans. The agent uses a simple knowledge base stored in code, so you can run everything locally.
Define the reusable tool
from langchain.tools import tool
MARKET_FACTS = {
"brazil": {
"demand": "Fast-growing premium delivery segment in São Paulo and Rio",
"competitors": ["Mercado Livre", "Magazine Luiza"],
"regulations": "ANATEL registration required for electronics imports",
"support": "Portuguese 24/7 coverage expected",
},
"germany": {
"demand": "Strong repeat purchase loyalty programs",
"competitors": ["Zalando", "Otto"],
"regulations": "BfDI expects explicit opt-ins for marketing data",
"support": "German + English, business hours acceptable",
},
}
@tool
def market_facts(country: str) -> str:
"""Summaries demand, competitors, regulation, and support notes for a country."""
data = MARKET_FACTS.get(country.lower())
if not data:
return f"No entry for {country}."
lines = [
f"Demand: {data['demand']}",
f"Competitors: {', '.join(data['competitors'])}",
f"Regulations: {data['regulations']}",
f"Support: {data['support']}",
]
return "\n".join(lines)
Compile the LangGraph agent
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
app = create_react_agent(model, [market_facts])
Invoke the graph
result = app.invoke({"messages": [
("human", "Outline a six-month launch plan for Germany."),
]})
for msg in result["messages"]:
print(msg.type.upper(), "::", msg.content)
Complete script
# langgraph_agent.py
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
MARKET_FACTS = {
"brazil": {
"demand": "Fast-growing premium delivery segment in São Paulo and Rio",
"competitors": ["Mercado Livre", "Magazine Luiza"],
"regulations": "ANATEL registration required for electronics imports",
"support": "Portuguese 24/7 coverage expected",
},
"germany": {
"demand": "Strong repeat purchase loyalty programs",
"competitors": ["Zalando", "Otto"],
"regulations": "BfDI expects explicit opt-ins for marketing data",
"support": "German + English, business hours acceptable",
},
}
@tool
def market_facts(country: str) -> str:
data = MARKET_FACTS.get(country.lower())
if not data:
return f"No entry for {country}."
lines = [
f"Demand: {data['demand']}",
f"Competitors: {', '.join(data['competitors'])}",
f"Regulations: {data['regulations']}",
f"Support: {data['support']}",
]
return "\n".join(lines)
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
app = create_react_agent(model, [market_facts])
if __name__ == "__main__":
result = app.invoke({"messages": [("human", "Outline a six-month launch plan for Brazil.")]})
final = result["messages"][-1]
print("\n=== PLAN ===\n", final.content)
Customize the fact base, add more tools, or route multiple tools through `ToolNode` for validation before execution.
Preview a session transcript
Thought: I should inspect the Brazil entry.
Action: market_facts("brazil") Demand: Fast-growing premium delivery segment in São Paulo and Rio
Competitors: Mercado Livre, Magazine Luiza
Regulations: ANATEL registration required for electronics imports
Support: Portuguese 24/7 coverage expected Drafted six-month plan with localization, loyalty pilot, compliance track, and national launch marketing. Run the live demo
Click the button below to run the Brazil scenario through a LangGraph agent on the server. If credentials are missing, the interface falls back to the saved transcript.
Operational checklist
- Model selection: use LangGraph runtime context to swap models per request.
- Observability: capture graph events with LangSmith or OpenTelemetry for replay.
- Human-in-the-loop: wire `HumanInterrupt` to approvals when agents need human confirmation.
"Treat agents as graphs: every tool, condition, and hand-off becomes explicit—and much easier to debug."
Review the LangGraph agent reference for details on `AgentState`, `ToolNode`, and advanced supervisor patterns.