Overview
Integrate claude-mem with Claude Code through hooks that compress transcripts, load context, and manage memory. Configure hooks in Claude Code’ssettings.json
file.
Hook Types
Three main hooks integrate with Claude Code’s lifecycle:1. SessionStart Hook
- Triggered: When you start a new Claude Code session
- Purpose: Loads relevant context from your previous sessions
- Location:
~/.claude-mem/hooks/session-start.js
2. PreCompact Hook
- Triggered: Before Claude Code compacts/clears your session
- Purpose: Compresses your transcript into memory before data is lost
- Location:
~/.claude-mem/hooks/pre-compact.js
3. SessionEnd Hook
- Triggered: When you end your Claude Code session or use
/clear
- Purpose: Optionally saves your memories before clearing context
- Location:
~/.claude-mem/hooks/session-end.js
Hook Configuration File
Configure hooks in~/.claude/.claude/settings.json
:
Hook Configuration Options
Command Type
- type: Always
"command"
for claude-mem hooks - command: Absolute path to the hook script
- timeout: Maximum execution time in milliseconds (default: 120000)
Timeout Settings
Hook Behavior Customization
Environment Variables
You can control hook behavior with environment variables:Configuration File Overrides
You can create aconfig.json
in the hooks directory to override CLI commands:
Advanced Hook Configuration
Multiple Hook Instances
You can configure multiple hooks for the same event:Conditional Hook Execution
Your hooks can check conditions before executing:Hook Response Format
Your hooks communicate with Claude Code using standardized JSON responses:PreCompact Hook Response
SessionStart Hook Response
Error Response
Hook Development
Creating Custom Hooks
You can create custom hooks that integrate with claude-mem:Hook Payload Structure
Your hooks receive different payloads based on their type:Installation and Management
Automatic Installation
You can automatically install hooks during setup:Manual Hook Management
Troubleshooting Hooks
Common Issues
Hook Not Executing
-
Check file permissions:
-
Verify paths in settings:
-
Test hook manually:
Timeout Issues
- Increase timeout values in hook configuration
- Check for blocking operations in custom hooks
- Verify system performance
Context Not Loading
-
Check project name detection:
-
Verify memory existence:
-
Enable debug logging:
Debug Mode
Enable comprehensive debugging:Performance Optimization
Reducing Hook Latency
-
Optimize compression settings:
-
Limit context loading:
-
Async operations:
Memory Management
- Hooks run in separate processes
- Large context data is streamed, not held in memory
- Cleanup temporary files in custom hooks
Security Considerations
File Permissions
Path Validation
- Always use absolute paths in hook configuration
- Validate file paths in custom hooks
- Avoid user input in command execution
Environment Isolation
- Hooks inherit environment variables
- Sensitive data should use secure storage
- Consider using environment-specific configurations
Migration Guide
Upgrading Hook Configuration
When upgrading claude-mem versions:-
Backup current configuration:
-
Run installation:
- Restore custom modifications if needed
Version Compatibility
- v3.6.x: Current hook format (no changes required)
- v3.5.x: Compatible with current hooks
- v3.4.x and earlier: Requires reinstallation
Related Documentation
- Settings Configuration - Main settings file
- Claude Integration - Integration details
- CLI Reference - Installation commands
- Troubleshooting - Common issues