Skip to content

Commit 03dfa19

Browse files
committed
use run_async in docs
1 parent 63500d7 commit 03dfa19

File tree

1 file changed

+75
-57
lines changed

1 file changed

+75
-57
lines changed

docs/models/anthropic.md

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-wit
8787
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
8888
4. **Cache All Messages**: Set [`AnthropicModelSettings.anthropic_cache_messages`][pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_cache_messages] to `True` to automatically cache all messages
8989

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:
9193

9294
```python {test="skip"}
93-
from pydantic_ai import Agent, CachePoint, RunContext
95+
from pydantic_ai import Agent
9496
from pydantic_ai.models.anthropic import AnthropicModelSettings
9597

96-
# Example 1: Use anthropic_cache_messages for automatic last message caching
9798
agent = Agent(
9899
'anthropic:claude-sonnet-4-5',
99100
system_prompt='You are a helpful assistant.',
@@ -102,16 +103,23 @@ agent = Agent(
102103
),
103104
)
104105

105-
async def main():
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?')
111+
print(f'Cache write: {result1.usage().cache_write_tokens}')
112+
print(f'Cache read: {result2.usage().cache_read_tokens}')
113+
```
114+
115+
### Example 2: Comprehensive Caching Strategy
108116

109-
# Subsequent calls with similar conversation benefit from cache
110-
result2 = await agent.run('What is the capital of Germany?')
111-
print(f'Cache write: {result1.usage().cache_write_tokens}')
112-
print(f'Cache read: {result2.usage().cache_read_tokens}')
117+
Combine multiple cache settings for maximum savings:
118+
119+
```python {test="skip"}
120+
from pydantic_ai import Agent, RunContext
121+
from pydantic_ai.models.anthropic import AnthropicModelSettings
113122

114-
# Example 2: Combine with other cache settings for comprehensive caching
115123
agent = Agent(
116124
'anthropic:claude-sonnet-4-5',
117125
system_prompt='Detailed instructions...',
@@ -127,27 +135,34 @@ def search_docs(ctx: RunContext, query: str) -> str:
127135
"""Search documentation."""
128136
return f'Results for {query}'
129137

130-
async def main(): # noqa: F811
131-
# All three cache points are used: instructions, tools, and last message
132-
result = await agent.run('Search for Python best practices')
133-
print(result.output)
134138

135-
# Example 3: Fine-grained control with manual CachePoint markers
139+
result = agent.run_sync('Search for Python best practices')
140+
print(result.output)
141+
```
142+
143+
### Example 3: Fine-Grained Control with CachePoint
144+
145+
Use manual `CachePoint` markers to control cache locations precisely:
146+
147+
```python {test="skip"}
148+
from pydantic_ai import Agent, CachePoint
149+
136150
agent = Agent(
137151
'anthropic:claude-sonnet-4-5',
138152
system_prompt='Instructions...',
139153
)
140154

141-
async def main(): # noqa: F811
142-
# Manually control cache points for specific content blocks
143-
result = await agent.run([
144-
'Long context from documentation...',
145-
CachePoint(), # Cache everything up to this point
146-
'First question'
147-
])
148-
print(result.output)
155+
# Manually control cache points for specific content blocks
156+
result = agent.run_sync([
157+
'Long context from documentation...',
158+
CachePoint(), # Cache everything up to this point
159+
'First question'
160+
])
161+
print(result.output)
149162
```
150163

164+
### Accessing Cache Usage Statistics
165+
151166
Access cache usage statistics via `result.usage()`:
152167

153168
```python {test="skip"}
@@ -162,11 +177,10 @@ agent = Agent(
162177
),
163178
)
164179

165-
async def main(): # noqa: F811
166-
result = await agent.run('Your question')
167-
usage = result.usage()
168-
print(f'Cache write tokens: {usage.cache_write_tokens}')
169-
print(f'Cache read tokens: {usage.cache_read_tokens}')
180+
result = agent.run_sync('Your question')
181+
usage = result.usage()
182+
print(f'Cache write tokens: {usage.cache_write_tokens}')
183+
print(f'Cache read tokens: {usage.cache_read_tokens}')
170184
```
171185

172186
### Cache Point Limits
@@ -181,13 +195,16 @@ Cache points can be placed in three locations:
181195
2. **Tool Definitions**: Via `anthropic_cache_tool_definitions` setting (adds cache point to last tool definition)
182196
3. **Messages**: Via `CachePoint` markers or `anthropic_cache_messages` setting (adds cache points to message content)
183197

184-
Each setting uses **at most 1 cache point**, but you can combine them:
198+
Each setting uses **at most 1 cache point**, but you can combine them.
199+
200+
#### Example: Using All 3 Cache Point Sources
201+
202+
Define an agent with all cache settings enabled:
185203

186204
```python {test="skip"}
187205
from pydantic_ai import Agent, CachePoint
188206
from pydantic_ai.models.anthropic import AnthropicModelSettings
189207

190-
# Example: Using all 3 cache point sources
191208
agent = Agent(
192209
'anthropic:claude-sonnet-4-5',
193210
system_prompt='Detailed instructions...',
@@ -202,22 +219,24 @@ agent = Agent(
202219
def my_tool() -> str:
203220
return 'result'
204221

205-
async def main(): # noqa: F811
206-
# This uses 3 cache points (instructions + tools + last message)
207-
# You can add 1 more CachePoint marker before hitting the limit
208-
result = await agent.run([
209-
'Context', CachePoint(), # 4th cache point - OK
210-
'Question'
211-
])
212-
print(result.output)
213-
usage = result.usage()
214-
print(f'Cache write tokens: {usage.cache_write_tokens}')
215-
print(f'Cache read tokens: {usage.cache_read_tokens}')
222+
223+
# This uses 3 cache points (instructions + tools + last message)
224+
# You can add 1 more CachePoint marker before hitting the limit
225+
result = agent.run_sync([
226+
'Context', CachePoint(), # 4th cache point - OK
227+
'Question'
228+
])
229+
print(result.output)
230+
usage = result.usage()
231+
print(f'Cache write tokens: {usage.cache_write_tokens}')
232+
print(f'Cache read tokens: {usage.cache_read_tokens}')
216233
```
217234

218235
#### Automatic Cache Point Limiting
219236

220-
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:
221240

222241
```python {test="skip"}
223242
from pydantic_ai import Agent, CachePoint
@@ -236,20 +255,19 @@ agent = Agent(
236255
def search() -> str:
237256
return 'data'
238257

239-
async def main(): # 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)
246-
'Question'
247-
])
248-
# Final cache points: instructions + tools + Context 2 + Context 3 = 4
249-
print(result.output)
250-
usage = result.usage()
251-
print(f'Cache write tokens: {usage.cache_write_tokens}')
252-
print(f'Cache read tokens: {usage.cache_read_tokens}')
258+
# Already using 2 cache points (instructions + tools)
259+
# Can add 2 more CachePoint markers (4 total limit)
260+
result = agent.run_sync([
261+
'Context 1', CachePoint(), # Oldest - will be removed
262+
'Context 2', CachePoint(), # Will be kept (3rd point)
263+
'Context 3', CachePoint(), # Will be kept (4th point)
264+
'Question'
265+
])
266+
# Final cache points: instructions + tools + Context 2 + Context 3 = 4
267+
print(result.output)
268+
usage = result.usage()
269+
print(f'Cache write tokens: {usage.cache_write_tokens}')
270+
print(f'Cache read tokens: {usage.cache_read_tokens}')
253271
```
254272

255273
**Key Points**:

0 commit comments

Comments
 (0)