Implementing Selective Preservation

The sliding window from the previous lecture is correct, but blunt. It rests on a single assumption: that the only messages worth keeping are the recent ones, plus the first message as an anchor. For many conversations that assumption is wrong, and the failure is severe.

Consider a coding-agent session. Early on — at message three, say — the user states a constraint: "we deploy with Docker, not systemd." The agent works for another twenty or thirty messages. Then the token budget crosses its threshold and the sliding window prunes the conversation back to its recent tail. The Docker constraint is gone, and it is irretrievable — nothing in the surviving messages records that the user ruled out systemd. The agent, asked to write deployment scripts, generates a systemd unit file. The whole point of context management is to let a user keep a long-lived conversation going indefinitely without exhausting the window; sliding window achieves that bound but throws away decisions that govern the rest of the session.

Selective preservation addresses this by classifying messages and keeping the ones that matter regardless of age. Where sliding window says "recent messages are important," selective preservation says "a message is important based on its characteristics — and recency is only one of them."

What "Important" Means: Three Categories

Most of what a context manager should preserve falls into three categories.

  1. Structural anchors. Messages that frame the whole session — above all, the first user message, which is the original task. This is the anchor from Lecture 7.1.
  2. Decisions and goals. Messages where the user committed to a constraint ("use psycopg2's pool, not SQLAlchemy") or where the agent took an action that changed the state of the world (an edit_file that modified code, an approved destructive operation).
  3. Recent exchanges. The last few messages, regardless of content, for conversational continuity.

Sliding window already handles category 3 — its entire mechanism is "keep the recent tail." Selective preservation adds categories 1 and 2.

Selective Preservation Is Not a Complete Solution

There is an important caveat to state up front. Sliding window, however lossy, always bounds the context: keep the last N and the size is capped, forever. Selective preservation does not have this property. Over a long enough session, "important" messages accumulate — a session can produce dozens of decisions and constraints — and if you keep all of them, the preserved set eventually fills the window on its own. At that point you are back where you started.

There are two responses. One is to cap the number of preserved important messages, dropping the oldest important ones first — which starts to feel like a sliding window over important messages. The other is to reach for compaction, a strategy that synthesizes rather than selects. Selective preservation works well when a session has a few dozen decisions scattered across hundreds of messages; it does not scale to sessions with hundreds of decisions. It is part of a strategy, not the whole strategy.

Heuristic Tagging: The Floor

How do you decide a message belongs to category 2? The cheapest approach is heuristic tagging — classifying a message from its structure alone, with no extra metadata. Three rules cover most cases.

The first user message is always preserved — the anchor rule, carried over from 7.1.

Assistant messages that called a mutating tool are preserved. A mutating tool is one that changes the state of the world: edit_file, delete_file, mkdir, run_command, write_file. Read-only calls like read_file and list_files are deliberately not preserved by this rule, because their results can be re-fetched cheaply if the agent needs them again — and keeping a read_file result in context is exactly the token cost that the progressive-disclosure tool design from Lecture 4.3 was built to avoid. This is the strongest heuristic signal: the state of the world changed at that message, and the agent will need to know it changed. The crucial enabling fact is that you wrote the tools, so you know which ones mutate — the classifier just looks up the requested tool name in a set you maintain:

MUTATING_TOOLS = {"edit_file", "delete_file", "mkdir", "run_command", "write_file"}

def is_mutating_assistant(msg):
    """Assistant message that called a state-changing tool."""
    if msg["role"] != "assistant":
        return False
    content = msg.get("content")
    if not isinstance(content, list):
        return False
    for block in content:
        if isinstance(block, dict) and block.get("type") == "tool_use":
            if block.get("name") in MUTATING_TOOLS:
                return True
    return False

User messages that state constraints are the hardest case, because intent is not visible in structure. Pattern-matching on authoritative words — "we use," "we deploy," "always," "never," "must" — catches some of them. (A practical aside for working with agents: when you state a constraint in strong, explicit language, you make it more likely to be preserved by exactly this kind of heuristic. The first message and clearly-phrased constraints are the ones that survive.)

But heuristics will miss things. A message like "the new release uses Python 3.12" matches no keyword pattern, yet it is a real constraint. A requirement buried in the middle of a long sentence slips through. Heuristics are a floor, not a ceiling: deterministic, cheap in both compute and dollars, good enough to catch obvious decisions — and not sufficient on their own. They can also over-preserve: an agent might call a mutating tool, then realize the action was a mistake, and the heuristic keeps it anyway.

Explicit Tagging: The Ceiling

The complement to heuristics is explicit tagging — marking a message as important at the moment it is created, by adding a flag to the message dictionary:

messages.append({
    "role": "user",
    "content": "We deploy with Docker, not systemd.",
    "important": True,
})

The Anthropic API ignores extra keys, so the flag is invisible to the model — it exists purely for the truncation function. Every message carries its own tag, the API contract is not broken, and when the context manager later curates the conversation it simply drops anything not marked important. There are two ways the flag gets set.

From the agent code, at known checkpoints. After a destructive operation succeeds, after the user confirms a plan, or after compaction completes (so the summary itself survives future truncations), the code sets important: True on the relevant message.

From the agent itself, through a tool. This is the more interesting option. Give the agent a mark_important tool whose description tells it to flag any message — the user's or its own — that matters for the rest of the conversation. When the model calls it, your code sets the flag on the most recent message. This is powerful precisely because the model is reasoning about importance with the full conversation in front of it — it is, by definition, the best-positioned classifier available, and it is the same model already running the session. The cost is real but modest: the tool description adds tokens to the system prompt, and each invocation is an extra round trip through the inner loop. For long-running sessions, that cost is easily worth it.

