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.
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 36711c75-fc63-49ad-9013-4a7cf5fae8a1 --target codexPrimero dry-run, confirma las escrituras y luego ejecuta este comando.
What it is
Claude Code Hooks are shell commands that execute automatically before or after specific tool uses within Claude Code sessions. They work like git hooks but for your AI coding assistant. This collection provides ready-to-use recipes for auto-formatting code, running tests, sending notifications, scanning for security issues, and enforcing workflow policies.
The collection targets developers using Claude Code who want to enforce code quality standards and automate repetitive tasks around AI-generated code. Each recipe is a JSON snippet you paste into your settings.json.
How it saves time or tokens
Without hooks, you need to manually run formatters, linters, and tests after every file Claude Code writes or edits. Hooks automate this entire class of tasks. A PostToolUse hook on Write and Edit operations means every generated file is automatically formatted, and pre-commit quality checks happen without manual intervention.
The token estimate for this workflow is approximately 2,600 tokens. Hooks run as shell commands outside the LLM context, so they consume zero tokens.
How to use
- Open Claude Code settings:
claude config
Or edit ~/.claude/settings.json directly.
- Add a hook to auto-format every file Claude writes:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
}
]
}
}
- Claude Code now auto-formats every file it creates or modifies.
Example
Multiple hook recipes for different automation needs:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "echo 'Running: '$CLAUDE_COMMAND >> ~/.claude/audit.log"
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true"
}
],
"Stop": [
{
"command": "terminal-notifier -message 'Claude Code finished' -title 'Done' 2>/dev/null || true"
}
]
}
}
Hook event types:
PreToolUse -- Before a tool runs (can block execution)
PostToolUse -- After a tool completes
Notification -- When Claude sends a notification
Stop -- When Claude finishes a response
Related on TokRepo
- AI Tools for Automation -- Workflow automation tools
- AI Tools for Coding -- Developer productivity integrations
Common pitfalls
- Hook commands run synchronously. A slow command (e.g., running a full test suite on every file save) blocks Claude Code until it completes. Keep hook commands fast or run them asynchronously with
&. - The
$CLAUDE_FILE_PATHvariable is only available for Write and Edit tool matchers. Other tools have different environment variables. Check the Claude Code documentation for the full variable list. - Hooks that exit with a non-zero status on PreToolUse can block the tool from running. Always add
|| trueto PostToolUse hooks to prevent false failures from interrupting the workflow.
Preguntas frecuentes
Four events: PreToolUse (before a tool runs, can block), PostToolUse (after a tool completes), Notification (on notifications), and Stop (when Claude finishes responding). Each accepts a matcher pattern and shell command.
Yes. PreToolUse hooks that exit with a non-zero status prevent the tool from running. This is useful for security policies like blocking writes to protected directories. PostToolUse hooks do not block.
No. Hooks run as shell commands in a separate process. They do not add to the LLM context or consume API tokens. The token cost is only for the hook configuration in settings.json.
Hooks go in ~/.claude/settings.json under the hooks key. You can also set project-level hooks in .claude/settings.json within your project directory for team-shared automation.
Yes. The Stop event fires when Claude finishes a response. Use it to trigger desktop notifications, Slack messages, or any shell command. The example uses terminal-notifier on macOS.
Referencias (3)
- Anthropic Claude Code Documentation— Claude Code hooks run shell commands before and after tool use
- Anthropic Docs— Claude Code settings configuration reference
- Biome— Biome formatter and linter for JavaScript and TypeScript
Relacionados en TokRepo
Fuente y agradecimientos
Community-maintained collection based on Claude Code documentation.
Contributions welcome — share your hook recipes!
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.
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.
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 Mastery — Hooks, Skills, MCP Guide (2026)
claude-code-mastery is a guide + templates for Claude Code hooks/skills/MCP; verified 511★ with a Quick Start to copy hooks and skills into `~/.claude`.