Video summary

Hooks in Claude Code

Main summary

Key takeaways

Technology

Key technological concept: Deterministic “hooks” in Claude Code

  • Hooks let you run commands at specific points in Claude Code’s lifecycle.
  • Unlike “regular” prompt instructions or other behaviors, hooks are deterministic: they always run with no exceptions.
    • Example: telling Claude in claude.md to run Prettier after edits may be inconsistent, but a hook will run every time.

Common use cases highlighted

  • Auto-formatting after file edits (e.g., run different formatters based on file type/extension).
  • Logging all executed commands for compliance/audit purposes.
  • Blocking dangerous operations (enforce hard safety rules).
  • Sending notifications when Claude finishes a task.

How hooks are configured

  • Hooks are configured in settings.json (also mentioned as .Claude/settings.json).
  • Configuration includes:
    • Selecting an event
    • Optionally setting a matcher to choose which tool(s) the hook applies to
    • Providing the command to run

Hook events described

  • User prompts: runs when a user submits a prompt before Claude processes it.
  • Pre-tool use: runs before a tool call.
  • Post-tool use: runs after a tool call completes.
  • Notification: runs when Claude sends a notification.
  • Stop: runs when Claude finishes responding.

Tutorial/guide examples

  1. Auto-format after edits (most common hook)

    • Use a post-tool use hook with a matcher like edit or multi-edit.
    • The command checks file extension and runs the appropriate formatter:
      • prettier for TypeScript
      • go format for Go
      • ruff/rough for Python (as stated)
  2. Blocking operations with pre-tool use

    • A pre-tool hook can prevent tool execution before it happens.
    • The hook receives:
      • the tool name
      • the input as JSON on stdin
    • Exit codes control behavior:
      • Exit code 0 → proceed
      • Exit code 2 → block the action
    • The STDERR message is returned to Claude as feedback so Claude can understand why it was blocked and adjust.

Team/project-level sharing

  • Hooks in .Claude/settings.json are project-level, so they can be checked into the repo.
  • This ensures the whole team gets identical enforcement.
  • Suggested practice: use the Claude project dir environment variable in commands so scripts run correctly regardless of Claude’s current working directory.

Main takeaway / recommendation

  • If something must happen every time (formatting, compliance logging, safety enforcement), put it in a hook, not in a prompt.

Main speakers/sources

  • Unspecified (no named speaker provided in the subtitles).

Original video