feat: Add comprehensive Anthropic prompt caching
Some checks failed
ci / build-test (push) Failing after 4m51s
Codespell / Check for spelling errors (push) Successful in 4s
rust-ci / Detect changed areas (push) Has been cancelled
rust-ci / Format / etc (push) Has been cancelled
rust-ci / cargo shear (push) Has been cancelled
rust-ci / Lint/Build — macos-14 - aarch64-apple-darwin (push) Has been cancelled
rust-ci / Lint/Build — macos-14 - x86_64-apple-darwin (push) Has been cancelled
rust-ci / Lint/Build — ubuntu-24.04 - x86_64-unknown-linux-gnu (push) Has been cancelled
rust-ci / Lint/Build — ubuntu-24.04 - x86_64-unknown-linux-musl (push) Has been cancelled
rust-ci / Lint/Build — ubuntu-24.04-arm - aarch64-unknown-linux-gnu (push) Has been cancelled
rust-ci / Lint/Build — ubuntu-24.04-arm - aarch64-unknown-linux-musl (push) Has been cancelled
rust-ci / Lint/Build — windows-latest - x86_64-pc-windows-msvc (push) Has been cancelled
rust-ci / Lint/Build — macos-14 - aarch64-apple-darwin (release) (push) Has been cancelled
rust-ci / Lint/Build — ubuntu-24.04 - x86_64-unknown-linux-musl (release) (push) Has been cancelled
rust-ci / Lint/Build — windows-11-arm - aarch64-pc-windows-msvc (release) (push) Has been cancelled
rust-ci / Lint/Build — windows-latest - x86_64-pc-windows-msvc (release) (push) Has been cancelled
rust-ci / Tests — macos-14 - aarch64-apple-darwin (push) Has been cancelled
rust-ci / Tests — ubuntu-24.04 - x86_64-unknown-linux-gnu (push) Has been cancelled
rust-ci / Tests — ubuntu-24.04-arm - aarch64-unknown-linux-gnu (push) Has been cancelled
rust-ci / Tests — windows-11-arm - aarch64-pc-windows-msvc (push) Has been cancelled
rust-ci / Tests — windows-latest - x86_64-pc-windows-msvc (push) Has been cancelled
rust-ci / CI results (required) (push) Has been cancelled
rust-ci / Lint/Build — windows-11-arm - aarch64-pc-windows-msvc (push) Has been cancelled
sdk / sdks (push) Has been cancelled

- Add cache_control to system instructions (stable, high value)
- Add cache_control to conversation history (4 messages before end)
- Implements full Anthropic caching hierarchy: tools → system → history
- Significant cost savings for repeated similar requests

Cache hierarchy:
1. Tools (last tool) - already implemented
2. System instructions - new
3. Conversation history - new (stable portion)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-16 21:43:01 +01:00
parent 63de226119
commit 462b219d3f

View File

@@ -56,7 +56,12 @@ pub(crate) async fn stream_chat_completions(
let mut messages = Vec::<serde_json::Value>::new(); let mut messages = Vec::<serde_json::Value>::new();
let full_instructions = prompt.get_full_instructions(model_family); let full_instructions = prompt.get_full_instructions(model_family);
messages.push(json!({"role": "system", "content": full_instructions})); // Add cache_control to system instructions for Anthropic prompt caching
messages.push(json!({
"role": "system",
"content": full_instructions,
"cache_control": {"type": "ephemeral"}
}));
let input = prompt.get_formatted_input(); let input = prompt.get_formatted_input();
@@ -413,6 +418,20 @@ pub(crate) async fn stream_chat_completions(
} }
debug!("Built {} messages for API request", messages.len()); debug!("Built {} messages for API request", messages.len());
// Add cache_control to conversation history for Anthropic prompt caching
// Add it to a message that's at least 3 messages before the end (stable history)
// This caches the earlier conversation while keeping recent turns uncached
if messages.len() > 4 {
let cache_idx = messages.len().saturating_sub(4);
if let Some(msg) = messages.get_mut(cache_idx) {
if let Some(obj) = msg.as_object_mut() {
obj.insert("cache_control".to_string(), json!({"type": "ephemeral"}));
debug!("Added cache_control to message at index {} (conversation history)", cache_idx);
}
}
}
debug!("=== End Chat Completions Request Debug ==="); debug!("=== End Chat Completions Request Debug ===");
let tools_json = create_tools_json_for_chat_completions_api(&prompt.tools)?; let tools_json = create_tools_json_for_chat_completions_api(&prompt.tools)?;