LangChain v1 是一个专注于生产环境的智能体构建基础框架。 我们围绕三个核心改进精简了框架:
createAgent
一种在 LangChain 中构建智能体的新标准方法,取代了 createReactAgent 来自 LangGraph,提供更简洁、更强大的 API。
Standard content blocks
一个新的 contentBlocks 属性,提供跨所有提供商的现代 LLM 功能统一访问。
Simplified package
langchain 包已精简,专注于智能体的基本构建块,旧功能已移至 @langchain/classic.
升级方法:
npm install langchain @langchain/core
pnpm install langchain @langchain/core
yarn add langchain @langchain/core
bun add langchain @langchain/core
有关完整的变更列表,请参阅 迁移指南.
createAgent
createAgent 是在 LangChain 1.0 中构建智能体的标准方式。它比从 LangGraph 导出的预构建 createReactAgent 提供更简单的接口,同时通过使用中间件提供更大的定制潜力。
const agent = createAgent({
model: "claude-sonnet-4-6",
tools: [getWeather],
systemPrompt: "You are a helpful assistant.",
});
const result = await agent.invoke({
messages: [
{ role: "user", content: "What is the weather in Tokyo?" },
],
});
console.log(result.content);
在底层, createAgent 建立在基本的智能体循环之上 -- 调用模型,让它选择要执行的工具,然后在它不再调用工具时完成:
更多信息,请参阅 智能体.
中间件
中间件是 createAgent的定义特性。它使 createAgent 高度可定制,提高了您可以构建的上限。
优秀的智能体需要 上下文工程:在正确的时间向模型提供正确的信息。中间件通过可组合的抽象帮助您控制动态提示、对话摘要、选择性工具访问、状态管理和防护栏。
预构建中间件
LangChain 提供了一些 预构建中间件 用于常见模式,包括:
- -
summarizationMiddleware:当对话历史过长时压缩对话历史 - -
humanInTheLoopMiddleware:需要敏感工具调用的审批 - -
piiRedactionMiddleware:在发送到模型之前删除敏感信息
createAgent,
summarizationMiddleware,
humanInTheLoopMiddleware,
piiRedactionMiddleware,
} from "langchain";
const agent = createAgent({
model: "claude-sonnet-4-6",
tools: [readEmail, sendEmail],
middleware: [
piiRedactionMiddleware({ patterns: ["email", "phone", "ssn"] }),
summarizationMiddleware({
model: "claude-sonnet-4-6",
trigger: { tokens: 500 },
}),
humanInTheLoopMiddleware({
interruptOn: {
sendEmail: {
allowedDecisions: ["approve", "edit", "reject"],
},
},
}),
],
});
自定义中间件
您也可以构建自定义中间件以满足您的特定需求。
通过使用以下方式实现任何钩子来构建自定义中间件: createMiddleware function:
| 钩子 | 运行时机 | 用例 |
|---|---|---|
beforeAgent | 在调用智能体之前 | 加载记忆、验证输入 |
beforeModel | 在每次 LLM 调用之前 | 更新提示、修剪消息 |
wrapModelCall | Around each LLM call | Intercept and modify requests/responses |
wrapToolCall | 在每个工具调用周围 | 拦截和修改工具执行 |
afterModel | 在每次 LLM 响应之后 | 验证输出、应用防护栏 |
afterAgent | 在智能体完成后 | 保存结果、清理 |
示例自定义中间件:
const contextSchema = z.object({
userExpertise: z.enum(["beginner", "expert"]).default("beginner"),
})
const expertiseBasedToolMiddleware = createMiddleware({
wrapModelCall: async (request, handler) => {
const userLevel = request.runtime.context.userExpertise;
if (userLevel === "expert") {
const tools = [advancedSearch, dataAnalysis];
return handler(
request.replace("openai:gpt-5.5", tools)
);
}
const tools = [simpleSearch, basicCalculator];
return handler(
request.replace("openai:gpt-5-nano", tools)
);
},
});
const agent = createAgent({
model: "claude-sonnet-4-6",
tools: [simpleSearch, advancedSearch, basicCalculator, dataAnalysis],
middleware: [expertiseBasedToolMiddleware],
contextSchema,
});
更多信息,请参阅 完整的中间件指南.
基于 LangGraph 构建
因为 createAgent 基于 LangGraph 构建,您可以自动获得对长时间运行和可靠代理的内置支持,通过:
Persistence
对话通过内置检查点自动跨会话持久化
Streaming
实时流式传输 token、工具调用和推理追踪
Human-in-the-loop
在敏感操作前暂停代理执行以等待人工批准
Time travel
将对话倒回任意时间点并探索替代路径和提示
您无需学习 LangGraph 即可使用这些功能——它们开箱即用。
结构化输出
createAgent 改进了结构化输出生成:
- 主循环集成:结构化输出现在在主循环中生成,无需额外的 LLM 调用
- 结构化输出策略:模型可以在调用工具或使用提供商端结构化输出之间进行选择
- 成本降低:消除了额外 LLM 调用产生的费用
const weatherSchema = z.object({
temperature: z.number(),
condition: z.string(),
});
const agent = createAgent({
model: "gpt-5.4-mini",
tools: [getWeather],
responseFormat: weatherSchema,
});
const result = await agent.invoke({
messages: [
{ role: "user", content: "What is the weather in Tokyo?" },
],
});
console.log(result.structuredResponse);
错误处理:通过 handleErrors 参数控制错误处理 ToolStrategy: - **解析错误**:模型生成的数据与预期结构不匹配 - **多次工具调用**:模型为结构化输出架构生成了 2 个或更多工具调用
标准内容块
优势
- 提供商无关:使用相同的 API 访问推理追踪、引用、内置工具(网络搜索、代码解释器等)和其他功能,不受提供商限制
- 类型安全:所有内容块类型均有完整的类型提示
- 向后兼容:标准内容可以 延迟加载,因此没有关联的破坏性变更
更多信息,请参阅我们关于 内容块
精简包
LangChain v1 简化了 langchain 包命名空间,专注于代理的基本构建块。该包仅暴露最有用和相关的功能:
其中大部分是从 @langchain/core 重新导出的,以方便使用,这为您提供了一个专注于构建代理的 API 界面。
@langchain/classic
遗留功能已移至 @langchain/classic 以保持核心包精简且专注。
包含内容 @langchain/classic
- - 遗留链和链实现
- - 检索器
- - 索引 API
- -
@langchain/community导出 - - 其他已弃用的功能
如果您使用任何这些功能,请安装 @langchain/classic:
npm install @langchain/classic
pnpm install @langchain/classic
yarn add @langchain/classic
bun add @langchain/classic
然后更新您的导入:
报告问题
请在 GitHub 上报告在 1.0 中发现的任何问题 GitHub 使用 'v1' 标签.
其他资源
LangChain 1.0
阅读公告
Middleware guide
深入了解中间件
Agents Documentation
完整代理文档
Message Content
新的内容块 API
Migration guide
如何迁移到 LangChain v1
GitHub
报告问题或贡献