Claude Code Hooks — Automate Your AI Workflow
Built-in automation system for Claude Code. Run shell commands on events like file edits, tool calls, and notifications. Lint on save, auto-test, and more.
What it is
Claude Code Hooks are built-in automation triggers in Claude Code that run shell commands in response to specific events: file edits, tool calls, notifications, and agent stops. They let you enforce coding standards, run tests, validate changes, and integrate with external tools automatically.
Hooks target developers using Claude Code who want automated quality checks integrated directly into their AI-assisted workflow without relying on Claude to remember to run them.
How it saves time or tokens
Without hooks, you need to manually remind Claude to run linters, formatters, or tests after each edit. Hooks make this automatic and deterministic. The PostToolUse event fires after every edit or write, so ESLint, Prettier, or your test suite runs without consuming additional tokens on reminder prompts. PreToolUse hooks can block operations before they execute, preventing bad patterns proactively.
How to use
- Add hooks to your
.claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"command": "eslint --fix $CLAUDE_FILE_PATH"
}
]
}
}
- Every file edit by Claude Code now auto-runs ESLint.
- Add multiple hooks for different events:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"command": "prettier --write $CLAUDE_FILE_PATH"
}
],
"Stop": [
{
"matcher": ".*",
"command": "npm test"
}
]
}
}
Example
A comprehensive hook configuration for a TypeScript project:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "echo 'Bash command about to run'"
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"command": "eslint --fix $CLAUDE_FILE_PATH && prettier --write $CLAUDE_FILE_PATH"
}
],
"Stop": [
{
"matcher": ".*",
"command": "npx tsc --noEmit"
}
]
}
}
Related on TokRepo
- Coding tools for AI development — tools that enhance AI-assisted coding workflows
- Automation tools — broader automation category
Common pitfalls
- Hook commands that take more than a few seconds block Claude from proceeding; keep hooks fast or run them asynchronously
- The
$CLAUDE_FILE_PATHvariable is only available for file-related tool events; using it in Stop hooks causes empty path errors - Hooks run in a shell subprocess; ensure the commands are available in your PATH and not aliased only in your interactive shell
Frequently Asked Questions
Five event types are available: PreToolUse fires before a tool executes, PostToolUse fires after completion, Notification fires when Claude sends a notification, Stop fires when Claude finishes a turn, and SubagentStop fires when a subagent completes.
Yes. PreToolUse hooks run before the tool executes. If the hook command returns a non-zero exit code, the tool execution is blocked. This lets you prevent operations that violate your coding standards before they happen.
The matcher field accepts a regex pattern. Use 'Edit|Write' to match file modification tools, 'Bash' for shell commands, or '.*' to match all tools. The pattern is matched against the tool name, not the tool arguments.
No. Hooks are shell commands that run outside the Claude conversation. They do not generate API calls or consume tokens. The hook output may be shown to Claude for context, but the hook execution itself is free.
Yes. Place the settings.json in the project-level .claude/settings.json directory and commit it to version control. All team members using Claude Code on the project will inherit the same hook configuration.
Citations (3)
- Anthropic Docs— Claude Code supports hooks for event-driven automation
- Anthropic— Claude Code is Anthropic's official CLI for Claude
- ESLint Docs— ESLint is a pluggable JavaScript linter
Related on TokRepo
Source & Thanks
Built into Claude Code by Anthropic.
Documentation: docs.anthropic.com/en/docs/claude-code/hooks
Discussion
Related Assets
/babysit — Auto-Respond to PR Review Comments
Open-source slash command that watches a PR for review comments and auto-pushes fixes. Inspired by Boris Cherny's /babysit pattern.
/loop — Local Recurring Task Scheduler (Boris-Style)
Open-source slash command for recurring local Claude Code tasks with a 3-day safety cap. Inspired by Boris Cherny's /loop scheduler.
/batch — Parallel Worktree Migration Slash Command
Open-source slash command that splits a migration across parallel git worktrees. Inspired by Boris Cherny's /batch worktree pattern.