Hooks in Claude Code
Hooks let you automatically run your own commands at specific points in the Claude Code workflow β e.g. before every tool call.
What a hook is
A hook is a shell command you define, which Claude Code automatically runs at a specific point in the workflow β not optionally decided by the model, but deterministically triggered by the system. This lets you enforce rules that shouldn't depend on a model's "discretion."
Important hook events
- PreToolUse: runs before a tool is executed β can even block the execution
- PostToolUse: runs after a tool has been executed
- UserPromptSubmit: runs when you submit a message
- SessionStart / Stop: runs at the start or end of a session
What hooks are used for
- Guardrails: automatically block dangerous commands (e.g.
rm -rf) before they run - Automatic formatting or linting after every file change
- Logging and notifications for certain actions
- Custom validation before an agent is allowed to continue
Configuration
Hooks are configured in Claude Code's settings.json β per project or globally β making them reproducible independent of the current prompt, because they run outside the model.
EXAMPLE
A PreToolUse hook checks every Bash command for the pattern 'rm -rf' and aborts execution with an error message before the command even runs.
QUICK QUIZ
What sets a hook apart from a normal instruction in the prompt?
SOURCES
- Claude Code docs: Hooks (reference) β code.claude.com
- Claude Code docs: Get Started with Hooks β code.claude.com