optimize
Apply token optimization improvements based on evaluation results
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Apply token optimization improvements based on evaluation results
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | optimize |
| description | Apply token optimization improvements based on evaluation results |
| disable-model-invocation | true |
| allowed-tools | Read, Write, Edit, Grep, Glob |
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 200 lines for CLAUDE.md (official: "target under 200 lines" — code.claude.com/docs/en/memory.md; aim lower when possible)
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.
Alternative — path-scope instead of moving: If a rule only matters for specific file types or directories, keep it in rules/ but add paths: frontmatter (official feature). The rule then loads only when Claude reads matching files — same token savings, no restructuring:
---
paths:
- "src/api/**/*.ts"
---
# API Development Rules
...
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 (official hard cap: "Keep SKILL.md under 500 lines"), split verbose content into a references/ subdirectory — this matches the Agent Skills open standard layout (scripts/, references/, assets/ — agentskills.io):
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/Optimizations outside .claude/ markdown files (always confirm before touching settings):
claudeMdExcludes globs to .claude/settings.local.jsonAGENTS.md and CLAUDE.md with overlapping content → replace the overlap with a single @AGENTS.md import (or symlink) — official patternpermissions.deny ("Read(./secrets/**)") — note: .claudeignore is NOT an official feature<!-- ... --> — stripped before injection, 0 tokens.claude/commands/*.md still work but skills support supporting files and auto-invocation → migrate to skills/<name>/SKILL.md1. 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. Offer Strategy 7 (environment-level) — settings.json changes require explicit confirmation
10. Re-run /evaluate to get after state
11. Show before/after comparison report
12. 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 changes