Compaction

Automatic context compaction: when to compress a conversation, and how hard.

Long conversations eventually outgrow the model's context window. Compaction watches the token count and, past a threshold, replaces the older turns with a structured summary so the run can keep going.

It compresses in proportion to how cheaply something can be recovered. A tool result you can fetch again is worth less than a decision you can't re-derive. And a dead end, meaning something already tried that didn't work, is the most valuable thing to keep and the first thing a naive summariser throws away.

Pass it to generateText, streamText, or an Agent and it runs itself.

let result = try await generateText(
  model: AnthropicModel("claude-sonnet-5"),
  messages: history,
  tools: tools,
  compaction: Compaction()
)

Declaration

public struct Compaction

Defined in Sources/AI/Core/Compaction.swift.

Initializer

init(
    budget: CompactionBudget = CompactionBudget(),
    pinning: CompactionPinning = .default,
    keepLastSteps: Int = 4,
    onCompact: (@Sendable (CompactionEvent) -> Void)? = nil
)

Properties

PropertyTypeDefault
var budgetCompactionBudget
var pinningCompactionPinning
var keepLastStepsInt
var onCompact(@Sendable (CompactionEvent) -> Void)?

See also