Expand description
Transcript compaction primitives for reducing context size while preserving useful state.
This crate provides the building blocks for compacting an agent transcript when it grows too large. The main concepts are:
- Compactors (
Compactor) decide when and how to compact in a single trait. Register one with the agent builder viaAgentBuilderCompactorExt::compactor. - Strategies (
CompactionStrategy) decide how the transcript is transformed: dropping reasoning, removing failed tool results, keeping only recent items, or summarising older items via a backend. - Pipelines (
CompactionPipeline) chain multiple strategies into a single pass. - Backends (
CompactionBackend) provide provider-backed summarisation for strategies that need it (e.g.SummarizeOlderStrategy).
Wire a StrategyCompactor (bundling a trigger closure + strategy +
optional backend) into the loop, or implement Compactor directly for
stateful triggers.
Structs§
- Agent
Compactor CompactionBackendthat summarises items by running a nested loop over a sub-agent.- Agent
Compactor Builder - Builder for
AgentCompactor. - Compaction
Context - Runtime context passed to each
CompactionStrategyduring execution. - Compaction
Pipeline - An ordered sequence of
CompactionStrategysteps executed one after another. - Compaction
Request - Input to a
CompactionStrategy. Carries the transcript plus request metadata so strategies can decide which items to keep, drop, or summarise. - Compaction
Result - Output of a
CompactionStrategy. - Compactor
Mutator - Adapts any
Compactorto aLoopMutatorso it can be registered directly viaAgentBuilder::mutator. Most callers reach this throughAgentBuilderCompactorExt::compactorrather than constructing it directly. - Drop
Failed Tool Results Strategy - Strategy that removes
Part::ToolResultparts whereis_erroristrue. - Drop
Reasoning Strategy - Strategy that removes
Part::Reasoningparts from every item. - Keep
Recent Strategy - Strategy that keeps only the
Nmost recent removable items and drops the rest. - Strategy
Compactor - A reusable
Compactorthat bundles a trigger closure with aCompactionStrategy(often aCompactionPipeline) and an optionalCompactionBackend. Use this when your trigger logic is a simple predicate over the transcript; implementCompactordirectly when you need richer state (token meters, atomics, etc.). - Strategy
Compactor Builder - Builder for
StrategyCompactor. - Summarize
Older Strategy - Strategy that replaces older transcript items with a backend-generated summary.
- Summary
Request - Request sent to a
CompactionBackendasking it to summarise a set of transcript items. - Summary
Result - Response from a
CompactionBackendcontaining the summarised items.
Enums§
- Agent
Compactor Build Error - Builder error for
AgentCompactor. - Compaction
Error - Errors that can occur during compaction.
- Compaction
Reason - The reason a compaction was triggered.
- Strategy
Compactor Build Error - Builder error for
StrategyCompactor.
Traits§
- Agent
Builder Compactor Ext - Extension trait that adds
compactortoAgentBuilder, wrapping anyCompactorin aCompactorMutatorand registering it viaAgentBuilder::mutator. - Compaction
Backend - Provider-backed summarisation service.
- Compaction
Strategy - A single compaction step that transforms a transcript.
- Compactor
- High-level compaction primitive. Implementations decide whether and how to compact, owning their own derived state (e.g. running token totals behind interior mutability) so the framework doesn’t need to plumb a separate observer or shared atomic.
Functions§
- context_
window_ trigger - Build a trigger closure that fires when the most recent transcript item’s
reported
usage.tokens.input_tokensreacheswindow * percent / 100. - item_
count_ trigger - Build a trigger closure that fires when the transcript grows beyond
max_itemsitems. Convenience matchingStrategyCompactorBuilder::item_count_trigger.
Type Aliases§
- Trigger
Fn - Boxed predicate driving
StrategyCompactor: it inspects the transcript and currentMutationPointand returns the reason to fire compaction, orNoneto skip.