LangChain vs LangGraph: A Practical Comparison Using Python
LangChain vs LangGraph in Python: agents, tools, state, persistence, human-in-the-loop and multi-agent orchestration, with practical code examples.
By Mudasser Tufail · · 20 min read
- Agentic AI
- LangChain
- LangGraph
- Python
- AI Agents
- Multi-Agent Systems
- LLM Engineering
Building an AI application is no longer just about sending a prompt to a Large Language Model (LLM) and displaying its response. Modern AI applications need to interact with external systems, execute tools, maintain conversation context, retrieve information, delegate tasks, and handle complex workflows.
As these requirements grow, developers need frameworks that provide more control over how AI applications operate.
Two important frameworks in the Python ecosystem are LangChain and LangGraph.
Both are part of the LangChain ecosystem, but they address different engineering requirements.
LangChain provides higher-level abstractions for building LLM applications and AI agents. It simplifies integrating models, tools, prompts, and middleware into an application.
LangGraph provides a lower-level orchestration framework for building stateful, multi-step workflows. It gives developers explicit control over execution flow, state transitions, persistence, and human intervention.
An important distinction is that LangChain's agent implementations use LangGraph primitives internally. Therefore, these frameworks are not necessarily competing alternatives. You can use LangChain for agent capabilities and LangGraph when you need more control over the overall workflow.
This article compares both frameworks from a practical engineering perspective using Python. We will explore their architectures, tools, agent execution, state management, multi-agent orchestration, persistence, human-in-the-loop, and production considerations.
By the end, you should understand when to use LangChain, when to use LangGraph, and when combining both makes more sense.
1. Understanding the Core Difference
Before comparing individual features, it is important to understand what each framework is designed to provide.
LangChain
LangChain is a framework for developing applications powered by Large Language Models.
It provides abstractions and integrations that help developers connect LLMs with external tools, data sources, and application logic.
Its capabilities include:
- Connecting to different LLM providers.
- Creating AI agents.
- Defining custom tools and functions.
- Managing prompts and messages.
- Integrating document loaders and retrievers.
- Building Retrieval-Augmented Generation (RAG) applications.
- Supporting middleware and structured outputs.
- Integrating with vector databases and external services.
One of its important features is create_agent(), which allows developers to create an agent by providing a model, tools, and instructions.
The framework manages the agent execution loop, including model calls and tool execution.
You can think of LangChain as a higher-level framework that simplifies building LLM-powered applications.
LangGraph
LangGraph is a low-level orchestration framework designed for building stateful and long-running AI workflows.
Instead of relying on a predefined agent execution loop, developers explicitly define how the application should operate.
It introduces three fundamental concepts:
- State: Shared information maintained throughout workflow execution.
- Nodes: Individual functions responsible for performing specific operations.
- Edges: Connections that define how execution moves between nodes.
For example, consider a customer support workflow:
- Receive a customer request.
- Classify the request.
- Retrieve relevant information.
- Generate a response.
- Decide whether human approval is required.
- Send the response.
Each operation can be implemented as a separate node, and the edges determine how the workflow moves between them.
LangGraph also provides capabilities such as:
- Stateful execution.
- Conditional routing.
- Checkpointing and persistence.
- Human-in-the-loop.
- Workflow streaming.
- Execution recovery.
- Short-term and long-term memory.
- Subgraphs and multi-agent orchestration.
You can think of LangGraph as an orchestration runtime that gives developers explicit control over how an AI workflow executes.
Architectural comparison
| Area | LangChain | LangGraph |
|---|---|---|
| Primary approach | Higher-level LLM and agent framework | Low-level workflow orchestration |
| Main abstraction | Models, tools, agents, middleware | State, nodes, and edges |
| Agent creation | create_agent() | Build an agent or workflow using graph primitives |
| Execution flow | Agent execution loop | Explicit graph-based execution |
| State management | Agent messages and runtime state | Shared graph state |
| Conditional routing | Agent decisions and middleware | Conditional edges and commands |
| Persistence | Agent capabilities backed by LangGraph | Checkpointers and stores |
| Human intervention | Middleware and agent integrations | Native interrupts |
| Multi-agent systems | Agent tools and integrations | Graphs, subgraphs, and explicit orchestration |
| Best suited for | LLM applications and configurable agents | Complex, stateful workflows |
The important distinction is not that LangChain cannot build complex agents or that LangGraph cannot build simple agents.
The difference is the level of control you want over the execution process.
2. Getting Started with Both Frameworks
Let's install the required Python packages and create a basic implementation using both frameworks.
For this comparison, we will use OpenAI as the model provider.
Installation
Install the required dependencies:
pip install -U langchain langchain-openai langgraph python-dotenv
Configure your OpenAI API key:
export OPENAI_API_KEY="your-api-key"
Alternatively, you can use a .env file:
OPENAI_API_KEY=your-api-key
LangChain: Creating a basic agent
LangChain provides create_agent() to create an agent with a model, instructions, and tools.
Let's create a simple weather assistant.
from langchain.agents import create_agent
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get weather information for a city."""
# Replace with a real weather API.
return f"The weather in {city} is sunny."
agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[get_weather],
system_prompt="You are a helpful weather assistant."
)
result = agent.invoke({
"messages": [
{
"role": "user",
"content": "What is the weather in Lahore?"
}
]
})
print(result["messages"][-1].content)
Let's understand the important components.
create_agent()
Creates an agent using the configured model, tools, and system instructions.
model
Defines which LLM the agent should use.
tools
Provides functions that the agent can invoke when required.
system_prompt
Defines the agent's behavior and responsibilities.
invoke()
Starts the agent execution. The framework manages the model and tool-calling loop until the agent produces a final response.
In this example, the model determines whether it needs to call get_weather() to answer the user's question.
You do not need to manually implement the complete tool-calling loop.
LangGraph: Creating a basic graph
Now let's implement a similar workflow using LangGraph.
Instead of defining an agent through a high-level helper, we will explicitly define the execution flow.
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class WeatherState(TypedDict):
city: str
weather: str
def get_weather(state: WeatherState):
city = state["city"]
# Replace with a real weather API.
weather = f"The weather in {city} is sunny."
return {
"weather": weather
}
def generate_response(state: WeatherState):
return {
"weather": state["weather"]
}
# Create the graph
workflow = StateGraph(WeatherState)
# Register nodes
workflow.add_node("get_weather", get_weather)
workflow.add_node("generate_response", generate_response)
# Define execution flow
workflow.add_edge(START, "get_weather")
workflow.add_edge("get_weather", "generate_response")
workflow.add_edge("generate_response", END)
# Compile graph
app = workflow.compile()
# Execute workflow
result = app.invoke({
"city": "Lahore",
"weather": ""
})
print(result["weather"])
Let's understand the important components.
TypedDict
Defines the structure of the shared state that flows through the graph.
StateGraph()
Creates a graph using the defined state schema.
add_node()
Registers a Python function as a node in the workflow.
add_edge()
Defines the execution order between nodes.
compile()
Compiles the graph into an executable application.
invoke()
Executes the graph using the supplied initial state.
Unlike the LangChain example, this implementation does not automatically create an LLM agent. We explicitly define the operations and their execution order.
This gives us more control over the workflow.
3. Tool Integration: How Do Agents Execute Actions?
An AI agent becomes useful when it can interact with external systems.
For example, consider a payment support agent that needs to:
- Retrieve a payment using a transaction ID.
- Check its current status.
- Retrieve the associated customer.
- Create a refund request when authorized.
- Return the result to the user.
Both frameworks can support this type of functionality, but their approaches are different.
LangChain: Defining tools
LangChain provides a convenient way to expose Python functions as tools.
For example:
from langchain.agents import create_agent
from langchain.tools import tool
@tool
def get_payment(payment_id: str) -> str:
"""Retrieve payment details using a payment ID."""
# Replace with your payment service.
return f"Payment {payment_id} is successful."
@tool
def get_customer(customer_id: str) -> str:
"""Retrieve customer information."""
# Replace with your customer service.
return f"Customer {customer_id} details retrieved."
payment_agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[
get_payment,
get_customer
],
system_prompt="""
You are a payment support assistant.
Retrieve payment information using the available tools.
Never initiate a refund without explicit application authorization.
"""
)
result = payment_agent.invoke({
"messages": [
{
"role": "user",
"content": "Check payment PAY-1001."
}
]
})
print(result["messages"][-1].content)
The @tool decorator converts a Python function into a tool that the agent can use.
The function name and docstring help the model understand what the tool does.
The agent decides when to call the available tools based on the user's request.
This approach is useful when you want to expose existing application functions without manually implementing the entire agent execution process.
LangGraph: Executing tools through graph nodes
LangGraph allows you to implement tool execution as part of a graph.
You can define separate nodes for payment retrieval, customer lookup, validation, and other operations.
For example:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class PaymentState(TypedDict):
payment_id: str
payment_details: str
customer_details: str
def retrieve_payment(state: PaymentState):
payment_id = state["payment_id"]
return {
"payment_details": (
f"Payment {payment_id} is successful."
)
}
def retrieve_customer(state: PaymentState):
return {
"customer_details": "Customer details retrieved."
}
def generate_response(state: PaymentState):
return {
"payment_details": (
state["payment_details"]
+ " Customer information: "
+ state["customer_details"]
)
}
workflow = StateGraph(PaymentState)
workflow.add_node("retrieve_payment", retrieve_payment)
workflow.add_node("retrieve_customer", retrieve_customer)
workflow.add_node("generate_response", generate_response)
workflow.add_edge(START, "retrieve_payment")
workflow.add_edge("retrieve_payment", "retrieve_customer")
workflow.add_edge("retrieve_customer", "generate_response")
workflow.add_edge("generate_response", END)
app = workflow.compile()
result = app.invoke({
"payment_id": "PAY-1001",
"payment_details": "",
"customer_details": ""
})
print(result["payment_details"])
Here, each operation is implemented as an independent node.
You can also introduce conditional edges to determine which operation should execute next.
For more advanced implementations, LangGraph supports integrating LangChain tools and agents into graph nodes.
When should you use each approach?
Use LangChain when you want to expose application functions to an LLM agent and let the agent decide which tools to invoke.
Use LangGraph when you need explicit control over which operations execute, their order, and the conditions that determine the next step.
These approaches are not mutually exclusive. You can use LangChain tools inside a LangGraph workflow.
4. Workflow Orchestration: Agent Loop vs Graph Execution
One of the most important differences between these frameworks is how they organize execution.
LangChain: Agent execution loop
LangChain agents typically operate through an execution loop.
The process looks like this:
- Receive user input.
- Send the input and available tool definitions to the LLM.
- The LLM decides whether to respond or call a tool.
- Execute the selected tool.
- Return the tool result to the LLM.
- Continue until the agent produces a final response.
A simplified representation:
User Request
|
v
Agent / LLM
|
v
Tool Required?
/ \
Yes No
| |
v v
Execute Final Answer
Tool
|
v
Tool Result
|
v
Agent
|
v
Final Answer
The framework handles the execution loop, while the developer configures the model, tools, instructions, and middleware.
This is useful when the LLM should dynamically decide which tools to use.
LangGraph: Explicit graph execution
LangGraph takes a different approach.
Instead of relying only on an agent loop, you define the workflow as a graph.
Consider an AI-powered customer support application.
User Request
|
v
Classify Request
|
+------+------+
| |
v v
Technical Billing
Support Issue
| |
v v
Search Docs Retrieve Payment
| |
+------+------+
|
v
Generate Reply
|
v
Human Review
|
+----+----+
| |
Approve Reject
| |
v v
Send Reply Revise
|
v
END
Each operation can be implemented as an individual node.
Conditional edges determine the next operation based on the current state.
For example:
def route_request(state):
if state["category"] == "billing":
return "billing"
return "technical"
You can connect this function to the graph using conditional edges:
workflow.add_conditional_edges(
"classify_request",
route_request,
{
"billing": "billing_agent",
"technical": "technical_agent"
}
)
The workflow is no longer dependent on the LLM deciding every transition.
Some transitions can be deterministic Python logic, while others can be based on LLM decisions.
This combination is particularly useful for enterprise applications where business rules and AI-driven decisions need to work together.
5. State Management and Memory
State management is an important consideration when developing production AI agents.
Consider a customer support conversation:
- The user reports a payment failure.
- The agent retrieves payment information.
- The user asks why the payment failed.
- The agent refers to the previous payment details.
- The user requests a refund.
The application needs to maintain relevant information throughout these interactions.
LangChain: Managing conversation state
LangChain agents maintain conversation messages as part of their execution state.
You can pass previous messages into subsequent invocations.
For example:
from langchain.agents import create_agent
agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[],
system_prompt="You are a payment support assistant."
)
conversation = [
{
"role": "user",
"content": "My payment PAY-1001 failed."
},
{
"role": "assistant",
"content": "I can help you investigate that payment."
}
]
result = agent.invoke({
"messages": conversation + [
{
"role": "user",
"content": "Can you explain the issue?"
}
]
})
print(result["messages"][-1].content)
The application can store and retrieve conversation messages from a database or another persistence layer.
For production applications, LangChain also provides middleware and agent capabilities for managing conversation context.
LangGraph: Managing shared state
LangGraph provides an explicit state structure that is shared across nodes.
Each node can read the current state and return updates.
For example:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class CustomerState(TypedDict):
customer_id: str
payment_id: str
payment_status: str
customer_name: str
response: str
def retrieve_payment(state: CustomerState):
return {
"payment_status": "Failed"
}
def retrieve_customer(state: CustomerState):
return {
"customer_name": "John"
}
def generate_response(state: CustomerState):
return {
"response": (
f"Hello {state['customer_name']}, "
f"your payment {state['payment_id']} "
f"has status: {state['payment_status']}."
)
}
workflow = StateGraph(CustomerState)
workflow.add_node("retrieve_payment", retrieve_payment)
workflow.add_node("retrieve_customer", retrieve_customer)
workflow.add_node("generate_response", generate_response)
workflow.add_edge(START, "retrieve_payment")
workflow.add_edge("retrieve_payment", "retrieve_customer")
workflow.add_edge("retrieve_customer", "generate_response")
workflow.add_edge("generate_response", END)
app = workflow.compile()
result = app.invoke({
"customer_id": "CUST-1001",
"payment_id": "PAY-1001",
"payment_status": "",
"customer_name": "",
"response": ""
})
print(result["response"])
Here, all nodes access the same logical state.
The payment retrieval node updates payment_status, while the customer retrieval node updates customer_name.
The response-generation node uses both values.
This makes the data flow between workflow operations explicit.
State vs conversation memory
These concepts are related but not identical.
| Consideration | LangChain | LangGraph |
|---|---|---|
| Conversation messages | Supported | Supported through message state |
| Shared workflow state | Agent runtime and middleware | Explicit state schema |
| State updates | Managed through agent execution | Nodes return state updates |
| Short-term memory | Conversation and agent state | Thread-level checkpointed state |
| Long-term memory | Integrations and application storage | Store abstraction |
| Persistence | Depends on configured capabilities | Checkpointer-based persistence |
The main difference is the level of control over state.
LangChain provides convenient agent abstractions, while LangGraph allows developers to define exactly what information flows through each workflow step.
6. Persistence and Checkpointing
Production agents often execute workflows that take several minutes or even hours.
For example:
- Processing large documents.
- Running multi-step research tasks.
- Performing code analysis.
- Executing business approval workflows.
- Coordinating multiple specialized agents.
If an application fails during execution, it should ideally recover without repeating the entire workflow.
LangChain: Agent persistence
LangChain agents are built using LangGraph primitives, which means they can use LangGraph's persistence capabilities.
For example, you can create an agent with a checkpointer.
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()
agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[],
system_prompt="You are a helpful assistant.",
checkpointer=checkpointer
)
config = {
"configurable": {
"thread_id": "customer-1001"
}
}
result = agent.invoke(
{
"messages": [
{
"role": "user",
"content": "My payment failed."
}
]
},
config=config
)
print(result["messages"][-1].content)
The checkpointer saves the agent's state associated with the specified thread.
When the same thread is used again, the agent can continue with its previous conversation state.
LangGraph: Checkpointing graph execution
LangGraph provides checkpointers that save graph state at execution checkpoints.
For example:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
class WorkflowState(TypedDict):
task: str
result: str
def process_task(state: WorkflowState):
return {
"result": f"Processed: {state['task']}"
}
workflow = StateGraph(WorkflowState)
workflow.add_node("process_task", process_task)
workflow.add_edge(START, "process_task")
workflow.add_edge("process_task", END)
checkpointer = InMemorySaver()
app = workflow.compile(
checkpointer=checkpointer
)
config = {
"configurable": {
"thread_id": "workflow-1001"
}
}
result = app.invoke(
{
"task": "Analyze customer feedback",
"result": ""
},
config=config
)
print(result["result"])
In this example, LangGraph saves the execution state using the in-memory checkpointer.
For production systems, you should use an appropriate persistent checkpointer, such as PostgreSQL, rather than relying on in-memory storage.
Why is checkpointing important?
Checkpointing provides several important capabilities:
- Recovering workflow state.
- Resuming interrupted executions.
- Supporting human approval workflows.
- Inspecting previous execution states.
- Maintaining state across application restarts.
Checkpointing does not automatically make every external operation safe to repeat. For example, sending a payment request or creating a refund should use idempotency and appropriate transaction controls.
7. Human-in-the-Loop
Human intervention is essential when AI agents perform sensitive or business-critical operations.
For example, consider a refund-processing agent.
The agent may retrieve payment information and prepare a refund request, but the actual refund should require human approval.
LangChain: Human approval through agent capabilities
LangChain supports human-in-the-loop patterns through middleware and tool execution controls.
You can configure a tool so that the agent pauses for approval before executing it.
For example:
from langchain.agents import create_agent
from langchain.tools import tool
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver
@tool
def create_refund(payment_id: str) -> str:
"""Create a refund for a payment."""
# Replace with your payment service.
return f"Refund created for {payment_id}."
agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[create_refund],
middleware=[
HumanInTheLoopMiddleware(
interrupt_on={
"create_refund": True
}
)
],
checkpointer=InMemorySaver()
)
When the agent attempts to call create_refund, execution pauses for human review.
The application can inspect the pending operation and approve, reject, or modify it using the human-in-the-loop interaction flow.
The important point is that the agent can propose the operation, but a human controls whether it proceeds.
LangGraph: Human approval using interrupt()
LangGraph provides the interrupt() function to pause graph execution and wait for external input.
For example:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import interrupt
from langgraph.checkpoint.memory import InMemorySaver
class RefundState(TypedDict):
payment_id: str
approval: str
result: str
def prepare_refund(state: RefundState):
print(f"Preparing refund for {state['payment_id']}")
return {}
def human_approval(state: RefundState):
decision = interrupt({
"message": "Approve this refund?",
"payment_id": state["payment_id"]
})
return {
"approval": decision
}
def execute_refund(state: RefundState):
if state["approval"] == "approved":
return {
"result": "Refund successfully processed."
}
return {
"result": "Refund rejected."
}
workflow = StateGraph(RefundState)
workflow.add_node("prepare_refund", prepare_refund)
workflow.add_node("human_approval", human_approval)
workflow.add_node("execute_refund", execute_refund)
workflow.add_edge(START, "prepare_refund")
workflow.add_edge("prepare_refund", "human_approval")
workflow.add_edge("human_approval", "execute_refund")
workflow.add_edge("execute_refund", END)
checkpointer = InMemorySaver()
app = workflow.compile(
checkpointer=checkpointer
)
config = {
"configurable": {
"thread_id": "refund-1001"
}
}
# First execution pauses for approval.
result = app.invoke(
{
"payment_id": "PAY-1001",
"approval": "",
"result": ""
},
config=config
)
# Resume after human approval.
from langgraph.types import Command
result = app.invoke(
Command(resume="approved"),
config=config
)
print(result["result"])
The first execution pauses when it reaches interrupt().
The application can present the pending refund request to a human reviewer.
Once the reviewer responds, the application resumes the graph using Command(resume=...).
In a production application, use a persistent checkpointer and authenticate the approval action independently.
Human-in-the-loop comparison
| Capability | LangChain | LangGraph |
|---|---|---|
| Pause for approval | Supported through middleware | Supported through interrupt() |
| Tool approval | Human-in-the-loop middleware | Graph-based approval nodes |
| Resume execution | Agent state and checkpointing | Command(resume=...) |
| Custom approval workflow | Middleware and application logic | Explicit graph nodes and routing |
| Multiple approval stages | Supported with configuration | Explicit workflow design |
LangChain offers a convenient way to introduce approval into agent tool execution.
LangGraph provides more flexibility when approval is one part of a larger business workflow.
8. Multi-Agent Orchestration
Multi-agent architecture becomes useful when a task involves multiple responsibilities that can be separated into specialized roles.
Consider an AI-powered code review system.
A single agent might be responsible for reviewing an entire codebase, identifying security issues, checking test coverage, analyzing performance, and producing a final report.
As complexity increases, it can be useful to divide this work among specialized agents.
For example:
Main Agent
|
+------------+------------+
| | |
v v v
Code Review Security Test Review
Agent Agent Agent
| | |
v v v
Code Issues Vulnerabilities Test Gaps
| | |
+------------+------------+
|
v
Final Report
Both frameworks can implement multi-agent systems, but their orchestration approaches differ.
LangChain: Agents as tools
One approach is to create specialized agents and expose them as tools to a manager agent.
The manager decides which specialist to invoke.
For example:
from langchain.agents import create_agent
from langchain.tools import tool
@tool
def security_review(code: str) -> str:
"""Review code for security vulnerabilities."""
# Replace with an actual security analysis implementation.
return "No critical security issues identified."
@tool
def test_review(code: str) -> str:
"""Review code for missing test coverage."""
# Replace with an actual test analysis implementation.
return "Three important test cases are missing."
manager_agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[
security_review,
test_review
],
system_prompt="""
You are a code review manager.
Use the security review tool to identify vulnerabilities.
Use the test review tool to identify missing test cases.
Combine the findings into a structured report.
"""
)
result = manager_agent.invoke({
"messages": [
{
"role": "user",
"content": "Review this Python code for security and test coverage."
}
]
})
print(result["messages"][-1].content)
This example exposes specialist capabilities as tools.
For a production implementation, each specialist can itself be an independent LangChain agent with its own model, tools, and instructions.
The manager agent remains responsible for coordinating the overall task.
LangGraph: Multi-agent orchestration using nodes
LangGraph allows developers to represent each specialized agent as an individual node.
For example:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class ReviewState(TypedDict):
code: str
security_result: str
test_result: str
final_report: str
def security_agent(state: ReviewState):
# Replace with an LLM-powered security agent.
return {
"security_result": "No critical vulnerabilities found."
}
def test_agent(state: ReviewState):
# Replace with an LLM-powered test agent.
return {
"test_result": "Three test cases are missing."
}
def generate_report(state: ReviewState):
return {
"final_report": (
f"Security: {state['security_result']}\n"
f"Testing: {state['test_result']}"
)
}
workflow = StateGraph(ReviewState)
workflow.add_node("security_agent", security_agent)
workflow.add_node("test_agent", test_agent)
workflow.add_node("generate_report", generate_report)
workflow.add_edge(START, "security_agent")
workflow.add_edge(START, "test_agent")
workflow.add_edge("security_agent", "generate_report")
workflow.add_edge("test_agent", "generate_report")
workflow.add_edge("generate_report", END)
app = workflow.compile()
result = app.invoke({
"code": "def login(username, password): pass",
"security_result": "",
"test_result": "",
"final_report": ""
})
print(result["final_report"])
Notice that both specialist nodes are connected to the starting point.
LangGraph can execute independent branches in parallel within the same graph superstep.
Once the required branches complete, the final report node combines their results.
For more complex applications, you can also create separate graphs for specialized agents and connect them through subgraphs.
Different multi-agent patterns
There are several ways to organize multi-agent systems.
| Pattern | Description | Suitable use case |
|---|---|---|
| Supervisor | A central agent delegates tasks to specialists | Research and code review |
| Sequential | Agents execute one after another | Document processing |
| Parallel | Multiple agents work independently | Security and test analysis |
| Router | An agent selects the appropriate specialist | Customer support |
| Handoff | Control transfers to another specialist | Multi-domain support |
| Hierarchical | Multiple levels of supervisors and agents | Complex enterprise workflows |
LangChain provides agent abstractions that simplify building these patterns.
LangGraph provides explicit control over the workflow, state, routing, and coordination between agents.
9. Conditional Routing and Workflow Control
Another important difference is how developers control execution paths.
LangChain: Model-driven decisions
With LangChain agents, the model typically determines which tools to invoke based on the user request and available tool descriptions.
For example:
from langchain.agents import create_agent
from langchain.tools import tool
@tool
def billing_support(query: str) -> str:
"""Handle billing-related questions."""
return "Billing support information."
@tool
def technical_support(query: str) -> str:
"""Handle technical support questions."""
return "Technical support information."
agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[
billing_support,
technical_support
],
system_prompt="""
Route billing questions to billing_support.
Route technical questions to technical_support.
"""
)
The LLM decides which tool to call.
This is useful when routing depends on natural language understanding.
LangGraph: Deterministic and dynamic routing
LangGraph provides conditional edges for controlling execution.
For example:
def classify_request(state):
query = state["query"].lower()
if "payment" in query or "refund" in query:
return "billing"
return "technical"
workflow.add_conditional_edges(
"classify_request",
classify_request,
{
"billing": "billing_agent",
"technical": "technical_agent"
}
)
Here, the routing decision is implemented through Python logic.
You can also use an LLM inside the classification node and then use the resulting classification to determine the next node.
This gives developers the flexibility to combine deterministic business rules with AI-driven decisions.
When is conditional routing important?
Consider an enterprise workflow with the following requirements:
- Billing requests must go through payment validation.
- Refund requests must require approval.
- Technical issues must search technical documentation.
- High-priority incidents must be escalated.
- Unauthorized operations must be rejected.
These requirements involve explicit business rules.
LangGraph makes these transitions easier to model and inspect because the execution flow is defined in the graph.
10. Error Handling and Recovery
AI applications interact with external APIs, databases, LLM providers, and third-party services.
Failures are inevitable.
A production system needs to handle different types of errors appropriately.
Examples include:
- Network failures.
- API rate limits.
- LLM timeouts.
- Tool execution errors.
- Invalid model responses.
- Database connection failures.
- Unexpected application exceptions.
LangChain: Handling errors
LangChain provides middleware and tool-level mechanisms for handling failures.
For example, you can catch errors inside a tool:
from langchain.tools import tool
@tool
def get_customer(customer_id: str) -> str:
"""Retrieve customer information."""
try:
# Replace with actual customer service.
customer = fetch_customer(customer_id)
return str(customer)
except Exception as error:
return f"Unable to retrieve customer: {error}"
The agent can receive the error as a tool result and decide how to proceed.
You can also configure middleware to handle model calls, tool execution, retries, and other agent lifecycle operations.
LangGraph: Node-level retry and recovery
LangGraph allows developers to configure retry policies for individual nodes.
For example:
from langgraph.graph import StateGraph, START, END
def search_documentation(state):
# Replace with an actual search service.
return {
"search_result": "Documentation retrieved."
}
workflow = StateGraph(dict)
workflow.add_node(
"search_documentation",
search_documentation,
retry_policy={
"max_attempts": 3,
"initial_interval": 1.0
}
)
workflow.add_edge(START, "search_documentation")
workflow.add_edge("search_documentation", END)
app = workflow.compile()
If the node encounters a retryable failure, LangGraph can retry its execution according to the configured policy.
You can also implement recovery branches, route errors to specialized nodes, or pause execution for human intervention.
Error-handling comparison
| Error scenario | LangChain | LangGraph |
|---|---|---|
| Tool failure | Tool and middleware handling | Node-level handling |
| Model failure | Model and middleware configuration | Node-level handling and retries |
| API rate limits | Integration-specific retries | Configurable node retry policies |
| Workflow recovery | Agent state and checkpointing | Checkpointing and graph recovery |
| Human intervention | Human-in-the-loop middleware | Interrupts |
| Custom recovery path | Agent and application logic | Explicit conditional branches |
LangChain is convenient when the error occurs within an agent or tool operation.
LangGraph is particularly useful when failures affect different stages of a larger workflow and you need explicit recovery paths.
11. Observability and Debugging
When an AI agent performs multiple model calls and tool executions, understanding its behavior becomes important.
For example:
- Why did the agent call a particular tool?
- Which tool was executed?
- Did an API request fail?
- Which node processed the request?
- How many model calls were made?
- Where did the workflow spend most of its time?
LangChain
LangChain integrates with LangSmith for tracing, debugging, evaluation, and monitoring.
It allows developers to inspect agent executions, model calls, and tool activity.
For example, you can enable tracing through environment variables:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY="your-api-key"
export LANGSMITH_PROJECT="customer-support-agent"
You can then inspect traces through LangSmith.
LangGraph
LangGraph also integrates with LangSmith.
In addition to tracing individual model and tool operations, graph execution provides visibility into:
- Node execution.
- State transitions.
- Conditional routing.
- Checkpoint history.
- Interrupted workflows.
- Subgraph execution.
This is particularly useful when debugging complex workflows.
Observability comparison
| Area | LangChain | LangGraph |
|---|---|---|
| Model calls | LangSmith tracing | LangSmith tracing |
| Tool execution | Agent and tool traces | Node and tool traces |
| Execution flow | Agent execution traces | Graph execution traces |
| State inspection | Agent state and messages | Graph state and checkpoints |
| Workflow debugging | Agent-level visibility | Node-level visibility |
| External monitoring | LangSmith integrations | LangSmith integrations |
Both frameworks benefit from LangSmith. The main difference is the execution structure you can inspect.
12. Practical Decision Matrix
The following table summarizes the main architectural trade-offs.
| Requirement | Better Starting Point | Reason |
|---|---|---|
| Simple LLM application | LangChain | Higher-level abstractions |
| Basic tool-calling agent | LangChain | create_agent() |
| RAG application | LangChain | Document and retrieval integrations |
| Custom agent behavior | LangChain | Tools, prompts, and middleware |
| Deterministic business workflows | LangGraph | Explicit execution flow |
| Complex conditional routing | LangGraph | Conditional edges |
| Long-running workflows | LangGraph | Durable execution and checkpointing |
| Human approval workflows | Both | Middleware or graph interrupts |
| Parallel processing | Both | Agent orchestration or graph branches |
| Complex multi-agent coordination | LangGraph | Explicit graph orchestration |
| Custom state transitions | LangGraph | Shared state and node updates |
| Agent execution tracing | Both | LangSmith integration |
This is a starting-point recommendation, not a strict feature ranking.
LangChain agents are already built using LangGraph primitives, so many capabilities overlap.
13. Can You Use LangChain and LangGraph Together?
Yes. In fact, combining both frameworks is often a good architectural decision.
LangChain provides convenient abstractions for models, tools, and agents.
LangGraph provides control over the larger workflow.
Consider an AI-powered customer support platform.
The application might need to:
- Classify incoming customer requests.
- Route the request to a specialized agent.
- Retrieve information from internal systems.
- Generate an appropriate response.
- Escalate sensitive requests.
- Request human approval.
- Send the final response.
You can use LangChain to create specialized agents and LangGraph to coordinate them.
For example:
from langchain.agents import create_agent
from langchain.tools import tool
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
# -------------------------
# LangChain Tools
# -------------------------
@tool
def search_knowledge_base(query: str) -> str:
"""Search the internal knowledge base."""
# Replace with actual vector database retrieval.
return "Relevant documentation found."
# -------------------------
# LangChain Agent
# -------------------------
support_agent = create_agent(
model="openai:gpt-4.1-mini",
tools=[search_knowledge_base],
system_prompt="""
You are a customer support specialist.
Use the knowledge base to answer customer questions.
"""
)
# -------------------------
# LangGraph State
# -------------------------
class SupportState(TypedDict):
customer_query: str
response: str
# -------------------------
# LangGraph Node
# -------------------------
def process_request(state: SupportState):
result = support_agent.invoke({
"messages": [
{
"role": "user",
"content": state["customer_query"]
}
]
})
return {
"response": result["messages"][-1].content
}
# -------------------------
# Build Workflow
# -------------------------
workflow = StateGraph(SupportState)
workflow.add_node(
"support_agent",
process_request
)
workflow.add_edge(START, "support_agent")
workflow.add_edge("support_agent", END)
app = workflow.compile()
# -------------------------
# Execute Workflow
# -------------------------
result = app.invoke({
"customer_query": "How can I reset my password?",
"response": ""
})
print(result["response"])
In this architecture:
LangChain is responsible for:
- Creating the support agent.
- Connecting the LLM.
- Defining tools.
- Retrieving relevant documentation.
- Generating the response.
LangGraph is responsible for:
- Managing the overall workflow.
- Maintaining shared state.
- Defining execution order.
- Controlling conditional transitions.
- Supporting persistence and human approval.
This separation makes it easier to scale the application as business requirements become more complex.
14. When Should You Choose LangChain?
LangChain is a strong starting point when you want to develop LLM applications without implementing every part of the agent execution process yourself.
Consider it for the following use cases.
1. Basic AI assistants
If you need an assistant that can answer questions, call external tools, and maintain conversation context, LangChain provides convenient abstractions.
2. RAG applications
LangChain provides integrations for document loading, text splitting, embeddings, vector databases, and retrieval.
It is a practical choice for building document-based question-answering applications.
3. Tool-calling agents
If your agent needs to interact with APIs, databases, and application services, LangChain makes it easy to expose Python functions as tools.
4. Applications with multiple model providers
LangChain provides integrations with different model providers, making it easier to build applications without tightly coupling the entire implementation to one provider.
5. Rapid development
When the primary requirement is to get an AI agent working quickly, higher-level abstractions can reduce development effort.
15. When Should You Choose LangGraph?
LangGraph is a strong option when your application requires explicit control over workflow execution.
Consider it for the following use cases.
1. Complex business workflows
Applications involving approvals, payment processing, customer support, and enterprise operations often require deterministic execution paths.
LangGraph allows developers to model these processes explicitly.
2. Long-running agents
If your agent needs to execute multiple operations, pause, and resume later, checkpointing provides an important foundation.
3. Human-in-the-loop systems
Applications that require human review before executing sensitive operations benefit from LangGraph's interrupt and resume capabilities.
4. Multi-agent orchestration
If you need supervisors, specialized agents, parallel execution, and complex routing, LangGraph provides explicit workflow control.
5. Applications with complex state
When multiple operations need to share and update information throughout execution, LangGraph provides a structured state-management approach.
6. Custom agent architectures
If you want complete control over how an agent behaves, which operations it performs, and when it transitions between different stages, LangGraph provides the required primitives.
16. Final Recommendation
LangChain and LangGraph solve related but different engineering problems.
LangChain is particularly useful when you want to build LLM-powered applications and agents using higher-level abstractions. Its model integrations, tools, agent creation, and middleware simplify the development process.
LangGraph is particularly useful when you need explicit control over workflow execution. Its state management, nodes, edges, checkpointing, conditional routing, and human-in-the-loop capabilities provide a foundation for complex AI workflows.
My recommendation is to choose based on the application requirements:
- Choose LangChain when you need to build an LLM application or tool-calling agent quickly.
- Choose LangGraph when you need explicit control over execution flow, state, and workflow recovery.
- Use both when you need LangChain's agent abstractions inside a larger LangGraph workflow.
Most importantly, do not introduce graph orchestration into every AI application just because it supports advanced agentic workflows.
Start with the simplest architecture that satisfies your requirements.
As your application grows and requires more control over state, execution, persistence, and human intervention, introduce LangGraph where it provides genuine value.
A production-ready AI agent is not simply an LLM that can call tools. It is a controlled system that can execute operations, maintain context, recover from failures, and follow the business rules defined by the application.
Official Documentation and References
The APIs evolve, so consult the official documentation when implementing or upgrading an integration.
LangChain
LangGraph
- LangGraph Python Overview
- LangGraph Quickstart
- LangGraph Graph API
- LangGraph Persistence
- LangGraph Interrupts
- LangGraph Memory
Observability
Related: my work as an agentic AI engineer, AI and LLM integration, and Claude Agent SDK vs OpenAI Agents SDK.
Let's work together
Looking to discuss a project or explore potential collaboration? Reach out anytime.