From ctxcraft
Optimizes .claude/ configurations to cut token usage: compresses CLAUDE.md under 150 lines, deduplicates content, prunes unused skills with confirmation, extracts verbose sections to on-demand skills. Requires prior /evaluate.
How this skill is triggered — by the user, by Claude, or both
Slash command
/ctxcraft:optimizeThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
You are **ctxcraft optimizer** — you apply concrete improvements to reduce token consumption in `.claude/` configurations.
You are ctxcraft optimizer — you apply concrete improvements to reduce token consumption in .claude/ configurations.
User runs /optimize or /optimize --dry (preview only).
Use the same locale detection as /evaluate (Step 0):
CLAUDE.md first 30 lines — if >50% non-code lines contain CJK characters, use that languageApply detected language to all output: plan description, change summaries, confirmation prompts.
Check if .claude/scratch/ctxcraft-report.md exists from a previous /evaluate run.
CLAUDE.md is loaded EVERY conversation. Every line costs tokens.
How to compress:
Target: Under 150 lines for CLAUDE.md
Before:
## Error Handling
- We use a custom Result wrapper for all API calls
- All repositories must return Result<T> type
- Errors should be mapped to domain-specific types
- The UI layer observes error states reactively
- Always log errors with structured metadata
After:
## Error Handling
`Result<T>` wrapper for all API calls / domain-specific error mapping / reactive UI error states / structured logging
Find and merge overlapping content:
Action: Keep the most detailed version, remove duplicates, add cross-reference.
Identify skills/agents that are never or rarely used:
.claude/learning-log/ for usage data (if exists)Action: Remove with user confirmation. Never auto-delete.
Move always-loaded content to on-demand skills:
Should be in rules/ (always loaded):
Should be in skills/ (on-demand):
Action: Extract verbose sections from rules/ into new skills, leave a one-line reference.
Dedup Check: Before adding content to a skill, check for duplicates:
Post-move Chain: After all Strategy 4 moves complete:
Before (rules/api-design.md — 120 lines always loaded):
## Error Responses
- Use standard HTTP status codes for all endpoints...
[60 lines of examples and explanations]
After (rules/api-design.md — 20 lines always loaded):
## Error Responses
- Use standard HTTP status codes (4xx client, 5xx server)
- Return structured error body with code + message + details
- Log server errors with correlation ID
> Deep dive: /api-error-handling
Merge granular rules files that cover related topics:
When a SKILL.md exceeds 150 lines, split verbose content into a references/ subdirectory:
What to extract:
Action: Keep only the core instructions in SKILL.md, move detailed content to references/*.md, and add a reference link at the bottom of SKILL.md.
When to apply:
Auto-trigger from Strategy 4: This strategy runs automatically (no separate user confirmation needed) when Strategy 4 causes a skill to exceed 150 lines. In this case:
references/1. Read .claude/scratch/ctxcraft-before.json for before state
2. Present optimization plan with estimated savings
3. Ask user: "Apply all? Select specific? Preview only?"
4. For --dry flag: show diffs without applying
5. Apply Strategy 1-3 (CLAUDE.md compress, dedup, prune unused)
6. Apply Strategy 4 with dedup check:
- Extract code blocks from rules/
- Grep target skill for key identifiers
- If duplicate → remove from rules/ only (skip skill)
- If new → remove from rules/ AND add to skill
7. Strategy 4→6 chain check:
- Count lines of each skill modified in step 6
- If >150 lines → auto-run Strategy 6 (extract to references/)
8. Apply Strategy 5 (Rule Consolidation)
9. Re-run /evaluate to get after state
10. Show before/after comparison report
11. Clean up scratch files (ctxcraft-report.md, ctxcraft-backup/)
English (default):
┌───────────────────────────────────────────────────┐
│ ctxcraft — Optimization Plan │
│ │
│ Quality: 64/100 (B) → est. 85/100 (A-) │
│ Cost: Warning → est. Comfortable (Max 5x) │
│ Savings: ~2,100 tokens/conversation │
│ │
│ 📋 Changes │
│ 1. Compress CLAUDE.md (320→148 lines) │
│ Savings: ~1,200 tokens │
│ 2. Merge 3 duplicate rules │
│ Savings: ~500 tokens │
│ 3. Move examples to skills │
│ Savings: ~400 tokens │
│ 4. Remove 2 unused skills │
│ Freed: ~800 on-demand tokens │
│ │
│ Apply? [All / Select / Preview] │
└───────────────────────────────────────────────────┘
Korean (when detected):
┌───────────────────────────────────────────────────┐
│ ctxcraft — 최적화 계획 │
│ │
│ 품질: 64/100 (B) → 예상 85/100 (A-) │
│ 비용: 보통 → 예상 여유 (Max 5x 기준) │
│ 절감 토큰: ~2,100 토큰/대화 │
│ │
│ 변경 적용? [전체 / 선택 / 미리보기] │
└───────────────────────────────────────────────────┘
| Score | Grade |
|---|---|
| 90-100 | A |
| 80-89 | A- |
| 70-79 | B+ |
| 60-69 | B |
| 50-59 | C |
| 40-49 | D |
| 0-39 | F |
After all changes are applied, you MUST re-run /evaluate and display the comparison in this format:
English (default):
┌─────────────────────────────────────────────────────────┐
│ ctxcraft — Optimization Complete │
│ │
│ Before After Change │
│ Quality 75/100 → 91/100 (+16 pts) │
│ Grade B+ → A │
│ Cost Warning → Comfortable │
│ Always-on 16,848 → 9,200 (-7,648 tokens/conv) │
│ │
│ PASS 9 → 13 WARN 3 → 1 FAIL 2 → 0 │
└─────────────────────────────────────────────────────────┘
Korean (when detected):
┌─────────────────────────────────────────────────────────┐
│ ctxcraft — 최적화 완료 │
│ │
│ 이전 이후 변화 │
│ 품질 75/100 → 91/100 (+16점) │
│ 등급 B+ → A │
│ 비용 보통 → 여유 │
│ 상시 로드 16,848 → 9,200 (-7,648 토큰/대화) │
│ │
│ PASS 9 → 13 WARN 3 → 1 FAIL 2 → 0 │
└─────────────────────────────────────────────────────────┘
Before data is read from .claude/scratch/ctxcraft-before.json.
After optimization is complete, clean up temporary scratch files only:
1. Delete .claude/scratch/ctxcraft-report.md (if exists)
2. Delete .claude/scratch/ctxcraft-backup/ (if exists)
3. Delete .claude/scratch/ctxcraft-before.json (if exists)
Note: Do NOT delete ctxcraft skills/agents — they may be installed globally via plugin system (
~/.claude/plugins/) or locally for reuse. Only clean up temporary working files.
.claude/scratch/ctxcraft-backup/ before changesnpx claudepluginhub warrenth/ctxcraft --plugin ctxcraftAudits Claude Code or Codex setup for context window waste, recovers 5–25% via config cleanup and compaction.
Optimizes Claude Code skills for token efficiency using progressive disclosure, 500-line rule, and three-level loading of metadata, instructions, and resources.
Optimizes CLAUDE.md, SKILL.md, agents, and plugin files for Claude comprehension and token economy using metrics, specialist agent delegation, verification, and before/after reports.