Skip to main content

AWS middleware integration

Middleware specifically designed for models hosted on AWS Bedrock. Learn more about middleware.

MiddlewareDescription
Prompt cachingReduce costs by caching repetitive prompt prefixes

Prompt caching

Reduce inference latency and input token costs by caching frequently reused prompt prefixes on Amazon Bedrock. bedrockPromptCachingMiddleware enables caching through modelSettings. ChatBedrockConverse then translates that into the correct AWS wire format at request time. Cache checkpoints are placed after the system prompt, tool definitions, and the most recent message where supported, so that the model can skip recomputation of previously seen content on subsequent requests. Cache placement varies by model family: for example, Nova skips some tool definition and tool-result cases.

Prompt caching is useful for the following:

  • Multi-turn conversations with long, consistent system prompts
  • Agents with many tool definitions that remain constant across invocations
  • Document-based Q&A where users ask multiple questions over the same uploaded context
  • Batch processing workloads with repeated static content

Supported models:

  • Anthropic Claude
  • Amazon Nova

Info

Learn more about AWS Bedrock prompt caching strategies and limitations. Cached content must exceed 1,024 tokens for a cache checkpoint to take effect, sometimes more depending on model. See supported models, regions, and limits.

API reference: BedrockPromptCachingMiddleware

import { createAgent, bedrockPromptCachingMiddleware } from "langchain";

const agent = createAgent({
  model: "bedrock:us.anthropic.claude-sonnet-4-5-20250929-v1:0",
  systemPrompt: "<Your long system prompt here>",
  middleware: [bedrockPromptCachingMiddleware({ ttl: "1h" })], // [!code highlight]
});
import { createAgent, bedrockPromptCachingMiddleware } from "langchain";
import { ChatBedrockConverse } from "@langchain/aws";

const agent = createAgent({
  model: new ChatBedrockConverse({ model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0" }),
  systemPrompt: "<Your long system prompt here>",
  middleware: [bedrockPromptCachingMiddleware({ ttl: "5m" })], // [!code highlight]
});

Model-specific behavior

The middleware handles differences between model families automatically:

FeatureChatBedrockConverse (Anthropic)ChatBedrockConverse (Nova)
System prompt caching✅✅
Tool definition caching✅❌
Message caching✅✅ (excludes tool result messages)
Extended TTL (1h)✅❌