Sub topic 9
Compacting a session without losing decisions
This chapter assumes you have used a coding agent in a terminal, know basic git, and understand that a model has a limited input size measured in tokens. It covers what compaction does, how to guide it, and how to check the result.
What compaction does
Compaction replaces the older part of a conversation with a shorter summary. The agent keeps working in the same session, but it now reads the summary instead of the full history. You free space and keep continuity.
The cost is that a summary is lossy. The model decides what matters, and it can't know which small detail you will need two hours later. Decisions, constraints, and the reasons behind them are the things most likely to be dropped, because they are short and look unremarkable next to long file listings.
When to compact
Compact at a natural boundary, such as after a subtask is finished and its tests pass. Do this before the window is nearly full. A summary written under pressure, or by a model already struggling with a crowded context, is worse than one written early.
Compact when you see these signs:
- Usage is past roughly half to two thirds of the window (see the earlier chapter on measuring usage).
- The agent repeats questions you already answered.
- The history is mostly exploration you no longer need, such as dead ends and large command output.
Guide the summary
An unguided summary tends to keep the story of what happened. You want the state of the work. Give the agent an instruction that names what to keep and what to drop.
Summarize this session for continuation. Keep:
1. The overall goal and the current subtask.
2. Decisions made and the reasons for them.
3. Files changed, with a one-line description of each change.
4. Failing tests or errors that are still unresolved.
5. The next three steps.
Drop: exploratory dead ends, full file contents, and raw command output.Each item in the "Keep" list targets a specific loss. Reasons for decisions stop the agent from reopening settled questions. File descriptions let it find work without rereading everything. Unresolved errors keep it from claiming success too early.
Verify the summary
Read the summary before you continue. It takes a minute and catches most problems. Check these points:
| Check | What a failure looks like |
|---|---|
| Goal | The summary describes a narrower or older task than the one you are on. |
| Decisions | A choice you made is missing, or its reason is gone. |
| Constraints | A rule such as "do not change the public API" has vanished. |
| Files | A changed file is not listed, or a file is listed that you never touched. |
| Open problems | A failing test is described as fixed. |
| Next steps | The steps are vague, such as "continue working". |
If something is missing, tell the agent to add it, and say it plainly: "Add the decision to keep load_settings() unchanged." Correcting the summary now costs one message. Discovering the gap later can cost an hour of wrong work.
Back up decisions outside the chat
Compaction is safer when the important facts also live in a file. If you keep a short progress file with the goal, decisions, and next steps, you can point the agent back to it after a summary goes wrong. A later chapter covers these files in detail. For now, a habit is enough: before compacting, ask the agent to append any new decisions to the notes file, then compact.
Repeated compaction
Each compaction summarizes a summary. Details fade a little every round, and errors can carry over. After two or three rounds, prefer starting a fresh session from your notes over compacting again.
Exercise
You finished a subtask, usage is at about 60%, and the agent's last reply contradicted a decision from earlier in the session. List two things you would do before and after compacting.
Show answer
Before compacting, give the agent an instruction that says to keep decisions with their reasons, constraints, changed files, unresolved errors, and next steps, and ask it to write new decisions to a notes file. After compacting, read the summary against a checklist (goal, decisions, constraints, files, open problems, next steps), correct anything missing, and rerun the tests yourself to confirm the status.