← Back to Insights

Insight

Rules That Run, Part 7: Leave a Note Before You Forget

Ariel Agor
Updated
The cover illustration with subtle ambient motion, for "Rules That Run, Part 7: Leave a Note Before You Forget".

Listen · Read by Ariel

0:00 / n/a

Rules That Run is a ten-part series on the rules that keep an AI agent setup honest. This is part 7.

Every long Claude Code session hits a wall. The context fills, and the harness compacts it: it asks a summariser to boil the conversation down and carries on from the summary. That works more often than you would expect. When it fails, the failure is quiet. A decision made early in the session is gone, or a constraint I stated once is no longer in view, and the agent carries on confidently without it.

The fix is a written handoff, kept in the repo, refreshed before compaction and read after it. As a line in an instruction file, that rule depends on the agent noticing when to follow it. Wired into hooks, it runs on token counts instead, with one awkward limit I had to design around.

The settings

In my setup, auto-compaction is set at 350,000 tokens for every model. Past about 300K, the standing preference is to stop and start a fresh session with a handoff file, instead of riding into compaction.

The reason is cost as much as memory. As part 2 showed, every turn re-reads the whole context, so a session sitting at several hundred thousand tokens pays for all of them on every step. A fresh session that starts from a good handoff is cheaper per turn and starts from a summary I chose instead of one the summariser chose.

The awkward limit

The obvious design is a PreCompact hook that tells the agent to write its notes before the squeeze. Claude Code does run a hook at that moment. The catch is what the hook can do there. It cannot talk to the model, and it cannot shape what the summariser writes. It can block the compaction, and that is all.

Blocking is dangerous at exactly that point. The session is near its hard limit, which is why compaction fired in the first place. Refusing to compact there risks the session failing outright. So the hook that runs at the right time has the wrong powers, and the agent has to write its notes earlier, while it still has room to do it well.

That forced a design with three layers.

The first layer is a nudge

From 80% of the expected compaction point, a hook injects a request into the agent's context: refresh the repo's HANDOFF.md. If the session keeps going, the request repeats roughly every 50K tokens.

This is the layer that does most of the work. It catches the agent while there is still room to write a careful handoff, and it repeats because one reminder in a busy session is easy to skip.

The word "expected" matters. The compaction point is not fixed. On this machine I have seen it land around 240K in some sessions and around 490K in others, depending on settings. A nudge pinned to a single number would fire far too early in one case and too late in the other. So the hook learns the point from the session's last compaction and scales the 80% threshold from there.

// ~/.claude/hooks/handoff-nudge.mjs (sketch)
const expected = lastCompactionPoint(session) ?? configuredCompactLimit();
const used = currentContextTokens(session);
if (used >= expected * 0.8 && used - lastNudgeAt(session) >= NUDGE_EVERY) {
  recordNudge(session, used);
  console.log(
    "Context is nearing compaction. Refresh HANDOFF.md in the repo root now: " +
    "done, next, decisions, invariants, unverified claims."
  );
}

The second layer is a mechanical snapshot

The PreCompact hook still has a job. It cannot talk to the model, but it can read files and write them. So at the moment of compaction it writes a snapshot with no model involved:

  • the working folder
  • git state: branch, uncommitted files, unpushed commits
  • how old HANDOFF.md is
  • my last messages, verbatim
  • files the session touched
  • the agent's last words before compaction

None of that needs judgment, which is the point. If the agent ignored every nudge, this still captures enough raw state to rebuild from. My last messages go in verbatim. A summary is most likely to paraphrase them wrong, and they are what the agent most needs to get right.

The third layer runs on resume

After compaction the session continues from the summary. A hook that runs on resume checks the handoff's age. Unless HANDOFF.md is under 30 minutes old, the agent's required first step is to refresh it, using the snapshot and whatever survived in the summary.

This catches the case where layer one failed. A stale handoff gets rebuilt immediately, while the snapshot is still fresh, instead of being trusted as current.

What goes in the handoff

The file has fixed sections. A free-form note invites a progress diary: what happened, in order. Named sections force answers to specific questions, and an empty section is visible at a glance.

# HANDOFF

## Done
- What was finished, with the evidence that it works.

## Next
- The next concrete step, specific enough to start without asking.

## Decisions
- What was decided, why, and whether it is live yet.

## Invariants
- Things that must stay true. Constraints the owner stated.

## Unverified claims
- Claim: <what is believed>
  Check: <the exact command that would prove or disprove it>

Two of these sections earn their place more than the others.

Decisions with why and whether live. A decision without its reason gets relitigated by the next session, which sees only the outcome and may reasonably undo it. "Whether live" separates what is decided from what is deployed. Those drift apart in long sessions, and a fresh session cannot tell them apart from the code alone.

Unverified claims with the exact command. This is part 5 carried across the session boundary. Anything the agent believes but has not shown goes in this section, next to the command that would settle it. The next session can run the command instead of inheriting the belief. Without that section, an unverified claim in a summary reads exactly like a verified one.

Rotating instead of compacting

All three layers exist to make compaction survivable. The better path is to avoid it. A /rotate command writes the handoff and starts a fresh session that opens by reading it.

Rotation trades the summariser's judgment for the agent's. I prefer the agent's, because I can tell it what a handoff must contain and check that it did. I cannot do either with the summary. A rotated session also starts small, which brings the per-turn cost back down.

The handoff file lives in the repo root, not in a hidden harness folder. That way it shows up in git status, and any session can read it. A person can too. Someone picking up the project later gets the current state in one short file instead of a transcript.

Why the hooks do the counting

A rule that says "in long sessions, write a handoff" asks the agent to judge when a session is long. The agent has no direct sense of its own context size; a hook can read the token count from the session and act on a number. That is the whole reason the nudge lives in a hook and not in the instruction file.

The repeat interval follows from the same reasoning. A single reminder lands in the middle of other work, and the work in progress tends to win. Repeating it every 50K tokens is crude. It also means the reminder keeps arriving until the handoff is fresh, and the snapshot and the resume check exist for the sessions where even that is not enough.

Build it yourself

  1. Set an explicit auto-compaction limit for every model you use, and pick a lower soft line where you rotate instead.
  2. Add a hook that, from 80% of the expected compaction point, asks the agent to refresh HANDOFF.md, and repeats at a fixed token interval.
  3. Learn the expected point from the session's last compaction instead of hard-coding it.
  4. In PreCompact, write a mechanical snapshot only: folder, git state, handoff age, the owner's last messages verbatim, files touched, the agent's last words. Do not block.
  5. On resume after compaction, make refreshing a stale handoff the first step.
  6. Give the handoff fixed sections, including unverified claims with the command that settles each one.
  7. Add a rotate command that writes the handoff and opens a fresh session on it.

Part 6: The Promise at the End of the Reply

Want this kind of automation working for your business?

Agor AI designs and ships the systems these posts describe, scoped in weeks, not quarters.

Book a Free Strategy Call