Alloy.Context.Compactor (alloy v0.12.4)

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.

Compaction options live under compaction::

  • :clear_tool_results - clear old bulky tool-result content before summary generation (default true)
  • :keep_recent_tool_results - newest tool results to preserve verbatim when clearing (default 3)
  • :summary_system_prompt - system prompt used for summary generation
  • :summary_prompt - user prompt instructions appended after the serialized conversation

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, opts \\ [])

@spec maybe_compact(
  Alloy.Agent.State.t(),
  keyword()
) :: {: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.