What are Claude Code hooks?
Hooks are commands Claude Code runs on its own at set moments: before it uses a tool, after it edits a file, when it needs you, when it finishes. They turn "please always do X" into something that actually happens every time.
That's the difference from CLAUDE.md. A line like "run Prettier after editing" is advice: Claude usually follows it, but it can forget. A hook is code: it runs whether Claude remembers or not. Use CLAUDE.md for guidance and hooks for rules that must hold.
The events you'll use most
| Event | Runs when | Good for |
|---|---|---|
PreToolUse | Before a tool runs; can block it | Blocking dangerous commands, protecting files |
PostToolUse | After a tool succeeds | Formatting edited files, logging commands |
UserPromptSubmit | When you send a prompt, before Claude sees it | Blocking pasted secrets, adding context |
Notification | When Claude needs your permission or input | Desktop notifications |
Stop | When Claude finishes a reply | Running tests, "done" notifications |
SessionStart | When a session starts, resumes or is compacted | Loading context, reminders after compaction |
PreCompact / PostCompact | Around context compaction | Saving notes before details are summarized |
SessionEnd | When the session closes | Quick cleanup (it gets 1.5 seconds by default) |
There are more (SubagentStop, PermissionRequest, PostToolUseFailure, ConfigChange, FileChanged…). Our checker knows all of them.
How the settings.json is laid out
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "your command here" }
]
}
]
}
}
- The event (
PostToolUse) holds a list of groups. - The matcher picks which tools the group applies to.
"Edit|Write"means either;"Bash"means shell commands;"mcp__github__.*"matches every tool from the GitHub MCP server. Leave it out to match everything. Names are case-sensitive:bashmatches nothing. - The hooks list holds what to run. The command gets a JSON description of what's happening on stdin: the tool name, the file path, the shell command, your prompt.
How a hook says yes or no
| Exit code | Means |
|---|---|
0 | All good, carry on. On SessionStart and UserPromptSubmit, anything printed is added to Claude's context. |
2 | Block it. The text written to stderr goes to Claude as the reason, so write something it can act on. |
| Anything else | The hook failed, but Claude carries on. You see a "hook error" notice. |
For finer control a hook can print JSON instead, for example to answer a permission prompt with allow, deny or ask. The generator sticks to exit codes, which are simpler and work everywhere.
Where the file goes
| File | Applies to |
|---|---|
~/.claude/settings.json | All your projects on this computer |
.claude/settings.json | This project, for everyone (commit it) |
.claude/settings.local.json | This project, just you (keep it out of git) |
Hooks from all of these files run together. Changes are usually picked up without a restart; type /hooks in Claude Code to see what it loaded and from which file.
Hooks on Windows
Claude Code runs hook commands with sh on macOS and Linux, and with Git Bash on Windows (or PowerShell when Git Bash isn't installed). Most examples online pipe the input through jq, which Windows doesn't have. Add "shell": "powershell" to a hook and it always runs in PowerShell, which reads JSON natively with ConvertFrom-Json. Pick Windows in the generator and every hook is written that way, with nothing to install.
Stop hooks that never stop
A Stop hook that exits with 2 keeps Claude working, which is how "run the tests before finishing" works. If the tests keep failing, Claude keeps trying. Claude Code gives each Stop hook a stop_hook_active field that's true when it's already continuing because of a Stop hook: exit 0 when it's true, and Claude gets one retry instead of a loop. Our test hook does this, and the checker warns when yours doesn't.
Keep hooks safe
- Hooks run with your full permissions, without asking. Read every hook before you add it.
- Look at
.claude/settings.jsonin any repo you clone. Claude Code holds hooks back until you accept the folder's trust dialog, so check before you click it. - Never put API keys in a hook command. settings.json gets committed; read keys from environment variables.
- A file-protection hook only guards Claude's Edit and Write tools. For secrets, also add a deny rule such as
Read(./.env)underpermissions.
Next: give Claude your project's rules
Hooks enforce the must-never-happen rules. For everything else (the test command, the code style, the gotchas) write an AGENTS.md and CLAUDE.md. To connect Claude to GitHub, a browser or your database, use the MCP config generator.
FAQ
What are Claude Code hooks?
Hooks are commands that Claude Code runs automatically at set moments: before a tool runs, after a file is edited, when Claude finishes, when it needs your attention, and more. Unlike instructions in CLAUDE.md, which Claude can forget or skip, a hook runs every time.
Where do I put hooks?
In a settings file under a top-level "hooks" key. ~/.claude/settings.json applies to all your projects, .claude/settings.json applies to one project and is committed for your team, and .claude/settings.local.json is for one project and only you.
How does a hook block something?
Exit with code 2. For PreToolUse that stops the tool call, for UserPromptSubmit it blocks the prompt, and for Stop it makes Claude keep working. Whatever the hook writes to stderr is passed to Claude as the reason. Exit 0 means carry on; any other exit code is a non-blocking error.
Why doesn't my hook run?
The usual causes: a matcher with the wrong case ("bash" instead of "Bash"), an event name with the wrong case, a relative script path, a script that isn't executable (chmod +x), or jq not being installed. Type /hooks in Claude Code to see which hooks loaded. Paste your settings.json into the checker on this page to find the rest.
Do hooks work on Windows?
Yes. Claude Code runs hook commands in Git Bash on Windows, or in PowerShell when Git Bash isn't installed. Adding "shell": "powershell" makes a hook always run in PowerShell, which can read the hook's JSON input without jq. This generator writes PowerShell versions when you pick Windows.
Are hooks safe?
Hooks run with your full user permissions, without asking. Only add hooks you understand, and look at .claude/settings.json in any repo you clone before you trust the folder: Claude Code holds hooks back until you accept the workspace trust dialog.
Does this tool upload anything?
No. The generator and the checker run entirely in your browser. Nothing you type or paste is sent anywhere.