Hooks

Hooks

Hooks are user-defined shell commands, HTTP endpoints, or LLM prompts that execute automatically at specific points in Claude Code's lifecycle.

Example: automatically run Prettier on every file Claude edits, so formatting stays consistent without manual intervention:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write" }
        ]
      }
    ]
  }
}

Claude Code lifecycle hook points, roughly in order: Session Start → UserPromptSubmit → [Agentic Loop: PreToolUse → PermissionRequest → (tool executes) → PostToolUse/PostToolUseFailure → SubagentStart/SubagentStop → TaskCompleted] → Stop → TeammateIdle → PreCompact → SessionEnd. Other async/setup/teardown hooks: Notification, ConfigChange, WorktreeCreate, WorktreeRemove, InstructionsLoaded.

How to troubleshoot hooks:

Related: Notification Hooks, Hooks Settings, Debug Mode, Agentic Loop