Automatic context cleanup and session management for Claude Code
This plugin automatically manages your Claude Code context by:
- 🧹 Cleaning up tool use/result pairs to save tokens
- 🔄 Auto-restarting sessions for optimal performance
- 📦 Maintaining backup history with timestamps
- 🔗 Repairing UUID chains for session integrity
After each tool use (Read, Edit, Grep, etc.), the plugin:
- Waits 4 turns to ensure data is no longer needed
- Removes tool_use and tool_result pairs from transcript
- Repairs UUID parent-child links automatically
- Keeps backups (last 5 with timestamps)
- tmux users: Seamlessly restarts in the same pane
- Non-tmux users: Auto-installs tmux and creates new session
- Context preserved: Uses
--continueflag for instant resume
Real-world example from our production tests:
📊 Cleanup Results:
- Removed Tool Pairs: 134
- Total Messages Removed: 264
- Repaired UUID Links: 103
- Message Count: 1186 → 922
- 🎯 Token Usage: 192k → 103k (46% reduction!)
💡 Another session: 170k → 50k (70% reduction!)
curl -fsSL https://raw.githubusercontent.com/professional-ALFIE/claude-code-context-manager/main/install.sh | bash- Clone the repository:
cd ~/.claude/plugins
git clone https://github.com/professional-ALFIE/claude-code-context-manager.git- Run the installer:
cd claude-code-context-manager
chmod +x install.sh
./install.sh-
The installer will:
- Copy hooks to your project's
.claude/hooks/directory - Set up the state management system
- Update your
.claude/settings.json(or create if missing) - Make scripts executable
- Copy hooks to your project's
-
Start Claude Code in tmux:
# First time: install tmux if not installed
brew install tmux # macOS
# or
sudo apt-get install tmux # Linux
# Start tmux session
tmux
# Run Claude Code inside tmux
claudeTool Use → PostToolUse Hook → countdown=5
↓
Turn 1: Stop Hook → "4 turns remaining..."
↓
Turn 2: Stop Hook → "3 turns remaining..."
↓
Turn 3: Stop Hook → "2 turns remaining..."
↓
Turn 4: Stop Hook → 🧹 Runs context-tool-cleaner.py → "1 turn remaining..."
↓
Turn 5: Stop Hook → 🔄 Restarts session (--continue)
- Scan: Find all tool_use and tool_result pairs
- Match: Pair them by tool_use_id
- Remove: Delete matched pairs from transcript
- Repair: Fix UUID chains (parentUuid → uuid)
- Backup: Save timestamped backup before writing
- Cleanup: Remove old backups (keep last 5)
The plugin automatically adds these hooks to .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Read|Edit|Write|Grep|Glob|Bash",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/post-tool-use.sh"
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/stop-hook.sh"
}
]
}
]
}
}Edit .claude/hooks/post-tool-use.sh:
# Default: 5 turns
jq '.countdown = 5' ...
# Shorter: 3 turns
jq '.countdown = 3' ...Edit .claude/hooks/scripts/context-tool-cleaner.py:
# Default: Keep 5 backups
if len(old_backups) > 5:
# Keep more: 10 backups
if len(old_backups) > 10:Edit .claude/settings.json:
"matcher": "Read|Edit|Write" // Only track these tools- Claude Code: Latest version
- Python 3: For context-tool-cleaner.py
- jq: JSON processor (auto-installed by installer)
- tmux: Required for auto-restart feature
For the auto-restart feature to work, you must:
- Install tmux first
- Start Claude Code inside a tmux session
# Install tmux
brew install tmux # macOS
# or
sudo apt-get install tmux # Linux
# Start tmux session
tmux
# Then run Claude Code inside tmux
claude- ✅ macOS (tested)
- ✅ Linux (tested)
⚠️ Windows: Requires WSL + tmux
# Check permissions
chmod +x .claude/hooks/*.sh
chmod +x .claude/hooks/scripts/*.py
# Verify settings
cat .claude/settings.json | jq '.hooks'The cleaner automatically keeps only the last 5 backups. If you see more:
# Manual cleanup
rm .claude-acc-*/projects/**/*.jsonl.bak.*For non-tmux users, ensure tmux is installed:
# macOS
brew install tmux
# Linux
sudo apt-get install tmuxYou can run the cleaner manually on any transcript:
python3 .claude/hooks/scripts/context-tool-cleaner.py /path/to/transcript.jsonlComment out the tmux section in .claude/hooks/stop-hook.sh:
# if [ -n "$TMUX" ]; then
# ...
# ficlaude-code-context-manager/
├── README.md # This file
├── install.sh # Installation script
├── .gitignore # Git ignore rules
├── settings.example.json # Example settings
└── hooks/
├── post-tool-use.sh # Triggers countdown
├── stop-hook.sh # Manages countdown & restart
└── scripts/
└── context-tool-cleaner.py # Core cleanup logic
Pull requests welcome! Please:
- Test on your setup
- Update README if adding features
- Keep backward compatibility
MIT License - Feel free to use and modify!
Built for the Claude Code community to maximize context window efficiency.
Questions? Open an issue on GitHub!