The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(context): expose tiered system prompt cache boundaries
Steven Enamakel01d60494cb98
diff --git a/src/openhuman/agent/context/manager.rs b/src/openhuman/agent/context/manager.rsindex 17cd59e7bd..47eace6f96 100644--- a/src/openhuman/agent/context/manager.rs+++ b/src/openhuman/agent/context/manager.rs@@ -216,13 +216,28 @@ impl ContextManager {/// Assemble the opening system prompt for a session using the/// manager's default [`SystemPromptBuilder`].///- /// The returned bytes are the full system prompt, intended to be- /// built once at session start and reused verbatim on every turn —- /// the inference backend's prefix cache picks up the stable prefix- /// automatically, so no boundary marker is emitted.+ /// The returned bytes are the full system prompt, intended to be built+ /// once at session start and reused verbatim on every turn. Callers that+ /// can carry cache breakpoints to the provider should use+ /// [`Self::build_system_prompt_tiered`] instead; this wrapper exists for+ /// the call sites that only want the bytes.pub fn build_system_prompt(&self, ctx: &PromptContext<'_>) -> Result<String> {- let prompt = self.default_prompt_builder.build(ctx)?;- Ok(prompt)+ Ok(self.build_system_prompt_tiered(ctx)?.text)+ }++ /// Assemble the system prompt and report its cache-tier boundaries.+ ///+ /// The doc comment above used to end "the inference backend's prefix cache+ /// picks up the stable prefix automatically, so no boundary marker is+ /// emitted." That is true of backends with automatic longest-prefix caching+ /// and false of Anthropic, which caches nothing without an explicit+ /// breakpoint — so on Anthropic-family models this codebase re-paid full+ /// input price on a ~37k-token prefix, every turn, silently.+ pub fn build_system_prompt_tiered(+ &self,+ ctx: &PromptContext<'_>,+ ) -> Result<crate::openhuman::agent::prompts::TieredPrompt> {+ self.default_prompt_builder.build_tiered(ctx)}/// Assemble the system prompt via a caller-supplied builder.diff --git a/src/openhuman/agent/harness/session/turn/context.rs b/src/openhuman/agent/harness/session/turn/context.rsindex 9281bb440b..1a110a67b0 100644--- a/src/openhuman/agent/harness/session/turn/context.rs+++ b/src/openhuman/agent/harness/session/turn/context.rs@@ -278,6 +278,15 @@ impl Agent {/// Builds the system prompt for the current turn, including tool/// instructions and learned context.pub fn build_system_prompt(&self, learned: LearnedContextData) -> Result<String> {+ Ok(self.build_system_prompt_tiered(learned)?.text)+ }++ /// As [`Self::build_system_prompt`], but reporting the cache-tier+ /// boundaries so the turn can hand them to the provider.+ pub fn build_system_prompt_tiered(+ &self,+ learned: LearnedContextData,+ ) -> Result<crate::openhuman::agent::prompts::TieredPrompt> {let tools_slice: &[Box<dyn Tool>] = self.tools.as_slice();let instructions = self.tool_dispatcher@@ -335,10 +344,20 @@ impl Agent {// Route through the global context manager so every// prompt-building call-site — main agent, sub-agent runner,// channel runtimes — shares one builder configuration.- let mut prompt = self.context.build_system_prompt(&ctx)?;+ let mut tiered = self.context.build_system_prompt_tiered(&ctx)?;if let Some(boundary) = render_tool_policy_boundary(&self.tool_policy_session, 2048) {- prompt = format!("{boundary}\n\n{prompt}");+ // The boundary is prepended, so every offset the builder reported+ // moves by exactly its length. It is itself stable for the session+ // (it renders the resolved tool policy, which the prompt freeze+ // pins), so it belongs inside the first cached tier — shifting+ // rather than dropping the breakpoints is what puts it there.+ let prefix = format!("{boundary}\n\n");+ let shift = prefix.len();+ tiered.text = format!("{prefix}{}", tiered.text);+ for offset in &mut tiered.breakpoints {+ *offset += shift;+ }}- Ok(prompt)+ Ok(tiered)}}