Skip to content

Commit 5e29917

Browse files
ronakrmclaude
andcommitted
Simplify prompt caching documentation structure
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]>
1 parent 0c8b9e0 commit 5e29917

File tree

1 file changed

+4
-67
lines changed

1 file changed

+4
-67
lines changed

docs/models/anthropic.md

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -82,74 +82,11 @@ agent = Agent(model)
8282

8383
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:
8484

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
8688

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-
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)
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-
async def main():
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-
def my_tool() -> str:
142-
"""Tool definition will be cached."""
143-
return 'result'
144-
145-
async def main():
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:
15390

15491
```python {test="skip"}
15592
from pydantic_ai import Agent, CachePoint, RunContext

0 commit comments

Comments
 (0)