Class TwoPhaseCompactor

java.lang.Object
com.tnsai.context.compaction.TwoPhaseCompactor
All Implemented Interfaces:
ContextCompactor

public class TwoPhaseCompactor extends Object implements ContextCompactor
Two-phase context compactor combining truncation and LLM summarization.

Phase 1 (TruncatingCompactor): truncates large tool-call arguments and tool-result content in older messages. This is fast and requires no LLM call.

Phase 2 (LLMContextCompactor): if truncation alone is insufficient (tokens still exceed the threshold), older messages are summarized via LLM.

Usage


 LLMClient summarizer = LLMClientFactory.create("openai", "gpt-4o-mini");
 TwoPhaseCompactor compactor = new TwoPhaseCompactor(
     new TruncatingCompactor(200),
     new LLMContextCompactor(summarizer)
 );

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

 // Only runs phase 2 if phase 1 didn't bring tokens below threshold
 CompactionResult result = compactor.compact(messages, config);
 
Since:
0.2.3
Author:
TnsAI Team
See Also:
  • Constructor Details

    • TwoPhaseCompactor

      public TwoPhaseCompactor(TruncatingCompactor truncator, LLMContextCompactor summarizer)
      Creates a two-phase compactor with the given phase-1 and phase-2 compactors.
      Parameters:
      truncator - the phase-1 truncating compactor
      summarizer - the phase-2 LLM summarizing compactor
  • Method Details

    • compact

      public CompactionResult compact(List<Map<String,Object>> messages, CompactionConfig config)
      Description copied from interface: ContextCompactor
      Compacts the given messages according to configuration.
      Specified by:
      compact in interface ContextCompactor
      Parameters:
      messages - the conversation messages to compact
      config - the compaction configuration
      Returns:
      the compaction result
    • shouldCompact

      public boolean shouldCompact(int currentTokens, int maxTokens, CompactionConfig config)
      Two-phase shouldCompact: checks threshold and delegates to phase 1 first.

      If current tokens are below the threshold, no compaction is needed. This allows callers to skip the compact() call entirely when the context is small enough.

      Specified by:
      shouldCompact in interface ContextCompactor
      Parameters:
      currentTokens - current token count in context
      maxTokens - maximum allowed tokens
      config - the compaction configuration
      Returns:
      true if compaction should be triggered
    • estimateTokens

      public int estimateTokens(List<Map<String,Object>> messages)
      Description copied from interface: ContextCompactor
      Estimates token count for a list of messages.
      Specified by:
      estimateTokens in interface ContextCompactor
      Parameters:
      messages - the messages to estimate
      Returns:
      estimated token count