Skip to content

Conversation

@baxen
Copy link
Collaborator

@baxen baxen commented Jan 2, 2026

Add cache_control fields to requests for Claude models to enable prompt caching:

  • System message gets cache_control
  • Last two user messages get cache_control (for incremental caching)
  • Last tool definition gets cache_control (so all tools are cached as prefix)

This reduces costs when using Claude models via Databricks by caching frequently repeated content like system prompts and tool definitions.

Add cache_control fields to requests for Claude models to enable prompt caching:
- System message gets cache_control
- Last two user messages get cache_control (for incremental caching)
- Last tool definition gets cache_control (so all tools are cached as prefix)

This reduces costs when using Claude models via Databricks by caching
frequently repeated content like system prompts and tool definitions.
Copilot AI review requested due to automatic review settings January 2, 2026 00:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds prompt caching support for Claude models accessed through Databricks by implementing Anthropic-style cache_control fields in API requests. This optimization reduces costs by caching frequently repeated content like system prompts, conversation history, and tool definitions.

Key changes:

  • Adds is_claude_model() function to detect Claude models by name
  • Implements apply_cache_control_for_claude() to add cache control markers to system messages, the last two user messages, and the last tool definition
  • Integrates cache control application into the create_request() flow for Claude models

"cache_control": { "type": "ephemeral" }
}]);
} else if let Some(content_array) = content.as_array_mut() {
// Content is already an array, add cache_control to the last element
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably can drop little comments like this (thanks claude opus!) but others I found helpful (assuming they are true!)

Copy link
Collaborator

@michaelneale michaelneale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice (comments helpful - just the odd one liner one) but this is pretty cool, wouldn't really know where to start to work all this out.

@baxen baxen merged commit f19ee33 into main Jan 4, 2026
25 checks passed
@baxen baxen deleted the baxen/db-ant-cache branch January 4, 2026 01:56
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.

3 participants