Package com.tnsai.context.compaction


package com.tnsai.context.compaction
Context compaction for long-running agent sessions.

This package provides mechanisms to automatically manage context size by summarizing older messages when approaching token limits.

Compaction Strategies

  • TruncatingCompactor (Phase 1) — truncates large tool-call arguments and tool-result content in older messages. Fast, no LLM call.
  • LLMContextCompactor (Phase 2) — uses an LLM to summarize older messages into a concise summary.
  • TwoPhaseCompactor — combines both: truncation first, then LLM summarization if still over threshold.

Quick Start


 CompactionConfig config = CompactionConfig.builder()
     .thresholdRatio(0.85)
     .preserveLastN(5)
     .build();

 // Two-phase compaction (recommended)
 TwoPhaseCompactor compactor = new TwoPhaseCompactor(
     new TruncatingCompactor(200),
     new LLMContextCompactor(llm)
 );
 CompactionResult result = compactor.compact(messages, config);
 
Since:
0.2.3
Author:
TnsAI Team