Skip to content
BENIBIX Learn

PreCompact Hook

AI⌨️ Claude CodeConfiguration

Also known as PreCompact

The event just before the chat is cut down. A hook can save it first.

Think of it like

It is like you take a photo of a whiteboard. Then you wipe it.

Example

Jack wants each long chat kept in full. His hook copies the whole chat before the summary takes its place.

Why it matters

Compaction throws detail away. This is the moment to keep it.

Definition

PreCompact is the hook event that fires just before Claude Code compacts the chat into a summary. The matcher is manual, for the /compact command, or auto, when the chat gets too long. A hook can block the compaction with exit code 2. It is often used to save a copy of the full chat first.

Example code

json
{
  "hooks": {
    "PreCompact": [
      { "matcher": "auto", "hooks": [{ "type": "command", "command": "./backup-transcript.sh" }] }
    ]
  }
}
Save the full chat before an auto compact.

Common mistake

Blocking every auto compact. Then the chat keeps growing and can run out of room.

Key takeaways

  1. 1
    Before compaction. Manual or auto.
  2. 2
    Can block. Exit 2.
  3. 3
    Save first. The full chat.