From 4a143bd6d294b18df8a83038179e877bdb379f71 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Tue, 9 Dec 2025 09:02:08 -0700 Subject: [PATCH] make prompt caching optional, run clippy --- signal-gateway/src/claude/mod.rs | 3 +++ signal-gateway/src/claude/worker.rs | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/signal-gateway/src/claude/mod.rs b/signal-gateway/src/claude/mod.rs index ae70005..dd9a090 100644 --- a/signal-gateway/src/claude/mod.rs +++ b/signal-gateway/src/claude/mod.rs @@ -45,6 +45,9 @@ pub struct ClaudeConfig { /// Maximum tool use iterations before giving up. #[conf(long, env, default_value = "10")] pub claude_max_iterations: u32, + /// Enable prompt caching (adds cache_control to system prompts). + #[conf(long, env)] + pub prompt_caching: bool, /// Compaction configuration. #[conf(flatten, prefix)] pub compaction: CompactionConfig, diff --git a/signal-gateway/src/claude/worker.rs b/signal-gateway/src/claude/worker.rs index 56e529f..3415541 100644 --- a/signal-gateway/src/claude/worker.rs +++ b/signal-gateway/src/claude/worker.rs @@ -253,9 +253,11 @@ impl ClaudeWorker { if !self.summary.is_empty() { system.push(SystemContent::text(&self.summary)); } - // Mark the last one as cached - if let Some(last) = system.last_mut() { - *last = std::mem::take(last).cached(); + // Mark the last one as cached if prompt caching is enabled + if self.config.prompt_caching + && let Some(last) = system.last_mut() + { + last.set_cached(); } let request_body = MessagesRequest { @@ -347,14 +349,16 @@ impl ClaudeWorker { let mut system: Vec = self .system_prompts .iter() - .map(|text| SystemContent::text(text)) + .map(SystemContent::text) .collect(); if !self.summary.is_empty() { system.push(SystemContent::text(&self.summary)); } - // Mark the last one as cached - if let Some(last) = system.last_mut() { - *last = std::mem::take(last).cached(); + // Mark the last one as cached if prompt caching is enabled + if self.config.prompt_caching + && let Some(last) = system.last_mut() + { + last.set_cached(); } for iteration in 0..max_iterations { @@ -506,9 +510,8 @@ impl SystemContent { } } - fn cached(mut self) -> Self { + fn set_cached(&mut self) { self.cache_control = Some(CacheControl::ephemeral()); - self } } @@ -558,7 +561,9 @@ impl MessageContent { #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] enum ContentBlock { - Text { text: Box }, + Text { + text: Box, + }, ToolUse { id: Box, name: Box,