一、引言:为什么需要新的 Agent 控制流模型
随着大模型能力的提升,AI 应用逐渐从简单的 **LLM 调用链(LLM Chain)**演进为复杂的 Agent 系统 。
典型 Agent 往往需要具备以下能力:
- 多轮推理(reasoning loop)
- 动态工具调用(tool selection)
- 并行任务执行(parallel tasks)
- 多 Agent 协作(multi-agent collaboration)
- 人类参与(human-in-the-loop)
- 长时间运行任务(long-running workflows)
传统 LLM pipeline 的结构通常是:
LLM → Tool → LLM → Tool
这种结构存在明显局限:
- 控制流难以表达循环
- 并行任务支持困难
- 状态管理复杂
- 多 Agent 协作缺乏统一模型
为了解决这些问题,LangChain 团队设计了 LangGraph。
LangGraph 的核心思想是:
使用带状态的有向图(Stateful Directed Graph)作为 Agent Runtime。
在 LangGraph 中:
Graph + State + Runtime
共同构成一个完整的 Agent Orchestration System。
而实现复杂控制流的关键,正是 LangGraph 提供的四类 控制流原语(Control Flow Primitives):
| 控制原语 | 作用 |
|---|---|
| Edge | Graph 级流程控制 |
| Command | Node 级动态路由 |
| Send | 并行任务分发 |
| Interrupt | 人类参与流程 |
本文将系统解析这四类控制原语的 设计理念、执行机制与架构意义。
二、LangGraph Runtime 执行模型
在深入控制流之前,需要理解 LangGraph 的运行机制。
LangGraph Runtime 可以抽象为:
Graph Definition
+ Execution Engine
+ State Store
+ Reducer System
其基本执行流程如下:
Node execution
↓
State update
↓
Reducer merge
↓
Edge resolution
↓
Next node scheduling
更具体地说:
-
节点执行(Node Execution)
节点执行逻辑,例如调用 LLM 或工具。
-
状态更新(State Update)
节点返回新的 state 片段。
-
Reducer 合并(Reducer Merge)
如果多个节点同时更新 state,需要通过 reducer 合并。
-
路径解析(Edge Resolution)
Graph 根据 edge 或 routing 决定下一步节点。
-
调度执行(Scheduling)
Runtime 调度新的节点执行。
因此 LangGraph 本质上是一个:
Event-driven Graph Runtime
而不是简单的 DAG workflow。
三、Edge:Graph 层控制流
Edge 是 LangGraph 中最基础的控制流机制,它定义了 Graph 的拓扑结构。
LangGraph 提供两种 Edge 类型:
add_edge
add_conditional_edges
3.1 静态 Edge
最简单的流程:
python
graph.add_edge("A", "B")
执行结构:
A → B
特点:
- 执行顺序固定
- 类似传统 workflow
适用于:
ETL pipeline
固定任务流程
工具处理管道
3.2 条件 Edge
更常见的情况是需要 动态路由。
LangGraph 提供:
python
graph.add_conditional_edges(
"A",
router,
{
"tool": "tool_node",
"end": END
}
)
执行流程:
A
↓
router(state)
↓
tool_node 或 END
Router 的职责是:
根据 state 决定下一节点
Router 可以返回:
单个节点
多个节点
END
3.3 并行执行
如果 router 返回多个节点:
["node_b", "node_c"]
Runtime 会并行执行:
node_b
node_c
这使 Graph 可以表达 fan-out workflow。
3.4 Edge 的设计哲学
Edge 模式强调:
Graph is the workflow
也就是说:
控制流 = Graph 拓扑
优点:
- 流程清晰
- 可视化友好
- 易于调试
缺点:
- 灵活性有限
- Agent 自主性较弱
四、Command:Node 层控制流
随着 Agent 复杂度增加,仅依赖 Edge 会产生一个问题:
控制逻辑被拆分为:
Node logic
+
Router logic
这导致代码分散。
为了解决这一问题,LangGraph 引入了 Command 模式。
4.1 Command 的核心思想
Command 是一种 Node-level control primitive。
节点可以同时完成:
更新状态
决定下一节点
示例:
python
from langgraph.types import Command
def node(state):
if state["score"] > 0.8:
return Command(
update={"result": "good"},
goto="node_b"
)
return Command(
update={"result": "bad"},
goto="node_c"
)
执行流程:
node execution
↓
Command(update, goto)
↓
state update
↓
schedule next node
4.2 动态路由能力
Command 允许节点在运行时动态决定下一节点:
node → any node
这非常适合:
Agent reasoning
multi-agent handoff
dynamic tool routing
例如:
planner → researcher → writer
每个节点可以决定下一步。
4.3 关于 "Edgeless Graph"
Command 常被描述为支持 edgeless graph。
更准确地说:
Command 提供运行时动态路由能力,
但目标节点必须已经存在于 Graph 中。
因此 Graph 仍然需要注册节点:
builder.add_node(...)
Command 只是减少了显式 Edge 的需求。
五、Send:并行任务分发
复杂 Agent 系统经常需要执行 并行任务。
例如:
多主题搜索
文档批量分析
查询扩展
LangGraph 提供 Send 原语。
5.1 Send 的基本机制
Send 用于在运行时创建多个任务。
示例:
python
from langgraph.types import Send
def planner(state):
topics = state["topics"]
return [
Send("research_node", {"topic": t})
for t in topics
]
执行流程:
planner
↓
spawn tasks
↓
research(topic1)
research(topic2)
research(topic3)
5.2 State 隔离
Send 创建的每个任务拥有独立 state:
branch_state = parent_state + payload
例如:
topic = "AI"
topic = "Robotics"
topic = "Biology"
每个 worker 处理自己的 state。
5.3 State 合并
并行执行完成后,需要合并 state。
LangGraph 使用 Reducer 机制。
例如:
python
Annotated[list, operator.add]
表示:
list + list
最终结果合并为:
results = [result1, result2, result3]
5.4 Send 的典型应用
Send 常用于:
Map-Reduce Agents
Parallel Search
Batch Processing
Document Summarization
六、Interrupt:Human-in-the-loop
在很多真实场景中,Agent 需要人类参与。
例如:
法律文书审核
医疗报告确认
代码评审
LangGraph 提供 Interrupt 原语。
6.1 Interrupt 的工作机制
Interrupt 的作用是:
暂停 Graph 执行
等待外部输入
示例:
python
from langgraph.types import interrupt
def review_node(state):
feedback = interrupt(
{"draft": state["draft"]}
)
return {"feedback": feedback}
执行流程:
writer
↓
review_node
↓
interrupt
↓
等待人工输入
↓
恢复执行
6.2 Checkpoint 机制
Interrupt 依赖 LangGraph 的 Checkpoint 系统。
执行流程:
node
↓
interrupt
↓
checkpoint 保存
↓
等待输入
↓
resume graph
恢复时:
加载 checkpoint
继续执行
这使 LangGraph 支持:
long-running agents
durable workflows
七、四种控制原语的统一模型
LangGraph 的控制流原语可以从架构层次理解。
Graph Layer
↓
Node Layer
↓
Runtime Layer
对应原语:
Graph Layer
Edge
Node Layer
Command
Runtime Layer
Send
Interrupt
作用分别是:
Edge → 定义 workflow 结构
Command → 实现 Agent 自主决策
Send → 实现并行任务
Interrupt → 支持人类协作
四者组合形成完整的 Agent orchestration 能力。
八、典型 Agent 架构模式
LangGraph 原语可以组合出多种 Agent 架构。
8.1 ReAct Agent
结构:
LLM
↓
Tool?
↓
Tool Node
↓
LLM
通常使用:
conditional edges
8.2 Planner--Executor
结构:
planner
↓
Send tasks
↓
workers
↓
aggregator
核心原语:
Send
8.3 Multi-Agent Collaboration
结构:
planner
researcher
writer
critic
Agent 之间通过:
Command
进行 handoff。
8.4 Human-in-the-loop Workflow
结构:
LLM
↓
draft
↓
Interrupt
↓
human review
↓
continue
九、LangGraph 的架构意义
LangGraph 并不仅仅是一个 workflow engine。
它更接近于:
Stateful Agent Runtime
结合了:
Workflow Engine
+
Actor Model
+
Graph Execution Runtime
其核心能力包括:
state management
dynamic routing
parallel execution
human collaboration
这使 LangGraph 成为构建复杂 Agent 系统的重要基础设施。
十、总结
LangGraph 的控制流体系由四个核心原语组成:
Edge
Command
Send
Interrupt
它们分别解决不同层面的控制问题:
Edge → workflow routing
Command → agent autonomy
Send → parallel execution
Interrupt → human interaction
四者共同构成一个完整的:
Stateful Agent Runtime
通过组合这些原语,开发者可以构建:
复杂推理 Agent
多 Agent 协作系统
大规模并行任务 Agent
人机协作流程
这也是 LangGraph 相比传统 LLM pipeline 的最大突破。