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 Doku: Hooks (Referenz) โ code.claude.com
- Claude Code Doku: Get Started with Hooks โ code.claude.com