by Ruya-AI
Context cleaning for Claude Code — prune bloated sessions, protect Agent Teams from context loss, auto-guard with tiered pruning
# Add to your Claude Code skills
git clone https://github.com/Ruya-AI/cozempic
20,000+ power users trust Cozempic to keep their Claude Code sessions lean.
Context cleaning for Claude Code ��� remove the bloat, keep everything that matters, protect Agent Teams from context loss.
Claude Code sessions fill up with dead weight: progress ticks, thinking blocks, stale file reads, duplicate CLAUDE.md injections, base64 screenshots, oversized tool outputs, and metadata bloat. A typical session carries 8-46MB — most of it noise that inflates every API call.
Cozempic removes it with 17 composable strategies across 3 prescription tiers, while your actual conversation, decisions, and working context stay untouched. The guard daemon runs automatically — install once, forget about it.
No comments yet. Be the first to share your thoughts!
usageZero external dependencies. Python 3.10+ stdlib only.
pip install cozempic
cozempic init
That's it. The guard daemon auto-starts on every session via hooks. No second terminal needed.
pip install cozempic
Then inside Claude Code:
/plugin marketplace add Ruya-AI/cozempic
/plugin install cozempic
This gives you MCP tools, skills (/cozempic:diagnose, /cozempic:treat, etc.), and auto-wired hooks.
# Auto-detect and diagnose the current session
cozempic current --diagnose
# Dry-run the standard prescription
cozempic treat current
# Apply with backup
cozempic treat current --execute
# Go aggressive on a specific session
cozempic treat <session_id> -rx aggressive --execute
# Check for session corruption
cozempic doctor
# View behavioral digest rules
cozempic digest show
# Show all strategies & prescriptions
cozempic formulary
| # | Strategy | Tier | What It Does | Expected |
|---|----------|------|-------------|----------|
| 1 | compact-summary-collapse | gentle | Remove all pre-compaction messages (already in the summary) | 85-95% |
| 2 | attribution-snapshot-strip | gentle | Strip attribution-snapshot metadata entries | 0-2% |
| 3 | progress-collapse | gentle | Collapse consecutive and isolated progress tick messages | 40-48% |
| 4 | file-history-dedup | gentle | Deduplicate file-history-snapshot messages | 3-6% |
| 5 | metadata-strip | gentle | Strip token usage stats, stop_reason, costs | 1-3% |
| 6 | thinking-blocks | standard | Remove/truncate thinking content + signatures | 2-5% |
| 7 | tool-output-trim | standard | Trim large tool results (>8KB or >100 lines), microcompact-aware | 1-8% |
| 8 | tool-result-age | standard | Compact old tool results by age — minify mid-age, stub old | 10-40% |
| 9 | stale-reads | standard | Remove file reads superseded by later edits | 0.5-2% |
| 10 | system-reminder-dedup | standard | Deduplicate repeated system-reminder tags | 0.1-3% |
| 11 | tool-use-result-strip | standard | Strip toolUseResult envelope field (Edit diffs, never sent to API) | 5-50% |
| 12 | image-strip | aggressive | Strip old base64 image blocks, keep most recent 20% | 1-40% |
| 13 | http-spam | aggressive | Collapse consecutive HTTP request runs | 0-2% |
| 14 | error-retry-collapse | aggressive | Collapse repeated error-retry sequences | 0-5% |
| 15 | background-poll-collapse | aggressive | Collapse repeated polling messages | 0-1% |
| 16 | document-dedup | aggressive | Deduplicate large document blocks (CLAUDE.md injection) | 0-44% |
| 17 | mega-block-trim | aggressive | Trim any content block over 32KB | safety net |
| 18 | envelope-strip | aggressive | Strip constant envelope fields (cwd, version, slug) | 2-4% |
| Prescription | Strategies | Risk | Typical Savings |
|---|---|---|---|
| gentle | 5 | Minimal | 85-95% (with compact boundary) |
| standard | 11 | Low | 25-45% |
| aggressive | 18 | Moderate | 35-60% |
Dry-run is the default. Nothing is modified until you pass --execute. Backups are always created.
The guard daemon monitors your session and prunes automatically:
# Auto-starts via SessionStart hook after cozempic init
# Or run manually:
cozempic guard --daemon
4-tier proactive pruning (every 30s):
| Tier | Threshold | Action | Reload? | |------|-----------|--------|---------| | Soft | 25% | gentle file cleanup | No | | Hard | 55% | standard prune | Yes (deferred if agents active) | | Emergency | 80% | aggressive prune | Yes (forced) | | User | 90% | manual aggressive | Yes |
Reactive overflow recovery — kqueue/polling file watcher detects inbox-flood overflow within milliseconds, auto-prunes with escalating prescriptions, circuit breaker prevents loops.
tmux/screen — reload resumes in the same pane via send-keys. Plain terminals open a new window.
Token thresholds auto-detect — 200K and 1M models detected automatically. Override with COZEMPIC_CONTEXT_WINDOW=200000 for Pro plan.
Cozempic extracts your corrections and persists them across compactions:
# View extracted rules
cozempic digest show
# Manually extract from current session
cozempic digest update
# Sync rules to Claude Code's memory system
cozempic digest inject
How it works:
~/.claude/projects/<cwd>/memory/)When Claude's auto-compaction fires, Agent Teams lose coordination state. Cozempic prevents this with five layers:
cozempic doctor # Diagnose issues
cozempic doctor --fix # Auto-fix where possible
| Check | What It Detects | Auto-Fix |
|-------|----------------|----------|
| trust-dialog-hang | Resume hangs on Windows | Reset flag |
| claude-json-corruption | Truncated/corrupted JSON | Restore from backup |
| corrupted-tool-use | tool_use.name >200 chars | Parse and repair |
| orphaned-tool-results | tool_result missing matching tool_use — causes 400 errors | Strip orphans |
| zombie-teams | Stale team directories with dead agents | Remove stale dirs |
| oversized-sessions | Session files >50MB | — |
| stale-backups | Old .jsonl.bak files wasting disk | Delete old backups |
| disk-usage | Session storage exceeding healthy thresholds | — |
cozempic init Wire hooks + slash command into project
cozempic list List sessions with sizes and token estimates
cozempic current [-d] Show/diagnose current session
cozempic diagnose <session> Analyze bloat sources
cozempic treat <session> [-rx PRESET] Run prescription (dry-run default)
cozempic treat <session> --execute Apply changes with backup
cozempic strategy <name> <session> Run single strategy
cozempic reload [-rx PRESET] Treat + auto-resume in new terminal
cozempic checkpoint [--show] Save team state to disk
cozempic guard [--daemon] Start guard (auto-starts via hook)
cozempic doctor [--fix] Check for known issues
cozempic digest [show|update|clear|flush|recover|inject]
cozempic self-update Upgrade to latest version from PyPI
cozempic formulary Show all strategies & prescriptions
After cozempic init, these hooks are wired automatically:
| Hook | When | What |
|------|------|------|
| SessionStart | Session opens | Guard daemon + digest inject |
| PostToolUse[Task] | Agent spawn | Team checkpoint |
| PostToolUse[TaskCreate\|TaskUpdate] | Todo changes | Team checkpoint |
| PreCompact | Before compaction | Checkpoint + digest flush |
| Stop | Session end | Checkpoint + digest flush |
--execute required to modify fileswrite → fsync → os.replace() — no partial writes.jsonl.bak before any modification