Detect context rot in Claude Code before it causes problems.
A context corruption / context rot detection plugin for Claude Code CLI - automatically detect when Claude's context window has degraded using a simple "canary" instruction technique.
In the early days of coal mining, miners would bring canaries into the mines with them. These small birds are extremely sensitive to toxic gases like carbon monoxide and methane. If dangerous gases were present, the canary would show signs of distress or die before the gas levels became lethal to the miners, giving them time to evacuate.
This plugin applies the same principle to AI context management. Instead of a bird, we use a simple instruction that Claude must follow (like "start every response with ///"). When Claude stops following this trivial instruction, it's our "canary" warning us that the context has become corrupted - time to run /compact or /clear!
When using Claude Code for extended sessions, the context window can become "corrupted" or "rotted" - Claude starts forgetting instructions, ignoring rules in CLAUDE.md, or producing inconsistent outputs. This is especially problematic when:
- Working on long coding sessions
- Having many back-and-forth exchanges
- Context window approaching capacity
This plugin uses a "canary in the coal mine" approach:
- Add a simple, easy-to-verify instruction to your CLAUDE.md (e.g., "Every response must start with
///") - The plugin monitors Claude's responses
- When Claude stops following this trivial instruction, it's a reliable indicator that context has degraded
- You get notified to run
/compactor/clear
- π Real-time monitoring - Daemon watches all Claude Code sessions
- π System notifications - Desktop alerts on macOS and Linux
- βοΈ Auto Compact threshold - Configure when auto-compaction triggers
- π« No jq dependency - Pure bash implementation
- π₯οΈ Cross-platform - Works on macOS and Linux
- π Auto-start - Runs on system boot (LaunchAgent/systemd)
git clone https://github.com/MichaelYangzk/claude-context-canary.git
cd claude-context-canary
./install-global.shThe installer will:
- Configure Auto Compact threshold (recommended: 50-70%)
- Set up the canary pattern (default:
^///) - Install the monitoring daemon
- Configure auto-start on boot
- Optionally add the canary instruction to your
~/.claude/CLAUDE.md
./install-global.shMonitors all Claude Code projects system-wide.
./install.shChoose between:
- Hook Method - Checks previous response when you send a message
- Daemon Method - Independent background process for real-time monitoring
- Both
Add to your CLAUDE.md file:
## Canary Instruction
Every response must start with ///Edit ~/.claude/canary-config.json:
{
"canary_pattern": "^///",
"failure_threshold": 2,
"auto_action": "warn",
"check_interval": 2
}| Parameter | Description | Default |
|---|---|---|
canary_pattern |
Regex to verify response format | ^/// |
failure_threshold |
Failures before critical alert | 2 |
auto_action |
warn or block conversation |
warn |
check_interval |
Check interval in seconds | 2 |
~/.claude/plugins/canary-daemon-global.sh status # Check status
~/.claude/plugins/canary-daemon-global.sh restart # Restart daemon
~/.claude/plugins/canary-daemon-global.sh stop # Stop daemon
~/.claude/plugins/canary-daemon-global.sh watch # Run in foreground (debug)- First failure: Warning notification
- Consecutive failures: Critical alert recommending
/compact - If
auto_action=block: Prevents sending more messages until cleared
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β CLAUDE.md β β Canary Daemon β β Notification β
β β β β β β
β "Start with ///"ββββββΆβ Monitor responsesββββββΆβ "Context rot β
β β β Check pattern β β detected!" β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
- Daemon monitors
~/.claude/projects/*/transcript.jsonlfiles - Extracts Claude's responses and checks against canary pattern
- Tracks consecutive failures
- Sends system notification when threshold exceeded
- Daemon log:
~/.claude/canary.log - State file:
~/.claude/canary-state.json - Config file:
~/.claude/canary-config.json
/compact or /clear. It only:
- Sends warning notifications
- Optionally blocks conversation until you take action
You must manually run /compact or /clear when notified.
- macOS: System Preferences β Notifications β Allow from Terminal
- Linux: Install
notify-send(apt install libnotify-bin)
# Check logs
cat ~/.claude/canary.log
# Run in foreground to debug
~/.claude/plugins/canary-daemon-global.sh watch- Adjust
canary_patternregex - Increase
failure_threshold - Make sure your canary instruction is clear and simple
# Stop daemon
~/.claude/plugins/canary-daemon-global.sh stop
# Remove files
rm -f ~/.claude/plugins/canary-*.sh
rm -f ~/.claude/canary-config.json
rm -f ~/.claude/canary-state.json
# macOS: Remove LaunchAgent
launchctl unload ~/Library/LaunchAgents/com.claude.canary.plist
rm -f ~/Library/LaunchAgents/com.claude.canary.plist
# Linux: Remove systemd service
systemctl --user disable claude-canary.service
rm -f ~/.config/systemd/user/claude-canary.service- Context window management - Managing LLM context limits
- Context rot / context corruption - Degradation of AI response quality over extended sessions
- Prompt injection detection - Monitoring AI behavior consistency
- Claude Code CLI - Anthropic's official CLI tool for Claude
Issues and PRs welcome!
MIT License
Keywords: Claude Code, context rot, context corruption, context window, LLM monitoring, Claude CLI, AI context management, canary test, prompt degradation detection