Lesson 458 of 2116
Hooks: Automating Reactions To Tool Calls
Hooks let you run scripts before or after Claude Code does anything. They're how you turn 'guidance' into 'enforcement' — or how you debug what the agent is doing.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1What hooks are
- 2hook
- 3PreToolUse
- 4PostToolUse
Concept cluster
Terms to connect while reading
Section 1
What hooks are
A hook is a small script Claude Code runs at a defined point in its lifecycle — before a tool call, after a tool call, when a session starts, when it stops. The hook can read what the agent is about to do, transform it, log it, or block it. Hooks are how you turn project rules from optional suggestions into enforced behavior.
The lifecycle events
Compare the options
| Event | Fires when | Use it for |
|---|---|---|
| PreToolUse | Before any tool call | Block, transform, or log |
| PostToolUse | After any tool call | Validate result, audit |
| UserPromptSubmit | When you submit a message | Inject context, log queries |
| SessionStart | On `claude` start | Pull in project state |
| Stop | Session end | Cleanup, summary, audit log |
Three hooks worth building
- 1PreToolUse blocking destructive bash: refuse `rm -rf`, `git push --force`, `db:reset` unless explicit confirmation
- 2PostToolUse format-on-edit: run Prettier or Biome after every Edit so files always land formatted
- 3UserPromptSubmit context injection: pre-pend the result of `git status` so every prompt has fresh repo state
A minimal hooks block. The matcher decides which tool calls fire the hook; the command runs the script.
{
"hooks": {
"PreToolUse": [
{
"matcher": { "tool": "Bash" },
"command": "./scripts/block-destructive.sh"
}
],
"PostToolUse": [
{
"matcher": { "tool": "Edit" },
"command": "pnpm prettier --write $CLAUDE_TOUCHED_FILE"
}
]
}
}Where hooks beat CLAUDE.md
CLAUDE.md says 'don't push to main directly.' A hook BLOCKS pushes to main, period. Anywhere the cost of a mistake is high enough that 'usually respects' isn't good enough, a hook is the right answer. The model is no longer the only line of defense.
Key terms in this lesson
The big idea: prompts persuade, hooks enforce. Build hooks for the rules you actually need to hold.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Hooks: Automating Reactions To Tool Calls”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 9 min
What Perplexity Is: Search-Augmented LLM, Not A Chatbot
Perplexity is built around the idea that every answer should cite its sources. Treating it like ChatGPT misses the point — and the reliability gap that comes with it.
Creators · 10 min
Spaces: Building Team Knowledge Bases In Perplexity
Spaces are Perplexity's project containers — system prompts, files, and shared chat history. They turn the search engine into a research workspace.
Creators · 10 min
Comet Browser: What It Does That Atlas And Operator Don't
Comet is Perplexity's full browser with a research-native sidebar and an action-capable agent. It plays differently than ChatGPT Atlas or Operator — and the differences matter.
