How to use Claude Code hooks to run a check every time

Day 4, 30 minutes · advanced

Goal of this lessonAdd Claude Code hooks that run your check after every edit, block Claude from finishing while it fails, and notify you when Claude waits.

Why it matters

A line in CLAUDE.md such as "run the check after every change" works almost every time. In a long run nobody watches, "almost" is the problem. The one time Claude skips the check is the time a broken card slips through.

Hooks close that gap. A hook is a command of yours that Claude Code runs at a fixed point in its work: after a file edit, when Claude tries to finish, when it needs your input. Claude Code runs it, not Claude, so it happens every time. The hooks guide calls this deterministic control.

Today you add three hooks to Ines's project. One runs the check after every edit. One stops Claude from finishing while the check fails. One sends you a desktop message when Claude is waiting for you. At the end you have seen a hook block Claude, and you know where to look when a hook does not fire.

How a hook works

A hook lives in a settings file, under hooks. It names an event, an optional matcher, and the command to run:

{
  "hooks": {
    "PostToolUse": [
      { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "git check 1>&2 || exit 2" } ] }
    ],
    "Stop": [
      { "hooks": [ { "type": "command", "command": "git check 1>&2 || exit 2" } ] }
    ]
  }
}
  • The event says when it runs. PostToolUse runs after a tool call succeeds. Stop runs when Claude finishes responding. Notification runs when Claude Code sends a notification, for example when it waits for your permission.
  • The matcher narrows it down. Edit|Write means: only after Claude's file edit tools, not after reading or shell commands. Matchers are case-sensitive.
  • The exit code is how the hook answers. Exit 0 means no objection. Exit 2 means "stop": what the command wrote to stderr (the error output) goes back to Claude. 1>&2 sends the check's normal output to stderr, so Claude reads the list of problems.

What exit 2 does depends on the event. After PostToolUse the edit has already happened, so Claude gets the problems as feedback and can fix them at once. On Stop, exit 2 keeps Claude from finishing: it has to carry on until the check passes. That is the best practices page pattern for a check that must pass before anything counts as done.

A safety catch: if a Stop hook blocks eight times in a row with no tool call in between, Claude Code lets the turn end anyway with a warning. So a check Claude cannot fix does not trap it forever.

Where to put each hook

Project hooks go in .claude/settings.json, so everyone who works on the project gets them. A desktop notification is personal, so it goes in your user file, ~/.claude/settings.json. Claude Code watches both files and normally picks up a changed hook without a restart.

On Windows, hook commands run in Git Bash when Git for Windows is installed, and in PowerShell when it is not. The commands in this lesson are written for Git Bash on Windows and the normal shell on macOS and Linux, so you need Git for Windows. The notification is the exception: its Windows version calls PowerShell itself.

When a hook does not fire

The hooks guide lists what to check, in this order:

  1. Type /hooks. Is the hook listed under the right event? If the list is empty, the JSON may be invalid: no comments and no comma after the last item.
  2. Is the matcher spelled exactly like the tool name, with the same capitals?
  3. Is it the right event? PreToolUse runs before a tool call, PostToolUse after it.
  4. Press Ctrl+O and look for the hook's message in the transcript.
  5. Start Claude Code with claude --debug-file and a file name, trigger the hook, and read that log.
  6. On Windows, a message that mentions Git Bash not being found means the hook ran without Git Bash.

You may also wonder about a hook that blocks edits to protected files. The guide's example script needs an extra JSON tool, jq. For one file you do not need it: your deny rule from lesson 3 already blocks edits to check.sh, with no script at all.

The steps

  1. Start in Manual mode in the practice folder.
  2. Ask Claude to add the two check hooks to the shared settings file.
  3. Confirm them with /hooks.
  4. Break a card on purpose and watch the hooks send Claude back to work.
  5. Add the notification hook to your user settings, with the command for your system.
  6. Test the notification by leaving a permission prompt waiting.
  7. Commit, and write down which hook does what.

Follow the steps for Claude Code below.

Example: Ines's tile saw

Ines asked Claude to add a card for a tile saw with Status: lent and to do nothing else. The edit went through, then the PostToolUse hook printed "Status is not in or out" back to Claude. Claude tried to end its turn, the Stop hook ran the check again and blocked it, and Claude changed the status to out. In Ctrl+O she could see both hook messages. Her instruction to "do nothing else" lost to a check that has to pass, which is exactly what she wanted.

Common mistakes

  • A hook command you never ran yourself. Hooks run in a non-interactive shell. Run the check on its own in the practice folder first and make sure it works there.
  • A Stop hook for a check Claude cannot fix. It blocks eight times and gives up. Make sure the check is one Claude can make pass.
  • Replacing the whole hooks block. When you add a hook, add the event next to the ones already there.

Today's actions

Ticks are not saved without an account

Do it in Claude Code

Claude Code

  1. Start Claude Code in your practice folder in Manual mode.
    cd kettleby-tools
    claude --permission-mode manual
  2. Ask Claude to add the two check hooks. Read the change in the prompt and choose Yes.
    Add these hooks to .claude/settings.json. Keep the permissions block as it is and change nothing else:
    "hooks": {
      "PostToolUse": [
        { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "git check 1>&2 || exit 2" } ] }
      ],
      "Stop": [
        { "hooks": [ { "type": "command", "command": "git check 1>&2 || exit 2" } ] }
      ]
    }
  3. Type /hooks. You should see one hook under PostToolUse and one under Stop. Press Esc to close the list.
  4. Ask for a broken card. Approve the file edit. The hooks send the check's problems back to Claude, and it cannot finish until the check passes. Press Ctrl+O to read the hook messages.
    Add items/tile-saw.txt for a tile saw on shelf A3 with Status: lent. Do nothing else.
  5. Ask Claude to add a desktop notification hook to your user settings. Claude Code asks before it writes in your ~/.claude folder: choose Yes.
    Add a Notification hook with an empty matcher to my user settings file ~/.claude/settings.json. Keep everything already in that file. Use the command for my operating system:
    macOS: osascript -e 'display notification "Claude Code needs your attention" with title "Claude Code"'
    Linux: notify-send 'Claude Code' 'Claude Code needs your attention'
    Windows: powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude Code needs your attention', 'Claude Code')"
  6. Test it. Stay in Manual mode, send this request, switch to another window and wait. After about six seconds of a waiting prompt you get a message. Then answer No.
    Create an empty folder named archive.
  7. Ask Claude to commit, then type /exit and write one line per hook.
    Commit items/tile-saw.txt and .claude/settings.json with the message: Add check hooks.

Note: On Windows, hook commands run in Git Bash, so you need Git for Windows; without it they run in PowerShell, where the check hooks can fail. On macOS, if no notification appears, Script Editor may lack notification permission: the hooks guide shows the fix. On Linux, notify-send needs a desktop; it does not work over SSH. If a hook does not fire, start with /hooks.

Paid planNeeds a paid Claude plan (Pro, Max, Ultra, Team or Enterprise) or a Claude Console account. The free Claude plan does not include Claude Code.

Checked on 4 December 2026 · Source: Claude Code docs: Automate actions with hooks (opens in a new tab)

Create a free account to save your progress. Sign in or sign up