Claude Hookbook

Hooks / context

Re-inject project context after compaction

Restores your project's key facts into Claude's context right after a compaction wipes the details.

SessionStartcompactlow riskneeds manual check

What it does

Fires on SessionStart with the compact matcher - the moment after context compaction. The script prints your standing context (stack, commands, conventions - edit the heredoc) to stdout, and SessionStart stdout is added to Claude's context. Official-guide pattern. Keep the output short; it rides along for the rest of the session.

When it runs

EventSessionStart
Matchercompact
Scopeproject
Platformsmacos, linux, windows
Timeout10s
Failure modeobserve
Verified againstClaude Code hooks reference, event/matcher schema checked 2026-09-10
Test statusneeds manual check - run the test steps below before relying on it

Config

Merge this into .claude/settings.json - or use the generator to combine hooks.

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/reinject-context-compact.sh",
            "timeout": 10
          }
        ],
        "matcher": "compact"
      }
    ]
  }
}

Script

Save as .claude/hooks/reinject-context-compact.sh and chmod +x it.

#!/bin/bash
# reinject-context-compact: print standing context after compaction.
# SessionStart stdout is added to Claude's context - keep this SHORT.
cat <<'CTX'
Project context (re-injected after compaction):
- Stack: <your stack, e.g. Next.js 15 + Postgres>
- Test command: <your test command>
- Conventions: <3-5 bullets Claude keeps forgetting>
CTX
exit 0

Install

  1. Save the script to .claude/hooks/reinject-context-compact.sh and chmod +x it.
  2. Edit the heredoc: 5-10 lines of the context Claude always needs.
  3. Add the config fragment to .claude/settings.json.

Test it

Sources

Related hooks

Session-start repo snapshot

Hands Claude a bounded snapshot - branch, dirty state, recent commits - at every session start and resume.

SessionStartstartup|resumelow riskneeds manual check

Pro Pack (later)

Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.

Waitlist opens soon.