bedrockPromptCachingMiddleware(
middlewareOptions: Partial<__type>
): AgentMiddleware<undefined, ZodObject<__type, "strip", ZodTypeAnyBasic usage with default settings
Custom configuration for longer conversations
Conditional caching based on runtime context
Optimal setup for customer support chatbot
Creates a prompt caching middleware for AWS Bedrock Converse models to optimize API usage.
This middleware automatically enables Bedrock's prompt caching when using AWS Bedrock Converse models. This can significantly reduce costs for applications with repetitive prompts, long system messages, or extensive conversation histories.
The middleware intercepts model requests and sets a cache control signal that
ChatBedrockConverse translates into Bedrock cachePoint breakpoints. Cache points are
inserted after the system prompt, after the tool definitions, and after the final message, so
the stable prefix of each request is cached. On subsequent requests with a matching prefix, the
cached representations are reused, skipping redundant token processing. Exact placement varies
by model (e.g. Amazon Nova models cache fewer breakpoints and ignore the "1h" TTL).
unsupportedModelBehaviorunsupportedModelBehaviorminMessagesToCacheConfiguration options for the caching behavior
import { createAgent } from "langchain";
import { bedrockPromptCachingMiddleware } from "langchain";
const agent = createAgent({
model: "bedrock:anthropic.claude-haiku-4-5-20251001-v1:0",
middleware: [
bedrockPromptCachingMiddleware()
]
});const cachingMiddleware = bedrockPromptCachingMiddleware({
ttl: "1h", // Cache for 1 hour instead of default 5 minutes
minMessagesToCache: 5 // Only cache after 5 messages
});
const agent = createAgent({
model: "bedrock:anthropic.claude-haiku-4-5-20251001-v1:0",
systemPrompt: "You are a helpful assistant with deep knowledge of...", // Long system prompt
middleware: [cachingMiddleware]
});const agent = createAgent({
model: "bedrock:anthropic.claude-haiku-4-5-20251001-v1:0",
middleware: [
bedrockPromptCachingMiddleware({
enableCaching: true,
ttl: "5m"
})
]
});
// Disable caching for specific requests
await agent.invoke(
{ messages: [new HumanMessage("Process this without caching")] },
{
configurable: {
middleware_context: { enableCaching: false }
}
}
);const supportAgent = createAgent({
model: "bedrock:anthropic.claude-haiku-4-5-20251001-v1:0",
systemPrompt: `You are a customer support agent for ACME Corp.
Company policies:
- Always be polite and professional
- Refer to knowledge base for product information
- Escalate billing issues to human agents
... (extensive policies and guidelines)
`,
tools: [searchKnowledgeBase, createTicket, checkOrderStatus],
middleware: [
bedrockPromptCachingMiddleware({
ttl: "1h", // Long TTL for stable system prompt
minMessagesToCache: 1 // Cache immediately due to large system prompt
})
]
});