Package com.tnsai.memory.strategy
Interface MemoryStrategy
- All Known Implementing Classes:
HybridStrategy,SlidingWindowStrategy,SummaryStrategy
public interface MemoryStrategy
Strategy for managing conversation memory within token limits.
MemoryStrategy defines how conversation history is managed when it exceeds the token budget. Different strategies offer different trade-offs between context preservation and token efficiency.
Available Strategies
SlidingWindowStrategy: Keeps most recent N tokensSummaryStrategy: Summarizes old messagesHybridStrategy: Combines sliding window + summary
Usage Example
MemoryStrategy strategy = SlidingWindowStrategy.builder()
.maxTokens(8000)
.preserveSystemPrompt(true)
.build();
List<Map<String, Object>> optimized = strategy.apply(history, tokenBudget);
-
Method Summary
Modifier and TypeMethodDescriptionApplies the strategy to the conversation history.default intestimateTokens(List<Map<String, Object>> history) Estimates the token count for the given history.default intestimateTokens(Map<String, Object> message) Estimates the token count for a single message's content.Returns a description of what this strategy does.getName()Returns the name of this strategy.
-
Method Details
-
apply
Applies the strategy to the conversation history.- Parameters:
history- The full conversation historybudget- The token budget configuration- Returns:
- Optimized history within the token budget
-
getName
String getName()Returns the name of this strategy. -
getDescription
String getDescription()Returns a description of what this strategy does. -
estimateTokens
Estimates the token count for the given history.Delegates to
TokenEstimatorfor content-aware estimation that accounts for whitespace density, punctuation, structured content, and other heuristics beyond simple length/4.- Parameters:
history- The conversation history- Returns:
- Estimated token count
-
estimateTokens
Estimates the token count for a single message's content.Returns 0 for messages with null content. Uses
TokenEstimatorfor content-aware estimation.- Parameters:
message- The message to estimate- Returns:
- Estimated token count for the message content
-