Skip to main content

ContextMinimizer

Struct ContextMinimizer 

Source
pub struct ContextMinimizer {
    pub max_turns: usize,
    pub strip_system_prompts: bool,
    pub strip_prior_tool_results: bool,
    pub max_context_chars: usize,
    /* private fields */
}
Expand description

Strips unnecessary context from LLM request messages to reduce attack surface.

Implements the context-minimization pattern: only the minimum necessary context is forwarded to the LLM, reducing the window for prompt injection attacks embedded in prior conversation turns.

Fields§

§max_turns: usize

Maximum number of conversation turns to keep.

§strip_system_prompts: bool

Whether to strip system prompts from forwarded tool contexts.

§strip_prior_tool_results: bool

Whether to strip prior tool results from context.

§max_context_chars: usize

Maximum total context characters.

Implementations§

Source§

impl ContextMinimizer

Source

pub fn new( max_turns: usize, strip_system_prompts: bool, strip_prior_tool_results: bool, max_context_chars: usize, ) -> Self

Create a new context minimizer with custom settings.

Source

pub fn with_strip_pattern(self, pattern: &str) -> Self

Add a custom strip pattern.

Source

pub fn minimize_context(&self, messages: &[Message]) -> Vec<Message>

Minimize a sequence of messages according to the configured policy.

Applies the following transformations in order:

  1. Strip system prompts (if configured)
  2. Strip tool results (if configured)
  3. Keep only the last max_turns user/assistant pairs
  4. Apply strip patterns to all remaining content
  5. Truncate to max_context_chars total
Source

pub fn minimize_text(&self, text: &str) -> String

Strip patterns from a single text string.

Trait Implementations§

Source§

impl Debug for ContextMinimizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ContextMinimizer

Source§

fn default() -> Self

Create a context minimizer with sensible defaults.

  • Keep last 10 turns
  • Strip system prompts from tool contexts
  • Do not strip tool results by default
  • 50,000 character maximum

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V