-
Notifications
You must be signed in to change notification settings - Fork 153
feat: implement summarizing conversation manager #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Unshure
merged 1 commit into
strands-agents:main
from
stefanoamorelli:feat/summarizing-conversation-manager
Jun 10, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
src/strands/agent/conversation_manager/summarizing_conversation_manager.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
"""Summarizing conversation history management with configurable options.""" | ||
|
||
import logging | ||
from typing import TYPE_CHECKING, List, Optional | ||
|
||
from ...types.content import Message | ||
from ...types.exceptions import ContextWindowOverflowException | ||
from .conversation_manager import ConversationManager | ||
|
||
if TYPE_CHECKING: | ||
from ..agent import Agent | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
DEFAULT_SUMMARIZATION_PROMPT = """You are a conversation summarizer. Provide a concise summary of the conversation \ | ||
history. | ||
|
||
Format Requirements: | ||
- You MUST create a structured and concise summary in bullet-point format. | ||
- You MUST NOT respond conversationally. | ||
- You MUST NOT address the user directly. | ||
|
||
Task: | ||
Your task is to create a structured summary document: | ||
- It MUST contain bullet points with key topics and questions covered | ||
- It MUST contain bullet points for all significant tools executed and their results | ||
- It MUST contain bullet points for any code or technical information shared | ||
- It MUST contain a section of key insights gained | ||
- It MUST format the summary in the third person | ||
|
||
Example format: | ||
|
||
## Conversation Summary | ||
* Topic 1: Key information | ||
* Topic 2: Key information | ||
* | ||
## Tools Executed | ||
* Tool X: Result Y""" | ||
|
||
|
||
class SummarizingConversationManager(ConversationManager): | ||
"""Implements a summarizing window manager. | ||
|
||
This manager provides a configurable option to summarize older context instead of | ||
simply trimming it, helping preserve important information while staying within | ||
context limits. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
summary_ratio: float = 0.3, | ||
preserve_recent_messages: int = 10, | ||
summarization_agent: Optional["Agent"] = None, | ||
summarization_system_prompt: Optional[str] = None, | ||
): | ||
"""Initialize the summarizing conversation manager. | ||
|
||
Args: | ||
summary_ratio: Ratio of messages to summarize vs keep when context overflow occurs. | ||
Value between 0.1 and 0.8. Defaults to 0.3 (summarize 30% of oldest messages). | ||
preserve_recent_messages: Minimum number of recent messages to always keep. | ||
Defaults to 10 messages. | ||
summarization_agent: Optional agent to use for summarization instead of the parent agent. | ||
If provided, this agent can use tools as part of the summarization process. | ||
summarization_system_prompt: Optional system prompt override for summarization. | ||
If None, uses the default summarization prompt. | ||
""" | ||
if summarization_agent is not None and summarization_system_prompt is not None: | ||
raise ValueError( | ||
"Cannot provide both summarization_agent and summarization_system_prompt. " | ||
"Agents come with their own system prompt." | ||
) | ||
|
||
self.summary_ratio = max(0.1, min(0.8, summary_ratio)) | ||
self.preserve_recent_messages = preserve_recent_messages | ||
self.summarization_agent = summarization_agent | ||
self.summarization_system_prompt = summarization_system_prompt | ||
Unshure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def apply_management(self, agent: "Agent") -> None: | ||
"""Apply management strategy to conversation history. | ||
|
||
For the summarizing conversation manager, no proactive management is performed. | ||
Summarization only occurs when there's a context overflow that triggers reduce_context. | ||
|
||
Args: | ||
agent: The agent whose conversation history will be managed. | ||
The agent's messages list is modified in-place. | ||
""" | ||
# No proactive management - summarization only happens on context overflow | ||
pass | ||
|
||
def reduce_context(self, agent: "Agent", e: Optional[Exception] = None) -> None: | ||
"""Reduce context using summarization. | ||
|
||
Args: | ||
agent: The agent whose conversation history will be reduced. | ||
The agent's messages list is modified in-place. | ||
e: The exception that triggered the context reduction, if any. | ||
|
||
Raises: | ||
ContextWindowOverflowException: If the context cannot be summarized. | ||
""" | ||
try: | ||
# Calculate how many messages to summarize | ||
messages_to_summarize_count = max(1, int(len(agent.messages) * self.summary_ratio)) | ||
|
||
# Ensure we don't summarize recent messages | ||
messages_to_summarize_count = min( | ||
messages_to_summarize_count, len(agent.messages) - self.preserve_recent_messages | ||
) | ||
|
||
if messages_to_summarize_count <= 0: | ||
raise ContextWindowOverflowException("Cannot summarize: insufficient messages for summarization") | ||
|
||
# Adjust split point to avoid breaking ToolUse/ToolResult pairs | ||
messages_to_summarize_count = self._adjust_split_point_for_tool_pairs( | ||
agent.messages, messages_to_summarize_count | ||
) | ||
|
||
if messages_to_summarize_count <= 0: | ||
raise ContextWindowOverflowException("Cannot summarize: insufficient messages for summarization") | ||
|
||
# Extract messages to summarize | ||
messages_to_summarize = agent.messages[:messages_to_summarize_count] | ||
remaining_messages = agent.messages[messages_to_summarize_count:] | ||
|
||
# Generate summary | ||
summary_message = self._generate_summary(messages_to_summarize, agent) | ||
|
||
# Replace the summarized messages with the summary | ||
agent.messages[:] = [summary_message] + remaining_messages | ||
Unshure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
except Exception as summarization_error: | ||
logger.error("Summarization failed: %s", summarization_error) | ||
raise summarization_error from e | ||
|
||
def _generate_summary(self, messages: List[Message], agent: "Agent") -> Message: | ||
"""Generate a summary of the provided messages. | ||
|
||
Args: | ||
messages: The messages to summarize. | ||
agent: The agent instance to use for summarization. | ||
|
||
Returns: | ||
A message containing the conversation summary. | ||
|
||
Raises: | ||
Exception: If summary generation fails. | ||
""" | ||
# Choose which agent to use for summarization | ||
summarization_agent = self.summarization_agent if self.summarization_agent is not None else agent | ||
|
||
# Save original system prompt and messages to restore later | ||
original_system_prompt = summarization_agent.system_prompt | ||
original_messages = summarization_agent.messages.copy() | ||
|
||
try: | ||
# Only override system prompt if no agent was provided during initialization | ||
if self.summarization_agent is None: | ||
# Use custom system prompt if provided, otherwise use default | ||
system_prompt = ( | ||
self.summarization_system_prompt | ||
if self.summarization_system_prompt is not None | ||
else DEFAULT_SUMMARIZATION_PROMPT | ||
) | ||
# Temporarily set the system prompt for summarization | ||
summarization_agent.system_prompt = system_prompt | ||
summarization_agent.messages = messages | ||
|
||
# Use the agent to generate summary with rich content (can use tools if needed) | ||
result = summarization_agent("Please summarize this conversation.") | ||
|
||
return result.message | ||
|
||
finally: | ||
# Restore original agent state | ||
summarization_agent.system_prompt = original_system_prompt | ||
summarization_agent.messages = original_messages | ||
|
||
def _adjust_split_point_for_tool_pairs(self, messages: List[Message], split_point: int) -> int: | ||
Unshure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Adjust the split point to avoid breaking ToolUse/ToolResult pairs. | ||
|
||
Uses the same logic as SlidingWindowConversationManager for consistency. | ||
|
||
Args: | ||
messages: The full list of messages. | ||
split_point: The initially calculated split point. | ||
|
||
Returns: | ||
The adjusted split point that doesn't break ToolUse/ToolResult pairs. | ||
|
||
Raises: | ||
ContextWindowOverflowException: If no valid split point can be found. | ||
""" | ||
if split_point > len(messages): | ||
raise ContextWindowOverflowException("Split point exceeds message array length") | ||
|
||
if split_point == len(messages): | ||
return split_point | ||
|
||
# Find the next valid split_point | ||
while split_point < len(messages): | ||
if ( | ||
# Oldest message cannot be a toolResult because it needs a toolUse preceding it | ||
any("toolResult" in content for content in messages[split_point]["content"]) | ||
or ( | ||
# Oldest message can be a toolUse only if a toolResult immediately follows it. | ||
any("toolUse" in content for content in messages[split_point]["content"]) | ||
and split_point + 1 < len(messages) | ||
and not any("toolResult" in content for content in messages[split_point + 1]["content"]) | ||
) | ||
): | ||
split_point += 1 | ||
else: | ||
break | ||
else: | ||
# If we didn't find a valid split_point, then we throw | ||
raise ContextWindowOverflowException("Unable to trim conversation context!") | ||
|
||
return split_point |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.