Class SummarizationStrategy
- All Implemented Interfaces:
ContextWindowStrategy
Algorithm:
- Include most recent messages (last 10)
- If older messages exist, create summary via LLM call
- Insert summary as system message prepended to the result
- 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 Summary
ConstructorsConstructorDescriptionCreate a summarization strategy in placeholder mode (no LLM call).SummarizationStrategy(LLMProvider llmProvider, String model) Create a summarization strategy backed by a real LLM. -
Method Summary
Modifier and TypeMethodDescriptiongetName()Get strategy name for logging and debugging.intGet estimated overhead tokens for this strategy.booleanCheck if this strategy requires an LLM provider.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.
-
Constructor Details
-
SummarizationStrategy
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) insideselectMessages(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 benullmodel- model identifier passed to the provider; must not benull
-
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
SYSTEMmessage 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 toFixedWindowStrategyto ensure a non-empty result is always returned.- Specified by:
selectMessagesin interfaceContextWindowStrategy- Parameters:
allMessages- complete conversation history; must not benullmaxTokens- maximum token budget; must be positiveestimator- token estimator; must not benull- Returns:
- selected messages fitting within
maxTokens, nevernull - Throws:
IllegalArgumentException- if any parameter fails validation
-
getName
Description copied from interface:ContextWindowStrategyGet strategy name for logging and debugging.Examples:
- "fixed" - Fixed window strategy
- "sliding" - Sliding window strategy
- "summarized" - Summarization strategy
- "semantic" - Semantic relevance strategy
- Specified by:
getNamein interfaceContextWindowStrategy- Returns:
- strategy name (lowercase, no spaces)
-
requiresLLM
public boolean requiresLLM()Description copied from interface:ContextWindowStrategyCheck 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:
requiresLLMin interfaceContextWindowStrategy- Returns:
- true if strategy needs LLM access
-
getOverheadTokens
public int getOverheadTokens()Description copied from interface:ContextWindowStrategyGet 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:
getOverheadTokensin interfaceContextWindowStrategy- Returns:
- estimated overhead in tokens
-