LangChain vs LangGraph Practical Guide: How to Choose Your Agent Framework, Explained Clearly
Starting from real project needs, telling you which framework to use
LangChain was almost synonymous with AI application development in 2023. But by 2026, many have migrated from LangChain to LangGraph—because LangGraph is better suited for building truly production-grade Agents.
How do you choose between these two frameworks?
1. Core Positioning Differences
LangChain: A toolbox for AI application development
LangGraph: A stateful Agent workflow framework
Simply put: LangChain helps you start quickly, LangGraph helps you get to production.
2. Code Comparison: Same Task, Two Implementations
Task: Automatically choose between search or calculator tools based on user question
#### LangChain Implementation (Concise but less control)
python
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain.tools import tool@tool
def search(query: str) -> str:
"""Search the internet for the latest information"""
return web_search(query)
@tool
def calculator(expression: str) -> str:
"""Calculate a mathematical expression"""
return eval(expression)
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, tools=[search, calculator], prompt=...)
executor = AgentExecutor(agent=agent, tools=[search, calculator])
result = executor.invoke({"input": "What is Apple's latest stock price, and what is it times 100?"})
#### LangGraph Implementation (Complex but controllable)
python
from langgraph.graph import StateGraph, END
from typing import TypedDict, Listclass AgentState(TypedDict):
messages: List[dict]
tool_calls: List[dict]
final_answer: str
def call_llm(state: AgentState) -> AgentState:
"""Call LLM to decide next step"""
response = llm.invoke(state["messages"])
return {"messages": state["messages"] + [response]}
def execute_tools(state: AgentState) -> AgentState:
"""Execute tool calls"""
results = []
for tool_call in state["messages"][-1].tool_calls:
result = tools[tool_call["name"]].invoke(tool_call["args"])
results.append({"role": "tool", "content": result})
return {"messages": state["messages"] + results}
def should_continue(state: AgentState) -> str:
"""Decide whether to continue calling tools or end"""
last_message = state["messages"][-1]
if last_message.tool_calls:
return "tools"
return END
Build the graph
graph = StateGraph(AgentState)
graph.add_node("llm", call_llm)
graph.add_node("tools", execute_tools)
graph.add_edge("tools", "llm")
graph.add_conditional_edges("llm", should_continue)
graph.set_entry_point("llm")app = graph.compile()
result = app.invoke({"messages": [{"role": "user", "content": "..."}]})
LangGraph has more code, but you have complete control over every step.
3. When to Choose Which
Choose LangChain when
Choose LangGraph when
Recommended combination in 2026
4. LangGraph Core Concepts
State
python
class MyState(TypedDict):
messages: List[BaseMessage] # conversation history
next_step: str # what to do next
attempts: int # retry count
result: Optional[str] # final result
State is the most core concept in LangGraph—the entire Agent execution revolves around state changes.
Conditional Edges
python
def route(state) -> str:
if state["attempts"] > 3:
return "give_up"
elif state["has_tool_calls"]:
return "execute_tools"
else:
return "end"graph.add_conditional_edges("llm_node", route)
Conditional edges allow you to implement complex branching logic, clearer than simple if-else.
5. Persistence and Resume from Checkpoints
LangGraph supports saving intermediate state to a database, allowing the Agent to be interrupted and resumed:
python
from langgraph.checkpoint.sqlite import SqliteSaverSave checkpoints to SQLite
memory = SqliteSaver.from_conn_string("checkpoints.db")
app = graph.compile(checkpointer=memory)First run
thread = {"configurable": {"thread_id": "task_001"}}
result = app.invoke(input_data, config=thread)Continue later (from where it left off)
result = app.invoke(None, config=thread)
This is crucial for long-running Agents (e.g., automated research, batch processing).
Further Reading
Also available in 中文.