This project was born out of a need to replace the garbage default compaction in Claude Code with something better.
Important
This is a massive hack. Until Anthropic provides a way to directly modify the compaction output via hooks, this is the best solution I could come up with.
The Morph compaction model seems to work well, and it's free if you make an account, but of course, you are free to use whatever model you like.
This project, along with the following documentation, is AI-generated. One thing to note that isn't noted below is that if you plan to compact early before auto compaction kicks in, you need to run the command with the custom instructions as well: /compact Do not summarize the conversation. Output only the following and nothing else: Summary provided via SessionStart hook.
This project is licensed under the Apache License, Version 2.0. You are free to use this project as you see fit so long as you comply with the license's terms.
When Claude Code compacts a session, this tool:
- Adds a small
Compact Instructionssection toCLAUDE.mdso Claude emits a placeholder instead of a full summary. - Runs your compactor in the
PreCompacthook against the full transcript. - Injects your compactor's output back into context during
SessionStart.
go install -trimpath -ldflags="-s -w" github.com/StayBlue/claude-compaction-hook@latestOr build from source:
go build -trimpath -ldflags="-s -w" -o compact-hook .Install in the current project:
compact-hook install --compactor "python3 /path/to/my-compactor.py"Install globally:
compact-hook install --global --compactor "python3 /path/to/my-compactor.py"install updates .claude/settings.json or ~/.claude/settings.json and adds the required Compact Instructions section to CLAUDE.md.
Remove it with:
compact-hook uninstall
compact-hook uninstall --globalRun install again with a new --compactor value to replace the existing command.
Your compactor can be any executable that:
- reads the formatted transcript from
stdin - receives the raw JSONL transcript path as
$1 - writes the replacement summary to
stdout - exits with code
0on success
The formatted transcript looks like this:
[user]: ...
[assistant]: ...
See examples/ for reference implementations.