Pro10 min

Hooks: Make Standards Non-Negotiable

Telling an agent to always run the linter is a suggestion it can forget. A hook is a guarantee. Hooks are shell commands the harness runs automatically at defined points in the agent's lifecycle, so your standards execute deterministically instead of depending on the model's good behavior.

Step 1: Pick the right lifecycle event

Common events include after a file edit, before a command runs, and when the agent finishes. A formatter on every edit and a test run before the agent declares victory are the two highest-value hooks.

.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "npm run format -- $CLAUDE_FILE" }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "npm test && npm run typecheck" }
        ]
      }
    ]
  }
}

Step 2: Let a failing hook block completion

When a hook exits non-zero, the harness feeds the failure back to the agent, which must fix it before finishing. The agent cannot mark a task done while tests are red, no matter how confident it is.

claude - api
Agent: task complete.
# Stop hook runs automatically
Hook 'npm test' FAILED: 1 test red
Agent: reopening, the hook blocked completion. Fixing the test ...
$

Step 3: Guard dangerous actions

Use a PreToolUse hook to inspect commands before they run and reject the dangerous ones (a destructive database drop, a force push to main). This is a deterministic safety net that does not rely on the model's judgment.

Hooks run with your permissions
A hook is real shell with your access. Keep hook commands simple and reviewed, and prefer them for enforcing safety rather than for clever side effects you would struggle to debug.
settings.json - hooks
Explorer
.claude/settings.json
.claude/agents/
CLAUDE.md
.claude/settings.json
1"Stop": [{ "hooks": [
2 { "type": "command",
3 "command": "npm test && npm run typecheck" }
4]}]
Hooks turn 'please run the tests' into a hard, automatic gate.

Hands-on tasks