Topic 4 of 5 · 4 min read
Context resets and compaction over long runs
Goal: Decide for a long run when to compact and when to restart from a handoff note, and spot drift from the original goal in a run log.
When the context fills, compact if the goal and recent work are still clear, and restart from a written handoff note if the session has drifted or gotten cluttered. Over hours, a model's own history becomes its biggest hazard.
Check yourself · 1 of 3
What is compaction in a long agent session?
Why it matters
A context window is finite, and more text in it is not always better. Anthropic's context engineering article notes that as the window fills, the model's ability to recall and use earlier details degrades. Long runs also drift. After 200 steps the agent may be optimising the last sub-problem it saw, not the goal you gave it.
How to do it
- Compaction summarises the history and continues in the same session. Use it when the task is one coherent thread and the summary can keep the decisions, open bugs and next step. Claude Code can do this automatically or on request with
/compact, and you can tell it what to keep. - A restart with a handoff note ends the session and starts a clean one that reads a note. Use it at a feature boundary, after a long debugging detour, when the agent repeats itself, or when compaction already ran twice.
- Write the handoff note before the reset. Goal in one sentence, what is done (with commit ids), what is half done, the next step, decisions and why, dead ends already tried. The dead ends stop the new session repeating them.
- Re-anchor on the original goal. Keep the goal text in the instruction file or the top of the progress file so every session starts from your words, not from a summary of a summary.
- Watch for drift in the log. Signs: edits in files unrelated to the task, new features nobody asked for, "improving" tests, a plan that quietly changed scope, or the same failed command repeated.
Before and after
Weak handoff:
Worked on exports. Mostly fine. Carry on.
Better handoff:
Goal: CSV export of invoices for finance (spec: docs/export.md).
Done: F-014 (commit 3fa91c2). Half: F-015 date filter, parser in
src/filters.ts, no tests yet. Next: write tests/filters.test.ts.
Decided: use UTC dates only (finance asked). Dead end: streaming
library X fails on Node 20, do not retry.
What goes wrong
- Compacting through a mess. A summary of a confused session preserves the confusion.
- Summary drift. Each compaction loses detail, and the goal slowly rewrites itself.
- Silent scope creep. The agent adds a caching layer because it seemed useful 90 minutes ago.
- No dead ends recorded. The new session spends an hour on the same failing approach.
Examples
Spotting drift in a run log
09:02 task: add CSV export for invoices
09:40 wrote src/export.ts
10:15 refactor logger to structured output
10:52 add Redis cache for invoice lookups
11:30 rename 40 test files to new convention
The task is the CSV export. Three later entries are unrelated to it. The agent drifted at 10:15 and the review should start there.
Compact or restart
At 14:00 the context is 85 percent full. The agent just finished F-016 cleanly and the next feature is unrelated, so the team restarts from a handoff note. At 16:30 a second agent is deep in one debugging thread with a clear hypothesis; the team compacts and tells it to keep the failing test name and the three theories already ruled out.
Key points
- Compact when the work is one clear thread; restart with a handoff note at feature boundaries or after a messy detour.
- A handoff note holds the goal, what is done with commit ids, what is half done, the next step, decisions and dead ends.
- Recording dead ends stops the next session from repeating a failed approach.
- Keep the original goal text in a file that every session reads, not only in summaries.
- Drift shows in logs as unrelated file edits, unrequested features, repeated failed commands and a changed scope.
Common mistakes
- Compacting repeatedly until the original goal is only a faint paraphrase.
- Restarting without a handoff note, so the next session starts with nothing.
- Leaving out dead ends, so the same failed approach gets tried again.
- Not reading the run log until the end, after hours of drift.
Check yourself · 2 of 3
An agent has finished a feature cleanly and the next feature is unrelated. The context is mostly full. What is the best move?
Check yourself · 3 of 3
A run log for the task "fix the login timeout" shows the agent renaming 40 test files and adding a cache layer. What is this?
Practice items, not counted towards your mastery.
Start here
- Effective harnesses for long-running agents (Anthropic) (opens in a new tab)ArticleFree
Describes the initializer session, feature list, progress file and one-feature-per-session pattern this skill teaches.
- Effective context engineering for AI agents (Anthropic) (opens in a new tab)ArticleFree
Covers compaction, structured note-taking and sub-agent hand-offs for tasks that outgrow the context window.
- Checkpointing (Claude Code docs) (opens in a new tab)DocsFree
Explains how Claude Code snapshots edits, rewinds code and conversation, and what checkpoints do not cover.
More for this topic
- How Claude remembers your project (Claude Code docs) (opens in a new tab)DocsFree
Shows how instruction files are loaded at the start of each session, the simplest persistent memory.