FactRetrievalHistoryCompressionStrategy

A history compression strategy that extracts structured facts about predefined concepts from the current conversation history using an LLM, then replaces the full history with a compact assistant message containing those extracted facts.

For each Concept in concepts, the strategy issues a separate structured LLM request against a snapshot of the current conversation (wrapped in XML tags) to extract either a single fact (FactType.SINGLE) or multiple facts (FactType.MULTIPLE). The original prompt and model are restored after each extraction so that the session state is not mutated.

The resulting compressed prompt contains:

  • All original system messages

  • The first user message (if present)

  • Any provided memoryMessages

  • A single assistant message with a [CONTEXT RESTORATION] block listing the extracted facts and the approximate number of tool interactions that occurred before compression

If no facts are extracted for any concept, the strategy delegates to fallback (by default NoCompression, which leaves the prompt unchanged). Provide a different strategy (e.g. a TLDR-based one) to guarantee the history still shrinks when no configured concept matched.

Parameters

concepts

A list of Concept objects that define the topics for which facts should be extracted.

fallback

Strategy used when no facts are extracted for any concept. Defaults to NoCompression.

Constructors

Link copied to clipboard
constructor(concepts: List<Concept>, fallback: HistoryCompressionStrategy = NoCompression)
constructor(vararg concepts: Concept)

Secondary constructor for FactRetrievalHistoryCompressionStrategy that initializes the instance with a variable number of Concept objects, converting them into a list.

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
open suspend override fun compress(llmSession: AIAgentLLMWriteSession, memoryMessages: List<Message>)

Extracts facts about each configured Concept from the current conversation history and replaces the prompt with a compressed version containing those facts.