Behaviour for context compaction strategies.
Context compaction reduces conversation history size while preserving essential information. This helps manage LLM context windows and avoid "context rot" - degraded performance as context length increases.
Callbacks
compact/2- Compacts the context using the strategy's approachshould_compact?/2- (optional) Returns whether compaction should occurintrospect/1- (optional) Returns strategy metadata for observability
Example
defmodule MyApp.Compaction.Custom do
@behaviour Puck.Compaction
@impl true
def compact(context, config) do
# Custom compaction logic
{:ok, compacted_context}
end
endBuilt-in Strategies
Puck.Compaction.Summarize- LLM-based summarization (preserves semantic meaning)
Usage
# Manual compaction
{:ok, compacted} = Puck.Compaction.compact(context, {Puck.Compaction.Summarize, %{
client: client,
keep_last: 3
}})
Summary
Callbacks
Compacts the context to reduce its size.
Returns metadata about the compaction strategy.
Returns whether the context should be compacted.
Functions
Compacts a context using the specified strategy.
Checks if context should be compacted using the specified strategy.
Types
Callbacks
@callback compact(Puck.Context.t(), config()) :: result()
Compacts the context to reduce its size.
Parameters
context- The context to compactconfig- Strategy-specific configuration
Returns
{:ok, compacted_context}on success{:error, reason}on failure
@callback introspect(config()) :: introspection()
Returns metadata about the compaction strategy.
Used by telemetry/tracing to identify the strategy being used.
Parameters
config- Strategy configuration
Expected Keys
:strategy- Strategy name (e.g., "summarize", "sliding_window")
@callback should_compact?(Puck.Context.t(), config()) :: boolean()
Returns whether the context should be compacted.
Implementations can check token counts, message counts, or other criteria.
Parameters
context- The context to checkconfig- Strategy-specific configuration (may include thresholds)
Returns
trueif compaction should occurfalseotherwise
Functions
Compacts a context using the specified strategy.
This is a convenience function that delegates to the strategy module.
Parameters
context- The context to compactstrategy_tuple- A tuple of{strategy_module, config}
Examples
{:ok, compacted} = Puck.Compaction.compact(context, {Puck.Compaction.Summarize, %{
client: client,
keep_last: 3
}})
Checks if context should be compacted using the specified strategy.
Falls back to true if the strategy doesn't implement should_compact?/2.
Parameters
context- The context to checkstrategy_tuple- A tuple of{strategy_module, config}
Examples
if Puck.Compaction.should_compact?(context, {Puck.Compaction.Summarize, config}) do
{:ok, compacted} = Puck.Compaction.compact(context, {Puck.Compaction.Summarize, config})
end