promptgarten ๐ŸŒฑ
๐ŸŒ ES
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?

SOURCES

RELATED TOPICS

Claude Code: Anthropic's CLI Agent โ—โ—โ—‹Slash Commands, Hooks & More: Steering Claude Code โ—โ—โ—‹Guardrails for Autonomous Agents โ—โ—โ—