$npx -y skills add aws-samples/sample-agent-skills-for-builders --skill strands-context-managerStrands conversation/context manager patterns, including sliding window with summarization. Use when building agents with context management, preventing session pollution, or implementing conversation compaction.
| 1 | # Strands Context Manager (Sliding Window with Summarization) |
| 2 | |
| 3 | Strands Agents SDK conversation manager combining sliding window and summarization strategies. |
| 4 | |
| 5 | ## When to Apply |
| 6 | |
| 7 | Reference this skill when: |
| 8 | - Building Strands agents with long conversations |
| 9 | - Implementing context window management |
| 10 | - Preventing session pollution in multi-turn dialogs |
| 11 | - Combining sliding window with summarization |
| 12 | |
| 13 | ## Critical Pitfalls |
| 14 | |
| 15 | ### 1. Inheritance Pitfall |
| 16 | **DON'T** inherit from `SummarizingConversationManager`. |
| 17 | **DO** inherit from base `ConversationManager`. |
| 18 | |
| 19 | ### 2. Infinite Recursion |
| 20 | Use `_is_summarizing` flag with try/finally to prevent recursion during summarization calls. |
| 21 | |
| 22 | ### 3. Hook Closure Problem (MOST CRITICAL) |
| 23 | Lambda closures capture session_manager references during initialization. Create summarization agent WITHOUT session_manager, hooks, or persistence. |
| 24 | |
| 25 | ## Implementation Pattern |
| 26 | |
| 27 | ```python |
| 28 | class SlidingWindowWithSummarizationManager(ConversationManager): |
| 29 | def __init__(self, window_size=40, summarization_agent=None, summarization_system_prompt=None): |
| 30 | self._is_summarizing = False |
| 31 | self._summarization_agent = summarization_agent # Lazy init when None |
| 32 | |
| 33 | def _get_summarization_agent(self): |
| 34 | # Create clean agent without session_manager |
| 35 | return Agent( |
| 36 | # NO session_manager, NO hooks |
| 37 | ) |
| 38 | ``` |
| 39 | |
| 40 | ## Usage |
| 41 | |
| 42 | ```python |
| 43 | from conversation_manager import SlidingWindowWithSummarizationManager |
| 44 | |
| 45 | manager = SlidingWindowWithSummarizationManager( |
| 46 | window_size=20, |
| 47 | ) |
| 48 | ``` |
| 49 | |
| 50 | ## Critical Test |
| 51 | |
| 52 | ```python |
| 53 | def test_no_session_pollution(): |
| 54 | # Verify internal messages don't persist |
| 55 | # Verify no "Please summarize" in session |
| 56 | ``` |
| 57 | |
| 58 | ## References |
| 59 | |
| 60 | - [Architecture](./references/architecture.md) - Detailed design and testing |