A compactor that summarizes older messages and keeps a recent window verbatim.
When the history exceeds :threshold_tokens (estimated via
ExAgent.Compaction.estimate_tokens/1 by default), everything older than the
last :keep_recent messages is replaced by a single system message produced by
the caller-supplied :summarize function — typically an ExAgent.run/3 call
to a cheap summarizer model.
Options
:threshold_tokens— compact once the estimate exceeds this (default4000).:keep_recent— number of trailing messages kept verbatim (default6).:summarize—(old_messages -> summary_text). Required to compact; if absent, the history is left untouched.:token_counter—(messages -> non_neg_integer()), defaultExAgent.Compaction.estimate_tokens/1.
Wiring
compaction = %ExAgent.Compaction.Capability{
compactor: ExAgent.Compaction.Summary,
opts: [
threshold_tokens: 6000,
keep_recent: 8,
summarize: fn old ->
{:ok, %{output: text}} = ExAgent.run(summarizer_agent, summarize_prompt(old))
text
end
]
}
ExAgent.new(model: "anthropic:claude-3-5-haiku", capabilities: [compaction])