Automate context loading and knowledge capture using Claude Code hooks.
-
Copy hooks to your project:
mkdir -p .ai/hooks cp hooks/*.sh .ai/hooks/ chmod +x .ai/hooks/*.sh
-
Configure hooks:
mkdir -p .claude cp hooks/settings.json .claude/settings.json
-
Approve hooks in Claude Code:
claude /hooks # Review and approve # Restart Claude Code to activate
File: session-resume.sh
Automatically displays context when starting a session:
- Recent session notes
- Current TODOs
- Recent decisions
- Active scratchpad
- Available knowledge patterns
Configuration:
{
"hooks": {
"SessionStart": [{
"hooks": [{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.ai/hooks/session-resume.sh"
}]
}]
}
}Example Output:
=== 📚 Context Resume ===
📝 Recent Session Notes:
Session 2024-01-15: Working on JWT auth...
📋 Current TODOs:
- [ ] Implement JWT middleware
- [ ] Add bcrypt hashing
💭 Recent Decisions:
[2024-01-15] Use RS256 for JWT signing
=== Ready to Continue ===
File: session-end.sh
Saves session state when ending:
- Timestamps session end in NOTES.md
- Archives scratchpad if it has content
- Preserves investigation history
Configuration:
{
"hooks": {
"SessionEnd": [{
"hooks": [{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.ai/hooks/session-end.sh"
}]
}]
}
}File: post-edit-reminder.sh
Reminds you to capture successful patterns after edits.
Configuration:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write|Edit|MultiEdit",
"hooks": [{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.ai/hooks/post-edit-reminder.sh"
}]
}]
}
}Example Output:
💡 Tip: If this worked well, capture the pattern:
.ai/knowledge/patterns/[domain].md
File: pre-compact.sh
Reminds you to create a recap before context compaction.
Configuration:
{
"hooks": {
"PreCompact": [{
"hooks": [{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.ai/hooks/pre-compact.sh"
}]
}]
}
}Example Output:
⚠️ Context window approaching limit
💡 Consider creating a 'Previously On...' recap:
./scripts/context-compact.sh --summarize
Simple echo reminder when stopping Claude Code.
Configuration:
{
"hooks": {
"Stop": [{
"hooks": [{
"type": "command",
"command": "echo '\n💾 Remember to update TODO.md and capture successful patterns'"
}]
}]
}
}Claude Code hooks require approval for security:
- Hooks are defined in
.claude/settings.json - First run, use
/hooksto review - Approve trusted hooks
- Restart Claude Code
Important: Only approve hooks you understand and trust.
Edit hook scripts to customize behavior:
# Example: Add context size check to session-resume.sh
if [[ -d ".ai/memory" ]]; then
total_size=$(du -sh .ai/memory | cut -f1)
echo "📊 Context Size: $total_size"
fiExtend session-resume.sh with project-specific info:
# Show current git branch
echo "🌿 Branch: $(git branch --show-current)"
# Show recent commits
echo "📝 Recent commits:"
git log --oneline -3Create new hooks for your workflow:
# .ai/hooks/pre-commit-check.sh
#!/bin/bash
# Remind to update DECISIONS.log before commits
if git diff --cached --quiet; then
echo "💡 Large commit detected"
echo "Did you update .ai/memory/DECISIONS.log?"
fiAdd to settings.json:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash.*git commit",
"hooks": [{
"type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.ai/hooks/pre-commit-check.sh"
}]
}]
}
}Hooks work seamlessly with the context management scripts:
# Hook runs automatically on SessionStart
# Manually run for detailed view:
./scripts/session-resume.sh --full# Hook reminds when context is large
# Run script to actually compact:
./scripts/context-compact.sh --summarize- Check hook approval:
/hooksin Claude Code - Verify script permissions:
chmod +x .ai/hooks/*.sh - Check script path in settings.json
- Restart Claude Code
View hook output in Claude Code:
# Test hook manually
bash .ai/hooks/session-resume.sh
# Check for errors
echo $?Remove from .claude/settings.json or use /hooks to disable.
- ✅ Keep hooks fast (<5 seconds)
- ✅ Use timeouts in settings.json
- ✅ Test hooks manually before adding
- ✅ Commit .claude/settings.json for team sharing
- ❌ Don't run expensive operations in hooks
- ❌ Don't modify files in PreCompact (read-only)
- ❌ Don't add hooks that require user input
- ❌ Don't use hooks for critical operations
Some hooks receive JSON input:
#!/bin/bash
# Parse SessionStart input
input=$(cat)
session_id=$(echo "$input" | jq -r '.session_id')
transcript_path=$(echo "$input" | jq -r '.transcript_path')
echo "Session ID: $session_id"Run hooks only in certain conditions:
#!/bin/bash
# Only show context if memory directory exists
if [[ ! -d ".ai/memory" ]]; then
exit 0
fi
# Show context...Share knowledge across projects:
#!/bin/bash
# Load patterns from team knowledge base
if [[ -d "../team-knowledge/patterns" ]]; then
echo "📚 Team Knowledge Available:"
ls ../team-knowledge/patterns/*.md
fi- Claude Code Hooks Documentation
- Main README - Core context persistence concepts
- Scripts README - Manual context management tools