ADK.Context.Compressor behaviour (ADK v0.0.1-alpha.1)

Copy Markdown View Source

Behaviour for context compression strategies.

Context compression reduces the number of messages/tokens sent to the LLM when conversation history grows too large. Strategies implement this behaviour to provide different compression approaches.

Configuration

Compression can be configured on the agent or via RunConfig:

agent = LlmAgent.new(
  name: "bot",
  model: "gemini-flash-latest",
  instruction: "Help",
  context_compressor: {ADK.Context.Compressor.Truncate, max_messages: 20}
)

Or triggered automatically when message count exceeds a threshold.

Summary

Callbacks

Compress a list of messages, returning a shorter list.

Functions

Create a compaction event summarizing what was compressed.

Apply compression to messages if they exceed the configured threshold.

Types

message()

@type message() :: %{role: atom(), parts: [map()]}

Callbacks

compress(messages, opts, context)

@callback compress(messages :: [message()], opts :: keyword(), context :: map()) ::
  {:ok, [message()]} | {:error, term()}

Compress a list of messages, returning a shorter list.

The opts keyword list contains strategy-specific configuration. The context map may include :model and :instruction for strategies that need to call the LLM (e.g., summarization).

Functions

compaction_event(original_count, compressed_count)

@spec compaction_event(non_neg_integer(), non_neg_integer()) :: ADK.Event.t()

Create a compaction event summarizing what was compressed.

The event has author "system:compaction" so it can be identified during session reload and content assembly.

maybe_compress(messages, opts)

@spec maybe_compress([message()], keyword() | nil) :: [message()]

Apply compression to messages if they exceed the configured threshold.

Returns the original messages if no compressor is configured or if the message count is below the threshold.