Claude Code hooks explained: events, settings and five examples
By Anjali Singh, maker of Doki · Updated 28 November 2026
A hook is a command that Claude Code runs at a set moment: when it finishes, before it runs a tool, when it needs you. Hooks are how you get notifications, block risky commands, format files after every edit, or give Claude extra context when a session starts.
The short answer
Hooks go in ~/.claude/settings.json under "hooks". Each one names an event (such as Stop), an optional matcher (such as a tool name), and a command to run. Claude Code passes the command a JSON description of what's happening on stdin. The command's exit code decides what happens next: 0 carries on, 2 blocks the action (for events that can be blocked) and sends your stderr text to Claude. Type /hooks in Claude Code to see which hooks are loaded.
The events you'll use most
Claude Code has more than 30 hook events. These are the ones most setups need; the rest are in the hooks reference.
| Event | Runs when | Can it block? | Typical use |
|---|---|---|---|
Stop | Claude finishes responding | Yes: Claude keeps going | "Done" notifications, checks before stopping |
Notification | Claude Code wants your attention, for example a permission prompt | No | "Needs you" notifications |
PreToolUse | Before a tool call runs | Yes: the call is cancelled | Blocking risky commands |
PostToolUse | After a tool call succeeds | No (it already ran) | Formatting or linting edited files |
UserPromptSubmit | You send a prompt, before Claude sees it | Yes: the prompt is dropped | Adding context, filtering prompts |
SessionStart | A session starts or resumes | No | Loading context such as git status |
SessionEnd | A session ends | No | Cleanup, logging |
SubagentStop | A subagent finishes | Yes | Checking a subagent's work |
PreCompact | Before the conversation is compacted | Yes | Saving notes first |
Where hooks go, and what they look like
Hooks can live in any settings file, and Claude Code merges them all:
~/.claude/settings.json: every project on your Mac..claude/settings.jsonin a project: shared with everyone who works on it..claude/settings.local.jsonin a project: just for you, not committed.- Plugins, skills and subagents can bring their own hooks too.
The shape is always the same: event, then a list of matcher groups, then the commands:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "echo edited >> ~/claude-edits.log" }
]
}
]
}
}
matcherfilters what the hook runs for. For tool events it's the tool name (Bash,Edit|Write); forNotificationit's the kind of notification. Leave it out, or use"", to match everything.typeis usually"command". Hooks can also call a URL ("http"), an MCP tool, or ask a model ("prompt").timeout(seconds) is optional. Command hooks get up to 10 minutes by default, so keep them quick or they'll hold Claude up.- If your settings file already has a
hookssection, add to it. Don't paste a second one.
What a hook receives
Every command hook gets a JSON object on stdin. All events include session_id, transcript_path, cwd (the project folder) and hook_event_name. Tool events add tool_name and tool_input; Notification adds the message and its notification_type. A PreToolUse hook for a shell command receives something like:
{
"session_id": "abc123",
"cwd": "/Users/you/shop",
"hook_event_name": "PreToolUse",
"tool_name": "Bash",
"tool_input": { "command": "npm test" }
}
The examples below read it with jq, which comes with macOS 15 and later (brew install jq on older versions). The environment variable CLAUDE_PROJECT_DIR holds the project's folder.
Exit codes: carry on, or block
0: success. Claude Code carries on. ForSessionStartandUserPromptSubmit, plain text you print is added to Claude's context.2: block. For events that can block, the action is stopped and what you wrote to stderr goes to Claude as the reason.- Anything else: an error is shown, and the action goes ahead anyway.
For finer control a hook can print a JSON object instead, for example permissionDecision (allow, deny or ask) from PreToolUse. The JSON must be the only thing on stdout, so make sure your shell profile doesn't print anything.
Five examples you can copy
1. A banner when Claude is done, with the project's name
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "osascript -e \"display notification \\\"$(basename \"$CLAUDE_PROJECT_DIR\") is done\\\" with title \\\"Claude Code\\\" sound name \\\"Glass\\\"\""
}
]
}
]
}
}
The banner comes from Script Editor, so allow Script Editor in System Settings, Notifications. More ways to get alerts are in how to get notified when Claude Code finishes.
2. An alert when Claude needs your permission
{
"hooks": {
"Notification": [
{
"matcher": "permission_prompt",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code needs your OK\" with title \"Claude Code\"'"
}
]
}
]
}
}
permission_prompt fires once a permission prompt has waited about six seconds. idle_prompt fires when Claude finished about a minute ago and you haven't typed.
3. Block dangerous shell commands
Save this as ~/.claude/hooks/block-danger.sh and run chmod +x on it:
#!/bin/bash
cmd=$(jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -Eq 'rm -rf (/|~)|git push (-f|--force)'; then
echo "Blocked by a hook: $cmd" >&2
exit 2
fi
exit 0
Then point a PreToolUse hook at it:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": "~/.claude/hooks/block-danger.sh" }]
}
]
}
}
A pattern list like this is a safety net, not a sandbox. For hard rules, use Claude Code's permission settings as well.
4. Format every file Claude edits
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path // empty' | xargs -I {} npx prettier --write --ignore-unknown \"{}\""
}
]
}
]
}
}
Put this one in the project's .claude/settings.json, so it only runs where Prettier is set up.
5. Start each session knowing what's changed
{
"hooks": {
"SessionStart": [
{
"hooks": [
{ "type": "command", "command": "cd \"$CLAUDE_PROJECT_DIR\" && git status --short && git log --oneline -5" }
]
}
]
}
}
Whatever a SessionStart hook prints is added to Claude's context, so Claude starts the session knowing which files are changed and what was committed last.
When a hook doesn't work
- Check it's loaded: type
/hooksin Claude Code. - Check the spelling: event names are case-sensitive (
PreToolUse, notpreToolUse), and the settings file must be valid JSON. - Trust the folder: hooks in a project's settings only run after you trust that folder.
- Read the log: start Claude with
claude --debug; the log shows each hook, its exit code and how long it took. - Run it by hand: pipe sample JSON into your script, for example
echo '{"tool_input":{"command":"rm -rf /"}}' | ~/.claude/hooks/block-danger.sh; echo $?.
A word on safety
Command hooks run with your full user permissions, without asking. Read the hooks in a project's .claude/settings.json before you trust a repository you didn't write, quote variables ("$VAR"), and use full paths in commands.
Hooks without editing JSON
If what you want is to know when sessions finish or need you, Doki sets up the Stop, Notification, UserPromptSubmit and SessionEnd hooks with one click and removes them when you disconnect. A desktop pet walks over and says which session it was and what Claude did. Its hook only talks to Doki on your own Mac and gives up after two seconds. To compare it with other tools, see Claude Code notification tools compared.
Questions
Where do Claude Code hooks go?
In a settings file, under "hooks": ~/.claude/settings.json for all your projects, .claude/settings.json to share them with a project's team, or .claude/settings.local.json for one project without committing them.
Why doesn't my Stop hook run when Claude asks for permission?
Because Claude hasn't finished; it's waiting. Use a Notification hook with the permission_prompt matcher for that.
How do I block a command with a hook?
Use PreToolUse, exit with code 2, and write the reason to stderr. Exit code 0 doesn't approve the command; it still goes through your normal permission rules.
Do hooks slow Claude Code down?
Claude waits for a hook to finish, and matching hooks run in parallel. A hook that shows a banner returns at once; one that waits on a slow server holds Claude up, so give those a short timeout.