Expand description
Context compaction — deciding when to compact and where to cut.
Compaction summarizes an old prefix of the conversation and keeps a recent
suffix. The prefix is discarded from what the model sees, so only the kept
suffix must be valid on the wire. The one invariant that makes a suffix
invalid is an orphaned ToolResult: a kept tool result whose matching
ToolCall (which lives in a preceding Assistant message) was summarized
away. Providers reject that as an unmatched tool result.
safe_cut returns the earliest index at which the conversation may be
split so the kept suffix ctx[cut..] is self-consistent. It only ever moves
the cut earlier than requested — summarizing less rather than breaking the
pairing — and returns 0 when no boundary pairs everything.
The reverse direction needs no check: the agent loop never leaves a
ToolCall without its ToolResult, and a ToolResult always follows its
ToolCall, so a kept call’s results (later indices) are kept too.
CompactionPolicy decides when to compact (the trigger) and how much
recent history to keep. Retention is absolute — a token budget plus a turn
floor — not a fraction of the window, so it means the same thing on an 8k
and a 1M model. The trigger uses the provider-reported input-token count
(never an estimate); only the cut boundary uses estimate_tokens.
Structs§
- Compaction
Policy - When to compact and how much recent context to retain.
- Summary
- A produced summary and the usage that generated it.
Enums§
- Compact
Outcome - What a compaction pass did.
Constants§
- DEFAULT_
KEEP_ RECENT_ TOKENS - Recent context kept verbatim by default, in tokens — an absolute budget (pi-style), independent of the model’s window size.
- DEFAULT_
KEEP_ RECENT_ TURNS - Complete turns always kept by default, whatever their token size.
- DEFAULT_
MIN_ REMAINING - Tokens held free below the working ceiling by default: the trigger fires once fewer than this many remain.
Functions§
- compact_
ctx - Summarize the older prefix of
ctxin place: keep the newest messages perpolicy, replace the rest with a summary message, and — when a session is open — persist the boundary so a resume rebuilds the same view. - cut_
for_ policy - Cut for
policy: keep the newest messages up tokeep_recent_tokensbut never fewer thankeep_recent_turnsturns, made tool-safe bysafe_cut. - estimate_
tokens - Rough per-message token estimate (~4 chars/token plus a small fixed overhead). Used only to place the cut boundary; the compaction trigger uses provider-reported token counts, never this.
- safe_
cut - Earliest index at which
ctxmay be split so the kept suffixctx[cut..]has no orphaned tool result. - summarize
- Summarize
conversationwith the configured LLM, optionally focused byinstructions(the/compact <prompt>argument). Drives the sameStreamFnthe agent uses, collecting the streamed text. Returns an error string on a stream error, or when the summarizer produces no text. - summary_
message - The message injected in place of a summarized prefix.