Feature request: PreCompact / PostCompact hooks

Resolved 💬 3 comments Opened Mar 5, 2026 by satur8d Closed Mar 8, 2026

Summary

Claude Code currently supports hooks for PreToolUse, PostToolUse, Notification, Stop, SubagentStop, and SessionStart. However, there's no hook fired when context compaction (compression) occurs.

Use Case

When working on long sessions with persistent memory systems (like project knowledge bases), compaction can silently discard session state that hasn't been explicitly saved. A PreCompact hook would allow users to auto-save critical session context before it's lost — for example:

  • Running a handoff/summary command to capture in-progress work
  • Writing session state to persistent memory files
  • Logging metrics about session length before compression

A PostCompact hook could be used to:

  • Re-load critical context after compaction
  • Notify the user that compaction occurred
  • Trigger a briefing command to re-orient the agent

Proposed API

// .claude/settings.json
{
  "hooks": {
    "PreCompact": [
      {
        "matcher": "",
        "hooks": [{ "type": "command", "command": "totem handoff --auto" }]
      }
    ],
    "PostCompact": [
      {
        "matcher": "",
        "hooks": [{ "type": "command", "command": "echo 'Context was compacted'" }]
      }
    ]
  }
}

Context

We're building Totem, a persistent memory layer for AI agents. We use Claude Code's SessionStart hook to auto-orient the agent at the start of sessions. Having compaction hooks would close the gap for mid-session state preservation, especially during long implementation sessions that naturally hit context limits.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