Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Hooks as enforcement mechanism for AI behavior — use when a written rule fails repeatedly in generation flow and deterministic structural enforcement is needed. Triggers include detecting cyclic rule failure, asking about Claude Code hooks/lifecycle events (SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop), or when a behavior guard needs to execute outside the AI's inference loop.
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-12 | ✗→✓ | ▲ Improved | 1% | 0% |
| case-01 | ✗→✓ | ▲ Improved | 28% | 0% |
| case-03 | ✗→✓ | ▲ Improved | -19% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 16% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 11% | 0% |
When AI behavior requires enforcement (preventing a repeated failure mode despite written rules), hooks are the structural solution — not additional rules, not wrapper skills, not optional validators.
| Problem | Wrong Approach | Right Approach | |---------|---|---| | AI breaks a rule in generation despite rule being written | Add more detailed rule text to instructions | Hook that executes at a fixed lifecycle point (e.g., before output) | | AI forgets to invoke a critical skill | Write "remember to invoke skill X" | Hook that blocks/gates generation until invoked, or hook that injects the skill's effect | | AI needs to check a fact every turn | Add fact to instructions as a bullet | Hook that injects the fact into context at SessionStart or UserPromptSubmit | | AI must stop before producing incorrect output | Write "verify before responding" | Hook that audits the response text at Stop, before delivery |
A hook executes at a known lifecycle event with access to the AI system state:
If a rule has failed ≥2 times by the same failure mode despite being documented and despite the AI acknowledging it, writing more rule text will not fix it. The design was wrong. Switch to a hook at the appropriate lifecycle point.
Wrong approach (fails): Add rule "never cite a fact without checking the transcript first."
The AI reads the rule, nods, and then in generation (high token velocity, pattern matching) produces a plausible-sounding citation without checking. The rule was there; the inference loop optimized it away.
Right approach (works): Hook at Stop that parses the response text, finds citations, cross-checks them against the session transcript, and blocks the response if citation is not present.
The hook runs outside the generation loop. The AI cannot skip it. The citation either exists or the output is rejected.
~/.claude/hooks/Hooks are powerful and should be used deliberately:
When adding a hook:
Hook name: <name>
Lifecycle: <SessionStart|UserPromptSubmit|PreToolUse|PostToolUse|Stop>
Trigger condition: <what failure mode does this prevent?>
Mechanism: <what does the hook do?>
Why not a rule: <why did the written rule fail?>
Audit/rollback: <how to test the hook? how to disable if it breaks something?>Other measured skills in the registry, with their headline benchmark lift.