Context Management¶
In the Strands Agents SDK, context refers to the conversation history that provides the foundation for the agent's understanding and reasoning. This includes:
- User messages
- Agent responses
- Tool usage and results
- System prompts
As conversations grow, managing this context becomes increasingly important for several reasons:
- Token Limits: Language models have fixed context windows (maximum tokens they can process)
- Performance: Larger contexts require more processing time and resources
- Relevance: Older messages may become less relevant to the current conversation
- Coherence: Maintaining logical flow and preserving important information
Conversation Managers¶
The SDK provides a flexible system for context management through the ConversationManager
interface. This allows you to implement different strategies for managing conversation history. There are two key methods to implement:
-
apply_management
: This method is called after each event loop cycle completes to manage the conversation history. It's responsible for applying your management strategy to the messages array, which may have been modified with tool results and assistant responses. The agent runs this method automatically after processing each user input and generating a response. -
reduce_context
: This method is called when the model's context window is exceeded (typically due to token limits). It implements the specific strategy for reducing the window size when necessary. The agent calls this method when it encounters a context window overflow exception, giving your implementation a chance to trim the conversation history before retrying.
For managing conversations, you can either build your own custom solution or leverage one of Strands' provided managers.
NullConversationManager¶
The NullConversationManager
is a simple implementation that does not modify the conversation history. It's useful for:
- Short conversations that won't exceed context limits
- Debugging purposes
- Cases where you want to manage context manually
from strands import Agent
from strands.agent.conversation_manager import NullConversationManager
agent = Agent(
conversation_manager=NullConversationManager()
)
SlidingWindowConversationManager¶
The SlidingWindowConversationManager
implements a sliding window strategy that maintains a fixed number of recent messages. This is the default conversation manager used by the Agent class.
from strands import Agent
from strands.agent.conversation_manager import SlidingWindowConversationManager
# Create a conversation manager with custom window size
conversation_manager = SlidingWindowConversationManager(
window_size=20, # Maximum number of messages to keep
)
agent = Agent(
conversation_manager=conversation_manager
)
Key features of the SlidingWindowConversationManager
:
- Maintains Window Size: Automatically removes messages from the window if the number of messages exceeds the limit.
- Dangling Message Cleanup: Removes incomplete message sequences to maintain valid conversation state.
- Overflow Trimming: In the case of a context window overflow, it will trim the oldest messages from history until the request fits in the models context window.