17. LangGraph vs LangChain

Chapter 17 of 18 · 15 min

LangChain is a framework of individual components: chains, retrievers, tools, memory stores. You compose them into pipelines by chaining .invoke() calls or using LCEL (LangChain Expression Language). LangGraph adds a graph execution runtime on top of those same components. The practical difference: in LangChain you manage control flow with Python conditionals; in LangGraph you manage it with the graph structure and conditional edges.

LangChain pipeline:
user_input → retriever → prompt → formatter → LLM → output_parser

LangGraph application:
START → retrieve → conditional_branch → tool_node ↔ model_node → aggregate → END
                               ↑ (loop back if tool call)

LangGraph does not replace LangChain—it uses LangChain components. You still use OllamaLLM, ChatPromptTemplate, tool bindings from langchain_core.tools, and retrieval chains. LangGraph provides the execution layer around those components: state management, checkpointing, conditional routing, subgraphs, and human-in-the-loop interrupts.

When to use LangChain alone: simple linear chains, one-shot RAG pipelines, batch processing tasks with no branching. When to add LangGraph: any workflow that branches, loops, waits for human input, calls multiple agents, or needs stateful multi-turn execution with crash recovery.

The migration path from LangChain to LangGraph: identify each step in your chain that involves branching or tool-looping. Wrap each such step in a StateGraph node. Connect nodes with edges. Add checkpointing. This is additive—you do not throw away existing chain code, you wrap it.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Take a three-step LangChain retrieval chain and wrap it in a LangGraph with a checkpoint. Add a conditional edge that routes to a fallback node when the retrieval returns fewer than 2 results. Confirm the graph structure is cleaner than equivalent Python if/else statements.