Claude Code can run shell commands, edit files, and push to git on your behalf. That power is the point — and the risk. Hooks are the mechanism for putting deterministic guardrails around it: small scripts the harness runs at defined points in the agent's loop, able to inspect — and block — what the agent is about to do.
This guide covers the PreToolUse hook specifically, because it's the one that can stop a dangerous action before it executes.
The hook lifecycle
Claude Code fires hooks at several lifecycle events. The most useful for safety are:
Event






