Task 4 · 8 tasks
Sliding-window conversation
Control how much history the model sees with a sliding-window conversation manager.
The “Overflowing Context” problem
Conversations grow, context windows don't, and every token costs budget. You need to decide what the model remembers. Make the trade-off visible with a tiny window.
Build it
Keep only the last two messages in context
Challenge
Keep a session as in Task 3 but limit the model's context to the last 2 messages.
Work in
phase1/starter/t4_conversation.py(look forTODO; an unfinished one prints[starter] TODO …) and run it withuv run bootcamp.py phase1 t4 --starter. The reference solution isphase1/t4_conversation.py;uv run bootcamp.py phase1 t4runs it.Hint 1
Storage and context are separate concerns in Strands. Context is handled by a conversation manager. See Strands: conversation management.
Hint 2
Import
SlidingWindowConversationManagerfromstrands.agent.conversation_managerand pass it asconversation_manager=.Solution
phase1/t4_conversation.pysession_manager = FileSessionManager(session_id="window_session", storage_dir=str(SESSIONS_DIR)) agent = Agent( ..., session_manager=session_manager, conversation_manager=SlidingWindowConversationManager(window_size=2), )Send three messages in one chat
terminal · your starter fileuv run bootcamp.py phase1 t4 --starterterminal · reference solutionuv run bootcamp.py phase1 t4Then type, one at a time:
My favorite color is blue,My favorite food is pizza,What was my first message?Experiments
- Raise
window_sizeto 6 and repeat. What changes? - A tool call adds messages too (call + result). Ask a database question with
window_size=2: what breaks? - Run
uv run bootcamp.py llmbefore and after a long chat. Smaller windows, smaller bills.
- Raise
Check your work
Phase 1 has no automated test: you check it by running the task and looking for the result below.
By the third message the agent no longer knows your first one (blue), but still knows the second (pizza).
Under the hood
The conversation manager trims agent.messages before every model call, keeping the newest N messages and never splitting a tool call from its result. The session manager still stores everything on disk: storage and context are separate decisions.