Hook
A hook is an action you define that Claude Code runs automatically at a fixed point in its work: before it uses a tool, after a file is edited, when a session starts or when Claude finishes a response. It is usually a terminal command written in the settings files. Unlike instructions, a hook is guaranteed to fire, whatever the model decides.
An automatic action that always starts at a particular moment, for example after every edit. Like an alarm that’s guaranteed to ring, without anyone having to remember.
How it shows up in Claude Code
Hooks go in settings.json: ~/.claude/settings.json for you, .claude/settings.json for the project, shared with the team. You choose the event (for example PostToolUse, after a tool has run, or PreToolUse, before it), a filter called matcher (for example only Edit|Write) and the command to run. The command receives the details of the action as JSON. The /hooks command shows which hooks you have and where each one comes from.
A PreToolUse hook can stop an action: if it ends with exit code 2, the tool doesn't run. That's how you block, say, any command that deletes files, even if Claude wants to run it.
The key difference: a rule in CLAUDE.md is a request Claude might forget, while a hook is enforced by Claude Code itself, every single time.
Example
Automatic formatting of the file that was just edited, as in the docs:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [{ "type": "command", "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write" }]
}
]
}
}
The command uses jq to read the file path from the JSON the hook receives and formats only that file. From now on, after every file Claude changes, the code tidies itself up without you asking.
Common mistakes
- Using exit code 1 to block something. For most events, 1 doesn't block and the action goes ahead; to block, use 2.
- Putting slow commands in a hook that runs after every edit, which drags down all your work.
- Running hooks copied from the internet without reading them. They run on your computer, with your rights.
Related terms and where you learn it
A hook often relies on a formatter or on jq, which reads the incoming JSON. Unlike CLAUDE.md, a hook doesn't depend on Claude remembering anything. Hooks can also be shipped in plugins. It's all covered in the lesson “What is a hook?” in module 07 · Hooks: automatic actions.
How it appears in the course
A hook is an action that starts by itself, every time something specific happens.
Introduction · Module 07, Hooks: automatic actions
A hook (like a coat hook: something you can hang an action on) is a command that Claude Code runs automatically at a particular moment.
What is a hook? · Module 07, Hooks: automatic actions
Every hook is tied to an event: a specific moment in Claude’s work.
When can a hook start? · Module 07, Hooks: automatic actions
Learn Claude Code from zero, in plain English
12 modules with 1–2 minute lessons, which you can also do on your phone. The first two modules are free.