Instructions in a CLAUDE.md file are advice; Claude Code hooks are guarantees. A hook is a shell command that Claude Code runs automatically at a specific moment in the session lifecycle — before a tool executes, after a file is edited, when a session starts, when Claude finishes responding. Because hooks are your code, not the model’s judgment, they are the right tool for anything that must happen every time: formatting edited files, running a linter, blocking dangerous commands, logging activity, or sending yourself a notification when a long task completes.
How Claude Code Hooks Work
Hooks are configured as JSON in a settings file — ~/.claude/settings.json for personal defaults, or .claude/settings.json in a project to share them with your team. Each hook listens to an event, optionally filters by a matcher (which tool triggered it), and runs one or more commands. When the event fires, Claude Code passes a JSON payload describing what happened — tool name, tool input, working directory — to your command on standard input. Your command’s exit code decides what happens next:
- Exit 0: everything is fine, proceed.
- Exit 2: block the action. On a
PreToolUsehook, the tool call is cancelled and whatever your script printed to stderr is shown to Claude as feedback, so it can adjust. - Any other exit code: treated as a hook error; the action generally proceeds, and the error is surfaced to you.
Hooks can also print a JSON result to stdout for finer control — for example a PreToolUse hook can return a permissionDecision of allow, deny or ask along with a reason.
Claude Code Hooks: The Events That Matter
| Event | Fires when | Typical use |
|---|---|---|
PreToolUse |
Before a tool runs | Validate or block tool calls |
PostToolUse |
After a tool succeeds | Format files, lint, log changes |
UserPromptSubmit |
You submit a prompt | Inject extra context, validate input |
SessionStart |
A session begins | Load environment info, check dependencies |
Stop |
Claude finishes responding | Run tests, send a “done” notification |
SubagentStop |
A subagent completes | Verify or collect subagent results |
PreCompact |
Before context compaction | Back up the transcript |
Notification |
Claude needs attention | Desktop or chat alerts |
Example 1: Auto-Format Every Edited File
This hook formats any file Claude touches with Edit or Write, using Prettier as the example formatter:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs -r npx prettier --write"
}
]
}
]
}
}
The script reads the event JSON from stdin, extracts the edited file path with jq, and formats just that file. Swap in shfmt, black, gofmt or your linter of choice — the shape stays identical. No more “please remember to format” instructions, and no formatting noise in your diffs.

Example 2: Block Dangerous Bash Commands
A PreToolUse hook on the Bash tool makes a hard guardrail. Save this as .claude/hooks/guard-bash.sh and make it executable:
#!/bin/bash
command=$(jq -r '.tool_input.command')
if echo "$command" | grep -qE 'rm -rf /|mkfs|dd if=|:\(\)\{'; then
echo "Blocked: destructive command pattern detected" >&2
exit 2
fi
exit 0
Then wire it up in settings:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": ".claude/hooks/guard-bash.sh" }
]
}
]
}
}
When the pattern matches, exit code 2 cancels the command and Claude sees your message, so it can pick a safer approach. Teams extend this idea to protect production config files, require ticket numbers in commit messages, or forbid edits outside the project directory.
Managing and Debugging Claude Code Hooks
Run /hooks inside a session to see every hook that is actually active, grouped by event — always verify here first, because a hook defined in the wrong settings file (or with a JSON syntax error) simply never fires. Note also where settings live: hooks defined in a project’s .claude/settings.json are shared with everyone who clones the repo, while entries in your personal ~/.claude/settings.json — and the gitignored .claude/settings.local.json — apply only to you, which is the right place for notification hooks that call your own phone or desktop.
Test scripts standalone by piping them a sample payload, e.g. echo '{"tool_input":{"command":"ls"}}' | .claude/hooks/guard-bash.sh; echo $?, and launch with claude --debug when you need to watch hook execution in the debug log.
One design rule: keep hooks fast and boring. A hook runs on the critical path of every matching action, so a slow formatter or a flaky network call will make the whole agent feel sluggish. Do heavy work in Stop hooks or background scripts instead.
Hooks vs. Permissions vs. CLAUDE.md
These three mechanisms complement each other. CLAUDE.md persuades — use it for conventions. Permissions decide — allow/ask/deny rules for tools, managed with /permissions. Hooks enforce and automate — the only option when something must execute, be checked, or be blocked deterministically regardless of what the model decides. Most mature setups, including the ones in our best practices guide, use all three.
For the complete event reference and advanced JSON output options, see Anthropic’s official hooks documentation. Start with formatting plus one guardrail hook, and you will feel the difference in a single afternoon.