Context in bands: 50, 70 and 85% of the compaction point
The biggest enemy of a long session is repeated compaction: the goal survives, but what is done and what remains get diluted. The hook-longrun measures the context and acts before the limit, in three bands. In this lesson you turn on the hook and trigger all three bands in a rehearsal, without spending a token.
1Where the bands sit: ~484, ~677 and ~822 thousand tokens
2Compaction × handoff: what each one keeps
3Turn on the hook for the five Claude Code events
4Rehearse the hook with a fake transcript
Goal: turn on the hook-longrun and trigger the three bands in a rehearsal, knowing what each one asks of the agent and when to do /clear + /prime.
01 · The bands over the compaction point
The percentage is of the point at which Claude Code compacts by size, not of the whole window.
In the measurements from 10/07 to 10/15/2026, no session compacted by size: all 93 compactions were due to idle time (lesson 4).
Hook-longrun 1.9.6 (10/18/2026). The bands change with LONGRUN_FAIXAS="50,70,85". The warning arrives one tool call later, because the transcript is written with a delay.
02 · Compact, write a handoff, or open a new session
What each path keeps, measured on 10/15/2026 (17 compactions with a handoff from the same session in the 3 h before).
1. Compaction
The summary cites 20 to 54 files and keeps recent steps and commands. Within the same session, it holds more concrete detail than the handoff.
But it doesn't separate what's confirmed from what's a guess, it mixes the decisions into the narrative, and it doesn't survive /clear.
2. Handoff
It cites 1 to 27 files, but it marks what was proven, and keeps the decisions and the next action in a section of their own.
It's the only one that works for another session, another day, or Codex. The two together are better than either one alone.
Mistake to avoidWriting the handoff from the compaction summary: it becomes a copy of a copy.
3. /clear + /prime
The new session rediscovers things: 80% of the first 25 steps were reads, versus 51–59% after compacting.
That's why the new session comes at band 3 or the 3rd compaction, at the end of the unit in progress, and not early.
4. Spend less context
Half of a 1.84 GB JSONL measured on 10/01/2026 was tool output. Write logs to a file and read the part you need (tail -50).
A subagent spends its own context and returns only the summary; Anthropic cites returns of 1,000 to 2,000 tokens. How to use subagents is the topic of course 3.
03 · Turn on the hook and rehearse the bands
The rehearsal writes a fake transcript with whatever number of tokens you want and calls the hook the way Claude Code would.
~/.claude/settings.json · "hooks" excerpt (merge with yours)
$ bash ensaio-hook.sh 500000
{"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext":
"Contexto em 52% do ponto de compactação (500 mil de 967 mil tokens). FAIXA 1 de 3:
antes de seguir, acrescente em longrun/2026-10-19-teste/canal.md o que você precisaria
se o histórico sumisse agora (…) Só acrescentar. Depois continue o trabalho normalmente."}}
$ bash ensaio-hook.sh 500000
$ bash ensaio-hook.sh 700000
{… "Contexto em 72% do ponto de compactação (700 mil de 967 mil tokens). FAIXA 2 de 3:
atualize state.md, progress.md e (…) canal.md; termine a unidade de trabalho atual e
grave o handoff agora (…)"},
"systemMessage": "Execução longa em 72% do ponto de compactação: hora de /compact
(o agente foi avisado)."}
$ bash ensaio-hook.sh 830000
{… "Contexto em 86% do ponto de compactação (830 mil de 967 mil tokens). FAIXA 3 de 3:
não abra trabalho novo. (…) /session-handoff, sessão nova e /prime (…) ao fim da
unidade em curso, sem largá-la no meio. (…)"},
"systemMessage": "Execução longa em 86% do ponto de compactação: hora de
/session-handoff + sessão nova + /prime."}
Line breaks and (…) added to fit the screen; the text is from hook-longrun 1.9.6. The additionalContext goes to the agent; the systemMessage appears to you.
There is no compaction hook or counter. Track the percentage in /status; what follows the bands is the AGENTS.md rule (lesson 1) and you.
Measured in the kit: Codex compacts near 90–93% of the window, around 230–240 thousand of 258 thousand, too late for good summary quality. Do the manual /compact earlier.
With an OpenAI model, automatic compaction runs on the server and ignores the compact_prompt; the structured prompt in ~/.codex/config.toml only applies to the manual /compact.
04 · Practice
The first exercise touches your settings.json: make a backup first.
E6 · Hook turned on for all five events
15 min
Copy settings.json to settings.json.bak and merge the hooks excerpt into the ones you already have (don't delete yours). Open a new Claude Code session to load it.
Done when:python3 -c "import json,os;h=json.load(open(os.path.expanduser('~/.claude/settings.json')))['hooks'];print(sorted(e for e,l in h.items() if 'hook-longrun' in json.dumps(l)))" → ['PostToolUse', 'PreCompact', 'SessionStart', 'Stop', 'UserPromptSubmit'].
E7 · The three bands in the rehearsal
10 min
Save ensaio-hook.sh and run the three-band sequence. Then delete /tmp/ensaio-hook and run LONGRUN_TETO=600000 bash ensaio-hook.sh 500000.
Done when: these appear, in this order: FAIXA 1 de 3, nothing, FAIXA 2 de 3, and FAIXA 3 de 3; with LONGRUN_TETO=600000, 500 thousand already lands in FAIXA 2 de 3 (83% of 600 thousand).
05 · Lesson quiz
Question 1 of 4
On a 1M model, at how many tokens does the hook warn for band 2 by default?skill
Correct
The calculation is based on the compaction point by size (~967 thousand), not on the window. The 112/157/191 thousand bands were from hook 1.2.x, which used the wrong cap of 225 thousand and fired in the middle of the work.
Not this one
The calculation is based on the compaction point by size (~967 thousand), not on the window. The 112/157/191 thousand bands were from hook 1.2.x, which used the wrong cap of 225 thousand and fired in the middle of the work.Review: The bands over the compaction point
Question 2 of 4
Band 3 fired in the middle of a refactor. What should the agent do?skill
Correct
Since 1.4.1, band 3 and the 3rd compaction no longer tell you to drop everything midway: the new session costs more to read (80% of the first 25 steps), so it comes at the end of the unit.
Not this one
Since 1.4.1, band 3 and the 3rd compaction no longer tell you to drop everything midway: the new session costs more to read (80% of the first 25 steps), so it comes at the end of the unit.Review: The bands over the compaction point
Question 3 of 4
Within the same session, which one keeps more concrete detail (files, steps)?
Correct
Measured on 10/15: the summary cites 20–54 files; the handoff, 1–27. The handoff wins at separating proven from guessed, at decisions, and at surviving /clear. That's why you use both together.
Not this one
Measured on 10/15: the summary cites 20–54 files; the handoff, 1–27. The handoff wins at separating proven from guessed, at decisions, and at surviving /clear. That's why you use both together.Review: Compact, write a handoff, or open a new session
Question 4 of 4
You use Codex. How do you follow the bands?
Correct
Codex has no compaction hook and no counter. The written rule and you play the role of the hook; and its automatic compaction ignores the compact_prompt with an OpenAI model.
Not this one
Codex has no compaction hook and no counter. The written rule and you play the role of the hook; and its automatic compaction ignores the compact_prompt with an OpenAI model.Review: Turn on the hook and rehearse the bands
Quiz complete
You got {a} of {n} right. Get them all right and the lesson is marked as complete.
Pick an answer. The explanation shows up after you try.
06 · In three sentences
About the right momentThe bands are 50/70/85% of ~967k: canal, then state + handoff + /compact, then nothing new and a new session at the end.
Both togetherThe compaction keeps more detail; the handoff keeps what's proven and the decisions, and survives /clear.
Rehearse firstWith a fake transcript you see every hook warning without spending a single token.