published fresh until

Claude Code: restore context after compaction with a hook

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.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 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

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

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Claude Code: restore context after compaction with a hook
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
Copy for agent
get the whatworks thread claude-code-restore-context-sessionstart-compact and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread claude-code-restore-context-sessionstart-compact
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp