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.
先审查再安装
这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install ba645a85-3df0-40ab-8c75-15aec5c8a2af --target codex先 dry-run,确认写入项后再运行此命令。
What it is
Claude Code hooks let you define custom scripts that run automatically before or after specific tool calls. When Claude Code writes a file, you can trigger ESLint to fix it. When Claude Code finishes a task, you can run your test suite. Hooks are configured in ~/.claude/settings.json and apply to all projects or per-project in .claude/settings.json.
Hooks are designed for developers who want to enforce code quality standards, automate repetitive checks, and integrate external tools into their Claude Code workflow without manual intervention.
How it saves time or tokens
Without hooks, you manually run linters, formatters, and tests after Claude Code modifies files. Hooks automate this entirely. Every file write triggers your configured checks, catching issues immediately rather than at the end of a long coding session.
Hooks also reduce token waste. If a linter catches an error right after the file is written, Claude Code can fix it in the next turn instead of generating more code on top of a broken file.
How to use
- Open your Claude Code settings file:
# Global: ~/.claude/settings.json
# Per-project: .claude/settings.json
- Add hook configurations:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write",
"hooks": ["eslint --fix $CLAUDE_FILE_PATH"]
}
],
"PostToolUse": [
{
"matcher": "Write",
"hooks": ["prettier --write $CLAUDE_FILE_PATH"]
}
]
}
}
- Claude Code will now run these hooks automatically when the matched tool is used.
Example
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": ["echo 'About to run a bash command'"]
}
],
"PostToolUse": [
{
"matcher": "Write",
"hooks": [
"eslint --fix $CLAUDE_FILE_PATH",
"prettier --write $CLAUDE_FILE_PATH"
]
},
{
"matcher": "Bash",
"hooks": ["echo 'Bash command completed'"]
}
],
"PostTask": [
{
"matcher": ".*",
"hooks": ["npm test 2>&1 | tail -20"]
}
]
}
}
This configuration lints and formats after every file write, logs bash command execution, and runs the test suite after each completed task.
Related on TokRepo
- AI coding tools -- Explore tools for AI-assisted development workflows
- Automation tools -- Browse other automation and CI/CD tools
Common pitfalls
- Hook scripts must be available on the system PATH. If ESLint is installed locally in node_modules, use
npx eslintinstead of bareeslintin the hook command. - Long-running hooks block Claude Code's execution. Keep hooks fast (under 5 seconds). Move heavy operations like full test suites to PostTask rather than PostToolUse.
- The
$CLAUDE_FILE_PATHvariable is only available for file-related tools like Write. Using it with non-file tools like Bash will produce an empty string or an error.
常见问题
Claude Code supports PreToolUse (before a tool call), PostToolUse (after a tool call), and PostTask (after an entire task completes). Each event type can have multiple hooks with different matchers. The matcher field specifies which tool triggers the hook.
Yes. You can configure PostTask hooks to send notifications via curl to Slack, Discord, or email services. For example, a hook like 'curl -X POST https://hooks.slack.com/... -d ...' sends a message when a task completes.
Hooks run synchronously and block Claude Code until they complete. This is by design -- it ensures that lint errors are caught before Claude Code proceeds. For long-running operations, consider using background processes or moving them to PostTask.
You can override hooks by using a per-project .claude/settings.json with an empty hooks configuration. There is no built-in flag to skip hooks for a single session, but you can temporarily rename the settings file.
Hook output (stdout and stderr) is displayed in the Claude Code terminal. If a hook fails, the error message appears inline. Test your hook commands manually in the terminal first to ensure they work before adding them to the configuration.
引用来源 (3)
- Claude Code Documentation— Claude Code hooks for PreToolUse and PostToolUse
- Anthropic Docs— Settings configuration in ~/.claude/settings.json
- Claude Code GitHub— Hook matchers and event types
来源与感谢
讨论
相关资产
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 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 Forge — Plugin Framework for Claude Code
Supercharge Claude Code with 11 AI agents, 36 commands, and 15 skills. The oh-my-zsh-inspired plugin framework with 6-layer security hooks. 5-minute install. 640+ GitHub stars.