Reference · 2026

Claude Code Hooks

Claude Code hooks are shell commands the harness runs automatically on events — before or after a tool runs, when a session starts, or when you submit a prompt. They let you enforce policies and automate your workflow deterministically, outside the model’s judgment. Here’s every event, how to add one, and when to reach for a hook.

The hook events

Each event fires at a specific point and receives event-specific JSON on stdin.

PreToolUse

Before a tool runs. Can allow, deny, or ask — the deterministic place to block risky actions.

PostToolUse

After a tool completes. Add advisory context or run follow-ups like format-on-save or tests.

UserPromptSubmit

When you submit a prompt. Inject context or block the prompt before Claude sees it.

SessionStart

At session start, resume, or clear. Inject repo and branch context into the conversation.

SessionEnd

When a session ends. Clean up temporary state or log the session.

Stop

When the main agent finishes responding. Common for “run the test suite on stop”.

SubagentStop

When a spawned subagent finishes its work.

PreCompact

Just before Claude Code compacts the context window.

Notification

On notifications — for example when Claude needs permission or goes idle.

A quick example

Format every file right after Claude edits it — a PostToolUse hook in your settings.json:

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

Hooks receive the event JSON on stdin. Exit 0 to continue (optionally printing additionalContext to advise Claude); exit 2 to block the action.

Hook, skill, or subagent?

The three ways to change Claude Code’s behavior are not interchangeable:

Hook enforces the rule — deterministic, harness-run, regardless of what the model decides.
Skill teaches the how — on-demand expertise, loaded only when relevant.
Subagent isolates the work — a separate context with its own restricted tools.

Full breakdown in the anatomy of a 2026 Claude Code plugin.

Plugins that ship hooks

See advisory, fail-safe hooks in action:

Frequently asked questions

What are Claude Code hooks?

Hooks are shell commands the Claude Code harness runs automatically on events — before or after a tool runs, at session start, when you submit a prompt, and more. Because the harness runs them (not the model), they fire deterministically every time, which makes them ideal for enforcing policies and automating your workflow.

What events can a hook listen to?

PreToolUse, PostToolUse, UserPromptSubmit, SessionStart, SessionEnd, Stop, SubagentStop, PreCompact, and Notification. Each event carries event-specific JSON on stdin — the tool name, tool input and output, the prompt, or session info.

How do I add a hook?

Add a hooks block to your settings.json (user or project scope), or ship a hooks/hooks.json file inside a plugin. Each entry has a matcher (for example "Edit|Write") and a command to run. Plugins reference bundled scripts with ${CLAUDE_PLUGIN_ROOT} so they resolve wherever the plugin is installed.

Can a hook block a tool call?

Yes. A PreToolUse hook can deny a tool (exit code 2, or a permissionDecision of "deny"), which is how you enforce hard rules like “never write outside the repo” or “never commit a secret”. Exit 0 with additionalContext is advisory — it surfaces a suggestion to Claude without blocking.

Where do plugin hooks live?

In hooks/hooks.json at the plugin root, which Claude Code auto-detects. The command references bundled scripts via ${CLAUDE_PLUGIN_ROOT} so they resolve to wherever the plugin is installed.

Are hooks safe?

Hooks run arbitrary commands with your permissions, so only install hooks you trust. Well-built plugin hooks are fail-safe (any error exits 0 and does nothing) and advisory by default, so they surface a suggestion without ever blocking your work or breaking a session.

Do hooks slow down Claude Code?

A well-written hook is near-instant. Keep hook commands fast and fail-safe; heavy or long-running work belongs in a slash command or a subagent, not a hook.

Automate your workflow with hooks

Browse plugins that ship ready-made hooks, or read the full setup guide.

Read the guide