Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Claude Code Hooks Reference

Disclaimer: This document reflects our current understanding of Claude Code’s hook system. It is a working reference for symposium development, not a substitute for the official docs. Details may be outdated or incomplete — always consult the primary sources.

Primary sources: Hooks reference · Hooks guide · Extending Claude Code

Hooks are user-defined shell commands, HTTP endpoints, or LLM prompts that execute at specific points in the agent lifecycle. They provide deterministic control — actions always happen rather than relying on the model.

Hook Types

TypeDescription
commandShell script; communicates via stdin/stdout/exit codes
httpPOSTs JSON to a URL endpoint; supports header interpolation with $VAR_NAME
promptSingle-turn LLM evaluation returning {ok: true/false, reason}
agentSpawns a subagent with tool access (Read, Grep, Glob) for up to 50 turns

Events

EventTriggerCan block?Matcher target
SessionStartSession begins/resumesNostartup, resume, clear, compact
SessionEndSession terminates (1.5s default timeout)Noclear, resume, logout, etc.
UserPromptSubmitUser submits prompt, before processingYes (exit 2)None
PreToolUseBefore tool callYesTool name regex (Bash, Edit|Write, mcp__.*)
PostToolUseAfter tool succeedsNoTool name regex
PostToolUseFailureTool failsNoTool name regex
PermissionRequestPermission dialog appearsYesTool name regex
PermissionDeniedAuto-mode classifier denialNo (retry: true available)Tool name regex
StopMain agent finishes respondingYesNone
StopFailureTurn ends on API errorNo (output ignored)rate_limit, authentication_failed, etc.
SubagentStartSubagent spawnedNoAgent type
SubagentStopSubagent finishesYesAgent type
NotificationSystem notificationNopermission_prompt, idle_prompt, etc.
TaskCreatedTask createdYes (exit 2 rolls back)None
TaskCompletedTask completedYes (exit 2 rolls back)None
TeammateIdleTeammate about to go idleYesNone
ConfigChangeConfig file changes during sessionYes (except policy_settings)Config source
CwdChangedDirectory changeNoNone
FileChangedWatched file changesNoBasename
WorktreeCreateGit worktree createdYes (non-zero fails)None
WorktreeRemoveGit worktree removedNoNone
PreCompactBefore compactionNomanual, auto
PostCompactAfter compactionNomanual, auto
InstructionsLoadedCLAUDE.md loadedNoLoad reason
ElicitationMCP server requests user inputYesMCP server name
ElicitationResultMCP elicitation resultYesMCP server name

Configuration

Settings merge with precedence (highest first): Managed → Command line → Local → Project → User.

FileScope
Managed policy (MDM, registry, server, /etc/claude-code/)Organization-wide
.claude/settings.local.jsonSingle project, gitignored
.claude/settings.jsonSingle project, committable
~/.claude/settings.jsonAll projects (user)

Configuration structure

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "./validate.sh",
            "if": "Bash(rm *)",
            "timeout": 60,
            "statusMessage": "Validating...",
            "async": false,
            "shell": "bash"
          }
        ]
      }
    ]
  }
}
  • matcher: regex matched against event-specific values (tool name, session source, notification type).
  • if: permission-rule syntax for additional filtering on tool events (e.g., Bash(git *), Edit(*.ts)).

Input Schema (stdin)

Base fields (all events)

{
  "session_id": "string",
  "transcript_path": "string",
  "cwd": "string",
  "permission_mode": "default|plan|auto|bypassPermissions|...",
  "hook_event_name": "string"
}

PreToolUse additions

  • tool_name: string
  • tool_input: object with tool-specific fields (command for Bash, file_path/content for Write, etc.)
  • tool_use_id: string

PostToolUse additions

  • tool_name, tool_input, tool_use_id (same as PreToolUse)
  • tool_response: string (tool output)

Stop additions

  • stop_hook_active: boolean
  • last_assistant_message: string

Output Schema (stdout)

Output is capped at 10,000 characters.

Universal fields

FieldTypeDescription
continuebooleanfalse stops Claude entirely
stopReasonstringMessage for user when continue is false
systemMessagestringWarning shown to user
suppressOutputbooleanOmits stdout from debug log

PreToolUse decision output

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "allow|deny|ask|defer",
    "permissionDecisionReason": "string",
    "updatedInput": { "command": "safe-cmd" },
    "additionalContext": "string"
  }
}

Decision precedence across parallel hooks: deny > defer > ask > allow. The allow decision does not override deny rules from settings. updatedInput replaces the entire tool input; if multiple hooks return it, the last to finish wins (non-deterministic).

Exit Codes

CodeMeaning
0Success; stdout parsed as JSON
2Blocking error — action blocked, stderr fed to Claude
OtherNon-blocking warning, action proceeds

Execution Behavior

  • All matching hooks run in parallel.
  • Identical handlers deduplicated by command string or URL.
  • Default timeouts: 600s (command), 30s (prompt), 60s (agent), 1.5s (SessionEnd, overridable via CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS).

Environment Variables

VariableDescription
CLAUDE_PROJECT_DIRAbsolute path to project root
CLAUDE_ENV_FILEFile for persisting env vars (SessionStart, CwdChanged, FileChanged only)
CLAUDE_CODE_REMOTE"true" in remote web environments

Enterprise Controls

  • allowManagedHooksOnly: true — blocks user/project/plugin hooks.
  • allowedHttpHookUrls — restricts HTTP hook destinations.
  • disableAllHooks: true — disables everything.
  • PreToolUse deny blocks even in bypassPermissions mode.

MCP Server Registration

In addition to hooks, symposium registers itself as an MCP server in the agent’s settings file. This provides an alternative integration path alongside the hook-based approach.

Configuration structure

The MCP server entry is added under mcpServers in the same settings file used for hooks:

{
  "mcpServers": {
    "symposium": {
      "command": "/path/to/cargo-agents",
      "args": ["mcp"]
    }
  }
}
  • Project-level: .claude/settings.json
  • User-level: ~/.claude/settings.json

Registration is idempotent — if the entry already exists with the correct values, no changes are made. If the entry exists but has stale values (e.g. the binary moved), it is updated in place.