Heuristics are the floor (automatic, free, coarse); the model-driven tool is the ceiling (precise, intelligent, slightly expensive). A practical agent combines them: heuristics catch mutating tool calls and the anchor; explicit tags handle constraints the code recognizes; the mark_important tool is reserved for sessions long enough to justify the overhead.

The Same Two API Constraints

Everything from Lecture 7.1 about the API contract still applies — pair integrity and alternating roles — but selective preservation triggers both more often, because it pulls a non-contiguous subset out of the conversation rather than a clean tail.

Pair integrity must now be enforced explicitly. If the keep set includes an assistant tool_use but not its matching tool_result (or vice versa), the request is invalid — the same orphaning problem a naive slice caused. The find_pair_partner helper makes the rule concrete: for any kept message that is half of a pair, find and keep its partner. An assistant tool_use pulls in the following user tool_result; a tool_result pulls in the preceding tool_use.

for i in list(keep):
    partner = find_pair_partner(messages, i)
    if partner is not None:
        keep.add(partner)

Alternating roles is now the common case rather than an edge case. Sliding window kept a contiguous tail and only risked a role collision at the anchor join; selective preservation routinely places two same-role messages next to each other — for example, the anchor (user) immediately followed by an explicitly-tagged constraint (user), with the assistant turn between them dropped. The fix is the same ensure_alternating_roles pass from 7.1, inserting a brief synthetic acknowledgment of the opposite role between any adjacent same-role messages.

Both helpers are imported from the 7.1 work, not reimplemented. The two constraints are properties of the messages array itself, not of any one strategy, so the infrastructure is shared; only the selection logic differs. (Lab 5 pulls find_pair_partner and ensure_alternating_roles into a single context_management.py module that every strategy imports.)

Putting It Together

The main function builds a set of indices to keep — recent N, then anchors, mutating decisions, and explicit tags — then expands the set to include every pair partner, and finally runs the alternating-roles repair:

def selective_preserve(messages, keep_last_n=4):
    keep = set()

    # Recent N messages.
    start_recent = max(0, len(messages) - keep_last_n)
    for i in range(start_recent, len(messages)):
        keep.add(i)

    # Anchors, mutating decisions, explicit tags.
    for i, msg in enumerate(messages):
        if is_anchor(msg, i) or is_mutating_assistant(msg) or is_explicitly_tagged(msg):
            keep.add(i)

    # Pair integrity: every kept tool_use keeps its tool_result and vice versa.
    for i in list(keep):
        partner = find_pair_partner(messages, i)
        if partner is not None:
            keep.add(partner)

    selected = [messages[i] for i in sorted(keep)]
    return ensure_alternating_roles(selected)

The standalone demo in selective_preservation.py runs this on the same base conversation as the 7.1 sliding-window demo, with two user constraints explicitly tagged (a psycopg2 requirement at message 3 and an exception-safety requirement at message 10). Running it on the 14-message conversation with keep_last_n=4 produces a 10-message result:

  [orig  0] user      | 'Refactor the database layer to use connection pooling.'
  [ack   ] assistant | 'Understood.'
  [orig  3] user      | "Use psycopg2's pool — we don't want SQLAlchemy here." *important*
  [orig  6] assistant | text('Adding the pool now.') + tool_use(edit_file, id=tu_003)
  [orig  7] user      | tool_result(id=tu_003)
  [ack   ] assistant | 'Understood.'
  [orig 10] user      | 'Make sure the pool is checked back in even on exception.' *important*
  [orig 11] assistant | tool_use(edit_file, id=tu_004)
  [orig 12] user      | tool_result(id=tu_004)
  [orig 13] assistant | 'Updated models/user.py to check connections back into the pool.'

The kept set is the anchor [0], the two explicit tags [3] and [10], the two edit_file decisions [6] and [11] each with its tool_result pulled in by pair integrity ([7], [12]), and the recent message [13]. Two synthetic acks restore role alternation at the joins. The contrast is the lesson: a sliding window of size 4 would have started at index 10, losing both the original task description and the psycopg2 constraint at message 3 — precisely the failure that motivated the strategy.

The Limits of Selection

Both sliding window and selective preservation are subsetting strategies: they keep some messages and drop others, but they never combine, summarize, or rephrase. That is fine when a session has a handful of important moments scattered through a long stretch of routine work. It breaks down in three situations:

When subsetting fails, the answer is synthesis: have the model read the conversation and write a single new message that captures what matters. Unlike selection, compaction produces wholly new text rather than choosing among existing messages. That is the subject of the next lecture.

Takeaways

Selective preservation keeps what matters regardless of age, organized around three categories: structural anchors, decisions and goals, and recent exchanges. Heuristics are the floor — the first user message and mutating tool calls, detected for free from message structure. Explicit tagging is the ceiling — an important: True flag set by the agent code at checkpoints or by the model itself through a mark_important tool, precise but requiring foresight or a round trip. The two API constraints from 7.1 still hold, and trigger more often: find_pair_partner enforces pair integrity over a non-contiguous selection, and the reused ensure_alternating_roles pass repairs the frequent same-role adjacencies. And selection has a ceiling of its own — when important content grows too large or depends on dropped context, the strategy gives way to compaction.