Context hygiene: new sessions, checkpoints and git
By the end of this lesson you will have a Status section in PLAN.md that the agent wrote before you cleared its memory, a fresh session that picked up the next step from that file alone, and one bad change undone — with Claude Code's rewind where you have it, and with git, which every agent has.
The idea
Everything in a session — your prompts, the files it read, every command's output, the attempts that failed — sits in one context window, and the agent reasons over all of it on every turn. It fills up. Long before it is full, it is cluttered: an instruction from an hour ago competes with a correction from a minute ago, and a dead end it abandoned is still there to be repeated.
Signs a session has gone stale
- It reintroduces a mistake you already corrected.
- It forgets a rule it followed at the start.
- Its answers grow longer while its changes grow worse.
- It has tried the same fix twice and is about to try it a third time.
/context in Claude Code, or /status in Codex, shows how full the window is. /compact, in both, summarises the conversation so far to free space and keeps going. /clear, in both, starts a new conversation with an empty context; the rules file is there again, the history is not. When in doubt, clear: a new session that reads a good plan beats an old one that remembers a bad afternoon.
When to start fresh
- At every new task. One task, one session.
- After a commit that finished something.
- After two failed attempts at the same fix — a third in the same context usually repeats the first two.
- Before asking for a review, as the previous lesson showed.
Hand the state over in a file
Clearing loses what the session knew. So before you clear, have it write down what matters, in the place the next session will read: PLAN.md, which your rules file already points to. A short Status section — done, in progress, next step, and what was tried and did not work — is the whole hand-over. The last item saves the next session from rediscovering your dead ends.
Two undo buttons
Claude Code takes a checkpoint before every prompt you send. /rewind, or Esc twice on an empty prompt, opens a list of your prompts; pick one and restore the code, the conversation, or both, to that point. It is fast and it is local to the session, with limits worth knowing:
- It tracks edits made by the agent's file tools. A file changed only by a shell command it ran is not restored.
- Edits made by a subagent are generally not restored.
- Your own edits outside the session are not tracked.
- It is not version control.
Codex has no checkpoint of your files. Esc twice on an empty prompt goes back to an earlier message so you can edit it and continue from there, but only the conversation goes back: the files stay as they are. On Codex, git is the only undo button.
Git is the undo that always works and that you control: Start From Zero's git restore, git revert and branches. The habit that makes it safe is the one from Start From Zero — commit what works before asking for the next thing. Then a bad change is one git restore . away, whatever made it.
commit → prompt → change → review
├ good → commit
└ bad → /rewind (Claude Code, this session's edits)
or git restore . (anything, any agent)
Try it now
The prompt
In a session that has done some work — the review from the previous lesson is fine — type /context in Claude Code, or /status in Codex, and look at how full the window is. Then paste:
Before I clear this session, add or update a "## Status" section at the
end of PLAN.md: what is done, what is in progress, the next step, and
anything we tried that did not work (write "nothing" if nothing failed).
Keep it under fifteen lines. Then stop.
Read the section and correct anything wrong. Commit it yourself (git add -A, then git commit -m "plan: status section, so the next session starts from the file"), and type /clear. Then:
Read PLAN.md and tell me the next step, in one sentence. Do not start it.
What the agent should do
- Before the clear: edit one file,
PLAN.md, adding a Status section of four short parts and changing nothing above it. - After the clear: read the plan and name the same next step you would, without touching any file.
Check it yourself
git diff HEAD~1 -- PLAN.mdshows only the Status section added.- After /clear, the same command shows a much smaller conversation than before.
- The fresh session's next step matches the one in the file.
- Ask the agent for a change you will not keep — "rename the Watchlist heading to Board" — and let it edit. In Claude Code, type /rewind, choose that prompt and restore the code:
git statusshows the rename gone (it lists only whatever was already uncommitted before the prompt). In Codex, pressEsctwice and pick that prompt: the conversation goes back,git statusstill shows the change, andgit restore .is what removes it. - Ask it to rename a file with a shell command,
git mv. In Claude Code, try /rewind once more: the rename stays, because a shell command made it and no file tool did. In either agent, git undoes it:git mvthe file back.
Break it on purpose
Keep one session going far past its useful life: three or four unrelated small tasks in a row without clearing, then ask it to repeat the rules it follows. Compare with the answer a fresh session gives. Whatever the long session dropped or blurred is what it was quietly no longer following.
Compare with ours
Our Status section
## Status
Done: My rules in AGENTS.md; sortByMove with tests; page ordered by move.
In progress: nothing.
Next: decide whether ties should break by ticker; ask before changing.
Tried and dropped: sorting inside quotes() — it mixed fetching with
display, and the page is the only place that wants this order.
The commit is on the branch ref/work-with-an-agent/7 of the starter repository.
Checkpoint
Nothing more to commit: you committed the Status section before the clear, and the rename and the git mv from steps 4 and 5 are undone. Check the state:
git log -1 --format=%s # plan: status section, so the next session starts from the file
git status # nothing to commit
Then answer this lesson's questions.