Claude Code Hooks: Complete Guide to Automation (2026)
Claude Code hooks are one of the most powerful yet underutilized features of the Claude Code ecosystem. They enable event-driven automation that fires before or after Claude takes specific actions, giving you fine-grained control over your AI-assisted workflow.
What Are Claude Code Hooks?
Hooks are JSON-defined automation rules stored in .claude/hooks/ that respond to events in your Claude Code session. When a trigger event occurs (like saving a file or using a tool), the hook executes a command or injects a prompt into Claude's context.
Hook Triggers
Claude Code supports these trigger events:
- - PreToolUse: Fires before a tool is executed (can block the action)
- - PostToolUse: Fires after a tool completes
- - PostFileSave: Fires when a file is saved
- - PostFileCreate: Fires when a new file is created
- - PostFileDelete: Fires when a file is deleted
- - SessionStart: Fires when a new session begins
- - UserPromptSubmit: Fires when you send a message
Action Types
Hooks support two action types:
- - command: Runs a shell command (receives session context as JSON on stdin)
- - agent: Injects a prompt into Claude's context
Real-World Examples
Lint on Save Run ESLint automatically when TypeScript files are saved. This catches issues immediately without manual intervention.
Security Review Before Write Verify that write operations follow coding standards before Claude modifies files. Use PreToolUse with a matcher for write tools.
Auto-Format on Create When Claude creates a new file, automatically run Prettier to ensure consistent formatting.
Best Practices
- Keep hook commands fast (under 5 seconds) to avoid blocking Claude
- Use matchers to narrow which events trigger your hook
- Test hooks in a non-critical project first
- Use exit code 2 to block actions in PreToolUse hooks
- Combine hooks with skills for comprehensive automation
Conclusion
Hooks transform Claude Code from a reactive assistant into a proactive development partner. They enforce standards, automate checks, and ensure quality without manual intervention.