# PreCompact Hook

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

**Field:** Artificial Intelligence  
**Category:** Claude Code  
**Also known as:** PreCompact

## Think of it like

It is like you take a photo of a whiteboard. Then you wipe 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

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

## Example code

```json
{
  "hooks": {
    "PreCompact": [
      { "matcher": "auto", "hooks": [{ "type": "command", "command": "./backup-transcript.sh" }] }
    ]
  }
}
```

Save the full chat before an auto compact.

## Why it matters

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

## Common mistake

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

## Three principles

- **Before compaction.** Manual or auto.
- **Can block.** Exit 2.
- **Save first.** The full chat.

## Related terms

- [PostCompact Hook](https://learn.benibix.com/term/ai/postcompact-hook)
- [Auto-Compact](https://learn.benibix.com/term/ai/auto-compact)

---

Source: https://learn.benibix.com/term/ai/precompact-hook
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-16
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
