Package com.tnsai.memory.strategy
Class SummaryStrategy
java.lang.Object
com.tnsai.memory.strategy.SummaryStrategy
- All Implemented Interfaces:
MemoryStrategy
Memory strategy that summarizes old messages to save tokens.
Instead of dropping old messages entirely, this strategy summarizes them into a condensed form, preserving important context while reducing token usage.
How It Works
Before: [M1, M2, M3, M4, M5, M6, M7, M8] (8 messages, 4000 tokens) After: [Summary(M1-M5), M6, M7, M8] (4 items, 2000 tokens)
Summary Modes
- LLM: Uses an LLM to generate intelligent summaries
- Extractive: Picks key sentences (faster, no API calls)
- Custom: User-provided summarization function
Usage Example
// With LLM summarization
MemoryStrategy strategy = SummaryStrategy.builder()
.summarizer(llmClient)
.summaryPrefix("[Previous conversation summary]")
.keepRecentMessages(4)
.build();
// With custom summarizer
MemoryStrategy strategy = SummaryStrategy.builder()
.summarizer(messages -> "Summary: " + messages.size() + " messages")
.keepRecentMessages(4)
.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for SummaryStrategy. -
Method Summary
Modifier and TypeMethodDescriptionApplies the strategy to the conversation history.static SummaryStrategy.Builderbuilder()Creates an extractive summarizer that picks key sentences.Returns a description of what this strategy does.getName()Returns the name of this strategy.llmSummarizer(LLMClient llm) Creates an LLM-based summarizer.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.memory.strategy.MemoryStrategy
estimateTokens, estimateTokens
-
Method Details
-
builder
-
apply
Description copied from interface:MemoryStrategyApplies the strategy to the conversation history.- Specified by:
applyin interfaceMemoryStrategy- Parameters:
history- The full conversation historybudget- The token budget configuration- Returns:
- Optimized history within the token budget
-
getName
Description copied from interface:MemoryStrategyReturns the name of this strategy.- Specified by:
getNamein interfaceMemoryStrategy
-
getDescription
Description copied from interface:MemoryStrategyReturns a description of what this strategy does.- Specified by:
getDescriptionin interfaceMemoryStrategy
-
extractiveSummarizer
Creates an extractive summarizer that picks key sentences. -
llmSummarizer
Creates an LLM-based summarizer.
-