Event hooks for AI automation. Execute scripts before or after Claude Code actions.
Hooks are shell scripts that execute in response to Claude Code events. Use them for validation, logging, formatting, and automation.
.claude/hooks/*.shProject-level hook scripts
| Hook | Trigger | Use Case |
|---|---|---|
PreToolUse | Before tool execution | Validate, block dangerous ops |
PostToolUse | After tool execution | Logging, cleanup |
UserPromptSubmit | User sends message | Preprocessing, logging |
PostCommit | After git commit | Lint, test, notify |
Validate or block tool executions before they run.
#!/bin/bash
# .claude/hooks/pre-tool-use.sh
# Runs before any tool execution
TOOL_NAME="$1"
TOOL_ARGS="$2"
# Block dangerous operations
if [[ "$TOOL_NAME" == "Bash" && "$TOOL_ARGS" == *"rm -rf"* ]]; then
echo "BLOCKED: Dangerous rm -rf operation"
exit 1
fi
# Log tool usage
echo "[$(date)] Tool: $TOOL_NAME" >> ~/.claude/logs/tools.log
exit 0Run automation after successful commits.
#!/bin/bash
# .claude/hooks/post-commit.sh
# Runs after successful git commit
# Auto-format staged files
pnpm lint --fix
# Run quick tests
pnpm test --watch=false --passWithNoTests
# Notify success
echo "Post-commit checks passed"Process user input before Claude sees it.
#!/bin/bash
# .claude/hooks/user-prompt-submit.sh
# Runs when user submits a prompt
PROMPT="$1"
# Add timestamp to conversation
echo "[$(date '+%Y-%m-%d %H:%M')] User prompt received"
# Check for sensitive patterns
if [[ "$PROMPT" == *"API_KEY"* || "$PROMPT" == *"SECRET"* ]]; then
echo "WARNING: Prompt may contain sensitive data"
fi
exit 0Enable hooks in your settings file.
// .claude/settings.json
{
"hooks": {
"preToolUse": ".claude/hooks/pre-tool-use.sh",
"postToolUse": ".claude/hooks/post-tool-use.sh",
"userPromptSubmit": ".claude/hooks/user-prompt-submit.sh"
},
"permissions": {
"allow": ["Bash(git:*)"]
}
}