LangChain vs LangGraph: Choosing Between Chains and Stateful Graphs
LangChain gives you linear chains; LangGraph adds stateful, branching workflows. Simple examples of each and a plain way to decide which your app needs.
This article is published in English.
If you're stepping into AI development, two names keep popping up everywhere:
LangChain and LangGraph.
At first glance they can feel interchangeable. Are they the same tool? Do you need to learn both? And how do you decide which one fits your project?
Let's break it down with a straightforward example.
What is LangChain?
Picture LangChain as a toolbox for assembling AI applications.
It hands you ready-made pieces that let you work with LLMs and wire them up to other resources like:
- Prompts
- LLMs
- Tools
- APIs
- Documents
- Vector databases
- Retrievers
Say you wanted to build a basic AI tutor.
The pipeline might look like this:
Student Question
↓
Prompt
↓
LLM
↓
Answer
LangChain makes assembling this kind of pipeline fairly painless.
Simple LangChain Example
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(model="gpt-4o-mini")prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful programming teacher."),
("human", "{question}")
])chain = prompt | llmresponse = chain.invoke({
"question": "What is recursion?"
})print(response.content)
The logic here is simple and linear:
Question → Prompt → LLM → Answer
This kind of straight-line pipeline is exactly what LangChain excels at.
What is LangGraph?
Now suppose your AI tutor needs to handle more than a single pass.
Rather than just spitting out an answer, you want it to:
- Answer the student's question.
- Evaluate whether that answer is actually good.
- If it's good, stop there.
- If it's not good, revise it.
- Re-check the revised answer.
The flow now looks more like this:
Student Question
↓
Generate Answer
↓
Check Answer
↙ ↘
Good Bad
↓ ↓
Finish Improve
↓
Check Again
This is precisely the kind of scenario LangGraph was built for.
LangGraph lets you design workflows that can include:
- Multiple steps
- Conditional branches
- Loops
- Shared state
- Decision points
- Human-in-the-loop approval
- Richer agent behavior
A Simple LangGraph Example
To start, you define the state your application needs to keep track of.
from typing import TypedDict
class State(TypedDict):
question: str
answer: str
good: bool
This state tracks three things:
question
answer
good
From there, you build out individual steps, called nodes, inside your graph.
Generate Answer
def generate_answer(state):
response = llm.invoke(
f"Answer this question simply:\n{state['question']}"
) return {
"answer": response.content
}
Check Answer
def check_answer(state):
response = llm.invoke(
f"""
Is this answer good? Question:
{state['question']} Answer:
{state['answer']} Reply only YES or NO.
"""
) return {
"good": response.content.strip().upper() == "YES"
}
Next, you define a decision function:
def decision(state):
if state["good"]:
return "finish" return "improve"
What matters here is that LangGraph is the one wiring these nodes together and deciding which node runs next.
If the check fails, the graph can loop back to the improvement node and re-run the check afterward.
So What's the Main Difference?
The simplest way to frame the distinction is this:
LangChain = Building Blocks
LangChain supplies the individual components you assemble an AI application from.
For instance:
LLM
Prompt
Retriever
Tool
Vector Database
↓
LangChain
LangGraph = Workflow
LangGraph governs how those components interact and in what order.
Start
↓
Generate
↓
Check
↙ ↘
Good Bad
↓ ↓
Finish Improve
↓
Check
Are LangChain and LangGraph Competitors?
Not really.
They're designed to be used side by side.
For example:
LangGraph
↓
┌──────────┴──────────┐
↓ ↓
LangChain LangChain
Retriever Tools
↓ ↓
└──────────┬──────────┘
↓
LLM
LangChain supplies the building blocks and tools, while LangGraph orchestrates the larger workflow around them.
When Should You Use LangChain?
LangChain is the right choice when your application follows a fairly linear path.
Typical cases include:
- A basic chatbot
- A RAG-based application
- Answering questions from a PDF
- Searching through documents
- A straightforward LLM-powered app
- Simple tool invocation
A basic flow here might look something like:
User
↓
Retrieve Information
↓
LLM
↓
Answer
For these kinds of setups, building out a full graph structure is usually overkill.
When Should You Use LangGraph?
LangGraph earns its place when your application requires branching logic and decisions, not just a fixed sequence of steps.
This includes situations like:
- Autonomous AI agents
- Workflows spanning multiple steps
- Systems with several cooperating agents
- Human-in-the-loop processes
- Flows that need to loop
- Apps requiring persistent state across steps
- Advanced tool-calling chains
As an example, imagine an AI tutor structured like this:
Student asks question
↓
Analyze level
↓
Generate explanation
↓
Give quiz
↓
Check answer
↓
Correct?
↙ ↘
YES NO
↓ ↓
Next topic Explain again
That kind of branching, stateful behavior is exactly where LangGraph shines.
The Easiest Analogy
Picture constructing a house.
LangChain acts as your toolbox.
It hands you things like:
Bricks, tools, materials and components.
LangGraph acts as the blueprint.
It defines the sequence:
Lay the foundation → raise the walls → add the roof → inspect the work → if something's off, go back and correct it.
In short:
LangChain helps you construct the pieces.
LangGraph helps you manage the process.
Final Takeaway
Rather than viewing LangChain and LangGraph as unrelated tools, it's more accurate to see them as two layers of the same stack.
AI Application
↓
LangGraph
(Workflow / Logic)
↓
LangChain
(Tools / Components)
↓
LLM
If your use case is simple, LangChain alone will likely cover your needs.
Once your application demands state tracking, conditional logic, loops, or intricate agent behavior, LangGraph becomes the better fit.
In many production systems, though, the two are combined.
The rule worth remembering:
LangChain builds the pieces. LangGraph connects and controls the flow.