Claude Code Hooks: A Beginner's Guide with 3 Examples
- Published
- 4 min read
- 4 min read
Key takeaways
- A hook is a command Claude Code runs at a set moment, like after each file edit or before each shell command. It always runs: Claude doesn't choose.
- Put hooks in a settings file:
.claude/settings.jsonfor one project (shareable), or~/.claude/settings.jsonfor all your projects. - The most useful events:
PostToolUse(after a tool),PreToolUse(before a tool, can block it),Notification,Stop, andSessionStart. - To block an action, the hook exits with code 2, and what it writes to stderr tells Claude why.
- Type `/hooks` in Claude Code to see every hook and where it comes from.
Make Claude Code work your way
The free guide covers the settings that change everything, with every prompt ready to copy.
Get the free guideWhat are Claude Code hooks?
Hooks are your own commands that Claude Code runs at specific points: when a session starts, before or after it uses a tool, when it needs your attention, when it finishes. The difference with CLAUDE.md is control. A rule in CLAUDE.md is advice Claude may forget. A hook runs every time.
Use a hook for anything that must happen: format the code, run a check, block a command, send a notification.
Claude Code hook events
Claude Code has more than 30 events. These are the ones you'll use first:
| Event | When it runs | Can block? |
|---|---|---|
SessionStart | When a session starts or resumes | No |
UserPromptSubmit | When you send a prompt, before Claude reads it | Yes |
PreToolUse | Before a tool runs (Bash, Edit, Write…) | Yes |
PostToolUse | After a tool succeeds | No, it already ran |
Notification | When Claude Code needs your attention | No |
Stop | When Claude finishes its answer | Yes |
SubagentStop | When a subagent finishes | Yes |
PreCompact | Before the conversation is compacted | Yes |
Where to put your hooks
| File | Applies to | Shared? |
|---|---|---|
~/.claude/settings.json | All your projects | No |
.claude/settings.json | This project | Yes, commit it |
.claude/settings.local.json | This project, only you | No |
Each hook has three levels: the event (PostToolUse), a matcher that picks the tool (Edit|Write), and the command to run. If the file already has a hooks key, add your event next to the others instead of replacing it.
3 Claude Code hook examples
1. Format every file Claude edits
This runs Prettier on each file after Claude edits or writes it. Put it in .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}Claude Code sends the hook a JSON description of the tool call on stdin. Here, jq pulls out the file path and passes it to Prettier.
2. Get a notification when Claude needs you
This shows a Mac notification each time Claude Code waits for you. Put it in ~/.claude/settings.json so it works everywhere:
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
}
]
}
]
}
}On Windows, the docs give a PowerShell version that opens a message box instead.
3. Block a dangerous command
A PreToolUse hook on Bash can stop a command before it runs. This script, from the docs, blocks any command that starts with rm:
#!/bin/bash
input=$(cat)
command=$(jq -r '.tool_input.command' <<<"$input")
if [[ "$command" == rm* ]]; then
echo "Blocked: rm commands are not allowed" >&2
exit 2
fi
exit 0Exit code 2 blocks the action and sends your message to Claude. Exit code 0 lets the normal flow continue. Any other code is an error that doesn't block, so a policy hook must use 2.
Get the free Claude Code guide
The install and the 5 settings to know, step by step, with every prompt ready to copy.
Download the guideTips for writing safe hooks
- Hooks run with your permissions. Read any hook before you add it, especially from a shared project.
- For a hard allow or deny, use Claude Code's permission settings. Hooks are best for checks and automation.
- `PostToolUse` can't undo anything: the tool already ran. Block in
PreToolUseinstead. - Turn every hook off for a while with
"disableAllHooks": truein a settings file. - Not sure what's running? Type
/hooksto see each hook and the file it comes from.
Frequently asked questions
What are hooks in Claude Code?
Commands Claude Code runs at set moments, like after each file edit or before each shell command. Unlike instructions in CLAUDE.md, they run every time.
Where do I put Claude Code hooks?
In a settings file under a hooks key: .claude/settings.json for one project, or ~/.claude/settings.json for all your projects.
How does a hook block an action?
It exits with code 2. Claude Code stops the action and shows Claude the hook's stderr message. This works on blocking events like PreToolUse.
How do I see my hooks?
Type /hooks in Claude Code. It lists every hook with the file it comes from.