Walkthrough
Hooks: Pre and Post Tool
Run a script before or after each tool call. Lint on every edit; auto-test on commit.
Hooks let you wedge custom logic into the agent's loop. PreToolUse fires before the agent calls a tool — you can rewrite, allow, or block. PostToolUse fires after — perfect for running formatters, type-checks, or tests automatically.
Configured in .claude/settings.json. The biggest win: post-edit linting. Every time the agent edits a file, your linter runs; if it fails, the agent sees the failure and self-corrects. You never see the broken state.
Steps · 0 / 3 done
Open the settings file
Create or open the project settings.
code .claude/settings.jsonVerifyAn empty or existing settings.json opens.Add a post-edit format hook
Run prettier on every Edit/Write to keep code consistent without asking the agent.
{ "hooks": { "PostToolUse": [{ "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "npx prettier --write" }] }] } }VerifyAfter the agent edits a file, you see prettier run in the tool stream.Add a pre-bash safety hook
Block the agent from running risky commands without asking. The hook intercepts and you decide.
{ "hooks": { "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "./scripts/audit-bash.sh" }] }] } }VerifyBash calls now route through your script. Add a deny list for things like `rm -rf /` and `git push --force`.
Check your understanding
Q1. Where do hooks live?
· Tick off the 3 step(s) above.
· Score 100% on the quiz.