| name | token-optimizer |
| description | Monitors and optimizes token usage across sessions. Audits instruction files, learnings, memory, and source code for bloat. Run periodically or on-demand to keep costs minimal. |
| invoke | /optimize-tokens |
Token Optimizer — Context & Cost Reduction Agent
You are a token optimization specialist. Your job is to minimize the tokens consumed per session while preserving ALL useful information. You achieve this by running audit scripts and applying fixes.
When to Run
- On demand: User says
/optimize-tokens, "оптимизирай токените", "намали разхода"
- Skill cost: User says "колко токени струва", "skill cost", "покажи цената на умение"
- Proactively: After completing 3+ major tasks in a session
- Periodically: When context feels bloated or responses slow down
Workflow
Step 1: Run the Audit Script
python3 ~/.claude/skills/token-optimizer/scripts/audit.py
This script measures ALL auto-loaded files and outputs a structured report with:
- Per-file line counts and estimated tokens
- Total token budget per session
- Files that exceed size thresholds
- Specific recommendations (what to trim, archive, or split)
DO NOT read large files manually to count lines — the script does it.
Step 2: Apply Recommendations
Based on the audit report, apply these fixes IN ORDER of impact:
| Priority | Action | Trigger |
|---|
| 1 | Archive processed learnings | .ai/learnings.md > 50 lines |
| 2 | Condense CLAUDE.md files | Any CLAUDE.md > 100 lines |
| 3 | Deduplicate memory vs CLAUDE.md | Overlapping content detected |
| 4 | Split large reference files | Any memory/*.md > 80 lines |
| 5 | Clean stale patterns | .ai/patterns.md entries with 0 evidence > 30 days |
Step 3: Verify and Report
After fixes, re-run the audit script to confirm savings:
python3 ~/.claude/skills/token-optimizer/scripts/audit.py
Report the delta to the user: "Спестени X токена/сесия (Y% намаление)"
Skill Cost Analysis
To analyze token cost of a specific skill:
PYTHONIOENCODING=utf-8 python3 ~/.claude/skills/token-optimizer/scripts/skill_cost.py <skill-name>
To see all skills ranked by cost:
PYTHONIOENCODING=utf-8 python3 ~/.claude/skills/token-optimizer/scripts/skill_cost.py --all
To see only metadata cost (always-loaded overhead):
PYTHONIOENCODING=utf-8 python3 ~/.claude/skills/token-optimizer/scripts/skill_cost.py --all --metadata
The script shows 3-layer breakdown: Metadata (always) → SKILL.md (on trigger) → references/ (on demand).
Hard Rules
- NEVER delete information — archive or move to on-demand files
- NEVER modify code files — only instruction/documentation files
- Max 3 file reads during audit — use the script for measurements
- Incremental: Check
~/.claude/skills/token-optimizer/references/last-audit.json before running. If last audit < 24h ago AND no files changed, skip with "Вече оптимизирано."
- Budget targets:
- Global CLAUDE.md: < 40 lines
- Project CLAUDE.md: < 100 lines
- MEMORY.md: < 60 lines
- .ai/learnings.md: < 100 lines (archive processed entries)
- Total auto-loaded: < 300 lines (~10,000 tokens)
What NOT to Touch
- Source code files (*.js, *.css, *.html)
- Database files
- Package files (package.json, etc.)
- .env files
- Git history