Readwalk/claude Get it for Mac

Claude Code by voice

Claude Code hooks: what they are and how to set them up

Claude Code hooks run commands you define before tools run, after edits or when Claude finishes responding. Put them under "hooks" in a settings file such as ~/.claude/settings.json. Unlike instructions in CLAUDE.md, matching hooks run without Claude choosing to follow them. Use them to format code, block risky commands, play a sound or hear replies aloud.

What are Claude Code hooks?

A command hook is a shell command tied to a session event. The official hooks guide calls this "deterministic control": Claude Code runs the command when the event matches.

Other handler types are http, which POSTs event JSON to a URL; mcp_tool, which calls an MCP server tool; prompt, a single-turn Claude check; and agent, an experimental subagent with tools such as Read, Grep and Glob. Prompt and agent hooks suit decisions that need judgment. The examples below use command hooks.

Which hook events can you use?

Event When it fires
SessionStart A session begins or resumes
UserPromptSubmit You submit a prompt, before Claude processes it
PreToolUse Before a tool call; can block it
PermissionRequest A tool call needs a permission decision
PostToolUse After a tool call succeeds
Notification Claude Code sends a notification
Stop Claude finishes responding
SubagentStop A subagent finishes
PreCompact Before context compaction
SessionEnd A session ends

The basic lifecycle is SessionStart and SessionEnd per session, UserPromptSubmit and Stop per turn, and PreToolUse and PostToolUse per tool call, with PostToolUse requiring success.

The official hooks reference lists more events, including PostToolUseFailure, SubagentStart, ConfigChange, CwdChanged, FileChanged and WorktreeCreate.

Where do Claude Code hooks go in settings.json?

Choose the scope you need:

Hooks merge across settings files. If the same handler appears in more than one file, it runs once.

There are three nesting levels: the event, a matcher group containing a hooks array, and a handler with type, command and optional timeout in seconds:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "/path/to/lint-check.sh" }
        ]
      }
    ]
  }
}

Add entries to your existing hooks block if you have one. Don't create a second block.

How do matchers work?

For PreToolUse, PostToolUse and PermissionRequest, match the tool name: Bash, Edit|Write or mcp__.*, for example. Matchers are case-sensitive.

"*", "" or an omitted matcher matches everything. Values containing only letters, digits, _, -, spaces, , and | are exact names or lists. Other values are unanchored JavaScript regular expressions: Edit.* also matches NotebookEdit.

SessionStart matches startup, resume, clear, compact or fork. Notification matches types such as permission_prompt and idle_prompt.

UserPromptSubmit, Stop, TeammateIdle and some other events don't support matchers. They silently ignore one if you add it.

What does a hook receive, and how does it answer?

A command hook receives JSON on stdin. Common fields are session_id, transcript_path, cwd, permission_mode and hook_event_name. Tool events add tool_name and tool_input; UserPromptSubmit adds prompt; Stop and SubagentStop include last_assistant_message. Use jq to read fields.

The environment variable $CLAUDE_PROJECT_DIR points to the project root where the session started. Use it for script paths under .claude/hooks/.

Exit codes control what happens next:

For a policy that must block an action, use exit 2, not exit 1.

PreToolUse can return JSON with permissionDecision set to allow, deny, ask or defer:

{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "Use rg instead of grep for better performance"}}

Keep those fields inside hookSpecificOutput. At the top level, they're ignored. Matching hooks run in parallel; when PreToolUse decisions disagree, deny beats defer, defer beats ask, and ask beats allow.

Command hooks time out after 600 seconds by default; prompt hooks after 30, agent hooks after 60. Set timeout on a handler to change it.

What are some useful Claude Code hooks examples?

To add one:

  1. Open the settings file for your chosen scope.
  2. Merge the example into its hooks block. Append to an event's array if that event already has hooks.
  3. Run /hooks in Claude Code to check that it appears under the right event.

Command hooks run with your full user permissions. Review any hook before adding it, especially from a repo you don't know.

How do I format files after edits?

This PostToolUse hook passes the edited file's path to Prettier:

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Edit|Write",
      "hooks": [{
        "type": "command",
        "command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
      }]
    }]
  }
}

How do I block edits to protected files?

Create .claude/hooks/protect-files.sh to check the incoming file path for protected targets such as .env, package-lock.json and .git/. When it rejects an edit, the script must print a reason to stderr and exit 2.

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

This configuration calls your script; you'll need to write it before using the hook.

How do I restore a reminder after compaction?

SessionStart adds stdout to Claude's context. Use the compact matcher to repeat a project instruction after compaction:

{
  "hooks": {
    "SessionStart": [{
      "matcher": "compact",
      "hooks": [{
        "type": "command",
        "command": "echo 'Reminder: use Bun, not npm.'"
      }]
    }]
  }
}

For a Stop hook, follow the examples for a Claude Code notification sound when it's done or read Claude's reply aloud with a Stop hook.

Why is my Claude Code hook not working?

How does Readwalk use Claude Code hooks?

Readwalk for Mac is free and needs macOS 15 or later and Apple silicon. In Readwalk, choose Claude Sessions › Connect to Claude Code…. It saves ~/.claude/settings.json.before-readwalk before adding its hooks to your settings. Your other hooks stay. Disconnect from Claude Code removes only Readwalk's hooks.

When a switched-on session finishes, Readwalk reads Claude's messages in that session's voice, skipping tool calls and tool output. For the session you're talking to, it then chimes, listens for your spoken answer and sends it back. Other switched-on sessions report back in their own voices; sessions left off are left alone.

See what Connect to Claude Code changes in your settings and how to talk to Claude Code by voice.

Frequently asked questions

What are Claude Code hooks?

They're commands that run whenever their configured event matches, without Claude deciding to run them. Instructions in CLAUDE.md depend on Claude following them.

What is the difference between Claude Code hooks and skills?

A skill is a SKILL.md file of instructions Claude uses when relevant, or you invoke with /skill-name. A hook runs whenever its event matches, without that choice.

How do I disable Claude Code hooks?

Set "disableAllHooks": true in a settings file. To turn off one hook, remove its entry or move it out of the configuration. You can't disable an individual hook while keeping it configured.

Why isn't my Claude Code hook firing?

Check /hooks, then confirm the event and the matcher's case against the tool name. In an interactive session, accept the workspace trust dialog for that folder first.

Can Claude Code hooks read replies aloud?

Yes. A Stop hook can pass last_assistant_message to say on Mac. Readwalk for Mac uses its own hooks to read replies and listen for your answer.

Hands off the keyboard.

Readwalk for Mac reads Claude Code's replies aloud and sends your spoken answer back. Free, macOS 15 or later, Apple silicon. Away from your desk, Readwalk for iPhone reads the replies and takes your answer.