Skip to content

Claude Code hooks that actually save time

Claude Code hooks are easy to over-engineer. The right four save real time and prevent the failure modes you actually hit in week one.

Published29 Apr 2026

Updated

Read time3 min · 504 words

Tool focusClaude Code, Anthropic

Hooks are the most under-used feature of Claude Code. Most teams try them, write one or two, hit a friction point, and never come back. That is a shame, because the right small set of hooks fixes the failure modes most agentic workflows hit in the first week.

1. The pre-commit gate

The first hook is the easiest. Wire PreToolUse for the Bash tool to refuse to call git commit unless the linter and formatter pass. The hook is fifteen lines of shell and pays for itself the first time the agent tries to commit a file that breaks the build.

The reason this matters is that an agent committing a broken file is not just a quality issue; it pollutes the agent's own context with a "fix" loop that wastes tokens. Refusing the commit at the source is cheaper than letting the agent flail.

2. Format on save

Hook PostToolUse for the Write and Edit tools to run the formatter on the file the agent just touched. This is a one-line prettier --write or gofmt or whatever the project uses. The benefit is not just consistent style; it is that the agent sees its own output reformatted on the next read, which keeps its mental model of the codebase aligned with the actual files.

3. Custom validators where they earn their keep

Some projects have rules that a generic linter does not enforce: import order conventions, banned dependencies, internal API patterns. Wire those as a hook on PreToolUse for the Bash tool, gated to commands that touch the relevant paths. The agent gets instant feedback when it violates the rule, instead of reviewers catching it three rounds later.

The temptation here is to add too many. Resist. A hook that fires on every file write becomes noise. A hook that only fires when the rule is genuinely about to be broken is a force multiplier.

4. The memory-write guard

The fourth hook is one most teams do not think to add. Hook PreToolUse on writes to CLAUDE.md (or your equivalent memory file) to require a structured reason. The hook can be as simple as refusing the write if the diff does not include a # Why: line.

The reason this matters is that memory accretion is the single biggest long-term failure mode of agentic CLI tools. Every team that uses Claude Code for a year ends up with a CLAUDE.md that is half-stale, half-contradictory, and impossible to triage. The memory-write guard slows the accretion enough that someone has a fighting chance to keep it clean.

What not to hook

Some things are not worth hooking. Test runs are usually too slow to gate on every write. Network calls add unpredictable latency. Anything that takes more than a few hundred milliseconds will make the agent feel sluggish and your team will quietly disable the hook within a fortnight.

A useful rule: hook only the failures you hit more than once a week, with cheap, deterministic checks. Everything else is better handled in CI.

Related notes

07 May 2026 · 3 min

Eval suites for codebase-specific agent use

Most AI rollouts skip evals because they feel like overhead. A small, codebase-specific eval suite, built in an afternoon, is the cheapest way to keep model and prompt changes from becoming a vibes call.

02 May 2026 · 3 min

First MCP server tool design

Building an MCP server is mostly an API design problem with one extra constraint: the caller is a model, not a person. Naming and arguments matter more than transport.

22 Apr 2026 · 3 min

Codex on a real repo

Codex is a repo-aware coding agent. Used carelessly it generates churn the team has to clean up. Used with scope and a real review gate, it ships work.

© 2026 Magrathean UK Ltd. All rights reserved.

Registered in England & Wales: Company No. 16955343. Registered Office: 16 Caledonian Court West Street, Watford, WD17 1RY.