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

Usage Example


 MemoryStrategy strategy = SlidingWindowStrategy.builder()
     .maxTokens(8000)
     .preserveSystemPrompt(true)
     .build();

 List<Map<String, Object>> optimized = strategy.apply(history, tokenBudget);
 
  • Method Details

    • apply

      List<Map<String,Object>> apply(List<Map<String,Object>> history, TokenBudget budget)
      Applies the strategy to the conversation history.
      Parameters:
      history - The full conversation history
      budget - 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

      default int estimateTokens(List<Map<String,Object>> history)
      Estimates the token count for the given history.

      Delegates to TokenEstimator for 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

      default int estimateTokens(Map<String,Object> message)
      Estimates the token count for a single message's content.

      Returns 0 for messages with null content. Uses TokenEstimator for content-aware estimation.

      Parameters:
      message - The message to estimate
      Returns:
      Estimated token count for the message content