Learning resourcesReviewed · Anthropic

Claude Code: Automate actions with hooks

This guide shows developers using Claude Code how to automate routine tasks and checks, but reliable results depend on choosing the right events and handling failures correctly. It provides configurations for notifications, formatting, protected-file checks, context restoration and other actions, and explains command, HTTP, MCP tool, prompt and agent hooks.

Researched 13 Nov 2026

In short.

This guide helps developers using Claude Code automate actions and checks, but a hook can miss changes or let an action proceed when its event or failure handling is configured incorrectly. It combines practical settings examples with explanations of event matching, JSON input and output, permission decisions, parallel execution and troubleshooting. It is useful for learning notifications, formatting and narrowly scoped checks; advanced schemas require linked documentation. Agent hooks are described as experimental. Prices, licensing terms and a minimum version for the overall feature are not established, and no examples were executed.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage includes the complete selected documentation page at [https://code.claude.com/docs/en/hooks-guide.md], including examples, platform notes, permission caveats, experimental-feature notices and troubleshooting. Linked pages, the documentation index, external dependencies and the linked GitHub validator implementation are excluded. No examples were executed and no live behavior was checked. Pricing, reuse rights, comprehensive version compatibility and behavior beyond the captured explanations remain unknown or unassessed.

The overview

What this resource is

Automate actions with hooks is a guide in the Claude Code documentation at [https://code.claude.com/docs/en/hooks-guide.md]. It teaches the hook feature of an existing coding assistant rather than supplying a standalone tool or installable extension.

Hooks connect actions to events such as a session starting, a tool being used, a file changing or Claude finishing a response. The guide includes setup examples, operating rules and troubleshooting, while directing readers to a separate reference for complete schemas and advanced features.

Where the guide is useful

The guide is a useful starting point when an action should happen at a defined moment instead of depending on the assistant remembering to do it. Its notification walkthrough offers an approachable first configuration, while the formatting and protected-file examples connect hooks to everyday development tasks.

Its strongest technical material explains behavior that can otherwise be misleading: exit 0 does not approve a tool call, a completed action cannot be undone by a PostToolUse hook, and one hook denying an action does not prevent sibling hooks from running. The examples are starting patterns rather than evidence of comprehensive enforcement.

Who it is for

Beginners already using Claude Code can use the guide to understand hooks and configure desktop alerts. The walkthrough adds a JSON block to a settings file, and the guide also offers the option to describe the hook in the CLI and have Claude write it. Shell scripting and JSON parsing become relevant for custom checks.

Experienced developers and team maintainers get examples for project formatting, file protection, configuration logging, environment loading and shared HTTP handlers. Developers needing complete event schemas or advanced MCP and asynchronous behavior will also need the linked reference, whose contents are outside this page.

What it teaches

Worked examples cover desktop notifications, Prettier formatting after edits, blocking selected file edits, restoring context after compaction, logging configuration changes and reloading environment variables. Another example approves only ExitPlanMode permission requests and explains the additional output needed for tools requiring user interaction.

Command hooks run shell programs. HTTP hooks send event data to an endpoint, MCP tool hooks call configured server tools, prompt hooks request a single model evaluation, and agent hooks perform verification with tool access. The page explains event and tool matching, argument filters, settings locations, result handling and debugging.

Using it

What you need

You need Claude Code and access to an applicable settings location. Dependencies vary by example: the shell snippets commonly use jq, the formatting example invokes Prettier through npx, and environment loading uses a tool such as direnv. Script files must be executable where the guide requires it.

Notification requirements differ by platform. The macOS example uses osascript and may require Script Editor notification permission. Linux uses notify-send and needs a desktop notification daemon, which many headless environments lack. Windows uses a PowerShell dialog; from WSL, powershell.exe must be available through Windows interop.

Organization settings can restrict execution to managed hooks. Some notification types also require particular Claude Code releases; the page does not establish one minimum version covering the entire guide.

How hooks work

Claude Code fires an event and runs its matching handlers. Command hooks receive event data as JSON on standard input, including fields such as the session identifier, working directory and tool input. They communicate through standard output, standard error and an exit code; structured JSON provides event-specific decisions and additional context.

Exit 0 normally reports no objection rather than granting approval. Exit 2 can block supported events, but some events, including SessionStart, continue. Other failures are often non-blocking unless an applicable failure rule says otherwise. Valid structured output can also determine the outcome, so exit codes cannot be interpreted independently of the event and output.

Matching hooks run in parallel and finish before their results are combined. For PreToolUse decisions, precedence is deny, defer, ask, then allow. A denial does not cancel sibling hooks or their side effects. Matchers filter event-specific fields; the separate if field filters supported tool events by tool name and arguments, with best-effort Bash analysis.

How to get started

The first walkthrough adds a Notification entry inside the hooks object in ~/.claude/settings.json, then uses /hooks to confirm that it appears. Existing event entries should remain inside that same hooks object. The guide provides a notification command for each supported example platform and describes how to trigger a permission request to check the result.

Settings location controls scope. User settings cover all projects; .claude/settings.json is project-scoped and shareable; .claude/settings.local.json is local to the project. Hooks can also come from managed settings, plugins, skills and subagents.

Direct settings edits are normally detected automatically. Troubleshooting includes checking valid JSON, case-sensitive matchers, script permissions, dependency paths and whether managed settings permit the hook. None of these setup steps was performed here.

A concrete example

The formatting example registers a command handler under PostToolUse with the matcher Edit|Write. The command uses jq to extract .tool_input.file_path, then passes the quoted path to npx prettier --write --ignore-unknown. The intended result is that supported files are formatted after Claude edits them through those tools.

This example also demonstrates a coverage boundary: Edit|Write does not cover files rewritten through shell commands. The guide suggests FileChanged when the requirement concerns a particular file regardless of how it changes, and describes broader scanning approaches elsewhere on the page. It notes that successful formatting hooks can be silent, so their effect or debug output must be inspected. The example was not executed.

Assessment & evidence

Limits that affect reliability

PostToolUse runs after success and cannot undo the completed action. Stop fires whenever Claude finishes responding, rather than exclusively when a whole task is complete, and does not fire on user interrupts. The guide also describes a cap on repeated Stop-hook blocks. These distinctions matter when designing completion checks.

Failure handling is not uniformly blocking. HTTP failures normally allow continuation unless configured otherwise, and command failures depend on event, output and failure settings. Multiple PreToolUse input rewrites are neither merged nor given a guaranteed winner. Command hooks cannot directly invoke slash commands or tools through their output.

Permission interactions need careful reading. An allow result does not override all permission restrictions, and the guide describes certain mod and deployment configurations in which a mod can approve a call blocked by PreToolUse. Its best-effort if filter is explicitly not recommended as a hard permission boundary.

Agent hooks are experimental, and the guide recommends command hooks for production workflows. Cross-platform reliability, latency and the effectiveness of any particular protection script were not assessed through execution.

How to judge fit

Hooks are a good fit when you can describe a specific trigger and a concrete action: notify on a permission prompt, format after an edit, or inspect a proposed tool call before execution. Command hooks let you express the rule in code, while prompt and agent hooks introduce model judgment for conditions that cannot be resolved by a simple rule.

The practical implication is that reliable triggering and complete protection are different requirements. A hook may run exactly as configured while missing another path to the same file or accepting a failed check. Choose an event that observes the operation you care about, then define how failures should affect continuation. This is an inference from the page's documented coverage and failure behavior.

Related approaches

The guide distinguishes hooks from other Claude Code extensions. Skills provide additional instructions and executable commands, subagents run tasks in isolated contexts, and plugins package extensions for sharing. These serve related needs, but their linked documentation is not included, so their capabilities are not independently compared here.

For context needed at every session start, the page suggests considering CLAUDE.md. For hard allow or deny requirements, it directs readers to the permission system rather than relying on the best-effort if filter. Within hooks, use prompt evaluation when event data is enough and agent verification when inspecting the codebase is necessary, subject to the experimental status of agent hooks.

What supports these findings

The supporting source is the complete selected Claude Code documentation page at [https://code.claude.com/docs/en/hooks-guide.md]. It contains explanations, configuration examples, scripts, platform notes and troubleshooting. It is primary documentation rather than an independent measurement of behavior.

The captured source is identified by SHA-256 6430fc2f7ba4221db5c0842df470cf7c27d95b0d0a60df08f230ef0a5c095e8b. No commands, scripts or hooks were executed. Runtime behavior, performance, security effectiveness and compatibility with a particular installation remain unverified.

Access & freshness

Costs and usage

The page gives no prices for Claude Code access or hook execution. It does not establish whether prompt-hook or agent-hook model usage is included in any plan, how that usage is billed, or what a particular configuration would cost.

The execution types have different resource demands: command hooks run programs, HTTP hooks contact endpoints, prompt hooks use a Claude model, and agent hooks can perform multiple verification steps with tools. Their actual runtime, model usage, service charges and total cost are unknown.

Licensing and reuse

The supplied page does not state a license for its prose or example code. Permission to redistribute, adapt or relicense that material is therefore unknown from this evidence.

Claude Code access terms and the licenses of dependencies such as jq, Prettier and direnv are not established by this page. Links to documentation and a GitHub example do not establish those terms.

Version and freshness

The source snapshot was captured on 12 November 2026 at 23:14:45 UTC. The page does not provide a publication or last-update date, and the capture timestamp does not establish when each feature was introduced or whether the live page has since changed.

Several notification matchers have explicit release requirements. The quota auto-resume matchers require v2.1.234 or later, auth_storage_failure requires v2.1.288 or later, and terminal permission notifications for sandboxed network requests require v2.1.246 or later. These are specific feature requirements rather than a universal compatibility guarantee. Agent-hook behavior is expressly subject to change.

Practical next steps

Start with one concrete outcome, such as receiving a notification when Claude needs permission. Choose the intended user or project settings location, check the platform dependency, and use /hooks to inspect the configuration. Follow the guide's suggested trigger and inspect the actual effect before expanding the setup.

For formatting or protection rules, decide whether the requirement concerns a particular tool call, any change to a file, or the final working tree. Account for shell-driven edits and failure behavior. Consult the linked hooks reference at [https://code.claude.com/docs/en/hooks.md] for the exact event schema and decision fields before building more complex handlers; that reference is not assessed here.

Evidence for this report

selected-page.txtselected · checked 2026-11-12