HistoryCompressor
org.llm4s.context.HistoryCompressor
object 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:
- '''Group''' messages into semantic blocks (user-assistant pairs)
- '''Keep''' the last K blocks verbatim (recent context)
- '''Digest''' older blocks into
[HISTORY_SUMMARY]messages - '''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 Objecttrait Matchableclass Any
- Self type
-
HistoryCompressor.type
Members list
In this article