sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] SlidingWindowConversationManager ignores window_size when using tools

Open labeveryday opened this issue 1 year ago • 4 comments

Checks

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

Strands Version

1.1.0

Python Version

3.12.9

Operating System

Linux-5.10.237-230.949.amzn2.x86_64-x86_64-with-glibc2.26

Installation Method

pip

Steps to Reproduce

  1. Create an agent with SlidingWindowConversationManager and a defined window_size
  2. Configure the agent with tools
  3. Make multiple tool calls using agent.tool.
  4. Observe that the message count increases without respecting window_size
from strands import Agent
from strands_tools import calculator
from strands.agent.conversation_manager import SlidingWindowConversationManager


# Create a conversation manager with window size
conversation_manager = SlidingWindowConversationManager(
    window_size=12,
    should_truncate_results=True
)

# Create agent with tools
agent = Agent(
    model="anthropic.claude-3-sonnet-20240229-v1:0",
    tools=[calculator],
    conversation_manager=conversation_manager
)

# Run multiple tool calls
for i in range(20):
    agent.tool.calculator(input=f"{i} * 5")
    print(f"After call {i+1}, message count: {len(agent.messages)}")

Expected Behavior

The message count should be capped at the specified window_size (12 in the example).

# Test script for sliding window without tools works
from strands import Agent
from strands.agent.conversation_manager import SlidingWindowConversationManager
import time

# Create a conversation manager with window size of 12
conversation_manager = SlidingWindowConversationManager(
    window_size=12,  # Small window size to easily observe trimming
)

# Create the agent with no tools
agent = Agent(
    model="anthropic.claude-3-sonnet-20240229-v1:0",
    conversation_manager=conversation_manager,
    name="Echo Agent",
)

# Helper function to print message info
def print_message_info(agent):
    print(f"Message count: {len(agent.messages)}")
    # Print all messages in current history
    for i, msg in enumerate(agent.messages):
        content_preview = msg["content"][0].get("text", "")[:30] + "..." if msg["content"] else "[No text]"
        print(f"Message {i} ({msg['role']}): {content_preview}")
    print("-" * 40)

# Run a loop of 10 simple messages
prompts = [
    f"This is message {i+1}" for i in range(10)
]

print("Initial state:")

print_message_info(agent)

print("\nRunning message loop...\n")

for i, prompt in enumerate(prompts):
    print(f"Sending message #{i+1}: {prompt}")
    result = agent(prompt)
    print(f"Response: {result}")
    print_message_info(agent)
    time.sleep(1)

print("\nTest completed!")

Actual Behavior

The message count keeps increasing without limit (by 4 messages per tool call), ignoring the window_size setting.

❯ python main.py
After call 1, message count: 4
After call 2, message count: 8
After call 3, message count: 12
After call 4, message count: 16
After call 5, message count: 20
After call 6, message count: 24
After call 7, message count: 28
After call 8, message count: 32
After call 9, message count: 36
After call 10, message count: 40
After call 11, message count: 44
After call 12, message count: 48
After call 13, message count: 52
After call 14, message count: 56
After call 15, message count: 60
After call 16, message count: 64
After call 17, message count: 68
After call 18, message count: 72
After call 19, message count: 76
After call 20, message count: 80

Additional Context

The SlidingWindowConversationManager works correctly for regular conversation messages. The issue only appears with direct tool calls through agent.tool interfaces

Possible Solution

Modify the SlidingWindowConversationManager to be aware of tool message groups. Each direct tool call adds 4 related messages (user description, assistant tool use, user result, assistant acknowledgment). The manager should treat these as atomic units when applying window management.

Alternatively, implement a ToolAwareConversationManager as shown in the attached example code.

Related Issues

https://github.com/strands-agents/sdk-python/issues/465

labeveryday avatar Jul 31 '25 01:07 labeveryday

I've noticed that too. After debugging the code, only workaround I found was to add should_truncate_results=False:

conversation_manager = SlidingWindowConversationManager(
        window_size=20, should_truncate_results=False
    )

That avoids the bug.

iker592 avatar Aug 04 '25 09:08 iker592

Hi Both, Thank you for submitting this question. As you mentioned, it works correctly for regular agent calls. This is expected behavior in direct tool calls. If you believe this is an important use case for your needs, please feel free to create a feature request, and our team will evaluate it.

JackYPCOnline avatar Aug 07 '25 19:08 JackYPCOnline

Thanks for flagging! This is expected behavior for direct tool calls (agent.tool.*) — conversation management runs at the end of an event-loop cycle (or via the per_turn hook on BeforeModelCallEvent), neither of which fires for direct invocations, so the sliding window isn't applied to them by design.

If tool-aware management for direct calls would be useful for your workflow, a dedicated feature request is the best home for it. Marking wontfix as a bug since the current behavior is intended.

yonib05 avatar Jun 19 '26 16:06 yonib05

This issue has been automatically closed as it was marked for auto-closure by the team and no additional responses was received within 3 days.

github-actions[bot] avatar Jun 23 '26 15:06 github-actions[bot]