All writing
langgraph langchain ai-agents python

LangGraph vs LangChain: Architecture, State, and Migration

Ayush Kaushik 8 min read
LangGraph vs LangChain: Architecture, State, and Migration

If you are building anything beyond a simple prompt chain, the choice between LangGraph vs LangChain comes down to control flow. LangChain excels at linear sequences using LCEL. LangGraph replaces that with a stateful, cyclic graph architecture built for agents that need loops, branching, and persistence. I migrated a production RAG pipeline last quarter and the difference in debuggability alone justified the switch.

Core architectural differences (DAG vs chain)

LangChain Expression Language (LCEL) models computation as a Directed Acyclic Graph (DAG) of runnables. You pipe components together: prompt | model | parser. It is declarative, fast to prototype, and handles streaming natively. But it is fundamentally linear. You cannot easily model a loop where an agent calls a tool, reflects on the result, and decides to call another tool or retry.

LangGraph models your application as a state machine. Nodes are functions (or runnables). Edges determine the next node based on the current state. Crucially, edges can be conditional and cyclic.

# LangChain LCEL - Linear, great for RAG chains
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# LangGraph - Cyclic, stateful agent loop
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
class AgentState(TypedDict):
messages: Annotated[list, operator.add]
# custom fields persist across loops
retry_count: int
def agent_node(state: AgentState):
# logic here
return {"messages": [response]}
def should_continue(state: AgentState):
if state["retry_count"] > 3:
return "end"
return "continue"
workflow = StateGraph(AgentState)
workflow.add_node("agent", agent_node)
workflow.add_conditional_edges("agent", should_continue, {"continue": "agent", "end": END})
app = workflow.compile()

The graph compiles to a Runnable, so you can still drop it into existing LCEL pipelines. But the mental model shifts from “data transformation pipeline” to “state transition system.”

How does state management differ in production?

LangChain relies on RunnableConfig and callbacks for context passing. It works for request-scoped data. It falls apart when you need long-running conversations, human-in-the-loop checkpoints, or recovery after a crash.

LangGraph bakes persistence into the checkpointer interface. Every graph step writes a checkpoint to Postgres, SQLite, or Redis. You get time-travel debugging for free. I run a customer support agent that pauses for human approval on refunds over $500. The graph checkpoints before the human_review node. If the reviewer takes three days, the state sits in Postgres. No custom session store required.

from langgraph.checkpoint.postgres import PostgresSaver
DB_URI = "postgresql://user:pass@localhost:5432/checkpoints"
with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
# setup tables once
checkpointer.setup()
app = workflow.compile(checkpointer=checkpointer)
# Resuming a thread
config = {"configurable": {"thread_id": "user-123-session-456"}}
# This picks up exactly where it left off
for chunk in app.stream(inputs, config=config, stream_mode="values"):
print(chunk)

The trade-off: you own the database schema and migration strategy. LangChain’s ConversationBufferMemory is simpler for stateless chat widgets. LangGraph’s checkpointer is heavier infrastructure but survives pod restarts and deployments.

What multi-agent orchestration capabilities exist?

LangChain offers AgentExecutor and the newer create_react_agent (which actually uses LangGraph under the hood now). For true multi-agent systems — supervisor/worker, hierarchical teams, or debate-style consensus — LangGraph is the only viable option in this ecosystem.

You define sub-graphs as nodes. A supervisor node routes to specialist sub-graphs based on state. Each sub-graph maintains its own local state while the parent sees a summary. I built a financial analysis agent where a planner delegates to a sec_filing_agent, a news_agent, and a quant_model_agent. They run in parallel via Send API. The planner synthesizes results.

from langgraph.types import Send
def planner(state: AgentState):
# decide which specialists needed
tasks = [{"agent": "sec", "query": state["question"]},
{"agent": "news", "query": state["question"]}]
return [Send("specialist", task) for task in tasks]
# Specialist subgraph compiled separately
specialist_app = specialist_workflow.compile()
workflow.add_node("planner", planner)
workflow.add_node("specialist", specialist_app) # subgraph as node
workflow.add_edge("specialist", "synthesizer")

LangChain’s older initialize_agent with multiple tools forces sequential tool calling inside a single loop. It cannot express “run these three independent agents in parallel, then aggregate.” If you need that, you are already writing custom orchestration code on top of LangChain. LangGraph gives you the primitives.

Human-in-the-loop and streaming implementations

Both frameworks support streaming tokens via astream or astream_events. LangGraph adds interrupt() for blocking on human input mid-graph.

from langgraph.types import interrupt, Command
def human_review_node(state: AgentState):
# This pauses execution and saves checkpoint
decision = interrupt({"question": "Approve refund?", "amount": state["amount"]})
# Execution resumes here when Command(resume=...) is sent
return {"approved": decision["approved"]}
# In your API endpoint (FastAPI example)
@app.post("/resume/{thread_id}")
async def resume(thread_id: str, payload: dict):
config = {"configurable": {"thread_id": thread_id}}
# Resume passes data back into the interrupted node
async for chunk in app.astream(Command(resume=payload), config=config):
# stream updates to client via SSE/WebSocket
pass

