专为托管在 AWS Bedrock 上的模型设计的中间件。了解更多关于 中间件.
| 中间件 | 描述 |
|---|---|
| 提示词缓存 | 通过缓存重复的提示词前缀来降低成本 |
提示词缓存
通过在 Amazon Bedrock 上缓存频繁重用的提示词前缀来降低推理延迟和输入 token 成本。 BedrockPromptCachingMiddleware 通过以下方式启用缓存 model_settings. ChatBedrock 和 ChatBedrockConverse 然后在请求时将其转换为正确的 AWS 线路格式。缓存检查点放置在系统提示词、工具定义之后,以及支持的最新消息之后,以便模型可以跳过后续请求中已看到内容的重新计算。缓存放置因 API 和模型系列而异:例如,Nova 会跳过某些工具定义和工具结果的情况。
提示词缓存适用于以下场景:
- - 具有长且一致的系统提示词的多轮对话
- - 具有许多在调用之间保持不变的工具定义的智能体
- - 文档问答,用户在同一上传上下文中提出多个问题
- - 具有重复静态内容的批处理工作负载
支持的模型:
- Anthropic Claude
- Amazon Nova
API 参考: BedrockPromptCachingMiddleware
from langchain_aws import ChatBedrockConverse
from langchain_aws.middleware.prompt_caching import BedrockPromptCachingMiddleware
from langchain.agents import create_agent
agent = create_agent(
model=ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-5-20250929-v1:0"),
system_prompt="",
middleware=[BedrockPromptCachingMiddleware(ttl="1h")], # [!code highlight]
)
from langchain_aws import ChatBedrock
from langchain_aws.middleware.prompt_caching import BedrockPromptCachingMiddleware
from langchain.agents import create_agent
agent = create_agent(
model=ChatBedrock(model="us.anthropic.claude-sonnet-4-5-20250929-v1:0"),
system_prompt="",
middleware=[BedrockPromptCachingMiddleware(ttl="5m")], # [!code highlight]
)
Configuration options
缓存类型。对于 ChatBedrock,目前仅支持 'ephemeral' 。对于 ChatBedrockConverse,此值被忽略,因为 Converse API 始终使用 "default" 缓存类型。
缓存内容的生存时间。有效值: '5m' or '1h'。请注意,Amazon Nova 模型仅支持 '5m'.
开始缓存前的最小消息数。
使用不支持的模型时的行为。选项: 'ignore', 'warn', or 'raise'.
Full example
中间件将每个请求中的内容缓存到最新消息为止。在 TTL 窗口内(5 分钟或 1 小时)的后续请求中,之前看到的内容从缓存中检索,而不是重新处理,从而降低成本和延迟。
工作原理: 1. 第一个请求:系统提示词、工具和用户消息被发送到 API 并缓存 2. 第二个请求:从缓存中检索缓存的内容。只需要处理新消息 3. 每个轮次都延续此模式,每个请求都重用缓存的对话历史
from langchain_aws import ChatBedrockConverse
from langchain_aws.middleware.prompt_caching import BedrockPromptCachingMiddleware
from langchain.agents import create_agent
from langchain_core.runnables import RunnableConfig
from langchain.messages import HumanMessage
from langchain.tools import tool
from langgraph.checkpoint.memory import MemorySaver
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"The weather in {city} is sunny and 72F."
# System prompt must exceed 1,024 tokens for caching to take effect
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. " * 85
)
agent = create_agent(
model=ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-5-20250929-v1:0"),
system_prompt=LONG_PROMPT,
tools=[get_weather],
middleware=[BedrockPromptCachingMiddleware(ttl="5m")], # [!code highlight]
checkpointer=MemorySaver(), # Persists conversation history
)
# Use a thread_id to maintain conversation state
config: RunnableConfig = {"configurable": {"thread_id": "user-123"}}
# First invocation: Creates cache with system prompt, tools, and user message
response = agent.invoke(
{"messages": [HumanMessage("What is the weather in Miami?")]}, config=config
)
last_msg = response["messages"][-1]
print(last_msg.content)
# Check cache token usage
um = last_msg.usage_metadata
if um:
details = um.get("input_token_details", {})
cache_read = details.get("cache_read", 0) or 0
cache_write = details.get("cache_creation", 0) or 0
print(f"Cache read: {cache_read}, Cache write: {cache_write}")
# Second invocation: Reuses cached system prompt, tools, and previous messages
response = agent.invoke(
{"messages": [HumanMessage("How about Seattle?")]}, config=config
)
print(response["messages"][-1].content)
特定模型行为
中间件自动处理不同 API 和模型系列之间的差异:
| 功能 | ChatBedrockConverse (Anthropic) | ChatBedrockConverse (Nova) | ChatBedrock (Anthropic) |
|---|---|---|---|
| 系统提示词缓存 | ✅ | ✅ | ✅ |
| 工具定义缓存 | ✅ | ❌ | ✅ |
| 消息缓存 | ✅ | ✅(排除工具结果消息) | ✅ |
扩展 TTL(1h) | ✅ | ❌ | ✅ |