Skip to content
BENIBIX Learn

Claude Code Hook

AI⌨️ Claude CodeConfiguration

Also known as hooks, hook handler, Claude Code hooks

A script Claude Code runs on its own at a fixed point, such as before a tool.

Think of it like

It is like a doorbell that rings a bell in the next room. You choose what that bell does.

Example

Lucy wants each file Claude edits to be tidy. She adds a hook that runs her code tidier after each edit. It never forgets.

Why it matters

A rule in CLAUDE.md is a request. A hook always runs, so it is how you make something sure to happen.

Definition

A Claude Code hook is a script or other handler that Claude Code runs on its own at a fixed point in a session. The point is called an event, such as PreToolUse before a tool runs or Stop when Claude finishes. You set hooks in a settings file. A hook gets the details as JSON and can answer back, for example to block a tool call.

Example code

json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{ "type": "command", "command": "npx prettier --write ." }]
      }
    ]
  }
}
Format the code after every edit.
Each label is an event a hook can listen for.

Common mistake

Putting must-do steps in CLAUDE.md and hoping. Claude can skip a note. It cannot skip a hook.

Key takeaways

  1. 1
    Runs on its own. At an event.
  2. 2
    Set in settings. JSON.
  3. 3
    Can answer back. Block or add.