I use this pattern for the refund agent mentioned earlier. The FastAPI endpoint receives the reviewer’s decision, sends Command(resume=...), and the graph continues. LangChain requires you to build this state machine manually: persist state, render UI, handle callback, reload state, invoke chain. LangGraph makes it a first-class primitive.

Streaming intermediate state (not just tokens) is also cleaner. stream_mode="values" yields the full state after every node. stream_mode="updates" yields only the node output. Perfect for showing “Agent thinking…” -> “Searching web…” -> “Synthesizing…” in the UI without parsing callback events.

Production debugging and observability tooling

This is where I’ve been bitten most. LangChain callbacks (on_chain_start, on_tool_end, etc.) are verbose and fragile. Correlating a user request across nested chains, agents, and tools requires stitching together run_id trees manually. LangSmith helps but adds external dependency and cost.

LangGraph’s compiled graph is a single Runnable with a clear execution trace. Every node execution is a span. The state at each step is the checkpoint. You can replay a failed thread locally by loading the checkpoint and stepping through nodes in a debugger.

# Replay a specific checkpoint locally for debugging
checkpoint_tuple = checkpointer.get_tuple(config)
# Inspect state at that moment
print(checkpoint_tuple.checkpoint["channel_values"])
# Resume from that exact point with modified input
app.invoke(Command(resume={"override": "fix"}), config=config)

I integrate OpenTelemetry directly on the node functions. Since nodes are plain Python functions, instrumentation is standard.

from opentelemetry import trace
tracer = trace.get_tracer(__name__)
@tracer.start_as_current_span("retrieval_node")
def retrieve_node(state: AgentState):
# standard otel attributes
trace.get_current_span().set_attribute("query.length", len(state["question"]))
return {"docs": retriever.invoke(state["question"])}

No framework-specific callback handlers. Standard logs, metrics, traces. My on-call team prefers this.

Migration path from LangChain chains to LangGraph

You do not rewrite everything at once. LCEL chains are valid LangGraph nodes.

  1. Identify the cyclic part of your logic (the agent loop, the retry logic, the human review gate).
  2. Extract that into a StateGraph.
  3. Keep your RAG chains, SQL generators, and formatters as LCEL runnables.
  4. Call them from graph nodes.
# Existing LCEL chain - keep it
sql_chain = prompt | llm | StrOutputParser() | sql_validator
# New Graph Node - thin wrapper
def generate_sql(state: AgentState):
result = sql_chain.invoke({"question": state["question"], "schema": state["schema"]})
return {"sql": result, "retry_count": state.get("retry_count", 0) + 1}
# Conditional edge handles validation failure
def check_sql(state: AgentState):
if "error" in state["sql"]:
return "retry"
return "execute"

This incremental approach let me migrate a FastAPI AI agent over two sprints without downtime. The FastAPI endpoints stayed the same; they just invoked app.astream instead of chain.astream.

If you are comparing frameworks for a new multi-agent project, also check LangGraph vs CrewAI for a different architectural perspective. CrewAI abstracts the graph away; LangGraph exposes it.

When not to use LangGraph

  • Simple RAG or extractive QA: LCEL is faster to write, lower cognitive load, zero infrastructure for checkpointers.
  • Team unfamiliar with state machines: The learning curve is real. Cyclic graphs introduce infinite loop bugs that linear chains cannot have.
  • No need for persistence: If every request is stateless and independent, the checkpointer is dead weight.
  • Heavy reliance on LangChain-specific integrations: Some niche document loaders or vector store wrappers still expect LCEL runnables directly. They work inside nodes but add friction.

FAQ

Is LangGraph replacing LangChain? No. LangGraph is an extension built on top of LangChain core. It uses BaseChatModel, BaseRetriever, and LCEL runnables as nodes. You need LangChain installed to use LangGraph effectively.

Can I use LangGraph without a database? Yes. Use MemorySaver checkpointer for development or stateless workloads. It keeps checkpoints in process memory. It disappears on restart so do not use it for production human-in-the-loop flows.

How does LangGraph streaming compare to LangChain callbacks? LangGraph astream yields structured state updates per node. LangChain callbacks yield granular events (token, tool start, tool end). For building UIs, LangGraph’s stream_mode="values" is usually easier to consume. For fine-grained token streaming, both work well.

What is the performance overhead of the graph compiler? Negligible for most workloads. The compiler validates the graph structure at startup. Runtime overhead is a few function calls per node. I see <5ms added latency per hop in a 10-node graph on CPU. The database write for the checkpointer is the real cost; use async Postgres driver (asyncpg) and connection pooling.

Key Takeaways

  • LangChain LCEL = linear pipelines, RAG, simple chains. LangGraph = cyclic agents, state persistence, multi-agent orchestration.
  • Checkpointers turn “human-in-the-loop” from a custom engineering project into a configuration flag.
  • Migrate incrementally: wrap existing LCEL chains as graph nodes.
  • Observability improves because the graph structure is the trace.
  • Don’t reach for graphs until you have a loop, a branch, or a pause requirement.

Working on something similar?

If you're building backend or AI systems and want a second set of senior eyes, let's talk.

Keep reading

Related articles