Skip to main content

CtxBuilder

Trait CtxBuilder 

Source
pub trait CtxBuilder: Send + Sync {
    // Required methods
    fn build_messages(
        &self,
        conv: &Conversation,
        system_prompt: &str,
        turn_reminder: &str,
    ) -> (Vec<Message>, ContextStats);
    fn truncate_tool_output(&self, result: &mut ToolResult, tool_name: &str);
    fn ctx_window(&self) -> usize;
    fn name(&self) -> &'static str;

    // Provided methods
    fn needs_compression(
        &self,
        _conv: &Conversation,
        _system_tokens: usize,
    ) -> bool { ... }
    fn compression_plan(&self, _conv: &Conversation) -> Option<(String, usize)> { ... }
}
Expand description

Per-session context construction strategy. Selected once at AgentLoop::new via for_provider and rebuilt on ReloadConfig.

Required Methods§

Source

fn build_messages( &self, conv: &Conversation, system_prompt: &str, turn_reminder: &str, ) -> (Vec<Message>, ContextStats)

Build the messages array to send to the LLM for this turn.

Implementations are free to:

  • Transform system_prompt (strip tool schemas for small models, add cache-friendly markers for Claude, replace entirely for fine-tuned models)
  • Choose any render pipeline (delegate to crate::ctx::render::build_messages, call shared helpers directly, or roll their own)
  • Decide how to handle turn_reminder — per-turn dynamic context (git status, current task, prev edited files). The default policy prepends it to the last User message for system-prompt cache stability; a small-window model might drop it to save tokens; a cache-sensitive model might insert it as its own System message to keep the stable prefix clean. Pass "" when no reminder applies.
Source

fn truncate_tool_output(&self, result: &mut ToolResult, tool_name: &str)

Truncate a single tool output in place.

Source

fn ctx_window(&self) -> usize

Effective token budget for this strategy.

Reflects any defensive clamps the impl applies (e.g. OllamaCtx floors at 4K even if provider.context_window == 0). Callers that need the actual budget — ctx_budget_hint reset, datalog, per-tool truncation — should use this instead of Config::default_context_window(), which returns the raw, unclamped value and may diverge for degenerate configs.

Source

fn name(&self) -> &'static str

Human-readable name for logging / debugging.

Provided Methods§

Source

fn needs_compression(&self, _conv: &Conversation, _system_tokens: usize) -> bool

Whether the conversation should be compressed. Default: never.

Source

fn compression_plan(&self, _conv: &Conversation) -> Option<(String, usize)>

Produce a compression plan (content_to_summarize, messages_to_remove). Default: None (no compression).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§