Category · Development and code
What are hooks in Claude Code?
Hooks are scripts that Claude Code runs at precise moments. For example, a hook can block a tool call before it runs, or run the tests after every edit. Another one stops the agent from finishing while the tests fail.
By Laurent Poupet, Claude trainer and artificial intelligence architect · Updated
How it works
- A hook receives the tool call in JSON (JavaScript Object Notation), with the tool name and its arguments. It decides by reading these fields, then answers with its exit code or with a document.
- Other moments exist, such as submitting a request or starting a session. A startup hook can add context, for example the target environment or the rules of the day.
- A hook that blocks wins over allow rules, but a hook that allows never lifts a deny rule. Denial always comes first.
The trap to avoid
The trap is a slow or fragile hook, run on every tool call. Keep it short, with a maximum timeout set, and test it like production code.
A concrete example
A hook placed before every tool call reads the command Claude Code is about to run. If it sees a folder deletion outside the project, it answers with exit code 2 and the command does not run. A second hook, placed after every file change, formats the code without you having to ask.
What is the difference with an instruction written in CLAUDE.md?
| Criterion | Hooks in a coding assistant | an instruction written in CLAUDE.md |
|---|---|---|
| Nature | A script that runs | A text the model reads |
| Strength | Blocks every time | The model follows it most of the time |
| Good fit | A ban that must always hold | A habit, a convention |
Related terms
- Claude CodeDevelopment and code
- ClaudeModels and how they work
- Function callingTools and protocols
Where this is taught
Track 2 · Claude Code: the augmented developer
Automate with Hookslesson 6 · 1 hourTrack 3 · Skills, agents and connectors
Advanced hooks — Deterministic automationlesson 2 · 1 hour
Lesson content (step-by-step instructions, exercises with solutions, quizzes) is for students only, with an access code received after the first lesson.