← ContentsClaude Code · advanced

Hooks

Claude Code hooks are user-defined shell commands (or HTTP requests, MCP tool calls, or prompt-based evaluations) that execute automatically at specific points in Claude Code's lifecycle. Unlike asking Claude to perform an action, hooks guarantee deterministic behavior — they always run regardless of what the model decides. This makes hooks suitable for enforcing policies, automating repetitive tasks, and integrating Claude Code into existing development workflows. Hooks are configured as JSON objects in Claude Code's settings files and can target specific lifecycle events such as before a tool runs (PreToolUse), after a tool completes (PostToolUse), when a session starts (SessionStart), or when Claude needs user input (Notification). Each hook definition specifies a trigger event, an optional matcher pattern to filter which tools trigger it, and a handler command to execute. The hook receives contextual data via stdin as JSON (for example, the tool name and its inputs), and can communicate back to Claude Code via exit codes and stdout/stderr output. A hook can silently allow an action, block it with an explanatory message, or inject additional context into Claude's next turn — giving developers fine-grained, programmatic control over the agent's behavior.

When you’d use it

  • Desktop NotificationsA developer working on a long task steps away from their terminal. When Claude finishes processing and needs input or approval, a Notification hook fires a desktop alert (e.g., using `osascript` on macOS or `notify-send` on Linux), so the developer is immediately informed without needing to watch the terminal.
  • Automatic Code FormattingAfter every file edit, a PostToolUse hook runs the project's formatter automatically — for example, `prettier` on TypeScript files or `gofmt` on Go files. This ensures consistent code style without requiring Claude to remember to run the formatter, and without adding formatting instructions to every prompt.
  • Security and Compliance AuditingA PreToolUse hook intercepts every Bash command Claude attempts to run and logs it to an append-only audit file. This creates a complete, tamper-resistant record of all shell commands executed during the session, which can be reviewed for security audits or debugging.
  • Blocking Modifications to Protected FilesA PreToolUse hook on Edit, MultiEdit, and Write tools inspects the target file path. If the path matches sensitive files such as `.env`, `package-lock.json`, or anything inside `.git/`, the hook exits with code 2 and returns an explanatory message, preventing Claude from accidentally modifying critical files.
  • Enforcing Test Suite Quality GatesA PostToolUse hook triggers after Claude completes a task or modifies source files, automatically running the project's test suite. If tests fail, the hook pipes the error output back to Claude as context, prompting the model to investigate and fix the failures before marking the task complete.

What changed recently

  • 2025-11SDK support for hooks released: Claude Agent SDK (formerly Claude Code SDK) now supports subagents and hooks, enabling programmatic hook integration when building custom agents.
  • 2025-11-03Hooks included in Claude Code Plugins public beta: Plugins are shareable bundles of slash commands, agents, MCP servers, and hooks that install with a single command. Hooks can now be distributed and installed as part of a plugin.
  • 2025-11MessageDisplay hook event added: A new hook event that allows hooks to transform or suppress assistant message text as it is displayed to the user.
  • 2026-06-15Claude Agent SDK (including programmatic hook use) moved to separate billing: Agent SDK and `claude -p` usage no longer counts against Claude plan usage limits. Eligible Pro, Max, Team, and Enterprise users receive a separate monthly credit for Agent SDK usage.
This is the short version

The full chapter has three worked examples, the common pitfalls, and the workflow that makes it pay — plus the other 84 features, kept current.

Get Claude Master — $97
CLAUDEMASTER
An independent publication.
Independent product. Not affiliated with or endorsed by Anthropic. “Claude” is a trademark of Anthropic, used here only to describe the subject of this manual.
© 2026 Claude Master — All rights reserved.