专为托管在 AWS Bedrock 上的模型设计的中间件。了解更多关于 中间件.
| 中间件 | 描述 |
|---|---|
| 提示词缓存 | 通过缓存重复的提示词前缀来降低成本 |
提示词缓存
通过在 Amazon Bedrock 上缓存经常重用的提示词前缀来降低推理延迟和输入 token 成本。 bedrockPromptCachingMiddleware 通过以下方式启用缓存 modelSettings. ChatBedrockConverse 然后在请求时将其转换为正确的 AWS 线路格式。缓存检查点被放置在系统提示词、函数定义以及最新消息之后(在支持的范围内),以便模型可以在后续请求中跳过先前已处理内容的重新计算。缓存放置因模型系列而异:例如,Nova 会跳过某些函数定义和函数结果的情况。
提示词缓存适用于以下场景:
- - 具有长且一致的系统提示词的多轮对话
- - 具有多个在调用之间保持不变的函数定义的代理
- - 基于文档的问答,用户在相同的上传上下文中提出多个问题
- - 具有重复静态内容的批处理工作负载
支持的模型:
- Anthropic Claude
- Amazon Nova
API 参考: BedrockPromptCachingMiddleware
const agent = createAgent({
model: "bedrock:us.anthropic.claude-sonnet-4-5-20250929-v1:0",
systemPrompt: "",
middleware: [bedrockPromptCachingMiddleware({ ttl: "1h" })], // [!code highlight]
});
const agent = createAgent({
model: new ChatBedrockConverse({ model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0" }),
systemPrompt: "",
middleware: [bedrockPromptCachingMiddleware({ ttl: "5m" })], // [!code highlight]
});
Configuration options
是否应用提示词缓存。
缓存内容的生存时间。有效值: '5m' or '1h'。请注意,Amazon Nova 模型仅支持 '5m'.
开始缓存前的最小消息数。系统提示词算作一条消息。
使用不支持的模型时的行为。选项: 'ignore', 'warn', or 'raise'.
Full example
中间件将每个请求中的内容缓存至并包括最新消息。在 TTL 窗口内(5 分钟或 1 小时)的后续请求中,先前已处理的内容会从缓存中检索,而非重新处理,从而降低成本和延迟。
工作原理: 1. 第一个请求:系统提示词、函数和用户消息被发送到 API 并缓存 2. 第二个请求:从缓存中检索已缓存的内容。只需处理新消息 3. 每一轮都遵循此模式,每个请求都会重用缓存的对话历史
const getWeather = tool(
async ({ city }) => `The weather in ${city} is sunny and 72F.`,
{
name: "get_weather",
description: "Get the current weather for a city.",
schema: z.object({ city: z.string() }),
}
);
// System prompt must exceed 1,024 tokens for caching to take effect
const LONG_PROMPT =
"You are a helpful weather assistant with deep expertise in meteorology, " +
"climate science, and atmospheric phenomena. When answering questions about " +
"weather, provide accurate and up-to-date information. " +
"You should always strive to give the most helpful response possible. ".repeat(85);
const agent = createAgent({
model: new ChatBedrockConverse({ model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0" }),
systemPrompt: LONG_PROMPT,
tools: [getWeather],
middleware: [bedrockPromptCachingMiddleware({ ttl: "5m" })], // [!code highlight]
});
// First invocation: writes the cache (system prompt, tool definitions, and message)
let response = await agent.invoke({
messages: [new HumanMessage("What is the weather in Miami?")],
});
const last = response.messages.at(-1);
console.log(last?.content);
// Check cache token usage
if (AIMessage.isInstance(last)) {
const details = last.usage_metadata?.input_token_details;
if (details) {
console.log(`Cache read: ${details.cache_read ?? 0}, Cache write: ${details.cache_creation ?? 0}`);
}
}
// Second invocation within the TTL: reuses the cached system prompt and tool definitions
response = await agent.invoke({
messages: [new HumanMessage("How about Seattle?")],
});
console.log(response.messages.at(-1)?.content);
模型特定行为
中间件会自动处理模型系列之间的差异:
| 功能 | ChatBedrockConverse (Anthropic) | ChatBedrockConverse (Nova) |
|---|---|---|
| 系统提示词缓存 | ✅ | ✅ |
| 工具定义缓存 | ✅ | ❌ |
| 消息缓存 | ✅ | ✅ (不包括工具结果消息) |
延长 TTL (1h) | ✅ | ❌ |