Condukt.Compactor behaviour (Condukt v3.0.0)

Copy Markdown View Source

Behaviour for compacting a session's conversation history.

Long-running agents accumulate messages that grow past the model's context window. A compactor receives the current message list and returns a shorter one — by dropping old turns, eliding large tool results, or replacing a prefix with a summary.

Compactors are pluggable just like Condukt.SessionStore. Pass one to start_link/1 and Condukt will apply it before every native model request (including tool iterations) and after run completion before persistence. Compactors should be idempotent when no further pruning is needed.

MyApp.Agent.start_link(
  compactor: Condukt.Compactor.Sliding
)

MyApp.Agent.start_link(
  compactor: {Condukt.Compactor.Sliding, keep: 30}
)

Compaction can also be triggered manually with Condukt.compact/1.

Background compaction

Native sessions can opt into background_compaction: true or a keyword policy alongside :compactor. The callback runs in a supervised task on an older prefix while recent messages and new activity remain unchanged. Its replacement is adopted only between inference requests or when idle. Unlike synchronous mode, the callback receives only the selected prefix.

Defaults start work at 65% of the model's context window and wait at 90%. If the pending result fails or is insufficient, the loop tries one fresh compaction before returning {:error, {:compaction_failed, reason}}. See the Compaction guide for options, token estimates, cancellation, and the manual compaction contract.

Context-length rejections

When a provider rejects a native request as too long, the session compacts once more with context_overflow: true added to the compactor's options, then retries. The previous pass produced a history that did not fit, so a compactor may shrink harder when it sees this option; the built-in ones do. See the Compaction guide.

Built-in compactors

Implementing a compactor

defmodule MyApp.MyCompactor do
  @behaviour Condukt.Compactor

  @impl true
  def compact(messages, _opts) do
    {:ok, Enum.take(messages, -10)}
  end
end

Telemetry

Each compaction emits [:condukt, :compact, :start] and [:condukt, :compact, :stop]. Stop measurements include :duration, :before/:after message counts and :before_bytes/:after_bytes external-term sizes. Metadata identifies :agent and :session_id; stop also includes :status and :changed. Compactions triggered by a provider's context-length rejection add trigger: :overflow to both events. No message payload is included.

Summary

Functions

Dispatches compact/2 to the configured compactor module or {module, opts} tuple. Default options are merged underneath the tuple's options.

Callbacks

compact(list, keyword)

@callback compact(
  [Condukt.Message.t()],
  keyword()
) :: {:ok, [Condukt.Message.t()]} | {:error, term()}

Functions

compact(compactor, messages, default_opts \\ [])

Dispatches compact/2 to the configured compactor module or {module, opts} tuple. Default options are merged underneath the tuple's options.