用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/tomevault-io/skills-registry --skill langgraph-overview命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | langgraph-overview |
| description | 理解 LangGraph:用于构建有状态、长期运行 Agent 的低级编排框架,具有持久执行、流式传输和人机交互能力 Use when this capability is needed. |
| metadata | {"author":"evanfang0054"} |
LangGraph 是一个低级编排框架和运行时,用于构建、管理和部署长期运行的、有状态的 Agent。它受到 Klarna、Replit 和 Elastic 等公司的信任,用于生产 Agent 工作负载。
关键特性:
LangGraph 非常适合当您需要:
当您满足以下条件时考虑替代方案:
| 需求 | 使用 LangGraph | 使用 LangChain | 使用 Deep Agents |
|---|---|---|---|
| 快速原型开发 | ❌ | ✅ | ✅ |
| 自定义编排逻辑 | ✅ | ❌ | ⚠️ (有限) |
| 持久执行 | ✅ | ⚠️ (通过 LangGraph) | ✅ |
| 人机交互 | ✅ | ⚠️ (通过 LangGraph) | ✅ |
| 状态持久化 | ✅ | ❌ | ✅ |
| 生产部署 | ✅ | ⚠️ (与 LangGraph 一起使用) | ✅ |
| 学习曲线 | 高 | 低 | 中 |
LangGraph 将 Agent 工作流建模为图,具有三个核心组件:
| 能力 | 描述 |
|---|---|
| 持久执行 | Agent 在故障中持久存在并从检查点恢复 |
| 流式传输 | 执行期间的实时更新(状态、令牌、自定义数据) |
| 人机交互 | 暂停执行以供人工审查和干预 |
| 持久化 | 线程级别和跨线程的状态管理 |
| 时间旅行 | 从执行历史中的任何检查点恢复 |
受 Google 的 Pregel 系统启发:
import { ChatAnthropic } from "@langchain/anthropic";
import { tool } from "@langchain/core/tools";
import { SystemMessage, HumanMessage, AIMessage, ToolMessage } from "@langchain/core/messages";
import { StateGraph, StateSchema, MessagesValue, ReducedValue, START, END } from "@langchain/langgraph";
import { z } from "zod";
// 1. 定义工具
const multiply = tool(({ a, b }) => a * b, {
name: "multiply",
description: "Multiply two numbers",
schema: z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
}),
});
const add = tool(({ a, b }) => a + b, {
name: "add",
: ,
: z.({
: z.().(),
: z.().(),
}),
});
model = ({
: ,
: ,
});
toolsByName = { [add.]: add, [multiply.]: multiply };
tools = .(toolsByName);
modelWithTools = model.(tools);
= ({
: ,
: (
z.().(),
{ : x + y }
),
});
= () => {
response = modelWithTools.([
(),
...state.,
]);
{
: [response],
: ,
};
};
= () => {
lastMessage = state..(-);
(lastMessage == || !.(lastMessage)) {
{ : [] };
}
result = [];
( toolCall lastMessage. ?? []) {
tool = toolsByName[toolCall.];
observation = tool.(toolCall);
result.(observation);
}
{ : result };
};
= () => {
lastMessage = state..(-);
(!lastMessage || !.(lastMessage)) {
;
}
(lastMessage.?.) {
;
}
;
};
agent = ()
.(, llmCall)
.(, toolNode)
.(, )
.(, shouldContinue, [, ])
.(, )
.();
result = agent.({
: [ ()],
});
( message result.) {
.();
}
import { MemorySaver } from "@langchain/langgraph";
// 创建检查点器用于状态持久化
const checkpointer = new MemorySaver();
// 使用检查点器编译
const agent = new StateGraph(MessagesState)
.addNode("llmCall", llmCall)
.addNode("toolNode", toolNode)
.addEdge(START, "llmCall")
.addConditionalEdges("llmCall", shouldContinue, ["toolNode", END])
.addEdge("toolNode", "llmCall")
.compile({ checkpointer }); // 添加检查点器
// 第一轮对话
const config = { configurable: { thread_id: "1" } };
await agent.invoke(
{ messages: [new HumanMessage("Hi, I'm Alice")] },
config
);
// 第二轮 - agent 记住上下文
await agent.invoke(
{ messages: [new HumanMessage("What's my name?")] },
config
);
// 流式传输状态更新
for await (const chunk of await agent.stream(
{ messages: [new HumanMessage("Calculate 5 + 3")] },
{ streamMode: "updates" }
)) {
console.log(chunk);
}
// 流式传输 LLM 令牌
for await (const chunk of await agent.stream(
{ messages: [new HumanMessage("Hello!")] },
{ streamMode: "messages" }
)) {
console.log(chunk);
}
// 多种流式模式
for await (const [mode, chunk] of await agent.stream(
{ messages: [new HumanMessage("Help me")] },
{ streamMode: ["updates", "messages"] }
)) {
console.log(`${mode}:`, chunk);
}
✅ 节点逻辑:将任何异步函数定义为节点 ✅ 状态模式:自定义状态结构和 reducer ✅ 控制流:添加条件边、循环、分支 ✅ 持久化层:选择检查点器(MemorySaver、SQLite、Postgres) ✅ 流式模式:配置要流式传输的数据 ✅ 中断:在任何点添加人机交互 ✅ 递归限制:控制最大执行步数 ✅ 工具和模型:使用任何 LLM 或工具提供程序
❌ 核心图执行模型:基于 Pregel 的运行时是固定的 ❌ 超级步行为:无法更改节点的批处理方式 ❌ 消息传递协议:内部通信是预定义的 ❌ 检查点模式:内部检查点格式是固定的 ❌ 图编译:无法修改编译逻辑
// ❌ 错误 - 使用检查点器但没有 thread_id
await agent.invoke({ messages: [...] }); // 状态未持久化!
// ✅ 正确 - 始终提供 thread_id
await agent.invoke(
{ messages: [...] },
{ configurable: { thread_id: "user-123" } }
);
// ❌ 错误 - 消息将被覆盖,而不是追加
const BadState = new StateSchema({
messages: z.array(BaseMessageSchema), // 没有 reducer!
});
// ✅ 正确 - 使用 MessagesValue 进行自动消息处理
import { MessagesValue } from "@langchain/langgraph";
const GoodState = new StateSchema({
messages: MessagesValue, // 正确处理消息更新
});
// ❌ 错误 - StateGraph 不可执行
const builder = new StateGraph(State).addNode("node", func);
await builder.invoke(...); // 错误!
// ✅ 正确 - 必须先编译
const graph = builder.compile();
await graph.invoke(...);
// ❌ 错误 - 没有退出条件的循环
builder
.addEdge("nodeA", "nodeB")
.addEdge("nodeB", "nodeA"); // 无限循环!
// ✅ 正确 - 添加到 END 的条件边
const shouldContinue = (state) => {
if (state.count > 10) {
return END;
}
return "nodeB";
};
builder.addConditionalEdges("nodeA", shouldContinue);
// ❌ 错误 - 忘记 await
const result = agent.invoke(...); // 返回 Promise!
console.log(result.messages); // undefined
// ✅ 正确 - 始终 await
const result = await agent.invoke(...);
console.log(result.messages); // 可以工作!
# npm
npm install @langchain/langgraph
# yarn
yarn add @langchain/langgraph
# pnpm
pnpm add @langchain/langgraph
# 与 LangChain 一起使用(可选但常见)
npm install @langchain/core
# 生产持久化
npm install @langchain/langgraph-checkpoint-postgres
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
基于 SOC 职业分类