Skip to content

feat(anthropic): add cache_control field to ToolDefinition#1812

Closed
jimmiebfulton wants to merge 1 commit into
0xPlaygrounds:mainfrom
anemnez:pr/anthropic-tool-cache-control
Closed

feat(anthropic): add cache_control field to ToolDefinition#1812
jimmiebfulton wants to merge 1 commit into
0xPlaygrounds:mainfrom
anemnez:pr/anthropic-tool-cache-control

Conversation

@jimmiebfulton
Copy link
Copy Markdown
Contributor

Summary

Anthropic supports caching the tools layer by setting cache_control on the last ToolDefinition in the array. ToolDefinition currently has no such field — this is the only cacheable layer not yet exposed in rig.

SystemContent and the message Content variants already have cache_control. This brings ToolDefinition in line with the same pattern.

Changes

  • Add pub cache_control: Option<CacheControl> to ToolDefinition with #[serde(skip_serializing_if = "Option::is_none")].
  • Initialize cache_control: None at the two ToolDefinition construction sites in the TryFrom impls (completion.rs and streaming.rs).

No behaviour change for existing callers — the field defaults to None and is omitted from serialization when unset.

AI Assistance

This implementation was developed with significant AI assistance (Claude). The change and rationale were reviewed and validated by the author before submission.

Issue

Fixes #1811

Anthropic's prompt-caching API supports up to 4 cache breakpoints per
request. One of the three cacheable layers is the tools array: setting
cache_control on the last ToolDefinition tells Anthropic to cache all
tools up to that point independently of the system prompt.

ToolDefinition currently has no cache_control field, so callers have no
way to cache the tools layer. This adds the field following the same
pattern already used by SystemContent and the message Content variants.

- Add `pub cache_control: Option<CacheControl>` to `ToolDefinition`
  with `#[serde(skip_serializing_if = "Option::is_none")]` so it is
  absent from requests that do not use caching.
- Initialize `cache_control: None` at the two ToolDefinition
  construction sites (completion.rs and streaming.rs TryFrom impls).
@gold-silver-copper
Copy link
Copy Markdown
Contributor

Stacked this pr into #1815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(anthropic): add cache_control field to ToolDefinition

2 participants