HistoryCompressor

org.llm4s.context.HistoryCompressor

Deterministic history compression using structured digest extraction.

This compressor creates compact [HISTORY_SUMMARY] digests from older conversation blocks, preserving recent context verbatim while summarizing history. The digests extract key structured information that's likely to be referenced later.

==Compression Strategy==

The compressor uses a "keep last K" strategy:

  1. '''Group''' messages into semantic blocks (user-assistant pairs)
  2. '''Keep''' the last K blocks verbatim (recent context)
  3. '''Digest''' older blocks into [HISTORY_SUMMARY] messages
  4. '''Consolidate''' if total digest size exceeds the cap

==Information Extraction==

Each digest extracts structured information using regex patterns:

  • '''Identifiers''': IDs, UUIDs, keys, references
  • '''URLs''': HTTP/HTTPS links
  • '''Constraints''': Must/should/cannot requirements
  • '''Status Codes''': HTTP status codes, error codes
  • '''Errors''': Error messages and exceptions
  • '''Decisions''': "decided", "chosen", "selected" statements
  • '''Tool Usage''': Function/API call mentions
  • '''Outcomes''': Results, conclusions, completions

==Idempotency==

The compressor is '''idempotent''': if messages already contain [HISTORY_SUMMARY] markers, they are returned unchanged. This allows safe re-application.

Attributes

See also

SemanticBlocks for the block grouping algorithm

StructuredInfo for the extracted information types

Example
val compressed = HistoryCompressor.compressToDigest(
 messages = conversation.messages,
 tokenCounter = counter,
 capTokens = 400,  // Max tokens for digests
 keepLastK = 3     // Keep last 3 blocks verbatim
)
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def compressToDigest(messages: Seq[Message], tokenCounter: ConversationTokenCounter, capTokens: Int, keepLastK: Int): Result[Seq[Message]]

Compress history into digest(s), keeping the last keepLastK semantic blocks intact.

Compress history into digest(s), keeping the last keepLastK semantic blocks intact.

The compression process:

  1. Groups messages into semantic blocks
  2. Splits blocks into "older" (to digest) and "recent" (to keep)
  3. Creates [HISTORY_SUMMARY] messages for older blocks
  4. Consolidates digests if they exceed capTokens

Value parameters

capTokens

Maximum tokens for all digest messages combined

keepLastK

Number of most recent semantic blocks to keep verbatim

messages

Full conversation messages (without system prompt injection)

tokenCounter

Token counter for the target model

Attributes

Returns

Compressed messages with digests followed by recent blocks