You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models/anthropic.md
+47-2Lines changed: 47 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,18 +80,29 @@ agent = Agent(model)
80
80
81
81
## Prompt Caching
82
82
83
-
Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) to reduce costs by caching parts of your prompts. Pydantic AI provides three ways to use prompt caching:
83
+
Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) to reduce costs by caching parts of your prompts. Pydantic AI provides four ways to use prompt caching:
84
84
85
85
1.**Cache User Messages with [`CachePoint`][pydantic_ai.messages.CachePoint]**: Insert a `CachePoint` marker in your user messages to cache everything before it
86
86
2.**Cache System Instructions**: Set [`AnthropicModelSettings.anthropic_cache_instructions`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_instructions] to `True` (uses 5m TTL by default) or specify `'5m'` / `'1h'` directly
87
87
3.**Cache Tool Definitions**: Set [`AnthropicModelSettings.anthropic_cache_tool_definitions`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_tool_definitions] to `True` (uses 5m TTL by default) or specify `'5m'` / `'1h'` directly
88
+
4.**Cache All (Convenience)**: Set [`AnthropicModelSettings.anthropic_cache_all`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_all] to `True` to automatically cache both system instructions and the last user message
88
89
89
-
You can combine all three strategies for maximum savings:
90
+
You can combine multiple strategies for maximum savings:
90
91
91
92
```python {test="skip"}
92
93
from pydantic_ai import Agent, CachePoint, RunContext
93
94
from pydantic_ai.models.anthropic import AnthropicModelSettings
94
95
96
+
# Option 1: Use anthropic_cache_all for convenience (caches system + last message)
97
+
agent = Agent(
98
+
'anthropic:claude-sonnet-4-5',
99
+
system_prompt='Detailed instructions...',
100
+
model_settings=AnthropicModelSettings(
101
+
anthropic_cache_all=True, # Caches both system prompt and last message
102
+
),
103
+
)
104
+
105
+
# Option 2: Fine-grained control with individual settings
Anthropic enforces a maximum of 4 cache points per request. Pydantic AI automatically manages this limit:
163
+
164
+
-**`anthropic_cache_all`**: Uses 2 cache points (system instructions + last message)
165
+
-**`anthropic_cache_instructions`**: Uses 1 cache point
166
+
-**`anthropic_cache_tool_definitions`**: Uses 1 cache point
167
+
-**`CachePoint` markers**: Use remaining available cache points
168
+
169
+
When the total exceeds 4 cache points, Pydantic AI automatically removes cache points from **older messages** (keeping the most recent ones), ensuring your requests always comply with Anthropic's limits without errors.
170
+
171
+
```python {test="skip"}
172
+
from pydantic_ai import Agent, CachePoint
173
+
from pydantic_ai.models.anthropic import AnthropicModelSettings
174
+
175
+
agent = Agent(
176
+
'anthropic:claude-sonnet-4-5',
177
+
system_prompt='Instructions...',
178
+
model_settings=AnthropicModelSettings(
179
+
anthropic_cache_all=True, # Uses 2 cache points
180
+
),
181
+
)
182
+
183
+
asyncdefmain():
184
+
# Even with multiple CachePoint markers, only 2 more will be kept
185
+
# (4 total limit - 2 from cache_all = 2 available)
0 commit comments