# AnthropicPromptCachingMiddleware

> **Class** in `langchain_anthropic`

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

Prompt Caching Middleware.

Optimizes API usage by caching conversation prefixes for Anthropic models.

Requires both `langchain` and `langchain-anthropic` packages to be installed.

Applies cache control breakpoints to:

- **System message**: Tags the last content block of the system message
  with `cache_control` so static system prompt content is cached.
- **Tools**: Tags all tool definitions with `cache_control` so tool
  schemas are cached across turns.
- **Last cacheable block**: Tags last cacheable block of message sequence using
  Anthropic's automatic caching feature.

Learn more about Anthropic prompt caching
[here](https://platform.claude.com/docs/en/build-with-claude/prompt-caching).

## Signature

```python
AnthropicPromptCachingMiddleware(
    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, only `'ephemeral'` is supported. (default: `'ephemeral'`) |
| `ttl` | `Literal['5m', '1h']` | No | The time to live for the cache, only `'5m'` and `'1h'` are supported. (default: `'5m'`) |
| `min_messages_to_cache` | `int` | No | The minimum number of messages until the cache is used. (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-anthropic/middleware/prompt_caching/AnthropicPromptCachingMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/langchain-anthropic/middleware/prompt_caching/AnthropicPromptCachingMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/9f232caa7a8fe1ca042a401942d5d90d54ceb1a6/libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py#L35)