Module 7, Lecture 7.2 | Implementing Context Management
Sliding window is correct but blunt: it assumes only recent messages matter, so a constraint stated early — "we deploy with Docker, not systemd" — is silently dropped once the window slides, and the agent proceeds as if it was never said. Selective preservation fixes this by classifying messages and keeping the ones that matter regardless of age, organized around three categories: structural anchors (the first user message), decisions and goals (state-mutating tool calls and user constraints), and recent exchanges. The lecture covers two ways to tag importance — heuristics (the floor: first user message, mutating-tool calls like edit_file/delete_file, detected for free from message structure) and explicit tagging (the ceiling: an important: True flag set by the agent code at checkpoints, or by the model itself through a mark_important tool). The two API constraints from 7.1 still apply and trigger more often, since preservation pulls a non-contiguous subset: find_pair_partner keeps every tool_use with its tool_result, and the reused ensure_alternating_roles pass repairs same-role adjacencies. Selective preservation is more accurate than sliding window but is still a subsetting strategy — important messages accumulate over time and some information is spread across many messages, which is where compaction takes over in the next lecture.
Read the full lecture narrative
tool_use/tool_result pairing that find_pair_partner enforces, and the tool-name lookup behind the mutating-tool heuristic.mark_important tool and importance-based selection.