Loading...
Loading...
In Lesson 3 you learned about slash commands — workflows you trigger manually. Hooks are the opposite: they run automatically in response to events. You never invoke a hook. It fires on its own when its trigger condition is met.
This distinction matters. Commands are on-demand tools you reach for when you need them. Hooks are guardrails and automation that run whether you remember them or not. Together, they form a complete automation layer: commands handle the "I want to do X" cases, hooks handle the "whenever X happens, always do Y" cases.
Claude Code supports several hook trigger points. Each fires at a specific moment in the tool execution lifecycle:
Hooks are configured in .claude/settings.json under the hooks key. Each hook specifies a trigger type, an optional matcher (to filter which tools it applies to), and a command to execute:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "node .claude/hooks/validate-bash.js",
"description": "Block dangerous bash commands"
}
],
"SessionStart": [
{
"command": "cat .claude/memory.md",
"description": "Load memory on session start"
}
]
}
}The matcher field is optional. When present, the hook only fires for that specific tool. When absent, it fires for all tools of that trigger type. The command field runs a shell command — it can be a script, a CLI tool, or any executable.
The highest-value hooks are safety hooks — they prevent Claude from doing things that could damage your project:
rm -rf, git push --force, DROP TABLE, and other dangerous patterns.env, package-lock.json, or production configsmain or production branchesSafety hooks give you confidence to let Claude work autonomously. They act as programmatic guardrails that enforce your team's policies regardless of what instructions Claude receives.
Beyond safety, hooks can make your AI brain smarter over time:
Intelligence hooks turn your AI brain into a learning system. Every session generates data that makes the next session more effective.
Set up these three hooks to protect and enhance your codebase:
rm -rf /, git push --force, or DROP DATABASE. Write a simple script that reads stdin, checks for these patterns, and exits with code 1 to block execution..claude/recovery.md so nothing is lost during context compaction.Want pre-configured hooks that work out of the box?
The AI Brain Pro package includes 12 production-tested hooks covering safety, intelligence, context management, and automated quality checks.
View Pricing