Scroll AI Now

Claude Code hooks explained: events, examples, safety

What Claude Code hooks are, the 34 hook events in the docs, step by step setup for three useful hooks, and the safety checks to run first.

Short answerClaude Code hooks are handlers, usually shell commands, that run at set points, such as before a tool call or when Claude finishes responding. You usually add them to a settings file. They make certain actions always happen. On events that allow blocking, exit code 2 from a hook stops the action. Command hooks run with your full user permissions, so review them first.

What are Claude Code hooks?

Anthropic's guide Automate actions with hooks defines hooks as "user‑defined shell commands" that Claude Code runs at set points in its lifecycle.

The point is control. The guide says hooks mean "certain actions always happen rather than relying on the LLM to choose to run them".

The Hooks reference lists five handler types: command, http, mcp_tool, prompt and agent. The guide says most hooks use the command type, which runs a shell command.

The reference says "Agent hooks are experimental and may change". The guide adds: "For production workflows, prefer" command hooks.

Which hook events does Claude Code have?

We checked the Hooks reference on 27 November 2026 and counted 34 events in its summary table. Check the page before you build, since the list can change.

Here are the events this guide uses, plus a few you will meet often. The middle column is the reference's own wording.

EventWhen it fires (Hooks reference)
SessionStart"When a session begins or resumes"
UserPromptSubmit"When a prompt is submitted, before Claude processes it. Also fires on turns Claude Code starts on its own"
PreToolUse"Before a tool call executes. Can block it"
PermissionRequest"When a tool call needs a permission decision"
PostToolUse"After a tool call succeeds"
Notification"When Claude Code sends a notification"
Stop"When Claude finishes responding"
PreCompact"Before context compaction"
SessionEnd"When a session terminates"

The three hooks below use Notification, PostToolUse and PreToolUse.

Where do you put a hook?

The Hooks reference says "hooks are defined in JSON settings files". Plugins, skills and subagents can define hooks too. Where a hook lives sets its reach, per the Hooks reference table.

LocationScopeShared?
~/.claude/settings.jsonAll your projectsNo, local to your machine
.claude/settings.jsonSingle project"Yes, can be committed to the repo"
.claude/settings.local.jsonSingle projectNo, "gitignored when Claude Code saves a setting to it"
Managed policy settingsOrganization‑wideYes, admin‑controlled
Plugin hooks/hooks.jsonWhen plugin is enabledYes, bundled with the plugin
Skill frontmatter"See Hooks in skills and agents""Yes, defined in the skill file"
Subagent frontmatterWhile that subagent is running"Yes, defined in the subagent file"

When two settings files set the same key, the Claude Code settings page gives the order, highest first: "managed settings, command line, project local, shared project, user". For hooks, the reference says entries "merge across settings levels rather than replacing each other".

The settings page also says Claude Code reloads settings files when they change, so it applies "most edits to the running session without a restart", hooks included.

Each entry has a matcher that filters when it fires. The reference says a regex matcher "succeeds on a match anywhere in the value", so Edit.* also matches NotebookEdit.

Write ^Edit$ when you want an exact match. Then type /hooks in Claude Code. The reference says it opens "a read‑only browser for your configured hooks".

How do exit codes control Claude?

The catch: exit 1 without valid JSON output does not block, unless the hook is set to block on failure. The reference flags this "even though 1 is the conventional Unix failure code".

Some events cannot be blocked. The reference's per event table says exit 2 on PostToolUse "Shows stderr to Claude; the tool already ran".

On Stop, exit 2 "Prevents Claude from stopping, continues the conversation". For SessionStart, the guide says "exit 2 shows stderr to the user and execution continues".

Hook 1: how do I get a notification when Claude needs me?

The guide's first example uses the Notification event. Claude Code fires it "when Claude is waiting for input or permission".

It goes in ~/.claude/settings.json, which the reference table scopes to all your projects.

  1. Open ~/.claude/settings.json in your editor.
  2. Add a hooks block with a Notification entry, an empty matcher and one hook of type command.
  3. Set the command for your system. On Linux the guide uses "notify‑send 'Claude Code' 'Claude Code needs your attention'".
  4. On macOS the guide uses osascript, and on Windows powershell.exe. Take those commands from the guide's Get notified when Claude needs input section.
  5. Type /hooks to confirm the hook is listed under Notification. Then ask Claude to do something that needs permission and switch away from the terminal.

If nothing appears on macOS, the guide says to run its test command once in Terminal so Script Editor shows up in your notification settings. Then open System Settings, then Notifications, and turn on Allow Notifications for Script Editor.

Hook 2: how do I auto-format every file Claude edits?

