A2X← Back to A2XWorkshop resource
Claude Code Hooks interactive
A visual, build-your-own-pace guide

Hooks turn Claude Code from "please remember" into "it always happens."

A hook is a small command you register that fires automatically at set moments in a Claude Code session โ€” formatting files, blocking risky commands, or sending you a ping โ€” without depending on the model to choose to do it.

your
rules
โšก
๐Ÿ›ก๏ธ
๐ŸŽจ
๐Ÿ””

Pick how you want to use this. You can switch any time from the top bar.

01Beginner

Why hooks exist

Claude Code can edit your files and run terminal commands on its own. Often you want something to happen every single time โ€” reformat each file it touches, refuse to run destructive commands, ping you when it needs input.

You could just ask in your instructions: "always format after editing." But that's a suggestion the model might forget. A hook is a rule wired into the tool itself, so it runs deterministically โ€” not when the model feels like it.

Prompting
"Please format my code." โ†’ maybe runs, maybe doesn't.
โ†’
A hook
Format command fires on every edit, automatically.
02Beginner

When can a hook fire?

A Claude Code turn moves through stages, like a track. Hooks attach to specific points on that track. The orange points below are the moments most people hook into first.

A point you can hook Internal stage

The two you'll use constantly: PreToolUse fires before Claude runs a tool and can block it; PostToolUse fires after a tool succeeds. (In the Explore tab you can click each point.)

03Beginner

The anatomy of one hook

Every hook is just three answers: when, which, and what.

When

Event

The lifecycle moment โ€” e.g. PostToolUse.

Which (optional)

Matcher

Filter by tool name โ€” e.g. Edit|Write. Leave empty to fire on all.

What

Action

The command to run โ€” usually a shell command.

// run Prettier after Claude edits any file { "hooks": { "PostToolUse": [{ // โ† Event "matcher": "Edit|Write", // โ† Matcher "hooks": [{ "type": "command", "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write" // โ† Action }] }] } }
04Beginner

How a hook talks back

When a hook fires, Claude Code hands it a JSON description of what's happening โ€” on standard input. The hook does its work, then replies with an exit code.

1 ยท stdin
Hook receives JSON: which tool, the arguments, the session.
โ†’
2 ยท script runs
Your command inspects it and decides.
โ†’
3 ยท exit code
The number it returns tells Claude what to do.
exit 0

No objection. The action proceeds normally. (On a few events, anything printed to stdout is added to Claude's context.)

exit 2

Block it. Whatever you write to stderr is fed back to Claude as the reason, so it can adjust.

Need finer control than block-or-allow? Exit 0 and print a JSON object instead โ€” for a PreToolUse hook you can set permissionDecision to "allow", "deny", or "ask".

05Beginner

Watch one work, end to end

Here's a guardrail hook: "on any Bash command, block it if it contains rm -rf." Trace what happens when Claude tries to delete a folder.

#!/bin/bash โ€” block-rm-rf.sh INPUT=$(cat) # read the JSON from stdin CMD=$(echo "$INPUT" | jq -r '.tool_input.command') if echo "$CMD" | grep -q "rm -rf"; then echo "Blocked: rm -rf is not allowed here" >&2 # stderr โ†’ Claude exit 2 # block fi exit 0 # otherwise, carry on

PreToolUse fires โ†’ matcher Bash matches โ†’ script sees rm -rf โ†’ exits 2 โ†’ Claude Code cancels the command and shows Claude the reason. Try the full interactive version in Explore โ†’ Simulator.

06Beginner

Where hooks live

Hooks are defined in plain JSON settings files. Where you put one decides its scope. To browse what's active, run /hooks inside Claude Code (it's read-only โ€” you edit the files directly, or just ask Claude to add the hook for you).

FileScopeShareable?
~/.claude/settings.jsonAll your projectsNo โ€” local to your machine
.claude/settings.jsonOne projectYes โ€” commit it to the repo
.claude/settings.local.jsonOne projectNo โ€” gitignored
07Intermediate

Beyond shell commands

A hook's type doesn't have to be a shell command. There are five, and the last two let a model make the call when a rule needs judgment, not just pattern-matching.

command

Run a shell command. The default and most common.

http

POST the event JSON to a URL โ€” e.g. a shared team audit service.

mcp_tool

Call a tool on an already-connected MCP server.

prompt

Ask a Claude model (Haiku by default) for a yes/no decision.

agent

Spawn a subagent that can read files and run commands to verify, then decide. Experimental.

When several hooks match the same event, they all run in parallel. For permission decisions the most restrictive answer wins: deny beats ask beats allow.

08Intermediate

Safety & the gotchas

Hooks are powerful precisely because they run automatically โ€” which is also why a few things deserve care.

  • ๐Ÿ”
    They run with your credentials. A hook executes automatically in your environment โ€” a malicious one could exfiltrate data. Review any hook before you register it.
  • โ†ฉ๏ธ
    PostToolUse can't undo. It runs after the tool already executed. To prevent an action, use PreToolUse instead.
  • ๐Ÿ›‘
    Stop fires whenever Claude finishes responding โ€” not only at "task done." It also has a safety cap: after blocking 8 times in a row, Claude Code overrides it.
  • ๐Ÿ”ค
    Matchers are case-sensitive. If a hook never fires, check the matcher matches the tool name exactly, and confirm with /hooks.

That's the whole mental model. Head to Explore to poke at the lifecycle and run the simulator.

The session lifecycle

Each orange point is an event you can hook. Click one to see when it fires and whether it can block an action.

Hookable event Internal stage
โ†‘ Click any orange point on the track to inspect it.
Hook simulator

Pick something for Claude to attempt, toggle which guard hooks are active, then run it and watch the pipeline decide.

1 ยท What Claude tries to do
2 ยท Active hooks
Pick an action and at least one hook (or none) to see the difference.
Event reference

The events worth knowing, green for the everyday set and amber for intermediate. Click for detail.

โ†‘ Click any event card.
Built from the official Claude Code hooks documentation. Always review a hook before registering it.