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.
Instalación con revisión previa
Este activo requiere revisión. El prompt copiado pide dry-run, muestra escrituras y continúa solo tras confirmación.
npx -y tokrepo@latest install 587e4918-987f-4705-b4dd-e7abb7c7c9be --target codexPrimero dry-run, confirma las escrituras y luego ejecuta este comando.
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
Preguntas frecuentes
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.
Referencias (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
Relacionados en TokRepo
Fuente y agradecimientos
Built into Claude Code by Anthropic.
Documentation: docs.anthropic.com/en/docs/claude-code/hooks
Discusión
Activos relacionados
Claude Code Hooks — Automate Pre/Post Task Actions
Complete guide to Claude Code hooks for automating actions before and after tool calls. Set up linting, testing, notifications, and custom validation with shell commands.
OrchestKit — Claude Code Skills, Agents & Hooks
Claude Code plugin bundle with 100+ skills, agents, and hooks. Run `/ork:setup` to detect your stack, recommend MCPs, and onboard faster.
Claude Code Hooks — Custom Automation Recipes
Collection of ready-to-use Claude Code hook recipes for automating code formatting, testing, notifications, and security checks. Copy-paste into settings.json. Community-maintained.
Claude Code CLAUDE.md — Best Practices Template
Production-tested CLAUDE.md template for Claude Code projects. Covers coding conventions, test requirements, git workflow, and project-specific AI instructions.