Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

perf: pass pipe instructions as system prompt for Anthropic prompt caching

Louis Beaumontd4332c74c3e5
diff --git a/crates/screenpipe-core/src/agents/mod.rs b/crates/screenpipe-core/src/agents/mod.rs
index bc360f729..79b21996c 100644
--- a/crates/screenpipe-core/src/agents/mod.rs
+++ b/crates/screenpipe-core/src/agents/mod.rs
@@ -73,6 +73,7 @@ pub trait AgentExecutor: Send + Sync {
pid_tx: Option<tokio::sync::oneshot::Sender<u32>>,
line_tx: tokio::sync::mpsc::UnboundedSender<String>,
continue_session: bool,
+ _pipe_system_prompt: Option<&str>,
) -> Result<AgentOutput> {
let output = self
.run(
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rs
index 5cd379bf8..c03baefe0 100644
--- a/crates/screenpipe-core/src/agents/pi.rs
+++ b/crates/screenpipe-core/src/agents/pi.rs
@@ -486,6 +486,7 @@ impl PiExecutor {
provider_api_key: Option<&str>,
pid_tx: Option<tokio::sync::oneshot::Sender<u32>>,
continue_session: bool,
+ pipe_system_prompt: Option<&str>,
) -> Result<AgentOutput> {
let mut cmd = build_async_command(pi_path);
cmd.current_dir(working_dir);
@@ -497,6 +498,9 @@ impl PiExecutor {
}
cmd.arg("--provider").arg(resolved_provider);
cmd.arg("--model").arg(model);
+ if let Some(sys) = pipe_system_prompt {
+ cmd.arg("--append-system-prompt").arg(sys);
+ }
cmd.arg("-p").arg(prompt);
if let Some(ref token) = self.user_token {
@@ -585,6 +589,7 @@ impl PiExecutor {
pid_tx: Option<tokio::sync::oneshot::Sender<u32>>,
line_tx: tokio::sync::mpsc::UnboundedSender<String>,
continue_session: bool,
+ pipe_system_prompt: Option<&str>,
) -> Result<AgentOutput> {
let mut cmd = build_async_command(pi_path);
cmd.current_dir(working_dir);
@@ -599,6 +604,11 @@ impl PiExecutor {
}
cmd.arg("--provider").arg(resolved_provider);
cmd.arg("--model").arg(model);
+ // Pass pipe instructions as system prompt for Anthropic prompt caching.
+ // Pi's internal system prompt + this appended text form the cached prefix.
+ if let Some(sys) = pipe_system_prompt {
+ cmd.arg("--append-system-prompt").arg(sys);
+ }
cmd.arg("-p").arg(prompt);
if let Some(ref token) = self.user_token {
@@ -798,6 +808,7 @@ impl AgentExecutor for PiExecutor {
provider_api_key,
pid_tx,
continue_session,
+ None, // no pipe system prompt for trait-based calls
)
.await?;
@@ -828,6 +839,7 @@ impl AgentExecutor for PiExecutor {
provider_api_key,
None,
continue_session,
+ None,
)
.await;
}
@@ -846,6 +858,7 @@ impl AgentExecutor for PiExecutor {
pid_tx: Option<tokio::sync::oneshot::Sender<u32>>,
line_tx: tokio::sync::mpsc::UnboundedSender<String>,
continue_session: bool,
+ pipe_system_prompt: Option<&str>,
) -> Result<AgentOutput> {
let resolved_provider = provider.unwrap_or("screenpipe").to_string();
let resolved_model = Self::resolve_model(model, &resolved_provider);
@@ -884,6 +897,7 @@ impl AgentExecutor for PiExecutor {
pid_tx,
line_tx.clone(),
continue_session,
+ pipe_system_prompt,
)
.await?;
@@ -914,6 +928,7 @@ impl AgentExecutor for PiExecutor {
None,
line_tx,
continue_session,
+ pipe_system_prompt,
)
.await;
}
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 58d22b0f3..e64b9bffc 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -1168,6 +1168,7 @@ impl PipeManager {
Some(pid_tx),
line_tx,
history_enabled,
+ None, // manual run — gateway-side caching still applies
),
)
.await;
@@ -1571,6 +1572,7 @@ impl PipeManager {
Some(pid_tx),
line_tx,
history_enabled,
+ None, // manual run — gateway-side caching still applies
),
)
.await;
@@ -2192,6 +2194,11 @@ impl PipeManager {
let pipe_dir = pipes_dir.join(name);
+ let pipe_system_prompt = render_pipe_system_prompt(
+ body,
+ api_port,
+ preset_prompt.as_deref(),
+ );
let prompt = render_prompt_with_port(
config,
body,
@@ -2298,6 +2305,7 @@ impl PipeManager {
Some(pid_tx),
line_tx,
history_enabled,
+ Some(&pipe_system_prompt),
),
)
.await;
@@ -2670,11 +2678,41 @@ pub fn serialize_pipe(config: &PipeConfig, body: &str) -> Result<String> {
///
/// The header gives the LLM all the context it needs (time range, date,
/// timezone). No template variables needed in the prompt body.
-fn render_prompt_with_port(
- config: &PipeConfig,
+/// Build the static system prompt for a pipe.
+///
+/// Contains the pipe body (instructions from pipe.md) and the preset system prompt.
+/// These are identical across runs and across turns within a run, making them
+/// ideal for Anthropic prompt caching (90% input cost reduction on cache hits).
+fn render_pipe_system_prompt(
body: &str,
api_port: u16,
system_prompt: Option<&str>,
+) -> String {
+ let os = std::env::consts::OS;
+ let mut sys = String::new();
+
+ // Prepend preset system prompt if present
+ if let Some(sp) = system_prompt {
+ sys.push_str(sp);
+ sys.push_str("\n\n");
+ }
+
+ sys.push_str(&format!(
+ "OS: {os}\nOutput directory: ./output/\nScreenpipe API: http://localhost:{api_port}\n\n"
+ ));
+ sys.push_str(body);
+ sys
+}
+
+/// Build the dynamic user prompt for a pipe.
+///
+/// Contains time-varying context (time range, date, timezone) and any extra context.
+/// This changes every run so it won't be cached.
+fn render_prompt_with_port(
+ config: &PipeConfig,
+ _body: &str,
+ _api_port: u16,
+ _system_prompt: Option<&str>,
extra_context: Option<&str>,
) -> String {
let now = Local::now();
@@ -2695,22 +2733,10 @@ fn render_prompt_with_port(
let mut prompt = String::new();
- // Prepend preset system prompt if present
- if let Some(sp) = system_prompt {
- prompt.push_str("System prompt:\n");
- prompt.push_str(sp);
- prompt.push_str("\n\n");
- }
-
- let os = std::env::consts::OS; // "windows", "macos", "linux"
-
let header = format!(
r#"Time range: {start_time} to {end_time}
Date: {date}
Timezone: {timezone} (UTC{tz_offset})
-OS: {os}
-Output directory: ./output/
-Screenpipe API: http://localhost:{api_port}
"#
);
@@ -2720,8 +2746,6 @@ Screenpipe API: http://localhost:{api_port}
prompt.push_str(ctx);
}
- prompt.push('\n');
- prompt.push_str(body);
prompt
}