Hooks

Also: Claude Code hooks, lifecycle hooks, hook scripts

Hooks are user-defined shell commands that Claude Code runs automatically at specific lifecycle events, such as before a tool runs or when a session ends.

Hooks let you attach your own shell commands to events in Claude Code's lifecycle, so the tool runs deterministic automation at the moments you choose rather than relying on the model to remember. You configure them in your settings.json (at the user, project, or local level), where each hook is registered under an event and, for tool events, matched to specific tools.

Claude Code fires hooks at well-defined points: before and after a tool runs (PreToolUse and PostToolUse, for example around an Edit or Bash call), when you submit a prompt (UserPromptSubmit), and when the agent finishes responding or a session starts or ends (Stop, SessionStart, SessionEnd). A hook receives the event's JSON payload on stdin and can act on it: run a formatter or linter after edits, block a disallowed command, log activity, or send a notification.

A hook's exit code and output can feed information back to Claude or stop an action. An exit code of 0 succeeds, and an exit code of 2 signals a blocking error whose stderr is passed back to Claude; hooks can also emit structured JSON for finer control.

Because hooks are plain shell commands, they run with your permissions and complement Claude Code's permissions system rather than replacing it. They pair well with slash commands and MCP for repeatable workflows. For the exact event names, matcher syntax, and payload fields, see the official Claude Code documentation.

Why it matters

Hooks turn good intentions into guarantees: instead of hoping the model formats your code, runs tests, or skips a dangerous command, you encode those rules as shell commands that fire every time. That makes Claude Code's behavior more deterministic and auditable, and lets teams enforce conventions and guardrails consistently across a project.

Deep dive: Claude Code Hooks Explained: Automate Your Workflow

See Hooks in action across releases — browse the Claude Code changelog.

Get release alerts