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
Replace three separate sections (each with individual examples) with:
- A concise bulleted list of the three caching methods
- One comprehensive example showing all three methods combined
This reduces repetition and makes the documentation more scannable.
Per maintainer feedback on PR #3363.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Copy file name to clipboardExpand all lines: docs/models/anthropic.md
+4-67Lines changed: 4 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,74 +82,11 @@ agent = Agent(model)
82
82
83
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:
84
84
85
-
### 1. Cache User Messages with `CachePoint`
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
+
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
+
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
86
88
87
-
Insert a [`CachePoint`][pydantic_ai.messages.CachePoint] marker in your user messages to cache everything before it:
88
-
89
-
```python {test="skip"}
90
-
from pydantic_ai import Agent, CachePoint
91
-
92
-
agent = Agent('anthropic:claude-sonnet-4-5')
93
-
94
-
asyncdefmain():
95
-
# Everything before CachePoint will be cached
96
-
result =await agent.run([
97
-
'Long context that should be cached...',
98
-
CachePoint(),
99
-
'Your question here'
100
-
])
101
-
print(result.output)
102
-
```
103
-
104
-
### 2. Cache System Instructions
105
-
106
-
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:
107
-
108
-
```python {test="skip"}
109
-
from pydantic_ai import Agent
110
-
from pydantic_ai.models.anthropic import AnthropicModelSettings
111
-
112
-
agent = Agent(
113
-
'anthropic:claude-sonnet-4-5',
114
-
system_prompt='Long detailed instructions...',
115
-
model_settings=AnthropicModelSettings(
116
-
anthropic_cache_instructions=True
117
-
),
118
-
)
119
-
120
-
asyncdefmain():
121
-
result =await agent.run('Your question')
122
-
print(result.output)
123
-
```
124
-
125
-
### 3. Cache Tool Definitions
126
-
127
-
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:
128
-
129
-
```python {test="skip"}
130
-
from pydantic_ai import Agent
131
-
from pydantic_ai.models.anthropic import AnthropicModelSettings
132
-
133
-
agent = Agent(
134
-
'anthropic:claude-sonnet-4-5',
135
-
model_settings=AnthropicModelSettings(
136
-
anthropic_cache_tool_definitions=True
137
-
),
138
-
)
139
-
140
-
@agent.tool
141
-
defmy_tool() -> str:
142
-
"""Tool definition will be cached."""
143
-
return'result'
144
-
145
-
asyncdefmain():
146
-
result =await agent.run('Use the tool')
147
-
print(result.output)
148
-
```
149
-
150
-
### Combining Cache Strategies
151
-
152
-
You can combine all three caching strategies for maximum savings:
89
+
You can combine all three strategies for maximum savings:
153
90
154
91
```python {test="skip"}
155
92
from pydantic_ai import Agent, CachePoint, RunContext
0 commit comments