Alloy.Context.Compactor (alloy v0.10.1)

Copy Markdown View Source

Summary-based context compaction.

When the conversation approaches the configured reserve threshold, Alloy preserves the first message, keeps a recent verbatim token window, and replaces older context with a structured handoff summary. If summary generation fails, Alloy falls back to deterministic truncation.

Summary

Functions

Compacts messages, preserving the first message and the most recent N messages.

Forces compaction regardless of reserve budget. Used when the provider rejects the prompt as too long.

Compacts state messages when they exceed max_tokens - reserve_tokens.

Prefix used for synthetic handoff summary messages inserted by the compactor.

Functions

compact_messages(messages, opts \\ [])

@spec compact_messages(
  [Alloy.Message.t()],
  keyword()
) :: [Alloy.Message.t()]

Compacts messages, preserving the first message and the most recent N messages.

This helper intentionally stays deterministic and provider-free so it can serve as the fallback truncation strategy.

Options

  • :keep_recent - number of recent messages to preserve (default 10)

force_compact(state)

@spec force_compact(Alloy.Agent.State.t()) :: Alloy.Agent.State.t()

Forces compaction regardless of reserve budget. Used when the provider rejects the prompt as too long.

maybe_compact(state)

@spec maybe_compact(Alloy.Agent.State.t()) ::
  {:compacted | :unchanged, Alloy.Agent.State.t()}

Compacts state messages when they exceed max_tokens - reserve_tokens.

Returns {:compacted, state} when compaction occurred, or {:unchanged, state} when already within budget.

summary_prefix()

@spec summary_prefix() :: String.t()

Prefix used for synthetic handoff summary messages inserted by the compactor.