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
+46-2Lines changed: 46 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,13 +80,14 @@ 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**: Enable the [`AnthropicModelSettings.anthropic_cache_instructions`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_instructions][model setting](../agents.md#model-run-settings) to cache your system prompt
87
87
3.**Cache Tool Definitions**: Enable the [`AnthropicModelSettings.anthropic_cache_tool_definitions`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_tool_definitions][model setting](../agents.md#model-run-settings) to cache your tool definitions
88
+
4.**Cache Entire Conversation**: Enable the [`AnthropicModelSettings.anthropic_cache_all`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_all][model setting](../agents.md#model-run-settings) to automatically cache the entire conversation history by adding `cache_control` to the last 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
@@ -124,6 +125,49 @@ async def main():
124
125
print(f'Second: {result2.output}')
125
126
```
126
127
128
+
### Cache Entire Conversation with `anthropic_cache_all`
129
+
130
+
For long conversations where you want to cache the entire conversation history automatically, use the `anthropic_cache_all` setting:
131
+
132
+
```python {test="skip"}
133
+
from pydantic_ai import Agent
134
+
from pydantic_ai.models.anthropic import AnthropicModelSettings
135
+
136
+
agent = Agent(
137
+
'anthropic:claude-sonnet-4-5',
138
+
system_prompt='You are a helpful assistant.',
139
+
model_settings=AnthropicModelSettings(
140
+
anthropic_cache_all=True,
141
+
anthropic_cache_instructions=True,
142
+
anthropic_cache_tool_definitions=True,
143
+
),
144
+
)
145
+
146
+
asyncdefmain():
147
+
# First message - writes to cache
148
+
result1 =await agent.run('What is machine learning?')
149
+
150
+
# Subsequent messages reuse cached conversation history
151
+
result2 =await agent.run(
152
+
'Can you explain that in simpler terms?',
153
+
message_history=result1.all_messages()
154
+
)
155
+
156
+
# Each new message benefits from cached history
157
+
result3 =await agent.run(
158
+
'Give me an example.',
159
+
message_history=result2.all_messages()
160
+
)
161
+
```
162
+
163
+
This is particularly useful when:
164
+
165
+
- Building chatbots or conversational agents with long conversations
166
+
- Iterating on queries while reusing the same context
167
+
- Working with large amounts of conversation history that don't change between requests
168
+
169
+
### Cache Usage Statistics
170
+
127
171
Access cache usage statistics via `result.usage()`:
0 commit comments