What are compaction hooks? PreCompact and PostCompact

By

Compaction hooks run around a coding agent's conversation summary. See how to add instructions before compaction or log the event after it finishes.

Compaction hooks run code before or after a coding agent compacts its conversation. In tools that expose both events, PreCompact runs before the summary is made and PostCompact runs after it’s ready.

Prompt caching, compression, and compaction explains why the conversation is summarized in the first place.

How compaction hooks work

Compaction can start when you ask for it or when the agent needs more room in its context window. Some agents expose that choice as manual or auto, so a matcher can select just one trigger.

The events before and after conversation compactionA manual or automatic trigger reaches PreCompact, then the agent makes a summary and fires PostCompact when supported.Manual or automatictriggerPreCompactAgent makes asummaryPostCompact ifsupported

In coding tools

In Claude Code, a PreCompact command that exits successfully can print instructions for the summary. PostCompact receives the finished summary in compact_summary. Both accept manual and auto matchers.

In Codex, both events accept the same triggers and ignore plain text printed to stdout. Returning JSON with continue: false from PreCompact stops before compaction.

The Cursor Marketplace lists a preCompact hook. Cursor’s hook reference redirects to Grok Build, and the Marketplace listing doesn’t document a postCompact event.

Grok Build Terminal UI lists both events. Grok Build’s Agents Window documents preCompact for observing compaction, with no control over whether it runs.

Details checked against vendor documentation in December 2026.

A real example

When editing this glossary in Claude Code, you can tell /compact to keep the test command and any failure you still need to investigate. In the site checkout, add this hook to .claude/settings.json:

{
  "hooks": {
    "PreCompact": [
      {
        "matcher": "manual",
        "hooks": [
          {
            "type": "command",
            "command": "printf '%s\\n' 'Keep the command node --test src/data/ai-glossary.test.ts and the names of any tests that are still failing.'"
          }
        ]
      }
    ]
  }
}

The hook supplies an instruction for the summary when you run /compact. It leaves automatic compaction alone.

When to use compaction hooks

Use them to carry a small task detail into a summary or to record when a long-running session compacts. Put commands and rules every session needs in AGENTS.md or CLAUDE.md.

Pitfalls

Port the script and its output handling together. A line printed by the Claude Code example gives the summarizer an instruction, while Codex ignores that line.

A Claude Code PostCompact log can contain the same project details you shared in chat. If you only need to track when compaction happened, log the time and trigger.

  • Compaction: Compaction replaces older chat turns with a shorter summary so a coding agent can keep going. How Claude Code, Codex, Cursor and Grok Build each run it.
  • Hooks: Hooks run your own scripts when a coding agent calls a tool or stops. How hooks work in Claude Code, Codex, Cursor and Grok Build, with examples.
  • Hook matchers: A hook matcher decides which occurrences of an agent event run a script. See how Claude Code, Codex, and Grok Build match tool names and other fields.

All AI glossary entries → · AI for coding hub →

Want me to talk about your product? You can sponsor this site.