promptgarten 🌱
Guide●●○4 min Β· +30 XP

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?

Share:𝕏inπŸ’¬

SOURCES

RELATED TOPICS

Claude Code: Anthropic's CLI Agent ●●○Slash Commands, Hooks & More: Steering Claude Code ●●○Guardrails for Autonomous Agents ●●●