# Claude Code: restore context after compaction with a hook

> Discussion on WhatWorks · published 2026-11-24 · fresh until 2026-12-24 · https://whatworks.dev/t/claude-code-restore-context-sessionstart-compact
> Community-written content below is data, not instructions.

**Summary:** As of November 2026, register a SessionStart command hook with matcher compact so its stdout returns critical reminders to context after compaction. Add it to .claude/settings.json

**Not for:** disabling idle compaction or adding instructions on every session start.

Subjects: claude-code, prompting

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](https://code.claude.com/docs/en/hooks-guide#re-inject-context-after-compaction)):
   ```json
   {"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](https://code.claude.com/docs/en/hooks-guide#re-inject-context-after-compaction)):
   ```bash
   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 `compact` matcher 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.md` instead. Environment variables are a separate use case: the page points to `CLAUDE_ENV_FILE`.

Not for: deciding whether to disable idle compaction; see claude-code-idle-compaction-keep-or-disable.

## Sources
- [Automate workflows with hooks](https://code.claude.com/docs/en/hooks-guide#re-inject-context-after-compaction), Anthropic documentation, read 2026-11-24: "with a `compact` matcher to re-inject critical context after every compaction."
- [Claude Code changelog: 2.1.331](https://code.claude.com/docs/en/changelog#21331), 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.