Claude Code plugin that tracks token usage, identifies wasted context, and saves 30-50% on API costs. Heatmaps, ROI reports, budget alerts, efficiency scores, git-aware suggestions — all local, zero config.
npx skills add https://github.com/egorfedorov/claude-context-optimizer --skill cco-claudemdقم بتثبيت هذه المهارة باستخدام واجهة سطر الأوامر (CLI) وابدأ في استخدام سير عمل SKILL.md في مساحة عملك.
Stop burning tokens on files Claude never uses.
The average Claude Code session wastes 30-50% of tokens on files that are read but never actually used. Every Read call consumes context — whether the file was relevant or not.
package.json Claude reads "just in case"? 120 tokens, every time.At $15/M tokens (Opus), a developer spending $100/month is lighting $30-50 on fire on irrelevant context.
context-optimizer silently tracks every file read, edit, and search. It learns which files are actually useful and which are waste. Over time, it builds a profile of your coding patterns and tells you exactly where your tokens go — and how to stop wasting them.
The #1 token waste in Claude Code: re-reading the same file multiple times per session.
Read Cache runs as a PreToolUse hook and blocks redundant reads when the file hasn't changed.
💾 [read-cache] tracker.js is already in context (983 lines, ~9.3K tokens saved).
File unchanged — no need to re-read! Tip: use offset/limit to read a different section.
Typical savings: 30-60% fewer tokens per session from read deduplication alone.
.contextignore — block files you never needCreate a .contextignore file in your project root (like .gitignore) to permanently block wasteful reads. No more loading lockfiles, build output, or generated code.
# .contextignore
package-lock.json
yarn.lock
*.min.js
*.min.css
dist/**
node_modules/**
*.sql
🚫 [contextignore] package-lock.json matches pattern "package-lock.json" in .contextignore.
Use Grep to search inside, or remove the pattern from .contextignore to allow reading.
.contextignore in your repo root~/.claude/.contextignore for patterns across all projects*.lock, dist/**, *.min.js, *.generated.*.contextignore.example from the plugin to get startedWhen your context budget reaches 80%, the plugin automatically tells Claude to run /compact instead of just showing a warning. At 90%, it becomes urgent.
[context-budget] ⚡ Auto-compact recommended — 80% budget used.
Run /compact now to free ~12.5K tokens and keep the session efficient.
[context-budget] 🔴 Critical: 90% budget used (~90K/100K tokens).
Run /compact immediately or the session will lose older context.
/cco-budget auto on or /cco-budget auto offbudget-config.jsonEvery session automatically generates a summary saved to disk. Start your next session by running /cco-replay to see what was done before — no need to re-read files or guess context.
╔══════════════════════════════════════════════════════════════╗
║ RECENT SESSION SUMMARIES ║
╚══════════════════════════════════════════════════════════════╝
[1] Session Mar 24 14:30 (12 min)
Edited: src/read-cache.js, src/utils.js, README.md (3 files)
Context: 45K tokens, 12 files read, 28% waste
[2] Session Mar 24 10:15 (25 min)
Edited: src/tracker.js, src/budget.js (2 files)
Context: 82K tokens, 18 files read, 15% waste
Run /cco-anatomy to generate a compact project map. Claude reads one file instead of opening twenty to understand the codebase.
# Project Anatomy: my-app
Generated: 2024-01-15 | 45 files | ~38K tokens if all read
## Structure
| Path | Lines | ~Tokens | Type |
|------|-------|---------|------|
| src/server.ts | 450 | 4.1K | source |
| src/routes/api.ts | 280 | 2.6K | source |
...
## Heaviest files (read these with offset/limit)
1. src/server.ts — 450 lines (~4.1K tokens)
ContextShield runs as a PreToolUse hook and warns you before loading known-waste files. It checks historical patterns and suggests alternatives in real-time.
[context-shield] README.md went unused in 5 sessions (~12.4K tokens).
Use Grep to find specific content instead of full Read.
[context-shield] utils.js is usually edited with: tracker.js, budget.js.
Consider loading them together.
Run /cco-shield to see protection status and stats.
Run /cco-claudemd to analyze your CLAUDE.md for token waste: duplicates, verbose patterns, oversized code blocks, excessive whitespace. Get concrete suggestions with estimated savings.
CLAUDE.MD ANALYSIS — /project/CLAUDE.md — 342 lines | ~2.9K tokens
──────────────────────────────────────────────────────────────
⚠ 3 duplicate line(s) found (~25 saveable)
● "please make sure to" found 4x — Simplify to: "Always X" (~12 saveable)
● Code block at line 45 is 38 lines — consider shortening (~233 saveable)
○ 28% empty/separator lines — reduce for token savings (~98 saveable)
POTENTIAL SAVINGS: ~368 tokens
File patterns now have confidence scores (0.0-1.0) based on session count, usefulness consistency, and recency. High-confidence patterns produce stronger recommendations; old unused patterns decay naturally.
Run /cco-export html to generate a static HTML dashboard you can open in any browser:
Run /cco to get a visual breakdown of every file in your session. Green = useful. Red = waste.
Run /cco-report for a comprehensive dashboard: total tokens, waste ratio, cost estimates, trends, and actionable recommendations.
Run /cco-digest for a weekly efficiency grade (S/A/B/C/D/F) with breakdown by precision, edit ratio, search accuracy, and focus.
Set a token budget and get real-time warnings as you approach the limit. Auto-compact kicks in at 80%, critical alerts at 90%.
[context-budget] 70% of budget used (~70K/100K) | Est. cost: $1.050 (opus)
[context-budget] ⚡ Auto-compact recommended — 80% budget used.
[context-budget] You can free ~8.2K tokens with /compact:
drop README.md (~2.4K), tsconfig.json (~1.1K), package.json (~320)
Run /cco-git and the plugin analyzes your git diff, finds related test files, configs, and historically useful files — then suggests exactly what to load.
Create reusable context sets for different task types:
/cco-templates create bug-fix # Save files you always need for bug fixes
/cco-templates apply bug-fix # Load them instantly next time
The plugin learns from your behavior. When you start a new task, it silently suggests files you'll probably need based on historical patterns. No configuration required.
When installed as a plugin, commands are namespaced: /claude-context-optimizer:cco. With --plugin-dir, they're also available as /cco.
| Command | Description |
|---|---|
/cco |
Session heatmap — visual file-by-file token breakdown |
/cco-report |
Full ROI report — stats, trends, waste analysis, recommendations |
/cco-digest [days] |
Efficiency digest — score, grade, cost analysis (default: 7 days) |
/cco-budget [status|set|model|auto] |
Token budget — configure limits, cost model, auto-compact |
/cco-git |
Git-aware suggestions — smart file loading based on diff |
/cco-templates [list|create|apply|delete] |
Context templates — reusable file sets for task types |
/cco-export [md|html] |
Export reports — Markdown or static HTML dashboard |
/cco-clean |
Cleanup — remove old tracking data |
/cco-shield |
ContextShield status — waste protection stats |
/cco-claudemd |
CLAUDE.md analyzer — find and fix token bloat |
/cco-anatomy |
Project anatomy — compact codebase map with file sizes and token estimates |
/cco-replay [N] |
Session replay — recent session summaries for quick context recovery |
git clone https://github.com/egorfedorov/claude-context-optimizer.git ~/claude-context-optimizer
claude --plugin-dir ~/claude-context-optimizer
To make it persistent, add to ~/.claude/settings.json:
{
"plugins": [
"~/claude-context-optimizer"
]
}
This gives you full functionality: skills, auto-tracking hooks, Read Cache, ContextShield, and budget alerts.
npx skills add https://github.com/egorfedorov/claude-context-optimizer
Installs skills globally to ~/.agents/skills/ and symlinks them to Claude Code. Works with Amp, Cline, Codex, Cursor, Gemini CLI, and other compatible agents.
Note: Skills CLI installs skill prompts only. Auto-tracking hooks (Read Cache, ContextShield, budget alerts) require the plugin directory installation (Option 1) to function.
claude plugin update claude-context-optimizer@egorfedorov-plugins
Then restart Claude Code to apply the update.
Once installed, the plugin works automatically — no commands needed:
Before every file re-read (Read Cache):
On session start: Weekly savings streak, warnings about consistently wasted files, auto-generated template notifications.
Before every file read (ContextShield): Checks if the file was wasted in 3+ past sessions, suggests Grep alternatives, shows co-occurrence groups.
On every file read: Warns on 3+ reads without edits (suggests offset/limit), tiered warnings for large files (200+ soft, 500+ strong).
On budget thresholds (50%, 70%, 85%, 95%): Usage percentage, cost estimates, and at 85%+ lists specific files to drop with exact token savings.
On session end: Compares waste vs recent average, updates pattern database.
After 5+ sessions: Auto-creates file templates from frequently edited files.
You literally just code. The plugin watches and helps.
You use Claude Code normally
│
▼
┌─────────────────────┐
│ PreToolUse Hook │ Read Cache: blocks re-reads of unchanged files.
│ read-cache.js │ ContextShield: warns about historically wasted files.
│ context-shield.js │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ PostToolUse Hook │ Silent. Runs on every Read/Edit/Write/Glob/Grep/Agent.
│ tracker.js │ Records: file path, line count, token estimate, timestamp.
│ budget.js │ Tracks token accumulation, warns at thresholds.
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Session Store │ ~/.claude-context-optimizer/sessions/<id>.json
│ Per-file tracking │ Reads, edits, usefulness score, confidence score.
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ SessionEnd Hook │ Finalizes session. Computes waste. Updates patterns DB.
│ Confidence Learning │ Patterns scored 0.0-1.0, decay over time.
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ Reports & Insights │ /cco, /cco-report, /cco-digest, /cco-claudemd
│ HTML Dashboard │ Static Chart.js analytics, open in any browser
│ ContextShield │ Proactive waste prevention before file reads
│ Smart Suggestions │ Confidence-scored recommendations
└─────────────────────┘
A file is useful if: edited after reading (+3 per edit), read multiple times (+0.5 per re-read, diminishing), or partially read with offset/limit (+1 bonus).
A file is wasted if: usefulness score is zero or negative (read but never edited, no re-reads). Large files (100+ lines) read 3+ times without edits get a penalty.
Tokens are estimated using extension-specific ratios (e.g., 3.8 chars/token for JS/TS, 4.2 for Markdown, 3.2 for JSON) applied to line counts. Not exact, but consistent enough for comparative analysis.
~/.claude-context-optimizer/
├── sessions/ # Per-session tracking data (JSON)
├── budget/ # Per-session budget state
├── templates/ # User-defined context templates
├── exports/ # Exported reports (MD/HTML)
├── read-cache/ # Per-session read cache state
├── config.json # Budget and preference settings
├── patterns.json # Cross-session file usage patterns
└── global-stats.json # Aggregate statistics
claude-context-optimizer/
├── .claude-plugin/
│ └── plugin.json # Plugin manifest
├── src/
│ ├── utils.js # Shared constants, formatting, scoring, donation info
│ ├── read-cache.js # Smart Read Cache: blocks redundant file reads
│ ├── contextignore.js # .contextignore: pattern-based file blocking
│ ├── replay.js # Session Replay: recent session summaries
│ ├── anatomy.js # Project Anatomy: compact codebase map generator
│ ├── tracker.js # Core: file & token tracking engine + session summaries
│ ├── context-shield.js # ContextShield: PreToolUse waste prevention
│ ├── claudemd-analyzer.js # CLAUDE.md token bloat analyzer
│ ├── budget.js # Token budget monitor with alerts
│ ├── digest.js # Efficiency score & weekly digest
│ ├── git-context.js # Git-aware context suggestions
│ ├── report.js # ROI report generator
│ └── export.js # Chart.js HTML dashboard exporter
├── skills/
│ ├── cco/SKILL.md # /cco — session heatmap
│ ├── cco-report/SKILL.md # /cco-report — full ROI report
│ ├── cco-digest/SKILL.md # /cco-digest — efficiency digest
│ ├── cco-budget/SKILL.md # /cco-budget — budget manager
│ ├── cco-git/SKILL.md # /cco-git — git suggestions
│ ├── cco-export/SKILL.md # /cco-export — report export
│ ├── cco-templates/SKILL.md # /cco-templates — template manager
│ ├── cco-clean/SKILL.md # /cco-clean — data cleanup
│ ├── cco-shield/SKILL.md # /cco-shield — ContextShield status
│ ├── cco-claudemd/SKILL.md # /cco-claudemd — CLAUDE.md analyzer
│ ├── cco-anatomy/SKILL.md # /cco-anatomy — project anatomy
│ └── smart-loader/SKILL.md # Auto-suggestion skill (model-invoked)
├── agents/
│ └── context-analyzer.md # Deep analysis agent
├── hooks/
│ └── hooks.json # Hook configuration
├── assets/ # SVG visuals for README
├── docs/
│ └── index.html # Landing page (GitHub Pages)
└── package.json
This plugin:
~/.claude-context-optimizer//cco-clean --reset-allYour data never leaves your machine. Period.
Q: Does this slow down Claude Code?
A: No. Hook scripts run asynchronously and typically complete in <10ms.
Q: How accurate are the token estimates?
A: They use a ~4 tokens/line heuristic. Not exact, but consistent across sessions for reliable trends.
Q: Can I use this with Claude Sonnet / Haiku?
A: Yes. Set your model with /cco-budget model sonnet for accurate cost estimates.
Q: Will this work with subagents?
A: Yes. The PostToolUse hook fires for all tool calls, including those made by subagents.
Q: Does Read Cache break anything?
A: No. It only blocks truly redundant reads — same file, same range, no modifications since last read. If the file changed or you request a different section, the read goes through normally.
context-optimizer is 100% free and open source. No paywalls, no premium tiers, no telemetry. If it saves you money on tokens, consider supporting development:
| Chain | Address |
|---|---|
| BTC | bc1q428exz5t2h9rzk7z5ya70madh0j3rs6h4gfgyd |
| ETH (ERC-20) | 0xB3f0C8e42B7cA9d65920cEfe82e3fef1B5C9d0C9 |
| SOL | 8ctK8nt3CBkPZGfWQXX8TsnqUYUy4JAbT1EMhr8rsQxm |
PRs welcome: better token counting (AST-based), VSCode heatmap overlay, team pattern sharing, /cost integration, multi-language CLAUDE.md analysis.
MIT — do whatever you want with it.
Built with frustration at wasted tokens and love for efficiency.
If we saved you money, return the favor.