✻Founding-member pricing: every lesson for $19/mo. The first 3 lessons are free. Start lesson 1 →
Unit 7 · Hooks & Enforcement · Lesson 32 of 40

Verification Hooks: Gate Turns on Real Results

Make Claude Code prove its work: Stop hooks that block until tests and typechecks pass, prompt-based checks and safe patterns for verification gates.

ACADEMY11 min+72 XP4-question quiz

🎯 By the end of this lesson you'll be able to

  • Write a Stop hook that blocks on failing checks
  • Choose fast, reliable checks for gates
  • Use prompt and agent hooks for judgment calls
  • Avoid infinite loops and slow gates

"Done" should mean done

The most frustrating agent failure is premature success: "All done! Everything works!" when the build is red. A Stop hook fixes that. It runs when Claude tries to end its turn, and if your check fails, it blocks the stop and hands Claude the failure output to fix.

The one-line gate

.claude/settings.json
{
  "hooks": {
    "Stop": [
      { "hooks": [{ "type": "command", "command": "pnpm tsc --noEmit 1>&2 || exit 2" }] }
    ]
  }
}

How it works:

  • The typecheck output goes to stderr (1>&2), which is what Claude sees when the hook blocks.
  • If the typecheck fails, exit 2 blocks the stop.
  • Claude reads the errors, fixes them and tries to stop again, until the check passes.
▶Watch it runA gate catching premature success
A gate catching premature success

Picking good gate checks

Good gateWhy
TypecheckFast, deterministic, and catches a whole class of mistakes
Lint (changed files)Fast, and enforces conventions
Unit tests for affected packagesReal behavioral evidence
Risky gateWhy
A 20-minute full e2e suiteEvery stop becomes painfully slow
Flaky testsRandom blocks confuse Claude and waste turns
Anything that needs network or secretsFails for reasons Claude can't fix
✓
Keep gates fast and scoped

Run checks only on what changed, like pnpm test --changed or ruff check $(git diff --name-only). A gate that takes 10 seconds will be kept, and one that takes 10 minutes will be disabled.

Built-in loop protection

A Stop hook that can never pass could loop forever, so Claude Code caps consecutive blocked stops (8 by default). Still, design checks Claude can satisfy, and make the error output clear.

Judgment calls: prompt and agent hooks

Some checks aren't a command. "Did Claude actually address every item in the request?" For these, a prompt hook asks a model to evaluate, and an (experimental) agent hook can use tools to investigate:

json
{ "type": "prompt", "prompt": "Check whether the assistant ran the tests and reported their real output. If not, block with a reason." }

Use command gates for hard facts and prompt gates for softer criteria.

⇅How a Stop gate worksdrag or use arrows
  1. ⋮⋮Claude finishes its work and tries to stop
  2. ⋮⋮The Stop hook runs the typecheck
  3. ⋮⋮The check fails, so the hook exits 2 with errors on stderr
  4. ⋮⋮Claude reads the errors and fixes them
  5. ⋮⋮Claude tries to stop again, the check passes, and the turn ends
☑Add a gate to your projectsaved on this device

Check your understanding

?Lesson quiz10 XP per correct answer · +25 for a perfect score
QUESTION 1 OF 4

When does a Stop hook run?

Why: Stop fires when Claude finishes. Blocking it means 'not done yet'.
QUESTION 2 OF 4

In pnpm tsc --noEmit 1>&2 || exit 2, why redirect to stderr?

Why: Blocking hooks pass stderr back to Claude so it can fix the errors.
QUESTION 3 OF 4

Which makes the WORST gate?

Why: Slow, flaky gates waste turns and get disabled.
QUESTION 4 OF 4

What kind of hook evaluates soft criteria with a model?

Why: Prompt hooks ask a model to judge, which suits criteria a command can't check.
0 / 4 correct
Academy lesson

Unlock lesson 32 and the next 8

You've finished the free lessons. Join the Academy for $19/month to unlock every lesson, quiz, terminal simulation and puzzle, plus the final exam and your certificate of completion.

  • All 40 lessons across 9 units
  • 165+ quiz questions
  • Final exam + certificate
  • New lessons as Claude Code evolves
  • XP, streaks and badges
  • Cancel anytime

Already a member? Log in to continue.

Key takeaways

  • Stop hooks turn 'I'm done' into 'prove it'.
  • Pattern: run check → output to stderr → exit 2 on failure.
  • Gates must be fast, deterministic and fixable by Claude.
  • Use prompt hooks for judgment calls and command hooks for hard facts.

Nice work. Lesson 32 done?

Mark it complete to earn +72 XP, keep your streak alive and unlock badges.

Written by Curtis Cornell from Anthropic's public Claude Code documentation. Last reviewed November 14, 2026. Claude Code changes fast; if something here differs from your version, the official docs win.

Start lesson 1 free →