Class SlidingWindowStrategy

java.lang.Object
com.tnsai.memory.strategy.SlidingWindowStrategy
All Implemented Interfaces:
MemoryStrategy

public class SlidingWindowStrategy extends Object implements 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);