Package com.tnsai.context.compaction
Class TwoPhaseCompactor
java.lang.Object
com.tnsai.context.compaction.TwoPhaseCompactor
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionTwoPhaseCompactor(TruncatingCompactor truncator, LLMContextCompactor summarizer) Creates a two-phase compactor with the given phase-1 and phase-2 compactors. -
Method Summary
Modifier and TypeMethodDescriptionCompacts the given messages according to configuration.intestimateTokens(List<Map<String, Object>> messages) Estimates token count for a list of messages.booleanshouldCompact(int currentTokens, int maxTokens, CompactionConfig config) Two-phase shouldCompact: checks threshold and delegates to phase 1 first.
-
Constructor Details
-
TwoPhaseCompactor
Creates a two-phase compactor with the given phase-1 and phase-2 compactors.- Parameters:
truncator- the phase-1 truncating compactorsummarizer- the phase-2 LLM summarizing compactor
-
-
Method Details
-
compact
Description copied from interface:ContextCompactorCompacts the given messages according to configuration.- Specified by:
compactin interfaceContextCompactor- Parameters:
messages- the conversation messages to compactconfig- the compaction configuration- Returns:
- the compaction result
-
shouldCompact
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:
shouldCompactin interfaceContextCompactor- Parameters:
currentTokens- current token count in contextmaxTokens- maximum allowed tokensconfig- the compaction configuration- Returns:
- true if compaction should be triggered
-
estimateTokens
Description copied from interface:ContextCompactorEstimates token count for a list of messages.- Specified by:
estimateTokensin interfaceContextCompactor- Parameters:
messages- the messages to estimate- Returns:
- estimated token count
-