Hameau
← All articles

5 December 2026

Claude Code hooks explained, with copy-paste examples

Hooks are Claude Code's most underrated feature. They are commands Claude Code runs at specific moments of its life: before using a tool, after, when it needs you, when it has finished responding… Enough to get notified, block a dangerous action or wire Claude Code into your own tools.

What a hook looks like

A hook lives in a settings file, on three levels: the event, a filter (matcher) and the command to run.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/check.sh" }
        ]
      }
    ]
  }
}

Here, before every shell command (Bash) Claude wants to run, the check.sh script runs. With no matcher (or "*"), the hook fires every time.

Where to put them

  • ~/.claude/settings.json: for all your projects;
  • .claude/settings.json: for the project, shared with your team through git;
  • .claude/settings.local.json: for the project, but just for you.

The /hooks command, inside Claude Code, shows every active hook and where it comes from.

The most useful events

Claude Code offers about thirty. The ones we use most:

Event When
SessionStart a session starts or resumes
UserPromptSubmit you send a message, before it is processed
PreToolUse before a tool is used (and can block it)
PostToolUse after a tool succeeded
Notification Claude Code needs you (permission, idle)
Stop Claude finished responding
SubagentStart / SubagentStop a subagent starts / finishes
PreCompact right before the context is compacted
SessionEnd the session ends

What your script receives

Every hook gets a JSON object on its standard input. It always includes the session id (session_id), the working directory (cwd), the transcript path (transcript_path) and the event name (hook_event_name). Then event-specific fields: for PreToolUse, the tool name (tool_name) and its parameters (tool_input); for Notification, the message and its type.

The CLAUDE_PROJECT_DIR environment variable gives the project root: handy to point at scripts versioned in the repository.

What your script answers

The exit code matters:

  • 0: all good, Claude carries on;
  • 2: blocking error. The action stops and whatever your script writes to stderr goes back to Claude as the reason (for events that can block, such as PreToolUse);
  • anything else: non-blocking error, Claude carries on and reports the problem.

For finer decisions, a script can also answer in JSON (allow, deny or ask for confirmation on a tool call, for example).

Three copy-paste examples

Get notified when Claude is waiting

A macOS notification as soon as a session needs you, in ~/.claude/settings.json:

{
  "hooks": {
    "Notification": [
      {
        "hooks": [
          { "type": "command", "command": "osascript -e 'display notification \"Claude Code is waiting for you\" with title \"Claude Code\"'" }
        ]
      }
    ]
  }
}

Keep Claude away from .env files

In .claude/settings.json, on the tools that write files:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/protect-env.sh" }]
      }
    ]
  }
}

And the .claude/hooks/protect-env.sh script (make it executable with chmod +x):

#!/bin/bash
file=$(jq -r '.tool_input.file_path // empty')
if [[ "$file" == *.env* ]]; then
  echo ".env files are protected: ask the human." >&2
  exit 2
fi
exit 0

Keep a log of the activity

A log of everything your sessions do, one line per tool call:

{
  "hooks": {
    "PostToolUse": [
      {
        "hooks": [{ "type": "command", "command": "jq -c '{t: now, session: .session_id, tool: .tool_name}' >> ~/.claude/activity.jsonl" }]
      }
    ]
  }
}

It's the same idea, richer, that Hameau uses: a small local service receives your sessions' events through hooks and turns them into residents of a 3D village, who light up when they work and wave at you when they're waiting for your answer.

Mind security

A hook is a shell command run with your full user permissions. Claude Code's documentation insists: review and test every hook before adding it, quote your variables, use absolute paths, and be wary of hooks in a repository you didn't write. In interactive mode, Claude Code only enables them once you've trusted the folder.


Sources: official Claude Code documentation, hooks reference and hooks guide.