LangChain 與 LangGraph:如何選擇合適的框架
LangChain vs. LangGraph: How to Choose the Right Framework
LangChain 適合串接 LLM 操作,LangGraph 則適合構建需要持續狀態與多智能體的工作流程;兩者都是開源框架。LangChain 以鏈按序執行可重用元件,LangGraph 以節點、邊和狀態支援迴圈及跨互動管理狀態。兩者並非互斥;工作流程需要迴圈、持續狀態和多個智能體時,可在 LangChain 的串接與元件上加入 LangGraph。
This post is an AI-assisted written adaptation of a video by IBM Technology. All ideas belong to the original creator. Watch the original on YouTube.
LangChain and LangGraph are both open source frameworks designed to help developers build applications with large language models. That shared foundation is exactly why the choice can feel confusing at first — they look like competitors, and the internet treats them like a horse race.
The useful question isn't which framework is "better." It's why you'd reach for one over the other — and that answer depends on the shape of the application you're building. This guide starts by defining what each framework actually is, using a concrete example for each, then compares them head-to-head on primary focus, structure, components, state management, and use cases. By the end you should know which one fits your next project, and when the honest answer is both.
TL;DR
- LangChain is the abstraction layer. It wires LLM operations together by executing a sequence of functions in a chain — reusable components (document loaders, text splitters, prompts, LLMs, memory) combined to build a workflow.
- LangGraph is the workflow engine. A specialized library inside the LangChain ecosystem, purpose-built for stateful multi-agent systems, modeled as nodes (actions), edges (transitions), and state.
- Structure is where they split. LangChain's chain acts as a DAG — tasks run in a specific order and always move forward. LangGraph's graph allows loops and lets states move backwards and forwards.
- State management is the sharpest dividing line. LangChain can pass information back and forth through the chain but doesn't easily maintain persistent state across multiple runs; LangGraph makes state a core component every node can access and modify.
- They're complementary, not mutually exclusive. LangGraph lives inside the LangChain ecosystem, so the real choice is often about layering: use LangChain for the chaining and components, add LangGraph when the workflow needs loops, persistent state, and multiple agents.
Two Frameworks, One Big Question
LangChain and LangGraph are both open source frameworks designed to help developers build applications with large language models. That shared foundation is exactly why the choice can feel confusing at first.
The useful question isn't which framework is "better." It's why you'd reach for one over the other — and that answer depends on the shape of the application you're building.
A good place to start is to define what these two things actually are, beginning with LangChain.
▶ Watch this part of the video (00:00:00)
LangChain: The Abstraction Layer for Chaining LLM Calls
LangChain is the broader, more established of the two ecosystems. At its core, it's a way to build LLM-powered applications by executing a sequence of functions in a chain. Its job is to provide an abstraction layer that wires LLM operations together, rather than to act as a workflow engine. It's best thought of as the foundation layer.
A Concrete Example
Consider an application that needs to do three things in order: retrieve data from a website, summarize that data, and then use the summary to answer user questions. The workflow is retrieve → summarize → answer.
LangChain breaks that workflow into components:
- Retrieve. A document loader fetches and loads content from various data sources. If some documents are large, a text splitter breaks text into smaller, semantically meaningful chunks.
- Summarize. A chain orchestrates the summarization process. It can include a prompt component that instructs an LLM to perform the summarization, plus an LLM component that passes the request to the large language model of your choosing.
- Answer. A second chain generates the answer. It might include a memory component to store conversation history and context, along with another prompt component and another LLM component to produce the answer based on the summary and the recorded context.
Modularity Is the Point
One thing worth noting: the LLM used in the answer component can be a completely different large language model from the one used in the summarize component. Nothing in the design forces you to commit to a single model across the entire workflow.
That's the value of LangChain's modular architecture — you build complex workflows by combining high-level, reusable components rather than hand-rolling each step.
▶ Watch this part of the video (00:00:19)
LangGraph: Stateful Multi-Agent Workflows
LangGraph is a specialized library within the LangChain ecosystem. It is purpose-built for building stateful multi-agent systems that can handle complex, nonlinear workflows.
A task management assistant agent is a good illustration of the kind of problem LangGraph is designed for. The workflow starts by processing user input, then branches into actions such as adding tasks, completing tasks, and summarizing tasks.
LangGraph models this as a graph structure:
- Nodes represent actions. Add tasks, complete tasks, and summarize are all nodes.
- Edges represent the transitions between those actions.
- The process input node is the central node. User input arrives here, and it uses an LLM component to understand user intent and route to the appropriate action node.
A second central component is state. The state component maintains the task list across all interactions. Each action node interacts with it in a specific way:
- The add task node adds new tasks to the state.
- The complete task node marks tasks as finished.
- The summarize node uses an LLM to generate an overview of current tasks.
Because all nodes can access and modify the state, the assistant supports context-aware, stateful interactions. The graph structure lets the assistant handle various user requests in any order, always returning to the process input node after an action completes.
The result is a flexible, stateful agent that can maintain context over extended interactions.
▶ Watch this part of the video (00:03:17)
Head-to-Head: How They Actually Differ
The two frameworks are best compared across several dimensions rather than a single feature checklist. The difference in primary focus is what drives every other trade-off between them.
Primary focus. LangGraph exists to create and manage multi-agent systems and workflows. LangChain exists to provide an abstraction layer for chaining LLM operations into large language model applications. That is the core distinction.
Structure. LangChain adopts a chain structure that acts as a DAG — a directed acyclic graph — meaning tasks execute in a specific order and always move forward. You might start with task one, branch into tasks two and three, then come back to a central task four. This works well when you know the exact sequence of steps required.
LangGraph's graph structure is different because it allows for loops and revisiting previous states. State A can move backwards and forwards with states B and C. That is beneficial for interactive systems where the next step depends on evolving conditions or user input.
Components. LangChain uses memory, a prompt component, an LLM component for passing things to the large language model, and an agent component that forms chains between all of these. LangGraph uses nodes, edges, and states, all of which are part of a graph.
State management. LangChain's state management is somewhat limited. It can pass information back and forth through the chain, but it doesn't easily maintain a persistent state across multiple runs. Its memory components can maintain some state across interactions. LangGraph's state management is more robust because state is a core component that all nodes can access and modify, which allows for more complex, context-aware behaviors.
Use cases. LangChain excels at sequential tasks, such as a process that retrieves data, processes it, and outputs a result. It can handle non-sequential tasks to some extent through its agents feature. LangGraph's wheelhouse is scenarios with much more complexity: systems requiring ongoing interaction and adaptation, such as a virtual assistant that needs to maintain context over long conversations and handle varying types of requests.
▶ Watch this part of the video (00:05:41)
Which One Should You Use?
The two frameworks are aimed at different shapes of problem, so the decision usually comes down to how your workflow behaves rather than which one is "better."
Reach for LangChain when the work is a straightforward chain. Its focus is providing an abstraction layer for chaining LLM operations into large language model applications. The chain structure acts as a DAG — a directed acyclic graph — meaning tasks execute in a specific order and always move forward. You might start with task one, branch into tasks two and three, then converge back on task four. That is a great fit when you know the exact sequence of steps needed, such as a process that retrieves data, processes it, and outputs a result.
Reach for LangGraph when the workflow is stateful, multi-agent, and nonlinear. Its primary focus is creating and managing multi-agent systems and workflows. The graph structure allows for loops and revisiting previous states — state A can move backwards and forwards with state B and state C. That matters for interactive systems where the next step depends on evolving conditions or user input.
State management is the sharpest dividing line:
- LangChain can pass information back and forth through the chain, but it doesn't easily maintain a persistent state across multiple runs. Its memory components can hold some state across interactions.
- LangGraph treats state as a core component that all nodes can access and modify, which enables more complex, context-aware behaviors.
On use cases, LangChain handles sequential tasks well and can stretch to non-sequential work to some extent through its agents feature. LangGraph's wheelhouse is scenarios with much more complexity: systems requiring ongoing interaction and adaptation, like a virtual assistant that has to maintain context over long conversations and handle varying types of requests.
The two are complementary rather than mutually exclusive. LangGraph lives inside the LangChain ecosystem, so the choice is often about layering — use LangChain for the chaining and components, and add LangGraph when the workflow needs loops, persistent state, and multiple agents.
▶ Watch this part of the video (00:05:41)
Conclusion
The decision usually comes down to how your workflow behaves, not which framework is better. If you know the exact sequence of steps — retrieve, process, output — LangChain's DAG chain is a great fit and its modular components mean you can even swap models between steps. If the next step depends on evolving conditions or user input, and you need context to persist over extended interactions, reach for LangGraph. And remember you don't have to pick a side: start with LangChain, then layer LangGraph in when your workflow outgrows a straight line.
來源:DEV Community · dev.to