# Claude Code hooks

> Configure PreToolUse and PostToolUse hooks in settings.json to auto-format files and block dangerous commands.

Page: https://videotomd.com/b/IkaPHiMDazM/
Tool: Claude Code · Tags: hooks · 422 words, about 549 tokens
Pills: free · checked 2026-10-15 · Claude Code · file tools · shell · on demand · frontier · coding · starting out · building

## Recipe

- Kind: workflow
- Stage: starting out, building
- Loads: on demand
- Works in: Claude Code
- Needs: file tools, shell
- Bring: nothing
- Cost: free
- Model: frontier
- Tested in: not yet
- Checked: 2026-10-15
- For: coding

## Similar
- [A hook without hooks: the git pre-commit check](https://videotomd.com/b/text-git-pre-commit-check-any-harness/): Pick it when more than one coding agent touches the repo, because git runs the check no matter which harness made the change, and a missed hook config doesn't skip it.
- [Claude Code hooks instead of prompt rules](https://videotomd.com/b/AP7c9YTcaw8/): Pick it when the goal is turning repeated prompt rules into hooks across the session lifecycle instead of wiring a single formatter and a single block check.

## Put it here

Loads on demand: paste or attach it when the task comes up. To have it every session, put it where your tool reads instructions:

- Claude Code: Save as CLAUDE.md in the project root, or .claude/CLAUDE.md. Other agents share the repo: put the shared rules in AGENTS.md and make CLAUDE.md one line, @AGENTS.md. Commit it. Start a new session: the file is read at launch. Reload: new session (or /memory to open the files). Check it loaded: /context lists the Memory files it read; /skills, /agents, /hooks list the rest; the InstructionsLoaded hook event fires once per loaded instruction file. Source: https://code.claude.com/docs/en/memory (checked 2026-10-12).

## Access

Free, no signup. The complete file follows.

---

# Claude Code hooks

## Before you start
- Needs: Claude Code, a shell supported by your machine, and the formatter or validator to run.

## Steps
1. In `.claude/settings.json`, add a `PostToolUse` command hook matching `Edit|Write`. Point it at a script that reads the hook JSON from stdin and formats the changed file.
2. For blocking checks, use `PreToolUse`; return exit code `2` and write the reason to stderr when refusing an action.
3. Set a timeout in seconds. Use quoted `$CLAUDE_PROJECT_DIR` paths in POSIX scripts.
4. Test an edit, an allowed action and a blocked action; confirm the expected formatting and refusal before committing the settings and scripts.

## Your steps
A guide, not a guarantee: a recipe is better with your own steps in it. Add what you changed and why below; your AI keeps this section with the recipe and reads it first next time.
- (your first change, and why)

## Warnings
- Hooks execute with your user permissions; inspect shared scripts before enabling them.
- Only exit code 2 blocks the call; exit 1 or any other non-zero code is a non-blocking error, so a crashed guard lets the action go through.
- An Edit|Write matcher never sees files written by Bash commands, so those files go unformatted and unchecked unless you also match Bash.

## Harden
- Make the PreToolUse script fail closed: catch its own errors, such as unreadable stdin JSON or a missing validator, and exit 2 with the reason on stderr. Costs: A bug in the script blocks every matching tool call until you fix it.
- In the PostToolUse script, take the file path from the stdin JSON and skip anything that doesn't resolve under "$CLAUDE_PROJECT_DIR" before running the formatter. Costs: A few extra lines of path checks, and files edited outside the project are never formatted.
- After every change to .claude/settings.json, open the /hooks menu and check that each event, matcher and script path is registered the way you expect. Costs: One more manual step each time the settings or scripts change.

## Similar
- [A hook without hooks: the git pre-commit check](https://videotomd.com/b/text-git-pre-commit-check-any-harness/): Pick it when more than one coding agent touches the repo, because git runs the check no matter which harness made the change, and a missed hook config doesn't skip it.
- [Claude Code hooks instead of prompt rules](https://videotomd.com/b/AP7c9YTcaw8/): Pick it when the goal is turning repeated prompt rules into hooks across the session lifecycle instead of wiring a single formatter and a single block check.
