Skip to content

[FEAT] how SummarizingConversationManager handles ContextWindowOverflowException #981

@yangyaochia

Description

@yangyaochia

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

1.10.0

Python Version

3.10

Operating System

Linux/X86_64

Installation Method

pip

Steps to Reproduce

  1. When agent is using the tool and toolResult encountered context overflow, SummarizingConversationManager will invoke reduce_context to perform summarisation.
  2. However, when context overflow is not from all the messages but from the latest message itself, the single toolResult, SummarizingConversationManager will throw ContextWindowOverflowException with insufficient messages as the logic is assuming the context overflow coming from "all the messages".
    def reduce_context(self, agent: "Agent", e: Optional[Exception] = None, **kwargs: Any) -> 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.
            **kwargs: Additional keyword arguments for future extensibility.

        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")

example agent.message

{
    "role": "user",
    "content": [
        {
            "text": "give me xxxx"
        }
    ]
}
,
  
{
    "role": "assistant",
    "content": [
        {
            "text": "I'll get the xxxx."
        },
        {
            "toolUse": {
                "toolUseId": "tooluse_TWWIsSUfTBWFK5Rh00zSgQ",
                "name": "xxx",
                "input": {
                    ....
                }
            }
        }
    ]
}
,
  {
    "role": "user",
    "content": [
      {
        "toolResult": {
          "status": "success",
          "toolUseId": "tooluse_TWWIsSUfTBWFK5Rh00zSgQ",
          "content": [
            {
              "text": "{.....

Expected Behavior

  • SummarizingConversationManager should be able to handle such case but checking the number of messages

Actual Behavior

  • SummarizingConversationManager found out there is limited number of messages at the moment and then throw the ContextWindowOverflowException

Additional Context

No response

Possible Solution

Instead of message level summarization, is it possible to go deeper to the content level to generate summary from there.
Or is it recommended to have a new custom summarizing_content_manager

# Extract messages to summarize
            messages_to_summarize = agent.messages[:messages_to_summarize_count]
            remaining_messages = agent.messages[messages_to_summarize_count:]

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions