Sub topic 14
Putting it together: a routine for long sessions
This chapter combines the earlier techniques into one routine. It assumes you have used a coding agent in a terminal, know basic git, and know that a model reads a limited number of tokens at a time. You have already seen each technique on its own. Here you decide which one to use at each point in a multi-hour task.
The routine in one view
The routine has four phases: start, work, checkpoint, and decide. You repeat the work, checkpoint, and decide phases until the task is done.
Phase 1: Start with a small, stable base
Before the first prompt, set up what every later step depends on.
- Scope the task. Split the work into subtasks that each fit in one session, such as "add the TOML parser path" and "update the docs."
- Keep persistent instructions short. Put build commands and conventions in the project instruction file, and leave out anything that changes often. A stable prefix also helps prompt caching.
- Write a plan file. Create
PROGRESS.mdwith the goal, decisions, done items, next steps, and open questions.
Phase 2: Work with bounded reads and output
While working, keep each step cheap.
- Search first, then read a line range, not the whole file.
- Cap command output. Use
tail,head, andpytest -x -qso a log cannot flood the context. - Send noisy investigations, such as scanning many files for a pattern, to a subagent. Only its short answer returns to your main session.
# Locate the definition, then read only the lines around it
grep -rn "def parse_config" src | head -n 20
sed -n '120,180p' src/config.py
# Keep only the failure summary of a long test run
pytest -x -q 2>&1 | tail -n 30Phase 3: Checkpoint at natural boundaries
Checkpoint when a subtask finishes, before a risky change, and before any compaction. A checkpoint has two parts: update PROGRESS.md and make a git commit. The notes record why, and the commit records what.
Phase 4: Decide using usage and symptoms
After each checkpoint, check your context usage with your tool's usage command. Then look for symptoms: the agent repeats a question you already answered, forgets a decision, re-reads files it just read, or contradicts earlier work. Use the table to choose.
| Situation | Action |
|---|---|
| Usage is low and the agent behaves well | Keep going |
| Usage is moderate and the current subtask continues | Compact with a guided instruction |
| A subtask is finished and the next one is unrelated | Start a fresh session |
| Symptoms appear even after compaction | Start a fresh session from notes |
For compaction, 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.After compacting, verify the summary. Compare it with PROGRESS.md and check that each recorded decision is still present. If one is missing, restate it in the chat and add it to the notes.
Starting fresh
A fresh session begins with a short prompt such as: "Read PROGRESS.md and the diff of the last commit, then continue with the first unchecked item." The agent then loads about one small file and one small diff instead of the whole history.
Common errors
- Waiting until the window is nearly full. Act while there is still room to write a good summary.
- Compacting without checking the result, which silently loses decisions.
- Letting
PROGRESS.mdgo stale, so a fresh session resumes from wrong information. - Pasting whole files or full logs "just in case."
Exercise
Your agent has finished the parser change and is at moderate context usage. The next item is the unrelated docs update. It has not shown any symptoms. What do you do, and in what order?
Show answer
First update PROGRESS.md (mark the parser change done, note decisions) and commit. Then start a fresh session, because the subtask is finished and the next one is unrelated. Open it with a prompt telling the agent to read PROGRESS.md and continue with the first unchecked item. Compaction is not needed because there is no ongoing subtask to preserve.
Exercise
After compaction, the agent proposes keeping INI support silently, even though you decided earlier to deprecate it with a warning. What went wrong, and how do you fix it?
Show answer
The summary dropped a decision. Restate the decision in the chat, add it to the Decisions section of PROGRESS.md, and next time use a compaction instruction that explicitly says to keep decisions and their reasons. If the agent keeps drifting, start a fresh session from the notes.