Package com.tnsai.memory.strategy
Class SlidingWindowStrategy
java.lang.Object
com.tnsai.memory.strategy.SlidingWindowStrategy
- All Implemented Interfaces:
MemoryStrategy
Sliding window memory strategy that keeps the most recent messages.
This strategy maintains a rolling window of the most recent messages that fit within the token budget. Older messages are dropped to make room for new ones.
How It Works
Before: [M1, M2, M3, M4, M5, M6, M7, M8] (exceeds budget) After: [M5, M6, M7, M8] (fits in budget)
Features
- Preserves system prompts (optional)
- Always keeps the last user message
- Simple and predictable behavior
- Low computational overhead
Usage Example
MemoryStrategy strategy = SlidingWindowStrategy.builder()
.preserveSystemPrompt(true)
.build();
TokenBudget budget = TokenBudget.builder()
.totalContextWindow(8192)
.build();
List<Map<String, Object>> optimized = strategy.apply(history, budget);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for SlidingWindowStrategy. -
Method Summary
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
-