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
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
88
4.**Cache All Messages**: Set [`AnthropicModelSettings.anthropic_cache_messages`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_messages] to `True` to automatically cache all messages
89
89
90
-
You can combine multiple strategies for maximum savings:
90
+
### Example 1: Automatic Last Message Caching
91
+
92
+
Use `anthropic_cache_messages` to automatically cache the last user message:
91
93
92
94
```python {test="skip"}
93
-
from pydantic_ai import Agent, CachePoint, RunContext
95
+
from pydantic_ai import Agent
94
96
from pydantic_ai.models.anthropic import AnthropicModelSettings
95
97
96
-
# Example 1: Use anthropic_cache_messages for automatic last message caching
97
98
agent = Agent(
98
99
'anthropic:claude-sonnet-4-5',
99
100
system_prompt='You are a helpful assistant.',
@@ -102,16 +103,23 @@ agent = Agent(
102
103
),
103
104
)
104
105
105
-
asyncdefmain():
106
-
# The last message is automatically cached - no need for manual CachePoint
107
-
result1 =await agent.run('What is the capital of France?')
106
+
# The last message is automatically cached - no need for manual CachePoint
107
+
result1 = agent.run_sync('What is the capital of France?')
108
+
109
+
# Subsequent calls with similar conversation benefit from cache
110
+
result2 = agent.run_sync('What is the capital of Germany?')
When cache points from all sources (settings + `CachePoint` markers) exceed 4, Pydantic AI automatically removes excess cache points from **older message content** (keeping the most recent ones):
237
+
When cache points from all sources (settings + `CachePoint` markers) exceed 4, Pydantic AI automatically removes excess cache points from **older message content** (keeping the most recent ones).
238
+
239
+
Define an agent with 2 cache points from settings:
221
240
222
241
```python {test="skip"}
223
242
from pydantic_ai import Agent, CachePoint
@@ -236,20 +255,19 @@ agent = Agent(
236
255
defsearch() -> str:
237
256
return'data'
238
257
239
-
asyncdefmain(): # noqa: F811
240
-
# Already using 2 cache points (instructions + tools)
241
-
# Can add 2 more CachePoint markers (4 total limit)
242
-
result =await agent.run([
243
-
'Context 1', CachePoint(), # Oldest - will be removed
244
-
'Context 2', CachePoint(), # Will be kept (3rd point)
245
-
'Context 3', CachePoint(), # Will be kept (4th point)
0 commit comments