CertPrep
Domain 6 · Lesson 6.1 · 4 min read

Keeping Long Sessions on Track

Exam skillContext Management for Long-Running Sessions

Analyzing context bloat or drift in a long-running session or agent and selecting the management technique that restores intended behavior, such as compaction, tool-output pruning, subagent isolation, or criteria for starting a fresh session.

Recognize context bloat and drift in long sessions and agents, and restore behavior with the right technique (pruning stale tool output, compaction with guidance on what to keep, subagents for bulky side work, persistent notes for decisions) or by starting fresh with a short summary when the task changes.

What you need to know

Context is a budget. As it fills with old tool output and abandoned ideas, the instructions and decisions that matter carry less weight, and behavior drifts well before the window is full.

Signs

  • Re-reading files or re-running searches already done.
  • Contradicting a constraint set early in the session.
  • Following a pattern that was rejected an hour ago.
  • Slower replies and rising cost per turn.

/context in Claude Code (or token counts in your own harness) shows what fills the window.

Techniques

TechniqueWhat it doesUse when
Tool-output pruning (context editing)Removes old tool results, keeps the conversationStale logs, dumps or file contents crowd the context
CompactionReplaces history with a summaryContinuing the same long task; tell it what to keep
Subagent isolationBulky side work happens in a separate context; only findings returnResearch, large reviews, reading many pages
Persistent notesDecisions, constraints and progress live in a file or the system promptAnything that must survive compaction or sessions
Response shapingTools return only relevant rows and fields, with pagingPrevent bloat at the source
Fresh sessionStart clean, carrying a short summary if neededThe task changes, or accumulated context misleads

Choosing between compact and clear

  • Same task, still needed: compact (with "keep the approved pattern and the failing tests").
  • New, unrelated task: clear or start a new session.
  • Drift from conflicting material: compact with instructions to drop the rejected option, and record the decision where it persists.

Re-fetch, do not guess

When old tool results are cleared, the agent can call the tool again. Design for that (or keep key values in state); never let it guess a cleared value.

In the exam

Items describe a long-running session or agent that has drifted or slowed, and ask which technique restores intended behavior or when to start fresh. Match the cause to the technique. Distractors raise the window or max_tokens, repeat instructions in every message, compact constantly, or never compact at all.

Check your understanding

Question 1

A debugging agent's context is 85% stale stack traces; its hypothesis still matters. What first?

Show answer

Answer: A. Remove what is stale and keep the reasoning state.

Question 2

After compaction, an agent forgets a naming convention the user gave early. Fix?

Show answer

Answer: C. Compaction can drop early details; standing rules need a persistent home.

Question 3

A feature is done and an unrelated bug fix is next. How to begin?

Show answer

Answer: D. Unrelated history only adds noise to the new task.

Apply it: Keep Bluestem's underwriting agent on track

Exercise · about 20 minCase: Bluestem Lending

Bluestem's underwriting agent reads up to 40 documents per application. By the end, its context holds every document's full text and every credit-bureau response; memos start missing a rule the underwriter gave at the start ("exclude intercompany transfers from revenue"), and it sometimes re-reads documents.

  1. Redesign the reading step so bulk stays out of the main context.
  2. Protect the underwriter's rule.
  3. Decide what to prune and when.
    Done when: the main context holds decisions and findings, not raw documents.
Check your work
Show model answer

1. A reading subagent per document type (bank statements, tax returns, invoices) with read-only tools, each returning structured findings (period, revenue, flags) instead of text.

2. Store underwriter instructions in a notes file that the harness injects into the system prompt for that application; record them as "rules for this application" so compaction cannot drop them.

3. Clear credit-bureau responses once their key figures are in the findings; compact at about 60% context use with "keep rules, findings and open questions"; the agent can re-run a lookup if it needs a cleared value.

Key termsCompaction

Sources