Claude Code Hooks: Automate Your Workflow with Event-Driven Commands (2026)
Hooks run your own commands at fixed points in a Claude Code session. Where they live, which events exist, three hooks to start with, how a hook blocks an action, and the trust rules to know first.
Claude Code hooks are commands that run at set points in a session, such as before a tool call, after a file edit or when Claude needs your input. You add them to a settings file. A hook can format code after each edit or block an edit to a protected file, and a block holds in every permission mode.
Hooks are commands you define that Claude Code runs at specific points in a session. Anthropic's hooks guide describes them as deterministic control: the action always happens, instead of depending on the model choosing to do it. Instructions in CLAUDE.md are read as context. A hook runs every time its event fires.
For someone who built an internal tool with Claude Code, hooks are how a standard holds without repeating it in every request. The formatter runs after each edit, the env file stays untouched, and you get a notification when Claude is waiting on you. If you're new to the tool, read What Is Claude Code? first.
Where hooks live
A hook goes in a hooks block in a settings file. Where you put it sets who it applies to:
| Location | Scope | Shared |
|---|---|---|
~/.claude/settings.json | All your projects | No, it stays on your machine |
.claude/settings.json | One project | Yes, commit it to the repository |
.claude/settings.local.json | One project | No |
| Managed policy settings | The organization | Yes, set by an admin |
Plugins, skills and subagents can carry hooks too. Type /hooks in Claude Code to see every configured hook grouped by event. Setting "disableAllHooks": true in a settings file turns hooks off, though hooks in managed settings keep running unless that setting is also made there. The settings page explains which file overrides which.
The events you'll use first
The hooks guide lists 34 events as of December 2, 2026. These cover most early needs:
| Event | When it fires |
|---|---|
SessionStart | A session begins or resumes |
UserPromptSubmit | You submit a prompt, before Claude processes it |
PreToolUse | Before a tool call runs. It can block the call |
PermissionRequest | A tool call needs a permission decision |
PostToolUse | After a tool call succeeds |
Notification | Claude Code sends a notification |
Stop | Claude finishes responding |
InstructionsLoaded | A CLAUDE.md or rules file loads into context |
FileChanged | A file you chose to watch changes on disk |
SessionEnd | A session ends |
A matcher narrows when a hook fires. For tool events it matches the tool name, so "Edit|Write" fires only for Claude's file edit tools. Matchers are case-sensitive. The hooks reference lists every event and what each one matches on.
How a hook decides
When an event fires, Claude Code passes JSON to the command on standard input. It includes the session ID, the working folder and the event name, and for tool events the tool name and the arguments Claude passed to the tool.
The exit code is the hook's answer. Exit 0 means no objection, and on PreToolUse the call still goes through the normal permission prompts, because exit 0 doesn't approve anything. Exit 2 blocks the action on events that can be blocked, such as PreToolUse, and the text the command writes to standard error goes back to Claude as feedback. Some events, SessionStart among them, can't be blocked, and exit 2 there only shows the error text. Other exit codes let the action proceed on most events.
When several hooks match the same event, they run in parallel and all finish. For a PreToolUse decision the most restrictive answer wins, in the order deny, defer, ask, allow.
Three hooks to start with
A desktop notification when Claude needs you
Add this to ~/.claude/settings.json on a Mac. The guide has the Linux and Windows versions.
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
}
]
}
]
}
}Format every file Claude edits
This PostToolUse hook runs Prettier on each file after Claude's Edit or Write tool changes it. It reads the file path with jq, which you may need to install. Put it in the project's .claude/settings.json.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
}
]
}
]
}
}When it works, nothing appears in the conversation. Open the edited file to see the formatting.
Block edits to files that must not change
Save a script as .claude/hooks/protect-files.sh and make it executable with chmod +x. This version adapts the guide's example:
#!/bin/bash
FILE_PATH=$(jq -r '.tool_input.file_path // empty')
case "$FILE_PATH" in
*.env|*package-lock.json|*/.git/*)
echo "Blocked: $FILE_PATH is a protected file" >&2
exit 2
;;
esac
exit 0Then register it to run before every Edit or Write call:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"
}
]
}
]
}
}Claude can still change a file by running a shell command. The guide suggests adding an Edit deny rule for each path under permissions.deny, such as Edit(/.env), and using sandboxing to limit what commands can write.
Hooks that use a model or a web service
Most hooks have "type": "command". Four other types exist. An http hook posts the event JSON to a URL, which suits a shared audit service. An mcp_tool hook calls a tool on a connected MCP server. A prompt hook asks a Claude model for a yes or no answer, for example a Stop hook that checks whether every requested task is done and sends Claude back to work if not. An agent hook starts a subagent that can read files and run commands before it answers. Anthropic marks agent hooks as experimental.
Hooks and permission modes
Claude Code has several permission modes, from Manual, which asks before most edits and shell commands, through acceptEdits, plan, auto and dontAsk to bypassPermissions, which skips most prompts. The permissions page describes each one.
A PreToolUse hook runs before any permission mode check, and a hook that denies a call blocks it in every mode, bypassPermissions included. That makes a hook a good place for a rule nobody should get around by switching modes. It doesn't work the other way: a hook that answers "allow" can't override deny rules in your settings.
Check these before you add a hook
Command hooks run with your full user permissions, and the reference says to review and test every command before you add it. Quote shell variables, use absolute paths for scripts, and keep hooks away from files such as .env and keys.
In an interactive session, Claude Code holds back hooks from every settings file until you accept the workspace trust dialog for the folder. A claude -p or SDK run shows no dialog and treats the folder as trusted, so hooks committed in a repository's .claude/settings.json run straight away. Before you script Claude Code over a repository someone else wrote, read its .claude/ settings or pass --settings '{"disableAllHooks": true}' for that run.
A few limits shape what hooks can do. PostToolUse hooks can't undo an action, because the tool has already run. Stop hooks fire every time Claude finishes responding, which isn't always the end of a task. Command, HTTP and MCP tool hooks time out after 10 minutes by default, and the timeout field changes that for one hook.
When a hook doesn't fire
Run /hooks and confirm the hook is listed under the right event. Check that the matcher spells the tool name exactly, including capitals. If you see an error such as "PreToolUse hook error", run the script by hand with sample input, for example echo '{"tool_name":"Bash","tool_input":{"command":"ls"}}' | ./my-hook.sh, and fix whatever makes it exit with an unexpected code.
Sources
- Automate actions with hooks (Anthropic documentation) (checked 2026-12-02)
- Hooks reference: events, input and output, workspace trust and security (Anthropic documentation) (checked 2026-12-02)
- Configure permissions: permission modes and rules (Anthropic documentation) (checked 2026-12-02)
- Settings files and precedence (Anthropic documentation) (checked 2026-12-02)
Get the next one.
Field notes from real deployments, written the way this one was. No schedule promised and nothing sold to you. Unsubscribe whenever.
Frequently asked questions
What are Claude Code hooks?
Hooks are commands you define that Claude Code runs at fixed points in a session, such as before a tool call, after a file edit, when Claude needs input or when a session starts. They go in a hooks block in a settings file and run every time their event fires.
Where do I put a hook?
In a settings file. ~/.claude/settings.json applies to all your projects on your machine, .claude/settings.json applies to one project and can be committed for your team, and .claude/settings.local.json applies to one project for you only. Run /hooks to see what's configured.
Can a hook stop Claude Code from doing something?
Yes. A PreToolUse hook that exits with code 2 blocks the tool call, and its error text goes back to Claude as feedback. A hook's deny holds in every permission mode, including bypassPermissions. Claude can still reach a file through shell commands, so pair a file hook with permission deny rules.
Are Claude Code hooks safe?
Command hooks run with your full user permissions, so review every command before you add it. Interactive sessions hold back hooks until you trust the folder, but claude -p runs treat the folder as trusted, so check a repository's .claude settings before scripting Claude Code over it.
Need help implementing this?
Prometheus does AI consulting and implementation, remote, with onsite days available in Milwaukee. We set it up, train your team, and make sure it works.
let's talk
