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.
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.
Frequently Asked Questions
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.
Citations (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
Related on TokRepo
Source & Thanks
- Claude Code Hooks Documentation
- Configured in
~/.claude/settings.json
Discussion
Related Assets
Claude-Flow — Multi-Agent Orchestration for Claude Code
Layers swarm and hive-mind multi-agent orchestration on top of Claude Code with 64 specialized agents, SQLite memory, and parallel execution.
ccusage — Real-Time Token Cost Tracker for Claude Code
CLI that reads ~/.claude logs and breaks down Claude Code token spend by day, session, and project — pluggable into your statusline.
SuperClaude — Workflow Framework for Claude Code
Adds 16+ slash commands, 9 cognitive personas, and a smart flag system to Claude Code in one pipx install.