Task 4 · 8 tasks

Sliding-window conversation

Control how much history the model sees with a sliding-window conversation manager.

15 minEasy
Alice’s ask

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.

You

Build it

  1. 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 for TODO; an unfinished one prints [starter] TODO …) and run it with uv run bootcamp.py phase1 t4 --starter. The reference solution is phase1/t4_conversation.py; uv run bootcamp.py phase1 t4 runs 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 SlidingWindowConversationManager from strands.agent.conversation_manager and pass it as conversation_manager=.

    Solution
    phase1/t4_conversation.py
    session_manager = FileSessionManager(session_id="window_session", storage_dir=str(SESSIONS_DIR))
    agent = Agent(
        ...,
        session_manager=session_manager,
        conversation_manager=SlidingWindowConversationManager(window_size=2),
    )
  2. Send three messages in one chat

    terminal · your starter file
    uv run bootcamp.py phase1 t4 --starter
    terminal · reference solution
    uv run bootcamp.py phase1 t4

    Then type, one at a time: My favorite color is blue, My favorite food is pizza, What was my first message?

  3. Experiments

    • Raise window_size to 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 llm before and after a long chat. Smaller windows, smaller bills.

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.