Class SummarizationStrategy

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

public class SummarizationStrategy extends Object implements ContextWindowStrategy
Summarization strategy - includes recent messages plus LLM-generated summary of old conversation.

Algorithm:

  1. Include most recent messages (last 10)
  2. If older messages exist, create summary via LLM call
  3. Insert summary as system message prepended to the result
  4. Adjust token budget accordingly

Characteristics:

  • Pros: Preserves overall context, continuous narrative
  • Cons: Requires LLM call for summarization, slower (~1-2s)
  • Best For: Very long conversations where context is critical
  • Performance: O(n) time + LLM call, O(n) space
  • Requires LLM: Yes (falls back to placeholder when no provider is set)
  • Overhead: ~200 tokens for summary

Example (with LLM):


 LLMProvider provider = LLMProviderFactory.anthropic()
     .apiKey(System.getenv("ANTHROPIC_API_KEY"))
     .build();

 ContextWindowStrategy strategy = new SummarizationStrategy(provider, "claude-3-5-sonnet-20241022");

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

Example (placeholder mode via factory):


 import static dev.agenor.runtime.memory.llm.ContextWindowStrategies.SUMMARIZED;
 List<LLMMessage> selected = SUMMARIZED.selectMessages(allMessages, 2000, estimator);
 

Thread Safety: Instances constructed with SummarizationStrategy() are stateless and thread-safe. Instances constructed with SummarizationStrategy(LLMProvider, String) inherit the thread-safety of the provided LLMProvider.

Since:
0.6.0
See Also:
  • Constructor Details

    • SummarizationStrategy

      public SummarizationStrategy(LLMProvider llmProvider, String model)
      Create a summarization strategy backed by a real LLM.

      Use this constructor when actual summarization is needed. The provider is called synchronously (via CompletableFuture#join) inside selectMessages(java.util.List<dev.agenor.core.llm.LLMMessage>, int, dev.agenor.core.memory.llm.TokenEstimator) because that method is synchronous.

      Parameters:
      llmProvider - provider used to generate the summary; must not be null
      model - model identifier passed to the provider; must not be null
    • SummarizationStrategy

      public SummarizationStrategy()
      Create a summarization strategy in placeholder mode (no LLM call).

      Used by ContextWindowStrategies.SUMMARIZED. Summary text will be a descriptive placeholder string instead of a real LLM-generated summary.

  • Method Details

    • selectMessages

      public List<LLMMessage> selectMessages(List<LLMMessage> allMessages, int maxTokens, TokenEstimator estimator)
      Select messages to fit within the token budget, prepending an LLM-generated summary of older messages when the conversation exceeds 10 messages.

      The returned list always starts with a SYSTEM message containing the summary (real or placeholder), followed by the most recent messages that fit in the remaining token budget.

      If the summary message alone exceeds maxTokens, falls back to FixedWindowStrategy to ensure a non-empty result is always returned.

      Specified by:
      selectMessages in interface ContextWindowStrategy
      Parameters:
      allMessages - complete conversation history; must not be null
      maxTokens - maximum token budget; must be positive
      estimator - token estimator; must not be null
      Returns:
      selected messages fitting within maxTokens, never null
      Throws:
      IllegalArgumentException - if any parameter fails validation
    • 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