Sub topic 9
Compacting a Session Without Losing Decisions
What this chapter assumes
You have used a coding agent in a terminal or editor for sessions long enough to see the context fill up. You know that the context window is a fixed token budget. You also know how to check usage, which the earlier chapter on measuring covers. This chapter covers what to do once usage is high: compact the session, and check that the summary kept what matters.
What compaction does
Compaction replaces the older part of the conversation with a shorter summary. The agent, or a separate model call, reads the history and writes a condensed version. The session then continues with the summary in place of the original messages.
Compaction reduces size by discarding information, and the summarizer decides what to discard. A summary written without guidance tends to keep the story ("we investigated the loader, then fixed tests") and lose the details you need later: the exact function name, the reason you rejected an option, or the flag that made a test pass.
What is lost, and why it matters
Summaries fail in predictable ways. Knowing them tells you what to ask the summarizer to keep.
- Reasons behind decisions. A summary may say "use
tomllib" and omit that you chose it because the project cannot add dependencies. Later the agent may suggest a third-party library. - Rejected options. If the dead ends are dropped without a note, the agent may retry them.
- Exact identifiers. Paths, function names, error codes, and command lines get paraphrased. A paraphrased path is a wrong path.
- Constraints you stated once. A line like "do not touch the public API" is easy to lose, because it was never part of any tool output.
- Unresolved problems. A failing test that you meant to come back to can disappear if it was not the most recent topic.
- State that only exists in the conversation. Anything uncommitted, such as a half-finished plan, lives only in the history.
The last point matters most. Compaction is safe for facts that are also stored somewhere durable, such as git history, a notes file, or the code. It is risky for facts that exist only in the chat.
When to compact
Compact at a natural boundary, before the context is full. Good moments are:
- A subtask is finished and tests pass.
- You are about to switch to a different part of the codebase.
- Usage has passed a threshold you chose in advance, for example 60 to 70 percent.
- The agent has begun to show degradation symptoms, such as repeating questions or contradicting earlier decisions.
Avoid compacting in the middle of debugging. At that point the history holds the hypotheses you have tested and ruled out, and those are the facts a summary is most likely to blur. Finish the investigation, or write the findings to a file first.
Some tools also compact automatically near the limit. That is convenient, but it happens at a moment you did not choose, with instructions you did not write. If you rely on auto-compaction, keep decisions in durable files so a poor summary costs little.
Guiding the summary
Most agents let you pass instructions when you compact. Many offer a command such as /compact for this, but the name and options differ between tools, so check yours. The useful pattern is to name 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 line does a specific job.
- "Decisions made and the reasons for them" protects the first and second failure modes above. The reasons are what stop the agent from undoing a decision.
- "Files changed, with a one-line description" gives you something to check against
git status. - "Unresolved" failures prevent silent loss of open problems.
- "The next three steps" gives the continued session a direction, and it gives you a quick test of whether the summary understood the task.
- The "Drop" line saves most of the space. Full file contents and raw output are large and can be re-read, so they are the best material to discard.
You can adapt the list to the task. For a debugging session, add "hypotheses already ruled out and the evidence." For a refactor, add "the naming and style rules we agreed on."
Verifying the summary
After compaction, read the summary before you continue. It takes about a minute and is much cheaper than discovering a lost decision an hour later. Check four things:
- Goal and current subtask. Is the goal stated the way you would state it?
- Decisions and reasons. Compare against your own memory, or against the notes file if you keep one.
- Files. Compare the listed files with the real state of the repository.
- Next steps. Do they match what you intended to do next?
You can check the file list directly instead of trusting either memory:
# Compare the summary's file list with what actually changed
git status --short
git diff --statYou can also ask the agent a probe question that depends on a decision, for example: "Which library are we using to parse the settings file, and why?" A correct answer with the reason is good evidence. A vague answer, or one that proposes an option you rejected, means the decision was lost. Restate it in one short message, and then record it in a file so the next compaction cannot remove it.
Keeping decisions outside the summary
The strongest protection is to avoid depending on the summary for important facts. Keep a short progress file in the repository and update it when a decision is made, not at the end of the session:
# PROGRESS.md
## Goal
Migrate the settings loader from INI to TOML without changing the public API.
## Decisions
- Keep `load_settings()` signature unchanged.
- Use the standard library `tomllib` for parsing (read-only).
## Done
- [x] Added TOML parser path in `config.py`
- [x] Updated unit tests for the loader
## Next
- [ ] Update the docs page for configuration
- [ ] Run the full test suite and fix failures
## Open questions
- Should INI files still be accepted with a deprecation warning?With this file in place, compaction only has to carry the recent working state. After compacting, you can tell the agent to re-read PROGRESS.md, which restores the decisions in a few dozen lines. The next chapter covers these files in more detail.
Edge cases and failure modes
- Repeated compaction. Each pass summarizes a summary. Details degrade like a photocopy of a photocopy. After two or three compactions in one session, a fresh session started from a notes file is usually more reliable.
- Compacting too late. If the window is almost full, the summarization step itself has little room to work, and some tools truncate the input. Compact earlier than feels necessary.
- Stale file contents. A summary may remember what a file looked like before your last edit. After compacting, have the agent re-read any file it is about to modify.
- Lost tool state. Environment details such as the active virtual environment, a running server, or the working directory are not always carried over. Check them after compaction.
- Instructions that conflict. If the summary includes a rule that contradicts your persistent project instructions, the agent may follow either one. Persistent instructions are reloaded at the start of the context, so the cleaner fix is to correct the summary.
Compaction compared with other options
| Option | Keeps | Loses | Best when |
|---|---|---|---|
| Compact with instructions | Goal, decisions, recent state | Detail, exact wording | The task continues and the history is mostly relevant |
| Compact with no instructions | Whatever the summarizer picks | Unpredictable | Short tasks where loss is cheap |
| New session plus notes file | Only what is in the file | All conversation | Compacted several times, or switching tasks |
| Subagent for noisy work | Only the result returned | The subagent's working output | A search or test run that would flood the context |
Summary
Compact before the window is full, at a boundary between subtasks. Tell the summarizer what to keep, especially decisions and their reasons. Then verify the goal, decisions, files, and next steps before you continue. Store the facts you cannot afford to lose in a file, so a bad summary is an inconvenience and not a setback.
Exercise
You compact a session with no instructions. Afterward you ask the agent which parsing library you chose, and it proposes a third-party library you rejected earlier because the project cannot add dependencies. Name what went wrong and give two actions that prevent it from happening again.
Show answer
The summary dropped the decision and its reason (the no-new-dependencies constraint), so the agent had no record that the option was rejected. First, restate the decision and the reason in one message now. Second, record it under "Decisions" in a progress file such as PROGRESS.md, and on future compactions pass instructions that say to keep decisions with their reasons. Re-reading the file after compaction restores the constraint.
Exercise
List the four things to check in a summary after compaction, and give one command that helps you check one of them.
Show answer
Check the goal and current subtask, the decisions and their reasons, the list of changed files, and the next steps. For the file list, run git status --short or git diff --stat and compare the output with the files named in the summary.