Skip to content
Anyway Works
Plan a call
Plan a call

Claude Code hooks explained, with three examples

A hook in Claude Code is a shell command that runs at a fixed moment: before a tool runs, after a file is edited, when Claude needs your input, when it stops. It runs every time, whatever Claude decides. That is the difference with CLAUDE.md and skills, which Claude reads and may follow. Use a hook for what must always or never happen.

Hook, skill or CLAUDE.md?

What it isClaude can skip it
CLAUDE.mdInstructions read at the start of every sessionYes: it is context
SkillInstructions loaded when they are neededYes
HookA command run at a fixed momentNo

The docs put it this way: hooks give “deterministic control: certain actions always happen rather than relying on the LLM to choose to run them.” A line in CLAUDE.md asking Claude not to touch .env usually works. A hook that blocks it always works.

When does a hook run?

At an event. The ones you will use first:

  • PreToolUse: before a tool runs. It can block the tool.
  • PostToolUse: after a tool succeeded, for example after a file edit.
  • Notification: when Claude Code needs your input or permission.
  • Stop: when Claude finishes responding.
  • SessionStart: when a session begins or resumes.
  • UserPromptSubmit: when you send a prompt, before Claude reads it.

There are more than thirty in total, from compaction to subagents. Run /hooks inside Claude Code to see what you have set up.

How does a hook block something?

Through its exit code. Claude Code passes the details as JSON on the hook’s input: the tool, and what it was about to do. The hook answers:

  • exit 2: block the action. What the hook writes to its error output goes back to Claude as the reason.
  • exit 0: no objection. For PreToolUse this does not approve the action: the normal permission question still follows.

Where do you put a hook?

In a settings file, under hooks:

  • ~/.claude/settings.json: all your projects, only you.
  • .claude/settings.json: this project, shared through git.
  • .claude/settings.local.json: this project, only you.

Plugins, skills and subagents can carry hooks of their own.

Example 1: a message when Claude needs you

On Windows, in ~/.claude/settings.json, from the docs:

{
  "hooks": {
    "Notification": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "powershell.exe -Command \"[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude Code needs your attention', 'Claude Code')\""
          }
        ]
      }
    ]
  }
}

Now you can do something else while Claude works, and you hear when it waits for you.

Example 2: format every file Claude edits

In .claude/settings.json of the project. It runs Prettier after every edit:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
          }
        ]
      }
    ]
  }
}

It needs jq and a Bash shell; on Windows that is Git Bash, which the Claude Code install guide for Windows explains.

Example 3: never touch .env

A PreToolUse hook with the matcher Edit|Write runs a small script before every edit. The script reads the file path from its input and ends with exit 2 when the path matches .env, package-lock.json or .git/. Claude gets the reason and takes another way. The docs have the full script under “Block edits to protected files”.

What can a hook not do?

  • A command hook cannot run a slash command or call a tool itself.
  • A PostToolUse hook cannot undo what already happened.
  • A Stop hook also fires when Claude only answered a question, not just at the end of a task.
  • A hook has a time limit: 10 minutes for a command by default, less for some events.

Sources

Want Claude Code set up for your team, with the rules built in? Plan a call.

Read from the Claude Code documentation on 3 December 2026.

New posts and news about our products, such as Hocusclick, now and then. Every mail has a link that stops it. See the privacy policy.

contact@anywayworks.com