pub struct SystemPromptBuilder { /* private fields */ }Expand description
Builder for assembling a system prompt from multiple components.
The builder uses a fluent API to configure each component of the system prompt. Components are assembled in a fixed order optimized for LLM provider caching: stable sections first, then semi-stable, then dynamic.
§Component Order
- Identity (or custom prompt if provided)
- Tool descriptions
- Skill index (Level 0)
- Context files (AGENTS.md)
- User preferences
- Append prompt (if provided)
§Example
use talos_agent::prompt::SystemPromptBuilder;
let prompt = SystemPromptBuilder::new()
.with_user_preferences("Always use British English.".into())
.build();Implementations§
Source§impl SystemPromptBuilder
impl SystemPromptBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with the default identity and no other components.
All optional components start empty. Use the builder methods to configure tools, skills, context files, and other components.
pub fn with_strict_tool_format(self) -> Self
pub fn with_tool_format(self, format: &'static str) -> Self
Sourcepub fn with_template_var(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_template_var( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Sets a template slot value for {{slot}} substitution.
Sourcepub fn with_workspace_info(self, value: impl Into<String>) -> Self
pub fn with_workspace_info(self, value: impl Into<String>) -> Self
Sets workspace information for the default identity template.
Sourcepub fn with_model_info(self, value: impl Into<String>) -> Self
pub fn with_model_info(self, value: impl Into<String>) -> Self
Sets model information for the default identity template.
Sourcepub fn with_tools(self, tools: Vec<ToolDescription>) -> Self
pub fn with_tools(self, tools: Vec<ToolDescription>) -> Self
Sets the tool descriptions for inclusion in the system prompt.
Tools are sorted alphabetically by name to ensure stable ordering across turns, maximizing cache hit rates.
Sourcepub fn with_skill_index(self, skills: Vec<SkillIndex>) -> Self
pub fn with_skill_index(self, skills: Vec<SkillIndex>) -> Self
Sets the skill index for inclusion in the system prompt.
Only Level 0 metadata (name, description, triggers) is included. Full skill bodies are not loaded at this stage.
Sourcepub fn with_activated_skill(self, skill: Option<ActivatedSkillContext>) -> Self
pub fn with_activated_skill(self, skill: Option<ActivatedSkillContext>) -> Self
Sets the explicitly activated Skill context.
This content is cacheable after activation. The owning Agent
invalidates the stable-prefix cache whenever activation changes.
Sourcepub fn with_context_files(self, files: Vec<ContextFile>) -> Self
pub fn with_context_files(self, files: Vec<ContextFile>) -> Self
Sets the context files for inclusion in the system prompt.
Typically loaded from AGENTS.md files via ContextLoader.
Sourcepub fn with_user_preferences(self, prefs: String) -> Self
pub fn with_user_preferences(self, prefs: String) -> Self
Sets user-specific instructions for inclusion in the system prompt.
Sourcepub fn with_memory_section(self, section: Option<String>) -> Self
pub fn with_memory_section(self, section: Option<String>) -> Self
Sets a bounded memory section for inclusion in the system prompt.
Memory is advisory only — never authoritative over session context.
When None, no memory section is injected.
Sourcepub fn with_todo_section(self, section: Option<String>) -> Self
pub fn with_todo_section(self, section: Option<String>) -> Self
Sets a bounded session todo section for inclusion in the dynamic prompt suffix.
Todo context is advisory orchestration state. It is intentionally not part of the stable cached prefix because it can change between turns.
Sourcepub fn with_custom_prompt(self, prompt: String) -> Self
pub fn with_custom_prompt(self, prompt: String) -> Self
Sets a custom prompt that replaces the default identity.
When provided, the custom prompt is used instead of the default identity. The rest of the prompt (tools, skills, etc.) is still assembled normally.
Sourcepub fn with_append_prompt(self, prompt: String) -> Self
pub fn with_append_prompt(self, prompt: String) -> Self
Sets an append prompt that is added at the end of the system prompt.
The append prompt is always placed last, after all other components.
Sourcepub fn clear_append_prompt(&mut self)
pub fn clear_append_prompt(&mut self)
Clears the append prompt, removing any previously set value.
Sourcepub fn set_append_prompt_opt(&mut self, prompt: Option<String>)
pub fn set_append_prompt_opt(&mut self, prompt: Option<String>)
Sets the append prompt to an optional value.
Use None to clear the append prompt, or Some(prompt) to set it.
Sourcepub fn build(&self) -> String
pub fn build(&self) -> String
Assembles and returns the final system prompt as a string.
Components are assembled in the optimal order for caching:
- Identity (or custom prompt if provided)
- Tools (sorted by name)
- Skill index
- Context files
- User preferences
- Append prompt (if provided)
Empty components are omitted from the output.
Sourcepub fn build_stable_prefix(&self) -> String
pub fn build_stable_prefix(&self) -> String
Builds only the stable prefix (Identity + Tools + Skills).
These sections are cacheable and do not change between turns unless tools, skills, or the identity/custom prompt are modified. The result can be cached by the caller and reused across turns.
Returns None if there are no stable sections (should not happen in
practice since Identity is always present).
Sourcepub fn build_dynamic_suffix(&self) -> String
pub fn build_dynamic_suffix(&self) -> String
Builds only the dynamic suffix (Context + User Preferences + Runtime + Append).
These sections change every turn (e.g., datetime) or are semi-stable (context files, user preferences). Combined with a cached stable prefix, they form the complete system prompt.
Sourcepub fn build_with_cache_markers(&self) -> (String, Vec<CacheMarker>)
pub fn build_with_cache_markers(&self) -> (String, Vec<CacheMarker>)
Assembles the system prompt with cache control markers.
Returns the prompt string and a list of CacheMarkers indicating
which byte ranges are stable and suitable for provider caching.
Cacheable sections:
- Identity (or custom prompt)
- Tools
- Skill index
Semi-stable sections (context files, user preferences) and the append prompt are not marked for caching.
Sourcepub fn total_tokens(&self) -> usize
pub fn total_tokens(&self) -> usize
Estimates the total token count of the assembled prompt.
Uses a heuristic of 1 token per 4 characters, which is a reasonable approximation for English text. This is not exact and should not be used for billing purposes.
Trait Implementations§
Source§impl Clone for SystemPromptBuilder
impl Clone for SystemPromptBuilder
Source§fn clone(&self) -> SystemPromptBuilder
fn clone(&self) -> SystemPromptBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more