Class FixedWindowStrategy

java.lang.Object
dev.agenor.runtime.memory.llm.FixedWindowStrategy
All Implemented Interfaces:
ContextWindowStrategy

public class FixedWindowStrategy extends Object implements ContextWindowStrategy
Fixed window strategy - includes last N messages that fit in budget.

Algorithm:

  1. Start from most recent message
  2. Add messages while under token budget
  3. Stop when budget would be exceeded

Characteristics:

  • Pros: Simple, fast, predictable
  • Cons: May lose important early context
  • Best For: Short conversations, when recent context is most important
  • Performance: O(n) time, O(n) space
  • Requires LLM: No
  • Overhead: 0 tokens

Example:


 ContextWindowStrategy strategy = new FixedWindowStrategy();

 List<LLMMessage> selected = strategy.selectMessages(
     allMessages,
     2000,  // Token budget
     estimator
 );

 // Or use singleton from factory
 import static dev.agenor.runtime.memory.llm.ContextWindowStrategies.FIXED;
 List<LLMMessage> selected = FIXED.selectMessages(allMessages, 2000, estimator);
 

Thread Safety: This class is stateless and thread-safe.

Since:
0.6.0
  • Constructor Details

    • FixedWindowStrategy

      public FixedWindowStrategy()
      Create a new fixed window strategy instance.
  • Method Details

    • selectMessages

      public List<LLMMessage> selectMessages(List<LLMMessage> allMessages, int maxTokens, TokenEstimator estimator)
      Description copied from interface: ContextWindowStrategy
      Select messages to include in context window.

      Implementations must:

      • Return messages that fit within maxTokens budget
      • Preserve message order (oldest to newest)
      • Use provided TokenEstimator for token counting
      • Handle edge cases (empty list, budget too small, etc.)

      Example Implementation:

      
       public List<LLMMessage> selectMessages(
           List<LLMMessage> allMessages,
           int maxTokens,
           TokenEstimator estimator
       ) {
           List<LLMMessage> selected = new ArrayList<>();
           int currentTokens = 0;
      
           // Start from end (most recent)
           for (int i = allMessages.size() - 1; i >= 0; i--) {
               LLMMessage msg = allMessages.get(i);
               int msgTokens = estimator.estimateTokens(msg);
      
               if (currentTokens + msgTokens <= maxTokens) {
                   selected.add(0, msg);  // Add at start to maintain order
                   currentTokens += msgTokens;
               } else {
                   break;  // Budget exhausted
               }
           }
      
           return selected;
       }
       
      Specified by:
      selectMessages in interface ContextWindowStrategy
      Parameters:
      allMessages - all available messages (oldest to newest)
      maxTokens - maximum tokens for selected messages
      estimator - token estimator to use
      Returns:
      selected messages that fit in budget (oldest to newest)
    • getName

      public String getName()
      Description copied from interface: ContextWindowStrategy
      Get strategy name for logging and debugging.

      Examples:

      • "fixed" - Fixed window strategy
      • "sliding" - Sliding window strategy
      • "summarized" - Summarization strategy
      • "semantic" - Semantic relevance strategy
      Specified by:
      getName in interface ContextWindowStrategy
      Returns:
      strategy name (lowercase, no spaces)
    • requiresLLM

      public boolean requiresLLM()
      Description copied from interface: ContextWindowStrategy
      Check if this strategy requires an LLM provider.

      Some strategies (like summarization) need an LLM to generate summaries. Others (like fixed window) do not.

      Specified by:
      requiresLLM in interface ContextWindowStrategy
      Returns:
      true if strategy needs LLM access
    • getOverheadTokens

      public int getOverheadTokens()
      Description copied from interface: ContextWindowStrategy
      Get estimated overhead tokens for this strategy.

      Some strategies add overhead:

      • Fixed/Sliding: 0 tokens (no modifications)
      • Summarized: ~100-300 tokens (summary message)
      • Semantic: 0 tokens (just selection)
      Specified by:
      getOverheadTokens in interface ContextWindowStrategy
      Returns:
      estimated overhead in tokens