agentkit-compaction
Transcript compaction primitives for reducing context size while preserving useful state.
Compaction plugs into agentkit-loop through one generic seam — LoopMutator. This crate provides:
- Compactors (
Compactor,StrategyCompactor) — the mutator-shaped wrapper around triggers and strategies - Trigger helpers (
item_count_trigger,context_window_trigger) that decide when a compactor should fire - Strategies (
DropReasoningStrategy,DropFailedToolResultsStrategy,KeepRecentStrategy,SummarizeOlderStrategy) that drop, keep, or summarize transcript items - Backends (
CompactionBackend,AgentCompactor) for nested-loop semantic summarisation - Pipelines (
CompactionPipeline) for composing multiple steps into a single pass
Use it from agentkit-loop (or your own driver) when you need to trim older transcript state without losing essential context.
Quick start
Combine a trigger with a multi-step pipeline and register the result on the builder via AgentBuilderCompactorExt::compactor:
use ;
use ItemKind;
// Build a pipeline that:
// 1. Strips chain-of-thought reasoning parts
// 2. Removes failed tool results
// 3. Keeps only the 24 most recent items (preserving system/context)
let compactor = builder
.item_count_trigger // fire once transcript exceeds 32 items
.strategy
.build?;
let agent = builder
.model
.compactor
.build?;
Using a summarization backend
When you want older items to be condensed rather than dropped, pair SummarizeOlderStrategy with a CompactionBackend. The crate ships AgentCompactor, which runs a nested loop over a sub-agent:
use Arc;
use ;
use ItemKind;
let nested = new;
let compactor = builder
.trigger // 80% of a 200k window
.strategy
.backend
.build?;
let agent = builder
.model
.compactor
.build?;
Roll your own trigger
Triggers are plain closures with the TriggerFn shape — Fn(&[Item], MutationPoint) -> Option<CompactionReason> + Send + Sync. The built-ins are just convenience constructors:
use ;
let compactor = builder
.trigger
.strategy
.build?;