Expert guidance for creating, configuring, and using Claude Code hooks. Use when working with hooks, setting up event listeners, validating commands, automating workflows, adding notifications, or understanding hook types (PreToolUse, PostToolUse, Stop, SessionStart, UserPromptSubmit, etc).
<essential_principles> Hooks are event-driven automation for Claude Code that execute shell commands, LLM prompts, or multi-turn agents in response to tool usage, session events, and user interactions. They provide deterministic control over Claude's behavior without modifying core code.
1. Three Hook Types:
{"ok": true} or {"ok": false, "reason": "..."}.2. Event → Matcher → Hook — Events (PreToolUse, Stop, etc.) trigger matchers (regex on tool name/context) which fire hooks. See <quick_reference> for the full event table.
3. Configuration Locations (highest priority first):
~/.claude/settings.json.claude/settings.json.claude/settings.local.json/hooks interactive menu in Claude Code4. Environment Variables:
| Variable | Value |
|----------|-------|
| $CLAUDE_PROJECT_DIR | Project root directory |
| ${CLAUDE_PLUGIN_ROOT} | Plugin directory (plugin hooks only) |
| $ARGUMENTS | Hook input JSON (prompt/agent hooks only) |
| $CLAUDE_ENV_FILE | Path for persisting environment variables |
5. Safety Essentials:
stop_hook_active field in Stop/SubagentStop hooks — exit 0 if truechmod +x on script files"$CLAUDE_PROJECT_DIR" for spaces~/.zshrc/~/.bashrc with [[ $- == *i* ]] checks — non-interactive shells can corrupt JSON output-p); use PreToolUse instead
</essential_principles>If $ARGUMENTS provides clear intent, skip the menu and route directly.
</intake>
Intent-based routing (when $ARGUMENTS provides clear intent):
After reading the workflow, follow it exactly. </routing>
<quick_reference>
| Event | When it fires | Can block? | Matcher filters |
|-------|---------------|------------|-----------------|
| PreToolUse | Before tool execution | Yes | tool name |
| PostToolUse | After tool succeeds | Yes (decision: "block") | tool name |
| PostToolUseFailure | After tool fails | No | tool name |
| PermissionRequest | Permission dialog appears | Yes | tool name |
| UserPromptSubmit | User submits a prompt | Yes | no matcher support |
| Stop | Claude finishes responding | Yes | no matcher support |
| SubagentStart | Subagent is spawned | No | agent type |
| SubagentStop | Subagent finishes | Yes | agent type |
| SessionStart | Session begins/resumes | No | source (startup, resume, clear, compact) |
| SessionEnd | Session terminates | No | reason (clear, logout, etc.) |
| PreCompact | Before context compaction | No | trigger (manual, auto) |
| Notification | Claude needs attention | No | type (permission_prompt, idle_prompt, etc.) |
| TeammateIdle | Agent about to go idle | Yes (exit 2) | no matcher support |
| TaskCompleted | Task marked complete | Yes (exit 2) | no matcher support |
| ConfigChange | Config file changes | Yes (decision: "block") | config type |
See references/hook-types.md for detailed schemas and use cases for each event. </quick_reference>
<reference_index>
All in references/:
Types & Events: hook-types.md — complete event schemas and use cases Hook Types: command-vs-prompt.md — decision tree for command vs prompt vs agent Matchers: matchers.md — regex patterns, event-specific matching, MCP tools I/O Schemas: input-output-schemas.md — stdin JSON, exit codes, hookSpecificOutput Examples: examples.md — notifications, file protection, auto-format, logging, Stop hooks Troubleshooting: troubleshooting.md — hooks not triggering, JSON corruption, loops Toolkit: toolkit-structure.md — distributable hook packaging for claude-code-toolkit </reference_index>
<workflows_index>
All in workflows/:
| Workflow | Purpose | |----------|---------| | create-hook.md | Build a hook from scratch | | debug-hook.md | Diagnose and fix broken hooks | | create-toolkit-hook.md | Package hook for toolkit distribution | | get-guidance.md | Help decide event, type, and matcher | </workflows_index>
<success_criteria> A working hook configuration has:
jq)--debug flag or Ctrl+O verbose mode showing expected behaviorstop_hook_active flag)Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer