Practical guide · 3 min read
Claude Code Hooks: What Blocks Before Versus What Runs After
A practical guide to placing Claude Code command-hook checks before actions and automation after them, with explicit limits for async work and security boundaries.
ReleaseLog editorial · Published · Sources checked
AI-assisted draft and independent AI source review. Editorial method & corrections. Documented facts, corpus statistics, commentary, and examples are labeled separately; this is not a vendor benchmark.
Documented behaviour
Two phases, different powers
This guide focuses on command hooks: external programs attached to Claude Code lifecycle events. The documentation also supports prompt- and agent-based hook types, so the word hook alone does not imply deterministic execution.
For a synchronous PreToolUse command hook, exit code 2 can prevent the proposed tool call. PostToolUse occurs after the call, so it cannot prevent the original action. Event-specific output rules matter. Async command hooks do not hold up the action and cannot retroactively block it.
These mechanisms support checks, formatting and notifications. Hook files themselves are executable configuration, and the official guide shows why they need deliberate placement and review.
Reference: Claude Code hooks guide · Claude Code hooks reference
Editorial analysis
The failure scenario: gating in the wrong phase
Imagine a team that wants edits to generated files rejected. A check that runs only after a write can report the violation, and might attempt a repair, but the original mutation has already happened. Reversal and prevention are different requirements. Decide which one the workflow actually needs before selecting an event.
The opposite mistake is to put every slow task in the pre-action path. A formatting pass does not need veto power just because it belongs in the workflow. Our design rule is to separate fast admission decisions from work that observes or transforms the accepted result, then give each one an explicit completion condition.
Reference: Claude Code hooks guide
Hypothetical example
A hypothetical CI workflow
Consider a hypothetical repository with an authored source directory and a generated output directory. No deployment or benchmark is implied by this example.
A short synchronous pre-action check rejects direct file-editing tool calls to the generated directory. The team separately uses a post-edit formatter for the files it owns. That formatter finishes before those same files are edited again; putting a mutating formatter in the background would create a race with later edits.
Read-only reporting or a notification can run asynchronously where appropriate. A required CI check still gates a merge or release: an agent finishing its task does not establish that background tests passed. Script and configuration changes receive review because they change how the workflow behaves.
This hook arrangement covers the named tool events only. It does not confine shell processes or replace repository permissions, isolated execution and protected release controls. Keep those boundaries outside the model and outside a small command-matching rule.
Reference: Claude Code hooks reference · Claude Code hooks guide
Verification checklist
Checklist: assigning work to the right phase
- Write the requirement as prevention, repair, verification or notification.
- Use a synchronous pre-action decision when a proposed action must not happen.
- Check the specific event schema and exit-code behavior for the installed version.
- Serialize formatters or other work that mutates files still being edited.
- Use background execution only when the task can safely outlive the tool action.
- Make required CI results an explicit release condition rather than assuming a background launch succeeded.
- Review the hook code and record which event paths it covers; test a permitted case and a blocked case.
Reference: Claude Code hooks reference · Claude Code hooks guide
Editorial analysis
Tradeoffs and the sandbox line
The stopping point is a small, explainable workflow. Every hook should have one owner, one purpose and a visible outcome. If an operation fails, the operator should know whether the original action was blocked, completed with a failed follow-up, or is still waiting on another gate. A generic success message hides these distinctions.
Command hooks are a good fit for conditions that can be evaluated mechanically. When judgment is needed, the documented model-based hook types or another review process may help, but that choice does not turn their decision into an operating-system security boundary. Keep a short rationale for the mechanism you choose instead of expanding a blocklist indefinitely.
Sources & project records
Documentation and project code can change. Check the cited revision, fixture scope and your own environment before applying a recommendation. Source-check dates describe editorial review, not a feature release date.
- Claude Code hooks guide ↗
code.claude.com · Checked
- Claude Code hooks reference ↗
code.claude.com · Checked