ExAgent. Compaction. Summary
(ExAgent v3.0.0)
View Source
A compactor that summarizes older messages and keeps a recent window verbatim.
Stability — Candidate.
When the history exceeds :threshold_tokens (estimated via
ExAgent.Compaction.estimate_tokens/1 by default), the caller's :summarize
function can replace messages older than the last :keep_recent. The recent
window expands to keep calls with their results and retries. With an integer
window, older completed exchanges within the current run are also eligible.
A group carrying both instructions and conversation stays intact. A mixed group before the active user request bounds how far back the summary can reach. A mixed group after it bounds how far forward it can reach. This keeps an earlier prompt with system instructions ahead of later updates.
The summary is ordinary user-level context inserted before the active user request, never a new System instruction. Pinned messages may exceed the target window, and a summary is not guaranteed to reduce the model-specific token count. Context without an identifiable user request is left unchanged.
Options
:threshold_tokens— non-negative threshold (default4000); compact once the estimate exceeds it.:keep_recent— a non-negative number of trailing messages (default6), or{:turns, n}to keep the lastnuser turns verbatim (n >= 1). A turn begins at a request containing a user part and includes all subsequent messages up to the next such request. For example,{:turns, 1}keeps every tool exchange in the active turn. If there are fewer thannturns, the whole input is kept. Turn counting sees the incoming projection. Place compaction before capabilities that add synthetic user messages. An integer of zero still retains instructions and the active user request.:summarize— a function of the old messages, optionally taking the effective run deadline as its second argument. The deadline is an absolute monotonic millisecond timestamp on this node, ornilwhen the run has no deadline (ctx.deadlineofcompact/3). Required to compact; if absent, the projection is left untouched. Return valid UTF-8 text with at least one non-whitespace character,{:no_change}or{:error, reason}. Blank or invalid text returns{:error, :invalid_summary}.:token_counter—(messages -> non_neg_integer()), defaultExAgent.Compaction.estimate_tokens/1.:memo— anExAgent.Compaction.Memoprocess (pid or registered name). Recommended for long conversations; see "Reusing summaries".:preserve_signed_prefix— when true, summarize only groups after the last provider-protected exchange, using the same boundary asExAgent.Compaction.ClearToolResults. Defaults to true when the model profile declaresreasoning_binding: :prefixand false otherwise. Setting it to false can cause the adapter to retire signed reasoning.
:summarize may perform external IO. The callback's model requests, tokens and
costs are not automatically observable or charged to this run by this API.
Under an ExAgent.Fixture, a run the callback starts with ExAgent.run/3
records and replays through the calling run's fixture.
Repeated model requests can invoke it repeatedly; applications must not treat
compaction as an exactly-once side effect.
Reusing summaries
The canonical history stays append-only, so without :memo every model request
above the threshold summarizes again: one extra model call per step, and a new
prefix that defeats provider prompt caching.
With :memo, the last summary of each conversation is kept with the positions
and a digest of the exact messages it replaced. When they are still the first
replaceable messages, unchanged and at the same positions, later requests
reuse the summary and send the other messages verbatim. If that projection
exceeds the threshold and more messages are eligible, :summarize receives
the previous summary followed by the newly replaceable messages.
If an earlier message becomes replaceable (for example, the previous user
prompt in a tool-heavy turn), the summarizer receives all eligible original
messages in chronological order. Editing a covered message also rebuilds
the summary. Changing the summarizer
alone can reuse it, so switch to a fresh memo when the summarization
instructions, language or model change. Memo failures fall back to an ordinary summary.
Used as a run capability, Summary caps each memo wait at the remaining
run deadline and checks the deadline before calling :summarize. A callback
already running can finish after the deadline. Give any model or service it
calls its own timeout.
Wiring
Build the capability with ExAgent.Compaction.capability/2, which validates
the options above. Supply a trusted, explicitly configured model and
summarizer_agent; ReqLLM credentials and tool/stream qualification belong
to those instances.
compaction =
ExAgent.Compaction.capability(ExAgent.Compaction.Summary,
threshold_tokens: 6000,
keep_recent: 8,
summarize: fn old, deadline ->
case ExAgent.run(summarizer_agent, summarize_prompt(old), deadline: deadline) do
{:ok, %{output: text}} -> text
{:error, reason} -> {:error, reason}
end
end
)
ExAgent.new(model: model, capabilities: [compaction])A failing :summarize leaves the projection unchanged and is reported by
[:exagent, :compaction, :stop] with status: :error.
Summary
Functions
Compact a projection with the configured summarizer. A two-argument
summarizer receives ctx.deadline (nil without one).
Check the options documented above; :summarize is required. Unknown,
duplicate or missing options and invalid values raise ArgumentError.
Functions
Compact a projection with the configured summarizer. A two-argument
summarizer receives ctx.deadline (nil without one).
Check the options documented above; :summarize is required. Unknown,
duplicate or missing options and invalid values raise ArgumentError.