@@ -86,24 +86,26 @@ Anthropic supports [prompt caching](https://docs.anthropic.com/en/docs/build-wit
8686
8787Insert a [ ` CachePoint ` ] [ pydantic_ai.messages.CachePoint ] marker in your user messages to cache everything before it:
8888
89- ``` python
89+ ``` python {test="skip"}
9090from pydantic_ai import Agent, CachePoint
9191
9292agent = 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
104106Use ` anthropic_cache_instructions=True ` to cache your system prompt:
105107
106- ``` python
108+ ``` python {test="skip"}
107109from pydantic_ai import Agent
108110from 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
123127Use ` anthropic_cache_tools=True ` to cache your tool definitions:
124128
125- ``` python
129+ ``` python {test="skip"}
126130from pydantic_ai import Agent
127131from pydantic_ai.models.anthropic import AnthropicModelSettings
128132
@@ -136,17 +140,19 @@ agent = Agent(
136140@agent.tool
137141def 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
146152You 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
150156from pydantic_ai.models.anthropic import AnthropicModelSettings
151157
152158agent = 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
181190Access 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