Study FAQ
When should I use a Claude Code command hook for a guardrail?
Also asked as 2
- Which Claude Code hook can block a tool action before it runs?
- How do command, prompt, and agent hooks differ when enforcing Claude Code policies?
Answer
Use a deterministic command hook at a supported pre-action event when a rule can be expressed in code. For a tool action, match the relevant tools on PreToolUse, parse the documented event JSON, and return the documented denial for a policy violation. PostToolUse runs after execution and is suitable for observing or checking results. Prompt hooks use a model turn for judgment, and experimental agent hooks can perform multi-turn checks. Automate actions with hooks Claude Code hooks reference
A PreToolUse command hook can block a matched action with exit code 2 and a safe reason on standard error. A plain exit 0 leaves the normal permission flow in place. Check the event-specific reference because exit and output behavior vary across events. Parse and validate input as data, keep hook credentials narrow, and avoid copying event details into shell source or logs. Claude Code hooks reference
Confirm that the hook actually loads and matches each relevant operation. Test allowed, denied, malformed-input, timeout, and hook-failure paths using harmless fixtures. Keep authoritative permission controls in place because hooks cover only their configured lifecycle events and execution paths. Automate actions with hooks Configure Claude Code permissions
Full explanation
Securing Claude Applications with Guardrails, Hooks, and Secret Management
Study guide · CCDV-F
Read the full explanation