This hook uses PostToolUse with the matcher Edit|Write. The guide says "it runs only after file‑editing tools".

  1. Install jq if you do not have it. The guide's install commands are brew install jq on macOS and "apt‑get install jq on Debian and Ubuntu".
  2. The command also runs Prettier through npx. The guide does not give install steps for Prettier, so check that npx runs in your terminal first.
  3. Open .claude/settings.json in your project root. The Hooks reference table says this file "can be committed to the repo", so your team can share the hook.
  4. Add a PostToolUse entry with the matcher Edit|Write and one hook of type command.
  5. Take the command from the guide's Auto‑format code after edits section. It pulls the edited file path out with jq and passes it to Prettier.
  6. Test it the guide's way: "ask Claude to add a line with single‑quoted strings to a JavaScript file". With default settings, Prettier rewrites them to double quotes.

Do not expect a message. The guide says "When the hook succeeds, Claude Code shows nothing in the conversation".

Hook 3: how do I block edits to protected files?

The guide's example stops the Edit and Write tools from changing files such as .env, package‑lock.json or anything in .git/.

  1. Save the guide's script at .claude/hooks/protect-files.sh. Per the guide, it "checks the target file path against a list of protected patterns and exits with code 2 to block the edit".
  2. Make it executable on macOS and Linux with chmod +x .claude/hooks/protect-files.sh. The guide says "Hook scripts must be executable for Claude Code to run them".
  3. In .claude/settings.json, add a PreToolUse entry with the matcher Edit|Write.
  4. Set its command to run the script through $CLAUDE_PROJECT_DIR, as the guide shows.
  5. Consider adding onFailure set to block on this hook. The reference warns that otherwise "A policy hook with a wrong path or a crashing script then lets everything through". The reference says onFailure "Requires Claude Code v2.1.295 or later".
  6. Test it by asking "Claude to add a comment to your .env file". Then open .env and check it really is unchanged.

When it works, the guide says Claude "receives feedback explaining why the edit was blocked, so it can adjust its approach".

If the edit went through, look for a hook error notice in the transcript. The reference says a hook that fails to start is a non‑blocking error. Then "The action goes ahead" unless the hook is set to block on failure.

The catch: the guide states "Claude can still change the protected files with shell commands".

If you must see every change, the guide suggests a Stop hook that "scans the working tree once per turn".

Are Claude Code hooks safe?

The Hooks reference's security warning says: "Command hooks execute shell commands with your full user permissions". It adds: "They can modify, delete, or access any files your user account can access".

The same warning ends: "Review and test all hook commands before adding them to your configuration". Treat a hook script like any program you install.

Cloned repos need care. The reference says Claude Code "checks workspace trust before it runs any hook from a settings file".

But in a -p or SDK session, Claude Code "never shows the dialog and treats the folder as trusted". Hooks committed to that repo can then run.

For claude -p over a repo you did not write, the reference gives three steps. Review its .claude/ settings files, start with --bare, or turn hooks off for that run with --settings.

How to limit what a hook can do, from the reference's best practices list:

To pause all hooks, set disableAllHooks to true in your settings. The guide says hooks in managed settings "still run unless disableAllHooks is also set there". The reference notes "There is no way to disable an individual hook while keeping it in the configuration".

Which event should your hook use?

A quick decision rule, built from what the two Anthropic pages say each event can do:

Where to go next

New to Claude Code itself? Start with Claude Code for non developers.

For more to add to your setup, see the best Claude Code skills and the Claude Code repos page. Read any hook script in a repo before you copy it, since it runs with your permissions.

To find new tools to try, see today's agent skills in the feed. Scroll AI Now is a free learning feed. It does not run your automations or agents.

Questions

Why does my Claude Code hook not block when it exits with 1?

The Hooks reference counts exit 1 without valid JSON output as an error that lets the action go ahead. Hooks configured to block on failure are the exception. Use exit 2 to block.

Why does my protection hook let edits through?

Check that the script is executable with chmod +x, since the guide says hook scripts must be executable to run. The reference says a hook that fails to start is a non‑blocking error, so the action goes ahead unless onFailure is set to block.

Can a hook undo an edit Claude already made?

No. The guide says PostToolUse hooks "can't undo actions since the tool has already executed". Use a PreToolUse hook to block the edit first.

Do hooks in a cloned repo run automatically?

The Hooks reference says an interactive session holds back settings file hooks "until you accept the workspace trust dialog" for that folder or a parent folder whose trust covers it. In a -p or SDK session, it treats the folder as trusted, so committed hooks can run.

How do I turn Claude Code hooks off?

Set disableAllHooks to true in your settings file, or pass it with the --settings flag for one run. User or project settings can't turn off hooks an admin set through managed policy.

Can a hook use an AI model instead of a script?

Yes. The Hooks reference says prompt hooks "send a prompt to a Claude model for single‑turn evaluation", and agent hooks spawn a subagent that can use tools. It adds that "Agent hooks are experimental and may change".

Try it in your browser

Today's AI news, one card per swipe.
Free to start.

Open the web app