LangChain 和 Deep Agents 提供了用于常见用例的预构建中间件。每个中间件都适用于生产环境,并可根据您的具体需求进行配置。
与提供商无关的中间件
以下中间件适用于任何 LLM 提供商:
中间件 描述 摘要 在接近令牌限制时自动总结对话历史。 人工介入 暂停执行以等待人工批准工具调用。 模型调用限制 限制模型调用次数以防止过高成本。 工具调用限制 通过限制调用次数来控制工具执行。 模型回退 当主模型失败时自动回退到替代模型。 PII 检测 检测和處理个人身份信息 (PII)。 待办事项列表 为智能体配备任务规划和跟踪能力。 LLM 工具选择器 在主模型调用之前使用 LLM 选择相关工具。 工具重试 使用指数退避自动重试失败的工具调用。 模型重试 使用指数退避自动重试失败的模型调用。 LLM 工具模拟器 出于测试目的使用 LLM 模拟工具执行。 上下文编辑 通过修剪或清除工具使用来管理对话上下文。 文件系统 为智能体提供用于存储上下文和长期记忆的 filesystem。 子智能体中间件 添加启动子智能体的能力。
在接近令牌限制时自动总结对话历史,保留最近的消息同时压缩较旧的上下文。摘要功能适用于以下场景:
超过上下文窗口的长时间运行对话。
具有大量历史的多次对话。
需要保留完整对话上下文的应用程序。
import { createAgent , summarizationMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [weatherTool , calculatorTool] ,
middleware : [
summarizationMiddleware ( {
model : "gpt-4.1-mini" ,
trigger : { tokens : 4000 },
keep : { messages : 20 },
} ) ,
] ,
} ) ;
人工介入
在执行工具调用之前,暂停智能体执行以获取人工批准、编辑或拒绝。人工介入 适用于以下场景:
需要人工批准的高风险操作(例如数据库写入、金融交易)。
必须有人工监督的合规工作流。
人类反馈指导智能体的长时间运行对话。
人工介入中间件需要 检查点 来维持中断之间的状态。
import { createAgent , humanInTheLoopMiddleware } from "langchain" ;
function readEmailTool ( emailId : string ) : string {
/** Mock function to read an email by its ID. */
return `Email content for ID: ${ emailId } ` ;
}
function sendEmailTool ( recipient : string , subject : string , body : string ) : string {
/** Mock function to send an email. */
return `Email sent to ${ recipient } with subject ' ${ subject } '` ;
}
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [readEmailTool , sendEmailTool] ,
middleware : [
humanInTheLoopMiddleware ( {
interruptOn : {
sendEmailTool : {
allowedDecisions : [ "approve" , "edit" , "reject" ] ,
},
readEmailTool : false ,
}
} )
]
} ) ;
模型调用限制
限制模型调用次数以防止无限循环或过高成本。模型调用限制适用于以下场景:
防止失控的智能体进行过多的 API 调用。
在生产部署中强制执行成本控制。
在特定调用预算内测试智能体行为。
import { createAgent , modelCallLimitMiddleware } from "langchain" ;
import { MemorySaver } from "@langchain/langgraph" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
checkpointer : new MemorySaver () , // Required for thread limiting
tools : [] ,
middleware : [
modelCallLimitMiddleware ( {
threadLimit : 10 ,
runLimit : 5 ,
exitBehavior : "end" ,
} ) ,
] ,
} ) ;
达到限制时的行为。选项:'end'(优雅终止)或 'error'(抛出异常)
工具调用限制
通过限制工具调用次数来控制智能体执行,可以是全局所有工具或特定工具。工具调用限制适用于以下场景:
防止对昂贵的外部 API 进行过多调用。
限制网络搜索或数据库查询。
强制执行特定工具使用的速率限制。
防止失控的智能体循环。
import { createAgent , toolCallLimitMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , databaseTool] ,
middleware : [
toolCallLimitMiddleware ( { threadLimit : 20 , runLimit : 10 } ) ,
toolCallLimitMiddleware ( {
toolName : "search" ,
threadLimit : 5 ,
runLimit : 3 ,
} ) ,
] ,
} ) ;
要限制的具体工具名称。如果未提供,限制适用于 所有工具全局 。
线程(对话)中所有运行的最大工具调用数。在与相同线程 ID 的多次调用之间持久存在。需要检查点来维护状态。undefined 表示无线程限制。
单次调用(一次用户消息 → 响应周期)的最大工具调用数。每次新用户消息都会重置。undefined 表示无运行限制。 注意: 必须指定 threadLimit 或 runLimit 之一。
达到限制时的行为:
'continue'(默认)- 用错误消息阻止超出的工具调用,让其他工具和模型继续。模型根据错误消息决定何时结束。
'error' - 抛出 ToolCallLimitExceededError 异常,立即停止执行
'end' - 立即停止执行,并为超出的工具调用发送 ToolMessage 和 AI 消息。仅在限制单个工具时有效;如果其他工具有挂起的调用,则抛出错误。
指定限制:
线程限制 - 对话中所有运行的最大调用数(需要检查点)
运行限制 - 单次调用的最大调用数(每轮重置)
退出行为:
'continue'(默认)- 用错误消息阻止超出的调用,智能体继续
'error' - 立即抛出异常
'end' - 使用 ToolMessage + AI 消息停止(仅限单工具场景)
import { createAgent , toolCallLimitMiddleware } from "langchain" ;
const globalLimiter = toolCallLimitMiddleware ( { threadLimit : 20 , runLimit : 10 } ) ;
const searchLimiter = toolCallLimitMiddleware ( { toolName : "search" , threadLimit : 5 , runLimit : 3 } ) ;
const databaseLimiter = toolCallLimitMiddleware ( { toolName : "query_database" , threadLimit : 10 } ) ;
const strictLimiter = toolCallLimitMiddleware ( { toolName : "scrape_webpage" , runLimit : 2 , exitBehavior : "error" } ) ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , databaseTool , scraperTool] ,
middleware : [globalLimiter , searchLimiter , databaseLimiter , strictLimiter] ,
} ) ;
模型回退
当主模型失败时自动回退到替代模型。模型回退适用于以下场景:
构建能够处理模型故障的弹性智能体。
通过回退到更便宜的模型进行成本优化。
OpenAI、Anthropic 等提供商的冗余。
import { createAgent , modelFallbackMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
modelFallbackMiddleware (
"gpt-4.1-mini" ,
"claude-3-5-sonnet-20241022"
) ,
] ,
} ) ;
中间件接受可变数量的字符串参数,代表按顺序的回退模型: 当主模型失败时要按顺序尝试的一个或多个回退模型字符串 modelFallbackMiddleware (
"first-fallback-model" ,
"second-fallback-model" ,
// ... more models
)
PII 检测
使用可配置的策略检测和对话中的个人身份信息 (PII)。PII 检测适用于以下场景:
有合规要求的医疗和金融应用。
需要清理日志的客户代理。
任何处理敏感用户数据的应用程序。
import { createAgent , piiMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
piiMiddleware ( "email" , { strategy : "redact" , applyToInput : true } ) ,
piiMiddleware ( "credit_card" , { strategy : "mask" , applyToInput : true } ) ,
] ,
} ) ;
自定义 PII 类型
您可以通过提供 detector 参数来创建自定义 PII 类型。这允许您检测超出内置类型的特定于您用例的模式。
三种创建自定义检测器的方法:
正则表达式模式字符串 - 简单的模式匹配
RegExp 对象 - 对正则表达式标志有更多的控制
自定义函数 - 带有验证的复杂检测逻辑
import { createAgent , piiMiddleware , type PIIMatch } from "langchain" ;
// Method 1: Regex pattern string
const agent1 = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
piiMiddleware ( "api_key" , {
detector : "sk-[a-zA-Z0-9]{32}" ,
strategy : "block" ,
} ) ,
] ,
} ) ;
// Method 2: RegExp object
const agent2 = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
piiMiddleware ( "phone_number" , {
detector : /\+ ? \d {1,3} [ \s. - ] ? \d {3,4} [ \s. - ] ? \d {4} / ,
strategy : "mask" ,
} ) ,
] ,
} ) ;
// Method 3: Custom detector function
function detectSSN ( content : string ) : PIIMatch[] {
const matches : PIIMatch[] = [] ;
const pattern = /\d {3} - \d {2} - \d {4} / g ;
let match : RegExpExecArray | null ;
while ((match = pattern . exec (content)) !== null ) {
const ssn = match[ 0 ] ;
// Validate: first 3 digits shouldn't be 000, 666, or 900-999
const firstThree = parseInt (ssn . substring ( 0 , 3 ) , 10 ) ;
if (firstThree !== 0 && firstThree !== 666 && ! (firstThree >= 900 && firstThree <= 999 )) {
matches . push ( {
text : ssn ,
start : match . index ?? 0 ,
end : (match . index ?? 0 ) + ssn . length ,
} ) ;
}
}
return matches ;
}
const agent3 = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
piiMiddleware ( "ssn" , {
detector : detectSSN ,
strategy : "hash" ,
} ) ,
] ,
} ) ;
自定义检测器函数签名:
检测器函数必须接受一个字符串(内容)并返回匹配项:
返回 PIIMatch 对象数组:
interface PIIMatch {
text : string ; // The matched text
start : number ; // Start index in content
end : number ; // End index in content
}
function detector ( content : string ) : PIIMatch[] {
return [
{ text : "matched_text" , start : 0 , end : 12 },
// ... more matches
] ;
}
对于自定义检测器:
对于简单模式使用正则表达式字符串
当您需要标志时使用 RegExp 对象(例如,不区分大小写的匹配)
当您需要模式匹配之外的验证逻辑时使用自定义函数
自定义函数为您提供对检测逻辑的完全控制,并且可以实现复杂的验证规则
要检测的 PII 类型。可以是内置类型(email、credit_card、ip、mac_address、url)或自定义类型名称。
如何处理检测到的 PII。选项:
'block' - 检测到时抛出错误
'redact' - 替换为 [REDACTED_TYPE]
'mask' - 部分屏蔽(例如,****-****-****-1234)
'hash' - 替换为确定性哈希(例如,<email_hash:a1b2c3d4>)
detector
RegExp | string | function
自定义检测器。可以是:
RegExp - 用于匹配的_regex 模式
string - 正则表达式模式字符串(例如,"sk-[a-zA-Z0-9]{32}")
function - 自定义检测器函数 (content: string) => PIIMatch[]
如果未提供,则使用该 PII 类型的内置检测器。
待办事项列表
为智能体配备任务规划和跟踪能力,以处理复杂的多步骤任务。待办事项列表适用于以下场景:
需要跨多个工具协调的复杂多步骤任务。
进度可见性很重要的长时间运行操作。
此中间件会自动为智能体提供 write_todos 工具和系统提示,以引导有效的任务规划。
import { createAgent , todoListMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [readFile , writeFile , runTests] ,
middleware : [ todoListMiddleware ()] ,
} ) ;
没有可用的配置选项(使用默认值)。
LLM 工具选择器
使用 LLM 在主模型调用之前智能选择相关工具。LLM 工具选择器适用于以下场景:
拥有许多工具(10+)且大多数与查询不相关的智能体。
通过过滤不相关工具减少令牌使用量。
提高模型专注度和准确性。
此中间件使用结构化输出来询问 LLM 哪些工具与当前查询最相关。结构化输出模式定义可用工具的名称和描述。模型提供商通常会在幕后将此结构化输出信息添加到系统提示中。
import { createAgent , llmToolSelectorMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [tool1 , tool2 , tool3 , tool4 , tool5 , ... ] ,
middleware : [
llmToolSelectorMiddleware ( {
model : "gpt-4.1-mini" ,
maxTools : 3 ,
alwaysInclude : [ "search" ] ,
} ) ,
] ,
} ) ;
用于工具选择的模型。可以是模型标识符字符串(例如 'openai:gpt-4.1-mini')或 BaseChatModel 实例。默认为智能体的主模型。
要选择的工具最大数量。如果模型选择了更多,则只使用前 maxTools。如果未指定,则无限制。
无论选择如何都要包含的工具名称。这些不计入 maxTools 限制。
工具重试
使用可配置指数退避自动重试失败的工具调用。工具重试适用于以下场景:
处理外部 API 调用的瞬态故障。
提高依赖网络的工具的可靠性。
构建能够优雅处理临时错误的弹性智能体。
API 参考: toolRetryMiddleware
import { createAgent , toolRetryMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , databaseTool] ,
middleware : [
toolRetryMiddleware ( {
maxRetries : 3 ,
backoffFactor : 2.0 ,
initialDelayMs : 1000 ,
} ) ,
] ,
} ) ;
初始调用后的最大重试次数(默认情况下共 3 次尝试)。必须 >= 0。
tools
(ClientTool | ServerTool | string)[]
可选的工具或工具名称数组,以应用重试逻辑。可以是 BaseTool 实例列表或工具名称字符串。如果为 undefined,则适用于所有工具。
retryOn
((error: Error) => boolean) | (new (...args: any[]) => Error)[]
default: "() => true"
要么是重试的错误构造函数数组,要么是接受错误并返回 true 是否应重试的函数。默认是对所有错误重试。
onFailure
'error' | 'continue' | ((error: Error) => string)
default: "continue"
所有重试耗尽时的行为。选项:
'continue'(默认)- 返回带有错误详情的 ToolMessage,允许 LLM 处理失败并可能恢复
'error' - 重新抛出异常,停止智能体执行
自定义函数 - 函数接受异常并返回 ToolMessage 内容的字符串,允许自定义错误格式
已弃用值: 'raise'(改用 'error')和 'return_message'(改用 'continue')。这些已弃用值仍然有效但会显示警告。指数退避的乘数。每次重试等待 initialDelayMs * (backoffFactor ** retryNumber) 毫秒。设置为 0.0 表示恒定延迟。必须 >= 0。
重试之间的最大延迟(毫秒)(限制指数退避增长)。必须 >= 0。
中间件使用指数退避自动重试失败的工具调用。 关键配置:
maxRetries - 重试次数(默认:2)
backoffFactor - 指数退避乘数(默认:2.0)
initialDelayMs - 起始延迟(毫秒)(默认:1000ms)
maxDelayMs - 延迟增长上限(默认:60000ms)
jitter - 添加随机变化(默认:true)
失败处理:
onFailure: "continue"(默认)- 返回错误消息
onFailure: "error" - 重新抛出异常
自定义函数 - 返回错误消息的函数
import { createAgent , toolRetryMiddleware } from "langchain" ;
import { tool } from "@langchain/core/tools" ;
import { z } from "zod" ;
// Basic usage with default settings (2 retries, exponential backoff)
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , databaseTool] ,
middleware : [ toolRetryMiddleware ()] ,
} ) ;
// Retry specific exceptions only
const retry = toolRetryMiddleware ( {
maxRetries : 4 ,
retryOn : [TimeoutError , NetworkError] ,
backoffFactor : 1.5 ,
} ) ;
// Custom exception filtering
function shouldRetry ( error : Error ) : boolean {
// Only retry on 5xx errors
if (error . name === "HTTPError" && "statusCode" in error) {
const statusCode = (error as any ) . statusCode ;
return 500 <= statusCode && statusCode < 600 ;
}
return false ;
}
const retryWithFilter = toolRetryMiddleware ( {
maxRetries : 3 ,
retryOn : shouldRetry ,
} ) ;
// Apply to specific tools with custom error handling
const formatError = ( error : Error ) =>
"Database temporarily unavailable. Please try again later." ;
const retrySpecificTools = toolRetryMiddleware ( {
maxRetries : 4 ,
tools : [ "search_database" ] ,
onFailure : formatError ,
} ) ;
// Apply to specific tools using BaseTool instances
const searchDatabase = tool (
async ({ query }) => {
// Search implementation
return results ;
},
{
name : "search_database" ,
description : "Search the database" ,
schema : z . object ( { query : z . string () } ) ,
}
) ;
const retryWithToolInstance = toolRetryMiddleware ( {
maxRetries : 4 ,
tools : [searchDatabase] , // Pass BaseTool instance
} ) ;
// Constant backoff (no exponential growth)
const constantBackoff = toolRetryMiddleware ( {
maxRetries : 5 ,
backoffFactor : 0.0 , // No exponential growth
initialDelayMs : 2000 , // Always wait 2 seconds
} ) ;
// Raise exception on failure
const strictRetry = toolRetryMiddleware ( {
maxRetries : 2 ,
onFailure : "error" , // Re-raise exception instead of returning message
} ) ;
模型重试
使用可配置指数退避自动重试失败的模型调用。模型重试适用于以下场景:
处理模型 API 调用的瞬态故障。
提高依赖网络的模型请求的可靠性。
构建能够优雅处理临时模型错误的弹性智能体。
API 参考: modelRetryMiddleware
import { createAgent , modelRetryMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , databaseTool] ,
middleware : [
modelRetryMiddleware ( {
maxRetries : 3 ,
backoffFactor : 2.0 ,
initialDelayMs : 1000 ,
} ) ,
] ,
} ) ;
初始调用后的最大重试次数(默认情况下共 3 次尝试)。必须 >= 0。
retryOn
((error: Error) => boolean) | (new (...args: any[]) => Error)[]
default: "() => true"
要么是重试的错误构造函数数组,要么是接受错误并返回 true 是否应重试的函数。默认是对所有错误重试。
onFailure
'error' | 'continue' | ((error: Error) => string)
default: "continue"
所有重试耗尽时的行为。选项:
'continue'(默认)- 返回带有错误详情的 AIMessage,允许智能体可能优雅地处理失败
'error' - 重新抛出异常,停止智能体执行
自定义函数 - 函数接受异常并返回 AIMessage 内容的字符串,允许自定义错误格式
指数退避的乘数。每次重试等待 initialDelayMs * (backoffFactor ** retryNumber) 毫秒。设置为 0.0 表示恒定延迟。必须 >= 0。
重试之间的最大延迟(毫秒)(限制指数退避增长)。必须 >= 0。
中间件使用指数退避自动重试失败的模型调用。 import { createAgent , modelRetryMiddleware } from "langchain" ;
// Basic usage with default settings (2 retries, exponential backoff)
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool] ,
middleware : [ modelRetryMiddleware ()] ,
} ) ;
class TimeoutError extends Error {
// ...
}
class NetworkError extends Error {
// ...
}
// Retry specific exceptions only
const retry = modelRetryMiddleware ( {
maxRetries : 4 ,
retryOn : [TimeoutError , NetworkError] ,
backoffFactor : 1.5 ,
} ) ;
// Custom exception filtering
function shouldRetry ( error : Error ) : boolean {
// Only retry on rate limit errors
if (error . name === "RateLimitError" ) {
return true ;
}
// Or check for specific HTTP status codes
if (error . name === "HTTPError" && "statusCode" in error) {
const statusCode = (error as any ) . statusCode ;
return statusCode === 429 || statusCode === 503 ;
}
return false ;
}
const retryWithFilter = modelRetryMiddleware ( {
maxRetries : 3 ,
retryOn : shouldRetry ,
} ) ;
// Return error message instead of raising
const retryContinue = modelRetryMiddleware ( {
maxRetries : 4 ,
onFailure : "continue" , // Return AIMessage with error instead of throwing
} ) ;
// Custom error message formatting
const formatError = ( error : Error ) =>
`Model call failed: ${ error . message } . Please try again later.` ;
const retryWithFormatter = modelRetryMiddleware ( {
maxRetries : 4 ,
onFailure : formatError ,
} ) ;
// Constant backoff (no exponential growth)
const constantBackoff = modelRetryMiddleware ( {
maxRetries : 5 ,
backoffFactor : 0.0 , // No exponential growth
initialDelayMs : 2000 , // Always wait 2 seconds
} ) ;
// Raise exception on failure
const strictRetry = modelRetryMiddleware ( {
maxRetries : 2 ,
onFailure : "error" , // Re-raise exception instead of returning message
} ) ;
LLM 工具模拟器
使用 LLM 模拟工具执行以进行测试目的,用 AI 生成的响应替换实际工具调用。LLM 工具模拟器适用于以下场景:
在不执行真实工具的情况下测试智能体行为。
在外部工具不可用或昂贵时开发智能体。
在实际实现工具之前原型化智能体工作流。
import { createAgent , toolEmulatorMiddleware } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [getWeather , searchDatabase , sendEmail] ,
middleware : [
toolEmulatorMiddleware () , // Emulate all tools
] ,
} ) ;
tools
(string | ClientTool | ServerTool)[]
要模拟的工具名称 (string) 或工具实例列表。如果为 undefined(默认),则模拟 所有 工具。如果为空数组 [],则不模拟任何工具。如果为包含工具名称/实例的数组,则仅模拟这些工具。
用于生成模拟工具响应的模型。可以是模型标识符字符串(例如 'anthropic:claude-sonnet-4-6')或 BaseChatModel 实例。如果未指定,默认为智能体的模型。
中间件使用 LLM 为工具调用生成合理的响应,而不是执行实际工具。 import { createAgent , toolEmulatorMiddleware , tool } from "langchain" ;
import * as z from "zod" ;
const getWeather = tool (
async ({ location }) => `Weather in ${ location } ` ,
{
name : "get_weather" ,
description : "Get the current weather for a location" ,
schema : z . object ( { location : z . string () } ) ,
}
) ;
const sendEmail = tool (
async ({ to , subject , body }) => "Email sent" ,
{
name : "send_email" ,
description : "Send an email" ,
schema : z . object ( {
to : z . string () ,
subject : z . string () ,
body : z . string () ,
} ) ,
}
) ;
// Emulate all tools (default behavior)
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [getWeather , sendEmail] ,
middleware : [ toolEmulatorMiddleware ()] ,
} ) ;
// Emulate specific tools by name
const agent2 = createAgent ( {
model : "gpt-4.1" ,
tools : [getWeather , sendEmail] ,
middleware : [
toolEmulatorMiddleware ( {
tools : [ "get_weather" ] ,
} ) ,
] ,
} ) ;
// Emulate specific tools by passing tool instances
const agent3 = createAgent ( {
model : "gpt-4.1" ,
tools : [getWeather , sendEmail] ,
middleware : [
toolEmulatorMiddleware ( {
tools : [getWeather] ,
} ) ,
] ,
} ) ;
// Use custom model for emulation
const agent5 = createAgent ( {
model : "gpt-4.1" ,
tools : [getWeather , sendEmail] ,
middleware : [
toolEmulatorMiddleware ( {
model : "claude-sonnet-4-6" ,
} ) ,
] ,
} ) ;
上下文编辑
通过清除达到令牌限制时的旧工具调用输出(同时保留最近的结果)来管理对话上下文。这有助于在具有许多工具调用的长对话中保持上下文窗口可控。上下文编辑适用于以下场景:
具有许多工具调用且超过令牌限制的长对话
通过移除不再相关的旧工具输出来减少令牌成本
仅保留上下文中最近的 N 个工具结果
import { createAgent , contextEditingMiddleware , ClearToolUsesEdit } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [] ,
middleware : [
contextEditingMiddleware ( {
edits : [
new ClearToolUsesEdit ( {
triggerTokens : 100000 ,
keep : 3 ,
} ) ,
] ,
} ) ,
] ,
} ) ;
edits
ContextEdit[]
default: "[new ClearToolUsesEdit()]"
ClearToolUsesEdit 选项:触发编辑的令牌数量。当对话超过此令牌数量时,将清除旧的工具输出。
编辑运行时至少要回收的令牌数量。如果设置为 0,则清除所需的一切。
是否清除 AI 消息上原始工具调用的参数。当为 true 时,工具调用参数替换为空对象。
要从清除中排除的工具名称列表。这些工具永远不会清除其输出。
placeholder
string
default: "[cleared]"
为清除的工具输出插入的占位符文本。这替换了原始工具消息内容。
中间件在达到令牌限制时应用上下文编辑策略。最常见的策略是 ClearToolUsesEdit,它清除旧的工具结果同时保留最新的。 工作原理:
监控对话中的令牌数量
达到阈值时,清除旧的工具输出
保留最近的 N 个工具结果
可选择保留工具调用参数以用于上下文
import { createAgent , contextEditingMiddleware , ClearToolUsesEdit } from "langchain" ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [searchTool , calculatorTool , databaseTool] ,
middleware : [
contextEditingMiddleware ( {
edits : [
new ClearToolUsesEdit ( {
triggerTokens : 2000 ,
keep : 3 ,
clearToolInputs : false ,
excludeTools : [] ,
placeholder : "[cleared]" ,
} ) ,
] ,
} ) ,
] ,
} ) ;
文件系统中间件
上下文工程是构建有效智能体的主要挑战之一。特别是当使用返回可变长度结果的工具(例如 web_search 和 RAG)时,因为长工具结果会迅速填满上下文窗口。
Deep Agents 中的 FilesystemMiddleware 提供了四个用于交互短期和长期记忆的工具:
ls: 列出文件系统中的文件
read_file: 读取整个文件或文件的特定行数
write_file: 向文件系统写入新文件
edit_file: 编辑文件系统中的现有文件
import { createAgent } from "langchain" ;
import { createFilesystemMiddleware } from "deepagents" ;
// FilesystemMiddleware is included by default in createDeepAgent
// You can customize it if building a custom agent
const agent = createAgent ( {
model : "claude-sonnet-4-6" ,
middleware : [
createFilesystemMiddleware ( {
backend : undefined , // Optional: custom backend (defaults to StateBackend)
systemPrompt : "Write to the filesystem when..." , // Optional custom system prompt override
customToolDescriptions : {
ls : "Use the ls tool when..." ,
read_file : "Use the read_file tool to..." ,
}, // Optional: Custom descriptions for filesystem tools
} ) ,
] ,
} ) ;
短期与长期文件系统
默认情况下,这些工具写入图状态中的本地“文件系统”。要启用跨线程的持久存储,请配置 CompositeBackend,将特定路径(如 /memories/)路由到 StoreBackend。
import { createAgent } from "langchain" ;
import { createFilesystemMiddleware , CompositeBackend , StateBackend , StoreBackend } from "deepagents" ;
import { InMemoryStore } from "@langchain/langgraph-checkpoint" ;
const store = new InMemoryStore () ;
const agent = createAgent ( {
model : "claude-sonnet-4-6" ,
store ,
middleware : [
createFilesystemMiddleware ( {
backend : ( config ) => new CompositeBackend (
new StateBackend (config) ,
{ "/memories/" : new StoreBackend (config) }
) ,
systemPrompt : "Write to the filesystem when..." , // Optional custom system prompt override
customToolDescriptions : {
ls : "Use the ls tool when..." ,
read_file : "Use the read_file tool to..." ,
}, // Optional: Custom descriptions for filesystem tools
} ) ,
] ,
} ) ;
当您为 /memories/ 配置带有 StoreBackend 的 CompositeBackend 时,任何以 /memories/ 开头的文件都将保存到持久存储中,并在不同线程之间生存。没有此前缀的文件保留在易失性状态存储中。
子智能体
将任务委托给子智能体可以隔离上下文,使主(主管)智能体的上下文窗口保持清洁,同时仍能深入处理任务。
Deep Agents 中的子智能体中间件允许您通过 task 工具提供子智能体。
import { tool } from "langchain" ;
import { createAgent } from "langchain" ;
import { createSubAgentMiddleware } from "deepagents" ;
import { z } from "zod" ;
const getWeather = tool (
async ({ city } : { city : string }) => {
return `The weather in ${ city } is sunny.` ;
},
{
name : "get_weather" ,
description : "Get the weather in a city." ,
schema : z . object ( {
city : z . string () ,
} ) ,
},
) ;
const agent = createAgent ( {
model : "claude-sonnet-4-6" ,
middleware : [
createSubAgentMiddleware ( {
defaultModel : "claude-sonnet-4-6" ,
defaultTools : [] ,
subagents : [
{
name : "weather" ,
description : "This subagent can get weather in cities." ,
systemPrompt : "Use the get_weather tool to get the weather in a city." ,
tools : [getWeather] ,
model : "gpt-4.1" ,
middleware : [] ,
},
] ,
} ) ,
] ,
} ) ;
子智能体由 名称 、描述 、系统提示 和 工具 定义。您还可以为子智能体提供自定义 模型 或额外的 中间件 。这在您希望给予子智能体与主智能体共享的额外状态键时特别有用。
对于更复杂的用例,您也可以提供自己的预构建 LangGraph 图作为子智能体。
import { tool , createAgent } from "langchain" ;
import { createSubAgentMiddleware , type SubAgent } from "deepagents" ;
import { z } from "zod" ;
const getWeather = tool (
async ({ city } : { city : string }) => {
return `The weather in ${ city } is sunny.` ;
},
{
name : "get_weather" ,
description : "Get the weather in a city." ,
schema : z . object ( {
city : z . string () ,
} ) ,
},
) ;
const weatherSubagent : SubAgent = {
name : "weather" ,
description : "This subagent can get weather in cities." ,
systemPrompt : "Use the get_weather tool to get the weather in a city." ,
tools : [getWeather] ,
model : "gpt-4.1" ,
middleware : [] ,
};
const agent = createAgent ( {
model : "claude-sonnet-4-6" ,
middleware : [
createSubAgentMiddleware ( {
defaultModel : "claude-sonnet-4-6" ,
defaultTools : [] ,
subagents : [weatherSubagent] ,
} ) ,
] ,
} ) ;
除了任何用户定义的子智能体外,主智能体始终可以访问 general-purpose 子智能体。该子智能体具有与主智能体相同的指令和它有权访问的所有工具。general-purpose 子智能器的主要目的是上下文隔离——主智能体可以将复杂任务委托给此子智能器并获得简洁的答案,而不会因中间工具调用而产生膨胀。
特定于提供商的中间件
这些中间件针对特定的 LLM 提供商进行了优化。有关完整详细信息和示例,请参阅每个提供商的文档。
Anthropic 用于 Claude 模型的提示缓存、bash 工具、文本编辑器、内存和文件搜索中间件。