Task 4 · 8 tasks
Summarizing conversation
Keep long chats small and affordable: older turns become a running summary instead of being dropped.
The “Overflowing Context” problem
Conversations grow, context windows don't, and every token costs budget. Simply dropping old messages makes the assistant forget what Alice told it. Instead, fold the older turns into a short summary and keep only the latest ones verbatim.
Build it
Summarize older turns, keep the last two verbatim
Challenge
Keep a session as in Task 3, but when the context gets full, replace the oldest messages with a summary and keep the 2 most recent messages as they are, and make sure the summary keeps what the user told the agent. The starter already gives the model a deliberately tiny 2,700-token context window, so you see it happen within a few 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. Strands: conversation management.
Hint 2
Import
SummarizingConversationManagerfromstrands.agent.conversation_manager. Keep 2 recent messages (preserve_recent_messages=2), summarize most of the rest in one go (summary_ratio=0.8, the maximum), and turn onproactive_compression={"compression_threshold": 0.7}so it summarizes once the context is 70% full instead of waiting for an overflow error.Hint 3
Strands' default summary prompt lists topics and tool results and can drop personal facts. Pass your own with
summarization_system_prompt=, telling it to keep every fact the user stated about themselves.Solution
Paste each block below into
phase1/starter/t4_conversation.pyin place of the definition with the same name (the TODO), including any import line that comes with it. Nothing else in the file changes.phase1/starter/t4_conversation.py · replace SUMMARY_PROMPT, build_conversation_manager() (keep its import line)from strands.agent.conversation_manager import SummarizingConversationManager SUMMARY_PROMPT = """Summarize the conversation so far as short bullet points in the third person. Keep every fact the user stated about themselves (name, role, preferences such as a favorite color). Give each tool result one short line. Do not address the user.""" """Strands' default summary lists topics and tool results and may drop personal facts; this one keeps them.""" def build_conversation_manager() -> SummarizingConversationManager: """Summarize most (80%) of the older messages once the context is 70% full; keep the 2 most recent verbatim.""" return SummarizingConversationManager( summary_ratio=0.8, preserve_recent_messages=2, summarization_system_prompt=SUMMARY_PROMPT, proactive_compression={"compression_threshold": 0.7}, )Chat until it summarizes
terminal · your starter fileuv run bootcamp.py phase1 t4 --starterterminal · reference solutionuv run bootcamp.py phase1 t4Start from a fresh session: add a new session name, e.g.
uv run bootcamp.py phase1 t4 --starter --session colors1(next trycolors2), then type, one at a time:My favorite color is blue,List every department with its budget and number of active employees,My favorite food is pizza,What is my favorite color?The system prompt and the tool description alone take 1,000-1,300 tokens (it depends on the model's tokenizer), so the 1,890-token threshold is crossed only once the department table and its tool call are in the history. Then, before the next model call, the manager asks the model to summarize the oldest messages and prints
[context] prompt ~1,900 of 2,700 tokens: older messages summarized (N so far). Each later summary includes the previous one.The two numbers work together: a summary has to bring the prompt well under the threshold. With a low threshold and a small ratio, the prompt after a summary is still over the line, so the manager summarizes again before every model call.
Experiments
- Open
phase1/sessions/session_summary_session/(session_summary_session_starter/for your starter file): the session on disk still has every message, only the model's context was summarized. - Swap in
SlidingWindowConversationManager(window_size=2)and repeat in a fresh session (a new--sessionname). Now the[context]line says dropped and the agent forgets blue: a window drops old turns, a summary keeps their gist. (A warning that it found no valid trim point during the database question is expected: a window never splits a tool call from its result.) - Set
summary_ratio=0.5andcompression_thresholdto 0.5 and repeat in a fresh session: the[context]line now appears before nearly every model call, each one an extra summarization call you pay for. - Remove
summarization_system_promptto get Strands' default summary and repeat. Read the summary in the session'sagent.json: is your favorite color still in it? - Raise
CONTEXT_WINDOW_TOKENSto 200,000 (realistic). No[context]line in a short chat: summarization costs an extra model call, so it only runs when it pays off. - Run
uv run bootcamp.py llmbefore and after a long chat to see what the extra summary calls cost.
- Open
Check your work
Phase 1 has no automated test: you check it by running the task and looking for the result below.
After the department question you see at least one [context] ... older messages summarized line (none before it), and “What is my favorite color?” still gets blue: the agent reads it from the summary, not from the original message. No [context] line at all? Start from a fresh session and check CONTEXT_WINDOW_TOKENS is 2,700.
Failed? Run the same prompt on the reference (drop --starter). If the reference fails too, it's the model: rerun or try another MODEL_ID (uv run bootcamp.py llm models).
Under the hood
Before each model call the manager estimates the prompt size: the previous call's real token count (system prompt and tool descriptions included) plus an estimate for the messages added since. Past the threshold (compression_threshold × context_window_limit) it sends the oldest messages to the model with a summarization prompt and swaps them for one summary message, never splitting a tool call from its result. The session manager still stores everything on disk: storage and context are separate decisions. In Phase 2, AgentCore Memory's summary strategy does the same across sessions, on the server.