Project report / GitHub evidence

LangGraph: 状态化 Agent 工作流框架

这是可索引项目报告证据页:它保留 LangGraph: 状态化 Agent 工作流框架 的源材料入口、机制线索和限制提醒;正文仍需 reader/editor 与 academic public-copy review 后才能当作最终结论引用。

LangGraph: 状态化 Agent 工作流框架

基本信息

字段内容
GitHubhttps://github.com/langchain-ai/langgraph
Star20k+
技术栈Python, TypeScript, LangChain Core, Pydantic
许可证MIT
安装pip install langgraph
开发者LangChain 团队
产品LangGraph Studio, LangGraph Cloud

项目简介

LangGraph 是 LangChain 团队开发的状态化多 Agent 工作流框架。它将 Agent 工作流建模为有向图(Directed Graph),节点是 Agent 或函数,边是控制流或条件分支。

核心理念:图即程序(Graph as Program) — 用图结构表达复杂的 Agent 编排逻辑,支持循环、分支、并行和状态持久化。

目录结构

langgraph/
├── libs/
│   ├── langgraph/               # ★ 核心框架
│   │   └── langgraph/
│   │       ├── graph/           # 图构建 API
│   │       │   ├── state.py     # StateGraph(状态图)
│   │       │   ├── message.py   # 消息图
│   │       │   ├── _node.py     # 节点定义
│   │       │   └── _branch.py   # 条件分支
│   │       ├── pregel/          # ★ Pregel 执行引擎
│   │       │   ├── main.py      # 主循环
│   │       │   ├── _algo.py     # 图算法
│   │       │   ├── _executor.py # 执行器
│   │       │   ├── _runner.py   # 运行器
│   │       │   ├── _loop.py     # 循环控制
│   │       │   ├── _checkpoint.py # 检查点
│   │       │   ├── _retry.py    # 重试机制
│   │       │   ├── _tools.py    # 工具集成
│   │       │   ├── _io.py       # 输入输出
│   │       │   ├── _read.py     # 读取通道
│   │       │   ├── _write.py    # 写入通道
│   │       │   ├── _call.py     # 调用逻辑
│   │       │   └── _validate.py # 验证
│   │       ├── channels/        # 状态通道
│   │       │   ├── base.py      # BaseChannel
│   │       │   ├── last_value.py # LastValue
│   │       │   ├── binop.py     # BinaryOperator
│   │       │   └── ephemeral_value.py
│   │       ├── managed/         # 管理式状态
│   │       ├── _internal/       # 内部工具
│   │       ├── func/            # 函数式 API
│   │       ├── stream/          # 流式输出
│   │       └── utils/           # 工具函数
│   ├── checkpoint/              # 检查点基类
│   ├── checkpoint-postgres/     # PostgreSQL 持久化
│   ├── checkpoint-sqlite/       # SQLite 持久化
│   ├── prebuilt/                # 预构建 Agent
│   ├── cli/                     # CLI 工具
│   ├── sdk-py/                  # Python SDK
│   └── sdk-js/                  # JavaScript SDK
└── docs/

核心模块分析

1. StateGraph — 状态图

LangGraph 的核心抽象是 StateGraph

from langgraph.graph import StateGraph, START, END
from typing import TypedDict

class State(TypedDict):
    messages: list
    next_agent: str

graph = StateGraph(State)

# 添加节点(Agent 或函数)
graph.add_node("researcher", researcher_fn)
graph.add_node("coder", coder_fn)
graph.add_node("reviewer", reviewer_fn)

# 添加边(控制流)
graph.add_edge(START, "researcher")
graph.add_conditional_edges("researcher", route_fn)
graph.add_edge("coder", "reviewer")
graph.add_edge("reviewer", END)

app = graph.compile()
result = app.invoke({"messages": ["实现进化算法"]})

图构建 API

2. Pregel 执行引擎

LangGraph 使用 Pregel 模型(源自 Google Pregel 图计算框架)执行图:

3. 状态通道(Channel)系统

通道类型行为
LastValue保留最后一个值
BinaryOperatorAggregate使用二元操作聚合(如列表追加)
EphemeralValue临时值,不持久化
DeltaChannel增量更新

4. 预构建 Agent

from langgraph.prebuilt import create_react_agent

agent = create_react_agent(model, tools=[search_tool, code_tool])
result = agent.invoke({"messages": [("user", "搜索并分析")]})

5. 人机协作

# 在图中插入人工审核节点
graph.add_node("human_review", interrupt=True)
# 执行到此处暂停,等待人工输入后恢复

技术亮点

  1. 图即程序:用有向图表达 Agent 编排,直观且灵活
  2. Pregel 引擎:基于图计算的执行模型,支持并行和增量
  3. 状态持久化:Checkpoint 系统(PostgreSQL/SQLite),支持中断/恢复
  4. 人机协作:内置 interrupt 机制
  5. LangChain 生态:与 LangChain 工具和模型无缝集成
  6. Studio/Cloud:可视化调试和生产部署

与 Self-Evolve 关联

维度LangGraph 贡献
编排有向图编排模型,支持循环/分支/并行
状态Channel 系统 + Checkpoint 持久化
人机interrupt 机制的人工审核
工具LangChain 工具生态
启示图模型编排可用于设计自我进化的工作流图

参考资料

GitNexus 深度架构分析