Skip to main content

LlmCompaction

Struct LlmCompaction 

Source
pub struct LlmCompaction { /* private fields */ }
Expand description

Background LLM summarization behind the synchronous CompactionStrategy trait. See the module docs for the design, the cost trade-off, and the known limitation.

The state machine is per-session. Do not share one instance across concurrently running loops — Agent never does, but a hand-built AgentLoopConfig could.

Implementations§

Source§

impl LlmCompaction

Source

pub fn from_config(config: ModelConfig) -> Self

A compaction strategy that summarizes with the model in config, selecting the built-in provider for the config’s protocol and resolving the API key from the provider-conventional environment variable.

This mirrors Agent::from_config, and for the same reason: a bare (provider, model, key) triple lets the three drift apart, and drops the base_url, headers, and compat flags that every non-Anthropic provider needs.

The request is standalone, so this can (and usually should) name a cheaper model than the main loop’s.

Source

pub fn from_config_with( registry: &ProviderRegistry, config: ModelConfig, ) -> Result<Self, AgentBuildError>

Like from_config but resolves the provider from a caller-supplied registry, returning an error if the config’s protocol isn’t registered.

Source

pub fn from_provider( provider: Arc<dyn StreamProvider>, config: ModelConfig, ) -> Self

A compaction strategy that summarizes with an explicit provider.

The escape hatch for custom StreamProvider implementations and test doubles — pair with ModelConfig::mock. The config is still required so the model id, context window, and pricing stay defined together, matching Agent::from_provider.

Source

pub fn with_api_key(self, key: impl Into<String>) -> Self

Use an explicit API key instead of the environment-resolved one.

Source

pub fn with_trigger_ratio(self, ratio: f32) -> Self

Fraction of the budget at which background summarization starts. Clamped to [0.1, 0.95]. Default: DEFAULT_TRIGGER_RATIO.

Source

pub fn with_retain_tail_tokens(self, tokens: usize) -> Self

Token budget for the retained tail of recent messages.

Setting this too high relative to the context budget disables the strategy outright: summarization is first attempted at trigger_ratio · budget, and if the tail alone would consume all the history that exists at that point, there is nothing left to summarize. The default therefore derives from the budget — min(DEFAULT_RETAIN_TAIL_TOKENS, budget / 4), recomputed per call so it tracks the loop’s calibrated budget — rather than being a fixed number that silently no-ops on smaller context windows.

An explicit value here is used as given. If it turns out to be too large, a one-time warn! says so instead of failing quietly.

Source

pub fn with_system_prompt(self, prompt: impl Into<String>) -> Self

Replace the summarization system prompt.

Source

pub fn with_instruction(self, instruction: impl Into<String>) -> Self

Replace the summarization instruction (the “handoff briefing” prompt).

Source

pub fn with_max_summary_tokens(self, tokens: u32) -> Self

Cap on briefing length in output tokens. Default: 2000, floor: 256.

A briefing that hits this cap comes back with StopReason::Length and is rejected rather than spliced — a truncated handoff loses the “Open items” section the instruction puts last.

Source

pub fn with_timeout(self, timeout: Duration) -> Self

How long one summarization attempt may run. Default: DEFAULT_REQUEST_TIMEOUT.

Source

pub fn with_retry_config(self, retry: RetryConfig) -> Self

Retry policy for the summarization request. Default: RetryConfig::default; pass RetryConfig::none to disable.

Source

pub fn with_event_sender(self, events: UnboundedSender<AgentEvent>) -> Self

Emit AgentEvent::ContextCompacted on this channel when compaction runs, by either path.

CompactionStrategy::compact has no access to the loop’s event channel, so the sender has to come in from the side. Pair it with Agent::prompt_with_sender, where the caller owns the channel. Without it the per-compaction cost is still logged at info!, but nothing structured is emitted.

let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Sonnet 5"))
    .with_compaction_strategy(
        LlmCompaction::from_config(ModelConfig::anthropic(
            "claude-haiku-4-5",
            "Haiku 4.5",
        ))
        .with_event_sender(tx.clone()),
    );

Trait Implementations§

Source§

impl CompactionStrategy for LlmCompaction

Source§

fn compact( &self, messages: Vec<AgentMessage>, config: &ContextConfig, ) -> Vec<AgentMessage>

Compact messages to fit within the token budget defined by config. Read more
Source§

impl Debug for LlmCompaction

Redacts api_key; ModelConfig’s own Debug redacts header values.

Source§

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

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

impl Drop for LlmCompaction

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more