Claude Code hooks: the deterministic edge of AI-assisted delivery
The part of Claude Code that doesn't roll dice. A technical look at the events, the JSON contract, and why hooks belong in any serious AI-assisted delivery setup.
Most discussion of AI-assisted development still treats the assistant as the whole product: the prompts, the model, the chat. That framing misses the half of the system that carries the engineering weight. In Claude Code, that half is called hooks — shell commands the harness fires at defined lifecycle points, with a JSON contract on both sides. Hooks are the part of the loop that isn't probabilistic. They are how teams pin down behaviour they refuse to leave to the model.
The official reference lists 31 hook events grouped into cadences: session-level (SessionStart, SessionEnd), turn-level (UserPromptSubmit, Stop), the tool-use loop (PreToolUse, PostToolUse, PermissionRequest), subagent events, file and config events, and compaction events. Anywhere the harness changes state, there is a defined event, and the hook receives a JSON payload describing what's happening.
The configuration surface
Hooks are configured in settings.json at three scopes: ~/.claude/settings.json for user-wide defaults, .claude/settings.json at the project root for everything a team wants in source control, and .claude/settings.local.json for personal overrides — Claude Code adds the local file to .gitignore automatically. Organisations can also enforce a managed-policy file that takes precedence over both user and project settings.
The schema is straightforward. Each event maps to an array of matcher blocks. Each matcher block has a matcher string (matching tool names, agent types, or whatever the event defines as its filter), and a list of handlers. A handler has a type — command for shell scripts, http for webhook endpoints, mcp_tool for calling a tool on an MCP server, prompt for a single-turn Claude call, or agent to spawn a subagent. Most teams begin and end with command handlers; the others matter once orchestration gets more ambitious.
The two control points that earn their keep
Of the 31 events, two do the heavy lifting in practice: PreToolUse and PostToolUse.
PreToolUse fires before any tool call executes and is one of only two events that can both block and modify the tool input. The handler returns a JSON object with hookSpecificOutput.permissionDecision set to allow, deny, ask, or defer. When multiple hooks return different decisions, the documented precedence is deny > defer > ask > allow, so a single deny always wins. A PreToolUse handler can also return updatedInput to rewrite the tool call before it runs — useful for forcing flags, redirecting paths, or stripping arguments a team has decided are not negotiable.
PostToolUse fires after a successful tool call. It cannot undo the call, but it can inject additionalContext back into the model's working state, capped at 10,000 characters. That is the mechanism behind every "run the formatter after each edit and tell the model what changed" pattern that's become standard in serious Claude Code setups. The model sees the formatter's output as plain context in its next step.
Exit codes are the protocol
For command-type hooks, exit codes carry meaning. 0 is success: stdout, if it parses as JSON, becomes the structured response. 2 is a blocking error: the JSON is ignored entirely, stderr is fed back to Claude as feedback, and the action is prevented. Any other non-zero exit is a non-blocking error — the first line of stderr appears in the transcript, the rest goes to the debug log, and the loop continues. The discipline this imposes on hook authors is real. A script that exits 1 when it meant to block, or that returns malformed JSON on exit 0, will fail in ways that are hard to debug without reading the Anthropic spec carefully.
Matchers are not just strings
Matchers look like simple strings, but the documented evaluation rule is worth knowing. If the matcher contains only letters, digits, underscores, or pipe characters, it is treated as an exact string or a pipe-separated list of exact strings — so Edit|Write matches both, but nothing else. The moment a matcher contains any other character, it is evaluated as a JavaScript regular expression. That means mcp__memory__.* is a regex matching all tools from the memory MCP server, and a pattern like ^Notebook works as anchored regex. This dual behaviour is the source of more silent misconfigurations than it should be; the rule is documented but easy to forget.
For tool-use events, the matcher narrows by tool name. For SessionStart, it filters by source (startup, resume, clear, compact). For SubagentStart and SubagentStop, it filters by agent type. The cleanest configurations lean on this filtering — a hook that fires for every tool call is almost always doing too much.
Where hooks belong in the stack
The temptation, on first reading the docs, is to push every rule into hooks. That's the wrong instinct. Hooks and the project CLAUDE.md file are not interchangeable. CLAUDE.md communicates intent to the model and is honoured probabilistically — never with the guarantees an auditor would want. Hooks are enforcement. A rule like "never push to main" belongs in a hook because the cost of the model getting it wrong is a real-world consequence, not a stylistic miss.
The corollary is also true. Hooks are a poor place for rules about voice, style, or judgement — the kind a code reviewer would flag but not block. Pushing those into hooks creates brittle scripts that the model can't reason around and that human contributors will eventually start bypassing.
The interesting design question in AI-assisted delivery is not "what should the model do?" It's "what is a team prepared to leave to a probabilistic process, and what does it want guaranteed by code?" Hooks are the answer to the second half of that question.
What the trade-offs actually look like
Hooks are operational code. They run on every event they match. A slow hook on PostToolUse with no matcher will add latency to every tool call in the session. The timeout field in the handler config exists for a reason; so does the async flag for fire-and-forget side effects like logging.
Secrets need handling. HTTP-type hooks accept an allowedEnvVars list precisely because the harness will not pass arbitrary environment variables to outbound webhooks by default. Command-type hooks inherit the shell environment, so anything sensitive there is reachable by the hook script. Treat hook configurations as production code, not personal automation.
The blast radius is the whole session. A hook returning continue: false stops Claude Code entirely; one that consistently times out degrades every interaction. Hooks should fail closed for safety-critical decisions and fail open for observability — an audit logger that goes down should not bring development to a halt.
Bottom line
The maturity of an AI-assisted delivery practice can be measured by how much of its hook configuration is checked into the project repository alongside the code. Teams still pasting prompts into chat and hoping for the best are at one end of that spectrum. Teams whose .claude/settings.json defines blocking guards on destructive commands, post-edit formatters, audit logging, and CI feedback injection are at the other. The model has not changed between those two teams. The deterministic edge has.
Hooks are not an advanced feature to skip on first read. They are what turns Claude Code from a writing assistant into something a delivery team can be accountable for.