tool · Claude Code
smart-compact
Decide what survives compaction in Claude Code: pin what must never be lost, mark what is certainly garbage, and have the rest judged against the current goal. It runs entirely in hooks, so marking costs no extra model call.
The problem
When a long session compacts, Claude Code replaces the history with a summary written by the model. You don't control what the summary keeps: a constraint you stated two hours ago may come back paraphrased or not at all, while the contents of a file you deleted may still take up room. The model marks a line at the moment it writes it, when it knows best whether the line matters; when a compaction starts, a hook reads the marks from the session transcript.
| Mark | Meaning | What happens at compaction |
|---|---|---|
| ◆ text | Pin | Saved by the hook and put back verbatim after every compaction, native or smart. |
| ◇ text | Discard | The line and whatever it names are listed for the summarizer as “leave out completely, no pointer”. |
| ◆✗ fragment | Unpin | Retires an earlier pin that contains the fragment. |
| (no mark) | Undecided | Judged at compaction time against the current goal. |
A mark is a line of a reply, or of your own message, that starts with the symbol. Tool calls, tool results, the output of ! shell commands, subagent text, thinking, quoted lines (> …) and fenced code blocks are not scanned, so a web page or a file cannot create a mark by merely being read.
Install
git clone https://github.com/cleoanka/smart-compact
cd smart-compact
python3 install.py --claude-md
Then start a new Claude Code session. The installer copies hooks/smart-compact.py into ~/.claude/hooks/, adds five hook entries to ~/.claude/settings.json after backing the file up, and writes seven small command files into ~/.claude/commands/; with --claude-md it appends the marking rule to ~/.claude/CLAUDE.md. The rule matters: without it the model does not know the marks exist.
Requirements: Python 3.9 or newer, and a Claude Code version with the PreCompact, PostCompact, SessionStart, UserPromptExpansion and Stop hooks. Developed and tested on Claude Code 2.1.325 on macOS. The hook script has no dependencies outside the standard library; --uninstall removes the hooks, commands and script.
The ◆ and ◇ rules
Once the rule is in CLAUDE.md, the model writes marks on its own. You can mark things yourself: start a line of your message with ◆ , ◇ , !pin or !drop , or use these commands. They are answered by the hook; they never reach the model and use no tokens.
| /pin | Pin one line |
| /unpin | Remove a pin by number or fragment |
| /drop | Mark something as worthless for the next compaction |
| /undrop | Remove a discard mark before it is used |
| /pins | List pins and pending discard marks |
| /smartcompact | Compact now with the smart policy |
| /smartautocompact | Smart auto-compaction for this session |
A discard mark that names a pin, or is named by one, is ignored: protecting wins over discarding. To see a session's ◆/◇ layer next to its compactions, drop the transcript into the Flight Recorder.
What it adds
- No model calls: marks are a few characters in a reply the model is already writing.
- No cache invalidation during normal work: it does not change the system prompt, the tool list or earlier messages.
- The rule in
CLAUDE.mdis about 450 tokens of standing context, cached with the rest of your project context. - At compaction: up to about 1,700 characters of policy and 3,400 characters of discard list in the summarization request, then at most 8,000 characters of pins re-injected afterwards, plus about 500 characters for the recall command.
Where it loses
This section comes from the “What it does not do” and “What was tested” parts of the repository README, as of commit b2738c4.
- The measurements come from one small session of about 62K tokens. At that size the token difference was negligible, and the native summary happened to keep the pinned facts too. In that session the summary was 4,544 characters with the smart policy and 6,257 with a native compaction; the comparison says nothing certain about long sessions, which were not measured side by side.
- It cannot remove anything from the context before a compaction: a
◇line stays in the context until the next compaction happens. - Compaction itself is not free: it invalidates the conversation cache. A smart limit set too low makes you pay that often and can cost more than it saves.
- The discard list is an instruction to a language model, not a hard guarantee the way pins are. The hook logs how many marked lines were quoted back into a summary.
- Marking depends on the model: one model tested followed the rule on every run, another was inconsistent. The wording of the rule matters as well.
- Typing
/compact smartfor you is a keystroke injection; if you are typing a new prompt at that moment, the text gets mixed into your draft. It can only do this inside tmux, and in iTerm2 on macOS. - Not tested: Linux and Windows in real sessions (the unit tests run on Linux in CI), terminals other than tmux and iTerm2, and native auto-compaction kicking in at its own limit while smart auto is on.
Details, install options and tests: github.com/cleoanka/smart-compact.
For a team’s whole Claude Code setup, see Agent Ops: a fixed-price audit and setup.