Guides Claude Code published oct 7, 2026

Claude Code Hooks: Run Checks After Selected Edits

beginner

The Rundown

Claude Code hooks run a command when a chosen event happens, such as a successful file edit. You can use them to format files or run tests without asking Claude to do those jobs each time. Anthropic's hooks guide explains the wider feature.

In this guide, you will build a small shipping calculator, format its settings after a write, and run five checks after selected edits. Then you will introduce one error, see which test catches it, and repair the code.

The example watches Claude's Edit and Write tools for three named files. It leaves shell commands and manual editor changes outside its checks. A failed post-edit check leaves the saved change in place because PostToolUse runs after the tool succeeds.

University Pro members get the prepared project, two hook files, practice prompts, tests, and review checklist from this page. You can also ask Claude to build the example with the instructions below.

What are Claude Code hooks?

A hook links an event to an action. For this task, the event is a successful file edit and the action is a local script. The script formats the settings, runs tests, and reports the result.

We use PostToolUse, which runs after a successful tool call. A check that needs to act before a tool runs belongs under PreToolUse. Anthropic's hooks reference describes the events and their limits.

The practice rule is simple: shipping costs $5 below a $50 order total and $0 at $50 or above. Changing one comparison can break that rule while leaving the code readable and able to run. That gives you a useful error to test.

What you need

Use a current Claude Code installation in a terminal, a text editor, and Node.js 22.17.0 or later. This example uses Node's built-in test runner, so there are no extra packages to install. The remediated prepared files were verified with Node.js 24.11.1 on macOS 26.6; the Node 22.17.0 test-runner documentation documents the structured test:summary event used by the helper.

Check your tools in the terminal:

Prompt
claude --version
node --version

Use your normal Claude Code account and permission settings. The local hook files do not make model or network calls and do not require another AI subscription. Asking Claude to create, edit, or repair files still uses your usual Claude Code access.

Start in an empty practice folder. Review the hook and test code before enabling them. Command hooks run with your full user permissions, and the tests execute project code on your computer. Keep real customer files, secrets, and deployment access out of the exercise.

In an interactive session, Claude Code holds settings-file hooks until you accept workspace trust. Review .claude/settings.json and both hook files first, then trust only this disposable practice folder. Anthropic's hook security guidance explains why that review matters.

Common Claude Code hook problems

ProblemWhat to check
The hook does nothing.Check /hooks, workspace trust, the project folder, and whether the edit used Edit or Write.
The settings file will not load.Check /status and strict JSON punctuation, including trailing commas.
Formatting works but tests never run.Remove the format-only argument, then confirm the loaded entry changed.
The hook cannot find Node.Run node --version in the same terminal. Restart after fixing the installation or executable path.
Tests fail after an edit.Read the failed assertion and source code. Repair the code without weakening the test.
The check times out or cannot run.Treat it as an incomplete check. Investigate the runner and use a smaller test scope.
A shell command changes a file without triggering this hook.This matcher covers Edit and Write. Run the checks manually for that change.

For a private record of what Claude Code runs, start a practice session with:

Prompt
claude --debug-file hook-debug.log

Inspect the log after the edit. Keep it private because logs may include local paths and task details. The CLI reference documents the flag.

An organization can restrict which hooks run. Follow its policy rather than changing global permissions to force this example to load. The settings documentation explains managed settings and precedence.

Hooks vs permissions, Skills, and mods

Your goalUse
Run a known check after a tool call.A settings hook, as in this guide.
Decide which tools or paths Claude may use.Permission rules.
Save the instructions for a task you repeat.A Skill.
Add a custom review dialog, panel, or other interface behavior.A mod.

Anthropic documents permissions, Skills, and mods as separate features with some overlap. Choose the smallest setup that fits the task.

Our Claude Code mods tutorial shows a custom review question before selected edits. This guide adds a check after an edit, so you can learn and test each on its own.

Our Claude Code Insights guide helps you identify repeated problems in your coding sessions. When it points to a routine check you keep forgetting, use this hook workflow to test one improvement.

Build a stronger process for AI-written code

Hooks can make a small check repeatable. You still need to choose useful tests, review changes, and decide when an app is ready for users.

Nate Grahek's Intro to Vibe Coding covers planning, AI coding tools, debugging, deployment, and risks around authentication, payments, and API keys. It teaches the wider development process; this guide supplies the specific hooks exercise.

Watch Intro to Vibe Coding with University Pro →

$29 billed monthly · cancel anytime
Go Pro