Skip to content
ClaudeClaude

Hooks in Claude Code

Hooks give you deterministic control over Claude Code's behavior at key lifecycle events. Learn how to auto-format after edits, block dangerous operations, and share hooks with your team. Take the full course: claude.com/courses

May 6, 20263mWatch on YouTube ↗

At a glance

WHAT IT’S REALLY ABOUT

Deterministic hooks automate, log, and block actions in Claude Code

  1. Hooks are deterministic lifecycle triggers that run every time, unlike prompt instructions that Claude may occasionally miss.
  2. Hooks are configured in a project's settings.json by selecting an event, optionally scoping with a tool matcher, and specifying a command to execute.
  3. Post-tool-use hooks commonly implement automatic formatting after file edits by running formatters based on file extension.
  4. Pre-tool-use hooks can enforce hard safety rules by blocking tool calls when a script exits with code 2 and returning a reason to Claude via stderr.
  5. Project-level hooks stored in .claude/settings.json can be checked into a repo so an entire team shares the same guaranteed behaviors across environments.

IDEAS WORTH REMEMBERING

5 ideas

Use hooks for guarantees, not suggestions.

Prompt instructions (e.g., “run prettier after edits”) may be skipped occasionally, but hooks always run at the configured lifecycle point with no exceptions.

Pick the hook event based on when you need control.

Use user-prompt-submit before Claude processes input, pre-tool-use to gate tool calls, post-tool-use to react after a tool finishes, notification on notifications, and stop when Claude completes its response.

Auto-formatting is best implemented as a post-tool-use hook.

Match on edit/multi-edit so the hook runs whenever files change, then route to the right formatter (Prettier, gofmt, Ruff, etc.) based on file extension.

Pre-tool-use hooks enable enforceable safety policies.

Your hook receives the tool name and JSON input on stdin; exit code 0 allows execution, while exit code 2 blocks it and returns an stderr message that Claude uses as corrective feedback.

Blocking rules can target high-risk actions precisely.

Examples include preventing writes to production config directories, stopping batch commands containing risky patterns like rm -rf, or blocking commits to main—rules your team wants guaranteed, not merely recommended.

WORDS WORTH SAVING

5 quotes

Hooks let you run commands at different points in Claude Code's lifecycle.

Unknown

The key difference between hooks and everything else we covered is that hooks are deterministic. They always run.

Unknown

But a hook makes it happen every single time with no exceptions.

Unknown

This is how you enforce hard rules.

Unknown

If something needs to happen every time without fail, don't put it in a prompt. Put it in a hook.

Unknown

Deterministic vs prompt-based behaviorHook lifecycle events (submit, pre/post tool use, notification, stop)Tool matchers (edit, multi-edit)Auto-formatting after editsCompliance logging of executed commandsBlocking dangerous operations with exit codesRepo-wide, team-shared configuration via .claude/settings.json

High quality AI-generated summary created from speaker-labeled transcript.

Get more out of YouTube videos.

High quality summaries for YouTube videos. Accurate transcripts to search & find moments. Powered by ChatGPT & Claude AI.

Add to Chrome