A lightweight Python-based hook system for Claude Code that provides automatic validation and quality checks.
Claude Code Hooks allows you to set up automatic validation, quality checks, and notifications that run during your Claude Code sessions. The system uses Python scripts that integrate seamlessly with Claude's hook events.
- π Python-based - Simple, portable hook implementation
- β Code Quality Validation - Enforces clean code standards
- π¦ Package Age Checking - Prevents installation of outdated packages
- π Task Completion Notifications - Get notified when Claude finishes tasks
- π― Easy Installation - One command setup
- ποΈ Hierarchical Configuration - Different rules for different directories
- π Hook Introspection - See which hooks apply to any file
python3 install-hooks.pyThis will:
- Create a
.claude/directory in your project - Copy all hook scripts to
.claude/hooks/ - Generate
.claude/settings.jsonwith hook configurations - Add
.claude/settings.local.jsonto your.gitignore
The hook system uses three main entry points that Claude Code calls:
- PreToolUse - Runs before tools like Bash, Write, or Edit are executed
- PostToolUse - Runs after tools complete
- Stop - Runs when Claude stops or completes a task
Each hook receives event data via stdin and can:
- Provide suggestions and warnings
- Block operations that violate policies
- Send notifications
- Log activities
Enforces clean code standards on file edits:
- Maximum function length (30 lines)
- Maximum file length (200 lines)
- Maximum line length (100 characters)
- Maximum nesting depth (4 levels)
Prevents installation of outdated npm/yarn packages:
- Blocks packages older than 180 days (configurable)
- Shows latest available versions
- Runs on
npm installandyarn addcommands
Sends notifications when Claude completes tasks:
- Pushover support for mobile notifications
- macOS native notifications
- Linux desktop notifications
The hook system supports directory-specific configurations:
- Root Config:
.claude/hooks.json- Default settings for all files - Directory Overrides:
.claude-hooks.jsonfiles in any directory - Config Inheritance: Child directories inherit and can override parent settings
Example structure:
project/
βββ .claude/
β βββ hooks.json # Root configuration
βββ backend/
β βββ .claude-hooks.json # Stricter rules for backend
βββ frontend/
βββ .claude-hooks.json # Different rules for frontend
See which hooks apply to your files:
# List all hooks in the project
python3 hooks/list_hooks.py list
# Show effective hooks for a specific file
python3 hooks/list_hooks.py explain path/to/file.pyMAX_AGE_DAYS- Maximum age for packages (default: 180)CLAUDE_HOOKS_TEST_MODE- Enable test modePUSHOVER_USER_KEY- Your Pushover user keyPUSHOVER_APP_TOKEN- Your Pushover app token
- Get the Pushover app ($5 one-time): https://pushover.net/clients
- Create an app at: https://pushover.net/apps/build
- Add to your
.envfile:
PUSHOVER_USER_KEY=your_user_key
PUSHOVER_APP_TOKEN=your_app_token
The system uses a dispatcher pattern:
universal-*.py- Main dispatchers that route to specific hooks- Individual hook files handle specific functionality
- All hooks use JSON for input/output communication
- Create a new Python script in
hooks/ - Read JSON input from stdin
- Output JSON response with
actionfield - Register in the appropriate universal dispatcher
# Test with sample input
echo '{"tool_name": "Write", "file_path": "test.py"}' | python3 hooks/post-tool-hook.py
# Enable test mode
export CLAUDE_HOOKS_TEST_MODE=1MIT License - See LICENSE file for details