Skip to content

Commit 92509fe

Browse files
committed
Fix doc examples: wrap await in async functions and use single quotes
1 parent 57d051a commit 92509fe

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

docs/models/anthropic.md

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,26 @@ Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-wit
8686

8787
Insert a [`CachePoint`][pydantic_ai.messages.CachePoint] marker in your user messages to cache everything before it:
8888

89-
```python
89+
```python {test="skip"}
9090
from pydantic_ai import Agent, CachePoint
9191

9292
agent = Agent('anthropic:claude-sonnet-4-5')
9393

94-
# Everything before CachePoint will be cached
95-
result = await agent.run([
96-
"Long context that should be cached...",
97-
CachePoint(),
98-
"Your question here"
99-
])
94+
async def main():
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)
100102
```
101103

102104
### 2. Cache System Instructions
103105

104106
Use `anthropic_cache_instructions=True` to cache your system prompt:
105107

106-
```python
108+
```python {test="skip"}
107109
from pydantic_ai import Agent
108110
from pydantic_ai.models.anthropic import AnthropicModelSettings
109111

@@ -115,14 +117,16 @@ agent = Agent(
115117
),
116118
)
117119

118-
result = await agent.run("Your question")
120+
async def main():
121+
result = await agent.run('Your question')
122+
print(result.output)
119123
```
120124

121125
### 3. Cache Tool Definitions
122126

123127
Use `anthropic_cache_tools=True` to cache your tool definitions:
124128

125-
```python
129+
```python {test="skip"}
126130
from pydantic_ai import Agent
127131
from pydantic_ai.models.anthropic import AnthropicModelSettings
128132

@@ -136,17 +140,19 @@ agent = Agent(
136140
@agent.tool
137141
def my_tool() -> str:
138142
"""Tool definition will be cached."""
139-
return "result"
143+
return 'result'
140144

141-
result = await agent.run("Use the tool")
145+
async def main():
146+
result = await agent.run('Use the tool')
147+
print(result.output)
142148
```
143149

144150
### Combining Cache Strategies
145151

146152
You can combine all three caching strategies for maximum savings:
147153

148-
```python
149-
from pydantic_ai import Agent, CachePoint
154+
```python {test="skip"}
155+
from pydantic_ai import Agent, CachePoint, RunContext
150156
from pydantic_ai.models.anthropic import AnthropicModelSettings
151157

152158
agent = Agent(
@@ -159,29 +165,45 @@ agent = Agent(
159165
)
160166

161167
@agent.tool
162-
def search_docs(query: str) -> str:
168+
def search_docs(ctx: RunContext, query: str) -> str:
163169
"""Search documentation."""
164-
return f"Results for {query}"
165-
166-
# First call - writes to cache
167-
result1 = await agent.run([
168-
"Long context from documentation...",
169-
CachePoint(),
170-
"First question"
171-
])
172-
173-
# Subsequent calls - read from cache (90% cost reduction)
174-
result2 = await agent.run([
175-
"Long context from documentation...", # Same content
176-
CachePoint(),
177-
"Second question"
178-
])
170+
return f'Results for {query}'
171+
172+
async def main():
173+
# First call - writes to cache
174+
result1 = await agent.run([
175+
'Long context from documentation...',
176+
CachePoint(),
177+
'First question'
178+
])
179+
180+
# Subsequent calls - read from cache (90% cost reduction)
181+
result2 = await agent.run([
182+
'Long context from documentation...', # Same content
183+
CachePoint(),
184+
'Second question'
185+
])
186+
print(f'First: {result1.output}')
187+
print(f'Second: {result2.output}')
179188
```
180189

181190
Access cache usage statistics via `result.usage()`:
182191

183-
```python
184-
usage = result.usage()
185-
print(f"Cache write tokens: {usage.cache_write_tokens}")
186-
print(f"Cache read tokens: {usage.cache_read_tokens}")
192+
```python {test="skip"}
193+
from pydantic_ai import Agent
194+
from pydantic_ai.models.anthropic import AnthropicModelSettings
195+
196+
agent = Agent(
197+
'anthropic:claude-sonnet-4-5',
198+
system_prompt='Instructions...',
199+
model_settings=AnthropicModelSettings(
200+
anthropic_cache_instructions=True
201+
),
202+
)
203+
204+
async def main():
205+
result = await agent.run('Your question')
206+
usage = result.usage()
207+
print(f'Cache write tokens: {usage.cache_write_tokens}')
208+
print(f'Cache read tokens: {usage.cache_read_tokens}')
187209
```

0 commit comments

Comments
 (0)