LangEx.Middleware.Summarization (LangEx v0.11.3)

Copy Markdown View Source

Middleware that replaces older history with an LLM-written summary.

When the message list grows past :max_bytes, everything older than the last :keep messages is condensed into a single summary message with one LLM call, and the history is rewritten in place via LangEx.Message.remove_all/0 — so the summary persists and later turns build on it instead of resummarising from scratch. Unlike the byte-notice fallback in LangEx.ContextCompaction, the agent keeps the findings from early rounds, not just a list of which tools ran.

Use this instead of the built-in :compaction (pass compaction: false to LangEx.Prebuilt.agent/1), not alongside it.

Only history older than the last :keep messages is summarised — if the bulk sits within the kept window (few but very large recent messages) the hook is a safe no-op and the context stays large. Pair with LangEx.Middleware.ContextEditing when recent tool results are the bloat.

Options

  • :model / :provider - the summariser model (required; a cheaper/faster model than the main agent is a good fit)
  • :max_bytes - trigger threshold for the whole message list (default 200_000)
  • :keep - number of most recent messages kept verbatim (default 6); the boundary is shifted so a kept %Message.Tool{} never loses its originating tool-call message
  • :summary_prompt - system prompt for the summariser
  • :resilient and other options are forwarded to the summariser call

Summary

Functions

Builds a summarisation middleware. See the module doc for options.

Functions

new(opts \\ [])

@spec new(keyword()) :: LangEx.Middleware.t()

Builds a summarisation middleware. See the module doc for options.