Hooks in Claude Code are shell commands that run automatically when certain events happen, like before or after Claude uses a tool, or when a session starts or stops. You configure them in settings.json. They turn Claude Code from an assistant you watch into a workflow you can trust: auto-format on every edit, block a risky command, or get pinged when a long task finishes.
What can a hook do?
A hook runs your command at a precise moment and can react to what Claude is about to do. Common uses:
- Auto-format and lint after every file edit, so the codebase stays clean without you asking.
- Enforce rules: inspect a command before it runs and block it if it touches something it should not.
- Inject context at the start of a session.
- Notify you when Claude stops or finishes a long task.
Which events can you hook into?
The main lifecycle events are:
| Event | Fires when |
|---|---|
PreToolUse | Just before Claude runs a tool (you can block it) |
PostToolUse | Right after a tool runs (e.g. format the file it edited) |
UserPromptSubmit | When you send a message |
SessionStart | At the beginning of a session |
Stop | When Claude finishes responding |
How do you set up a hook?
Hooks live in settings.json, where you map an event to a command (and optionally a matcher that limits which tools trigger it). The /hooks command helps you configure them interactively.
A typical first hook runs your formatter on PostToolUse after an edit, so every change Claude makes comes back already formatted.
Why hooks change how you work
Without hooks, you re-check the same things by hand every session. With them, the guarantees are built in: formatting always happens, dangerous commands are always blocked, you are always notified. That is what makes it safe to let Claude run for longer on its own.
The takeaway
Hooks are deterministic guardrails around a probabilistic assistant. You define, once in settings.json, what must always happen on each event, and Claude Code enforces it for you from then on.
Hooks are one layer of a real Claude Code setup. The Harness Engineering course covers hooks, MCP, subagents and the workflow that ties them together. The first module is free, no card needed.