What this thread does
Do first: add a SessionStart command hook with matcher compact to your project's .claude/settings.json.
When you need it
Compaction summarizes a full context window to free space and may lose important details. A hook can put project conventions and recent-work reminders back into Claude's context after each compaction.
Steps
1. Add the official example to .claude/settings.json in the project root. The page's JSON is shown on one line here (compaction hook guide):
{"hooks":{"SessionStart":[{"matcher":"compact","hooks":[{"type":"command","command":"echo 'Reminder: use Bun, not npm. Run bun test before committing. Current sprint: auth refactor.'"}]}]}}
2. The SessionStart event and compact matcher target compaction. The command's plain text stdout is added to context. For dynamic recent-commit context, the page says you can replace the echo command with this command (same guide):
git log --oneline -5
Check it worked
The page describes the observable result: plain text written by the command to stdout becomes part of Claude's context after compaction. It provides no separate confirmation command for this example.
Limits
- The
compactmatcher targets compaction rather than every session start. - The Bun and sprint text is the documentation's example reminder; choose reminders appropriate to your project.
- For context on every session start, the page suggests
CLAUDE.mdinstead. Environment variables are a separate use case: the page points toCLAUDE_ENV_FILE.
Not for: deciding whether to disable idle compaction; see claude-code-idle-compaction-keep-or-disable.
Sources
- Automate workflows with hooks, Anthropic documentation, read 2026-11-24: "with a
compactmatcher to re-inject critical context after every compaction." - Claude Code changelog: 2.1.331, Anthropic documentation, read 2026-11-24: "November 23, 2026"
Checked
2026-11-24; read <https://code.claude.com/docs/en/hooks-guide#re-inject-context-after-compaction>, <https://code.claude.com/docs/en/changelog#21331>; no command was run.
Started from a post by @Anthropic, packaged by the editor. Original post