Reduce context cost across the full CLAUDE.md hierarchy - condense, deduplicate, and restructure module files
Optimizes CLAUDE.md files across the hierarchy to reduce context token usage by condensing, deduping, and restructuring content.
npx claudepluginhub jugrajsingh/skillgardenThis skill is limited to using the following tools:
Reduce context cost across ALL files while preserving signal. Operates on the full hierarchy: root, module-level, and .claude/rules/.
find . -name 'CLAUDE.md' -not -path '*/node_modules/*' -not -path '*/venv/*' 2>/dev/null | while read f; do echo "$(wc -l < "$f") $f"; done
wc -l .claude/rules/*.md 2>/dev/null
Read all files. Compute per-directory load:
## Current Metrics
Level Path Lines
Root ./CLAUDE.md {N}
Module src/billing/CLAUDE.md {N}
Module src/auth/CLAUDE.md {N}
Rule .claude/rules/testing.md {N}
Context Load Map:
Working in... Loaded Total
src/billing/ root + billing {N}
src/auth/ root + auth {N}
(anywhere) root only {N}
Worst case: {MAX}/250 (working in {WORST_PATH})
If worst-case <100 lines and no anti-patterns: "Hierarchy is already lean. No optimization needed."
The highest-value optimization. Module files that repeat root content waste context every time that module is visited.
| Before | After |
|---|---|
Module repeats npm test command | Remove — root already documents it |
| Module restates "use TypeScript strict" | Remove — root convention |
Module adds npm test -- src/billing/ | Keep — module-specific path |
Transform prose into terse directives across ALL files:
Rules of condensation:
| Before | After |
|---|---|
| "When writing tests, always use describe/it and make sure to include meaningful descriptions" | "Tests: describe/it, descriptive names" |
| "The project uses PostgreSQL as the primary database and Redis for caching" | "DB: PostgreSQL. Cache: Redis." |
Across ALL files, replace inline code examples with file pointers:
| Before | After |
|---|---|
| 20-line API handler example in module | "API pattern: see src/api/users.ts:15" |
| Test setup boilerplate | "Test setup: follow src/tests/helpers.ts" |
Cross-reference ALL files with detected linter configs:
ls .editorconfig .eslintrc* .prettierrc* ruff.toml biome.json .flake8 .rubocop.yml 2>/dev/null
Remove from ANY context file: indentation, quotes, semicolons, import ordering, line length.
Module files >80 lines should be split:
# Before: src/billing/CLAUDE.md (120 lines)
Contains billing overview + stripe details + invoice details
# After:
src/billing/CLAUDE.md (45 lines) — overview, billing-wide patterns
src/billing/stripe/CLAUDE.md (35 lines) — stripe integration specifics
src/billing/invoices/CLAUDE.md (30 lines) — invoice generation specifics
If multiple module files contain similar conventions for the same file type, extract to .claude/rules/:
# Before: 3 modules each have "use zod for validation"
src/billing/CLAUDE.md: "Validate with zod schemas"
src/auth/CLAUDE.md: "All inputs validated with zod"
src/api/CLAUDE.md: "Use zod for request validation"
# After: one rule file, remove from modules
.claude/rules/validation.md:
---
paths: ["src/**/*.ts"]
---
Input validation: zod schemas for all request/response types
Delete CLAUDE.md for directories that:
## Optimization Plan
Current worst-case load: {N}/250 lines (working in {WORST_PATH})
Target worst-case load: ~{N}/250 lines
### Changes by File
./CLAUDE.md ({N} → ~{N} lines)
- Condense verbose sections (-{N})
- Remove linter rules (-{N})
src/billing/CLAUDE.md ({N} → ~{N} lines)
- Remove duplicated root content (-{N})
- Extract stripe details to sub-module (-{N})
src/auth/CLAUDE.md ({N} → ~{N} lines)
- Condense 3 paragraphs to table (-{N})
- Replace inline example with file ref (-{N})
NEW: src/billing/stripe/CLAUDE.md (~{N} lines)
- Extracted from oversized billing module
NEW: .claude/rules/validation.md (~{N} lines)
- Cross-cutting rule extracted from 3 modules
DELETE: src/legacy/CLAUDE.md
- Directory has 0 source files
AskUserQuestion:
question: "Apply this optimization plan?"
header: "Optimize"
options:
- label: "Apply all (Recommended)"
description: "Execute all optimizations"
- label: "Cherry-pick"
description: "Choose which optimizations to apply"
- label: "Preview only"
description: "Show proposed file contents without writing"
For each approved optimization:
## Optimization Complete
### Before → After
Path Before After Change
./CLAUDE.md {N} {N} -{N}
src/billing/CLAUDE.md {N} {N} -{N}
src/billing/stripe/CLAUDE.md - {N} +{N} (extracted)
src/auth/CLAUDE.md {N} {N} -{N}
.claude/rules/validation.md - {N} +{N} (cross-cutting)
src/legacy/CLAUDE.md {N} - removed
### Context Load Map (post-optimization)
Working in... Before After
src/billing/ {N} {N} (-{SAVED})
src/billing/stripe/ - {N} (new path)
src/auth/ {N} {N} (-{SAVED})
(anywhere) {N} {N} (-{SAVED})
Worst case: {MAX}/250 (was {OLD_MAX})
△ Review with `git diff`. Run /claudemd:audit to validate.
| File Level | Ideal | Max |
|---|---|---|
| Root CLAUDE.md | 50-80 | 150 |
| Module CLAUDE.md | 30-50 | 80 |
| Sub-module CLAUDE.md | 15-30 | 50 |
| .claude/rules/ file | 15-30 | 50 |
| Max loaded at once | 100-150 | 250 |
This skill should be used when the user asks about libraries, frameworks, API references, or needs code examples. Activates for setup questions, code generation involving libraries, or mentions of specific frameworks like React, Vue, Next.js, Prisma, Supabase, etc.