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 7, 20263mWatch on YouTube ↗

EVERY SPOKEN WORD

  1. 0:000:02

    Intro

    1. SP

      [upbeat music]

  2. 0:020:33

    What hooks are in Claude Code: deterministic automation

    1. SP

      Hooks let you run commands at different points in Claude Code's lifecycle. The key difference between hooks and everything else we covered is that hooks are deterministic. They always run. So put it this way, you can tell Claude in your claude.md file to run prettier after every file edit, and most of the time it will do that, but sometimes it won't. It's not perfect. But a

  3. 0:330:48

    Practical use cases: formatting, compliance logging, safety blocks, notifications

    1. SP

      hook makes it happen every single time with no exceptions. Common use cases could include auto-formatting after file edits, logging all executed commands for compliance, blocking dangerous operations like modifying production files, and sending yourself

  4. 0:481:03

    How hooks are configured: events, matchers, commands (settings.json)

    1. SP

      notifications when Claude finishes a task. Hooks are configured in your settings.json file. You pick an event, optionally set a matcher for which tools it applies to, and provide a command to run. User

  5. 1:031:18

    Lifecycle events overview: prompt submit, pre/post tool use, notification, stop

    1. SP

      prompt submit runs when you submit a prompt before Claude processes it. Pre tool use, which runs before a tool call. Post tool use runs after a tool call completes. Notification runs when Claude sends a notification. And stop runs when Claude finishes responding.

  6. 1:181:33

    Auto-formatting pattern: post-tool-use hook for edits

    1. SP

      The most common hook, auto-formatting after edits. You set a post tool use hook with a matcher of edit or multi-edit, right? So it fires whenever Claude

  7. 1:331:48

    Formatter selection by file type: Prettier, gofmt, Ruff, etc.

    1. SP

      modifies a file. The command checks the file extension and runs the appropriate formatter. This could be prettier for TypeScript, go format for Go, rough for Python, whatever your project uses.

  8. 1:482:03

    Blocking unsafe actions: pre-tool-use hooks with stdin JSON

    1. SP

      Pre tool use hooks can block tool calls before they execute. So your hook receives a tool name and input as JSON on stdin. If it exits with code two, the action is blocked, and the stderr message gets fed back to

  9. 2:032:13

    Exit codes as control flow: proceed vs block with feedback to Claude

    1. SP

      Claude as feedback so Claude knows why it was blocked and can adjust. Exit code zero means proceed. Exit code two means block. This is how you enforce hard rules.

  10. 2:132:23

    Examples of enforceable rules: production writes, rm -rf, main branch commits

    1. SP

      Block writes to a production config directory, block batch commands that contain RM-RF, block commits to main, whatever your team needs to be guaranteed, not suggested.

  11. 2:232:33

    Team-wide setup: project-level hooks in .claude/settings.json

    1. SP

      Hooks configured in .claude/settings.json are project level and can be checked into your repo. This means that your entire team gets

  12. 2:333:21

    Robust scripting: use Claude project dir environment variable

    1. SP

      the same hooks automatically. Use the Claude project dir environment variable in your commands to reference scripts stored in your project so they work regardless of Claude's current working directory. [upbeat music] Hooks gives you deterministic control over Claude Code's behavior. Use post tool use for auto-formatting and logging. Use pre tool use to block dangerous operations. Configure them in the /hooks or in settings.json, and check them into your repository so your team gets them too. If something needs to happen every time without fail, don't put it in a prompt. Put it in a hook. [upbeat music]

Episode duration: 3:21

Install uListen for AI-powered chat & search across the full episode — Get Full Transcript

Transcript of episode IkaPHiMDazM

Get more out of YouTube videos.

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