Claude Code Hooks
Claude Code hooks allow you to execute custom TypeScript logic in response to lifecycle events like session start, tool usage, and user prompts. Hooks are powerful automation tools that can enforce workflows, add context, or modify behavior.What are Hooks?
Hooks are TypeScript functions that run automatically when specific events occur in Claude Code:- SessionStart: Run code when a new conversation begins
- UserPromptSubmit: Intercept and modify user prompts
- ToolUse: Monitor or block tool usage
- Custom Events: Create your own hook types
Installation
From PRPM Registry
The easiest way to install hooks is from the PRPM registry:Manual Installation
You can also create hooks manually:- Create a TypeScript file in
.claude/hooks/ - Add a
hook.jsonmanifest file - Register the hook in
prpm.json
Hook Structure
hook.json
Every hook needs ahook.json manifest:
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Hook name (kebab-case) |
version | string | Yes | Semver version |
description | string | Yes | What the hook does |
type | string | Yes | Hook event type |
enabled | boolean | Yes | Whether hook is active |
config | object | No | Hook-specific configuration |
hook.ts
Your TypeScript implementation:Hook Types
SessionStart
Runs when a new Claude Code session begins. Perfect for adding project-specific context or reminders. Use Cases:- Load project configuration
- Add coding standards reminders
- Check git status
- Load environment-specific context
UserPromptSubmit
Intercepts user prompts before they reach Claude. Can modify, block, or add context to prompts. Use Cases:- Add context automatically
- Enforce prompt patterns
- Block certain requests
- Log user interactions
ToolUse
Monitors tool usage and can block dangerous operations. Use Cases:- Prevent accidental deletions
- Log tool usage
- Require confirmation for dangerous operations
- Enforce tool usage patterns
Registering Hooks
After creating a hook, register it in yourprpm.json:
Building Hooks
Hooks must be compiled from TypeScript to JavaScript before Claude Code can use them.Automatic Building
Use thescripts field in your root prpm.json to automatically build hooks before publishing:
prpm publish, the prepublishOnly script runs automatically, ensuring hooks are always built before publishing.
Manual Building
Best Practices
1. Use prepublishOnly Scripts
Always useprepublishOnly to build hooks automatically:
2. Keep Hooks Fast
Hooks run on every event - keep them performant:3. Handle Errors Gracefully
Don’t let hook errors crash sessions:4. Use TypeScript Strictly
Enable strict mode for type safety:5. Document Your Hooks
Add comments explaining what your hooks do:Advanced Configuration
Conditional Hooks
Enable hooks only in certain conditions:Hook Priorities
Control hook execution order with priorities:Shared Configuration
Useconfig to share settings between hook code and Claude:
Troubleshooting
Hook Not Running
Check:- Is
enabled: truein hook.json? - Is the hook registered in prpm.json?
- Is dist/hook.js compiled and up to date?
- Does Claude Code have permission to read the file?
Build Errors
Common issues:- Missing dependencies: Run
npm installin the hook directory - TypeScript errors: Check your type definitions
- Import errors: Verify @claude/hooks is installed
Performance Issues
Solutions:- Move slow operations outside hooks
- Cache expensive computations
- Use async operations carefully
- Set timeout in hook.json config
Examples
Workspace Standards Hook
Git Branch Reminder
Next Steps
Publishing Hooks
Share your hooks with others
Hook Writer Skill
Install the hook writer skill to help create hooks