# BedrockPromptCachingMiddleware

> **Class** in `langchain_aws`

📖 [View in docs](https://reference.langchain.com/python/langchain-aws/middleware/prompt_caching/BedrockPromptCachingMiddleware)

Prompt Caching Middleware for ChatBedrock and ChatBedrockConverse.

Optimizes API usage by caching conversation prefixes for supported models
on AWS Bedrock. Supports Anthropic Claude and Amazon Nova models.

For ChatBedrock (InvokeModel API), adds ``cache_control`` to the last
message's content block. For ChatBedrockConverse (Converse API), appends
``cachePoint`` blocks to the system prompt and last message.

Requires both 'langchain' and 'langchain-aws' packages to be installed.

Learn more about prompt caching at:
- `Anthropic <https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching>`
- `AWS Bedrock <https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html>`

## Signature

```python
BedrockPromptCachingMiddleware(
    self,
    type: Literal['ephemeral'] = 'ephemeral',
    ttl: Literal['5m', '1h'] = '5m',
    min_messages_to_cache: int = 0,
    unsupported_model_behavior: Literal['ignore', 'warn', 'raise'] = 'warn',
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `type` | `Literal['ephemeral']` | No | The type of cache to use. For ChatBedrock, only ``"ephemeral"`` is supported. For ChatBedrockConverse, this value is ignored as the Converse API always uses ``"default"`` cache type. (default: `'ephemeral'`) |
| `ttl` | `Literal['5m', '1h']` | No | The time to live for the cache, only ``"5m"`` and ``"1h"`` are supported, default is``"5m"``. (default: `'5m'`) |
| `min_messages_to_cache` | `int` | No | The minimum number of messages until the cache is used, default is 0. (default: `0`) |
| `unsupported_model_behavior` | `Literal['ignore', 'warn', 'raise']` | No | The behavior to take when an unsupported model is used. ``"ignore"`` will ignore the unsupported model and continue without caching. ``"warn"`` will warn the user and continue without caching. ``"raise"`` will raise an error and stop the agent. (default: `'warn'`) |

## Extends

- `AgentMiddleware`

## Constructors

```python
__init__(
    self,
    type: Literal['ephemeral'] = 'ephemeral',
    ttl: Literal['5m', '1h'] = '5m',
    min_messages_to_cache: int = 0,
    unsupported_model_behavior: Literal['ignore', 'warn', 'raise'] = 'warn',
) -> None
```

| Name | Type |
|------|------|
| `type` | `Literal['ephemeral']` |
| `ttl` | `Literal['5m', '1h']` |
| `min_messages_to_cache` | `int` |
| `unsupported_model_behavior` | `Literal['ignore', 'warn', 'raise']` |


## Properties

- `type`
- `ttl`
- `min_messages_to_cache`
- `unsupported_model_behavior`

## Methods

- [`wrap_model_call()`](https://reference.langchain.com/python/langchain-aws/middleware/prompt_caching/BedrockPromptCachingMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/langchain-aws/middleware/prompt_caching/BedrockPromptCachingMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-aws/blob/10d18256d46953e5fc8dca313a2c41eee29c2a80/libs/aws/langchain_aws/middleware/prompt_caching.py#L30)