Skip to main content

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.
New to hooks? Install the hook writer skill to help you create hooks:
This skill teaches AI how to write TypeScript hooks with proper structure, best practices, and error handling.

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:
  1. Create a TypeScript file in .claude/hooks/
  2. Add a hook.json manifest file
  3. Register the hook in prpm.json
Example structure:

Hook Structure

hook.json

Every hook needs a hook.json manifest:
Fields:

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
Example:

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
Example:

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
Example:

Registering Hooks

After creating a hook, register it in your prpm.json:

Building Hooks

Hooks must be compiled from TypeScript to JavaScript before Claude Code can use them.

Automatic Building

Use the scripts field in your root prpm.json to automatically build hooks before publishing:
When you run prpm publish, the prepublishOnly script runs automatically, ensuring hooks are always built before publishing.

Manual Building

Best Practices

1. Use prepublishOnly Scripts

Always use prepublishOnly to build hooks automatically:
This prevents publishing stale JavaScript and keeps your dist files in sync.

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:
Higher priority = runs first.

Shared Configuration

Use config to share settings between hook code and Claude:
Access in your hook:

Troubleshooting

Hook Not Running

Check:
  1. Is enabled: true in hook.json?
  2. Is the hook registered in prpm.json?
  3. Is dist/hook.js compiled and up to date?
  4. Does Claude Code have permission to read the file?

Build Errors

Common issues:
  • Missing dependencies: Run npm install in 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