Skip to content

Commit 78aa4a3

Browse files
ronakrmclaude
andcommitted
Address PR review comments
- Rename anthropic_cache_tools to anthropic_cache_tool_definitions for clarity - Add backticks to docstrings for code identifiers (cache_control, tools) - Improve error message to mention alternative cache settings - Remove unnecessary cast for BetaToolUnionParam (line 441) - Add explanatory comment for necessary cast of BetaContentBlockParam (line 703) - Update Bedrock comment to link to issue #3418 All tests pass with these changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 9408b58 commit 78aa4a3

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

docs/models/anthropic.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async def main():
124124

125125
### 3. Cache Tool Definitions
126126

127-
Use `anthropic_cache_tools=True` to cache your tool definitions:
127+
Use `anthropic_cache_tool_definitions=True` to cache your tool definitions:
128128

129129
```python {test="skip"}
130130
from pydantic_ai import Agent
@@ -133,7 +133,7 @@ from pydantic_ai.models.anthropic import AnthropicModelSettings
133133
agent = Agent(
134134
'anthropic:claude-sonnet-4-5',
135135
model_settings=AnthropicModelSettings(
136-
anthropic_cache_tools=True
136+
anthropic_cache_tool_definitions=True
137137
),
138138
)
139139

@@ -160,7 +160,7 @@ agent = Agent(
160160
system_prompt='Detailed instructions...',
161161
model_settings=AnthropicModelSettings(
162162
anthropic_cache_instructions=True,
163-
anthropic_cache_tools=True,
163+
anthropic_cache_tool_definitions=True,
164164
),
165165
)
166166

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ class AnthropicModelSettings(ModelSettings, total=False):
150150
See [the Anthropic docs](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) for more information.
151151
"""
152152

153-
anthropic_cache_tools: bool
154-
"""Whether to add cache_control to the last tool definition.
153+
anthropic_cache_tool_definitions: bool
154+
"""Whether to add `cache_control` to the last tool definition.
155155
156-
When enabled, the last tool in the tools array will have cache_control set,
156+
When enabled, the last tool in the `tools` array will have `cache_control` set,
157157
allowing Anthropic to cache tool definitions and reduce costs.
158158
See https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching for more information.
159159
"""
@@ -437,8 +437,8 @@ def _get_tools(
437437
]
438438

439439
# Add cache_control to the last tool if enabled
440-
if tools and model_settings.get('anthropic_cache_tools'):
441-
last_tool = cast(dict[str, Any], tools[-1])
440+
if tools and model_settings.get('anthropic_cache_tool_definitions'):
441+
last_tool = tools[-1]
442442
last_tool['cache_control'] = BetaCacheControlEphemeralParam(type='ephemeral')
443443

444444
return tools
@@ -692,13 +692,16 @@ def _add_cache_control_to_last_param(params: list[BetaContentBlockParam]) -> Non
692692
"""
693693
if not params:
694694
raise UserError(
695-
'CachePoint cannot be the first content in a user message - there must be previous content to attach the CachePoint to.'
695+
'CachePoint cannot be the first content in a user message - there must be previous content to attach the CachePoint to. '
696+
'To cache system instructions or tool definitions, use the `anthropic_cache_instructions` or `anthropic_cache_tool_definitions` settings instead.'
696697
)
697698

698699
# Only certain types support cache_control
699700
# See https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#what-can-be-cached
700701
cacheable_types = {'text', 'tool_use', 'server_tool_use', 'image', 'tool_result'}
701-
last_param = cast(dict[str, Any], params[-1]) # Cast to dict for mutation
702+
# Cast needed because BetaContentBlockParam is a union including response Block types (Pydantic models)
703+
# that don't support dict operations, even though at runtime we only have request Param types (TypedDicts).
704+
last_param = cast(dict[str, Any], params[-1])
702705
if last_param['type'] not in cacheable_types:
703706
raise UserError(f'Cache control not supported for param type: {last_param["type"]}')
704707

tests/models/test_anthropic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def test_cache_control_unsupported_param_type():
429429

430430

431431
async def test_anthropic_cache_tools(allow_model_requests: None):
432-
"""Test that anthropic_cache_tools adds cache_control to last tool."""
432+
"""Test that anthropic_cache_tool_definitions adds cache_control to last tool."""
433433
c = completion_message(
434434
[BetaTextBlock(text='Tool result', type='text')],
435435
usage=BetaUsage(input_tokens=10, output_tokens=5),
@@ -439,7 +439,7 @@ async def test_anthropic_cache_tools(allow_model_requests: None):
439439
agent = Agent(
440440
m,
441441
system_prompt='Test system prompt',
442-
model_settings=AnthropicModelSettings(anthropic_cache_tools=True),
442+
model_settings=AnthropicModelSettings(anthropic_cache_tool_definitions=True),
443443
)
444444

445445
@agent.tool_plain
@@ -514,7 +514,7 @@ async def test_anthropic_cache_tools_and_instructions(allow_model_requests: None
514514
m,
515515
system_prompt='System instructions to cache.',
516516
model_settings=AnthropicModelSettings(
517-
anthropic_cache_tools=True,
517+
anthropic_cache_tool_definitions=True,
518518
anthropic_cache_instructions=True,
519519
),
520520
)

0 commit comments

Comments
 (0)