by nadimtuhin
Reusable setup prompts for optimizing Claude Code documentation. Achieve 90% token savings on any project in 5 minutes.
# Add to your Claude Code skills
git clone https://github.com/nadimtuhin/claude-token-optimizerGuides for using ai agents skills like claude-token-optimizer.
Last scanned: 5/21/2026
{
"issues": [],
"status": "PASSED",
"scannedAt": "2026-05-21T07:54:03.785Z",
"semgrepRan": false,
"npmAuditRan": true,
"pipAuditRan": true
}Stop wasting Claude Code's context on old docs
Cut token usage by 90% so Claude can focus on your actual code
Claude Code loads everything at session start. My RedwoodJS project was burning 11,000 tokens before I even started coding - that's 1,783 lines of documentation, old session notes, and completed task history eating into my context window.
Structure your docs so Claude only loads what it needs:
Real result: 11,000 โ 1,300 tokens. That's 9,700 tokens freed up for your code.
Option A โ no install (npx):
npx claude-token-optimizer init
Option B โ one-line curl install (global cto alias):
curl -fsSL https://raw.githubusercontent.com/nadimtuhin/claude-token-optimizer/main/install.sh | bash
cto init
Option C โ npm global:
npm install -g claude-token-optimizer
cto init
All three options auto-detect your framework and create the optimized structure in ~30 seconds.
Measure first โ see your actual token waste before committing:
npx claude-token-optimizer measure
# or
cto measure
your-project/
โโโ CLAUDE.md # Claude reads this first
โโโ .claudeignore # Keeps old docs from loading
โ
โโโ .claude/
โ โโโ COMMON_MISTAKES.md # Your top 5 critical bugs
โ โโโ QUICK_START.md # Common commands
โ โโโ ARCHITECTURE_MAP.md # Where things are
โ โโโ completions/ # Task history (0 tokens)
โ โโโ sessions/ # Old work (0 tokens)
โ
โโโ docs/
โโโ INDEX.md
โโโ learnings/ # Load these as needed
โโโ archive/ # Old docs (0 tokens)
Before:
Session start loads:
โโโ All docs โ 8,000 tokens
โโโ Old sessions โ 2,000 tokens
โโโ Task history โ 1,000 tokens
โโโ Misc โ ???
Total: ~11,000 tokens
After:
Session start loads:
โโโ CLAUDE.md โ 450 tokens
โโโ COMMON_MISTAKES.md โ 350 tokens
โโโ QUICK_START.md โ 100 tokens
โโโ ARCHITECTURE_MAP.mdโ 150 tokens
Total: ~800 tokens (90% reduction)
Everything else?
Available when you ask, but costs 0 tokens until you do.
Got patterns for 13 frameworks with common mistakes and best practices:
| Framework | Example | |-----------|---------| | Express.js | examples/express.md | | Next.js | examples/nextjs.md | | Vue.js | examples/vue.md | | Nuxt.js | examples/nuxtjs.md | | Angular | examples/angular.md | | Django | examples/django.md | | Rails | examples/rails.md | | NestJS | examples/nestjs.md | | Laravel | examples/laravel.md | | FastAPI / Flask | examples/fastapi.md | | Go (Gin/chi) | examples/go.md | | Spring Boot | examples/spring-boot.md | | Svelte / SvelteKit | examples/svelte.md |
Each includes the top 5 critical mistakes for that framework (N+1 queries, auth issues, etc).
cto init auto-detects your framework from package.json, requirements.txt, go.mod, composer.json, pom.xml, or Gemfile โ no flag needed for most projects. The detected framework is written into CLAUDE.md's Tech Stack line automatically.
Pass --framework to override:
cto init --framework nextjs
cto init --framework django
cto init --framework go
Supported: express, nextjs, vue, nuxtjs, angular, django, rails, nestjs, laravel, fastapi, go, spring-boot, svelte
If you want more control or framework-specific patterns:
UNIVERSAL_SETUP.mdAdd your critical bugs to .claude/COMMON_MISTAKES.md
Only add bugs that took >1 hour to debug. Keep it short.
Document your commands in .claude/QUICK_START.md
npm run dev, database commands, whatever you use daily.
Map your architecture in .claude/ARCHITECTURE_MAP.md
Where controllers live, how routing works, etc.
Create topic files in docs/learnings/ as you learn
One file per topic (testing, API design, deployment)
Archive completed work
Task docs go to .claude/completions/, old sessions to .claude/sessions/archive/
Claude loads only what it needs. Everything else sits there at 0 token cost until you explicitly ask for it.
| Command | Description |
|---------|-------------|
| cto init | Set up documentation structure (auto-detects framework) |
| cto measure | Show auto-loaded token cost; guides first-time users to cto init, initialized projects to cto compress |
| cto audit | Health check โ 19 structural checks, CI-friendly, exits 1 on errors (--fix auto-creates missing files and patches .claudeignore) |
| cto compress | Reduce CLAUDE.md size with deterministic rules (dry-run safe) |
| cto prune | Remove stale sections interactively โ archives, never deletes |
| cto diff | Token delta after compress/prune โ before vs after, % saved |
| cto watch | Live token dashboard โ refreshes on file change, ASCII bar charts |
| cto hooks | Install, list, and manage Claude Code hook templates |
| cto update | Update cto to the latest npm version |
| cto update --content | Refresh project files to latest templates (merge-safe โ never overwrites your custom content) |
# Check CLAUDE.md health (exits 1 on errors โ CI-friendly)
cto audit
# Reduce token count with rule-based compression
cto compress
# Remove completed tasks, session notes, and empty sections
cto prune
# See how many tokens compress/prune saved (compares CLAUDE.md vs .bak)
cto diff
CI integration โ exits 1 on errors, machine-readable JSON:
# If cto is installed globally
cto audit --json
# Without global install (CI / one-shot)
npx claude-token-optimizer audit --json
JSON output: { "errors": 0, "warnings": 0, "infos": 2, "results": [...] }
Exit codes: 1 when errors > 0, 0 for warnings/info-only or all clear.
Works in GitHub Actions, GitLab CI, and any shell that checks exit codes.
Claude Code hooks run shell scripts on events โ before/after tool calls, on session start/end. Pair them with claude-token-optimizer to get active token monitoring.
templates/hooks/
โโโ pre-tool-token-guard.sh # PreToolUse ยท warn/block heavy auto-loaded files
โโโ pre-tool-read-guard.sh # PreToolUse ยท block lock files, binaries, huge reads
โโโ pre-tool-bash-guard.sh # PreToolUse ยท block find /, cat node_modules, bare grep -r
โโโ post-write-token-diff.sh # PostToolUse ยท log per-file write token cost
โโโ session-end-token-report.sh # PostToolUse ยท log session token totals
โโโ user-prompt-inject-context.sh # UserPromptSubmit ยท auto-load topic docs from docs/learnings/
โโโ user-prompt-inject-snapshot.sh # UserPromptSubmit ยท inject previous session snapshot (warm restart)
โโโ user-prompt-validate-claude-md.sh # UserPromptSubmit ยท validate CLAUDE.md structure once/session
โโโ user-prompt-ghost-scanner.sh # UserPromptSubmit ยท detect unreferenced CLAUDE.md sections (ghost tokens)
โโโ stop-session-snapshot.sh # Stop ยท write session snapshot after each turn
โโโ stop-path-guard.sh # Stop ยท exit 2 if assistant mentions missing paths
โโโ notification-token-display.sh # Notification ยท show session token estimate on first notification
# Install all hooks at once
cto hooks install --all
# Print the settings.json block for all installed hooks (pipe-safe JSON)
cto hooks settings
Copy the settings output into ~/.claude/settings.json (merge with existing content).
Or install one at a time:
cto hooks install pre-tool-token-guard
cto hooks install user-prompt-inject-context
cto hooks list # see all available
cto hooks status # see what's installed
pre-tool-token-guard.sh โ fires once per session, warns when auto-loaded files exceed a threshold:
โ ๏ธ Token warning: ~3,400 tokens in auto-loaded files (target: <2000)
Run: cto measure to see the breakdown
CTO_WARN_TOKENS โ warn threshold (default: 2000)CTO_BLOCK_TOKENS โ block threshold, exit 2 (default: 8000)user-prompt-inject-context.sh โ the most powerful hook: keyword-matches the user's prompt against filenames in docs/learnings/ and injects matching files as context Claude sees before answering. Zero token cost for files that don't match.
User: "how should I handle database migrations?"
โ ๐ก Auto-loaded: docs/learnings/database.md (~85 tokens)
Claude now answers from your project's migration docs, not training data.
Put your proje
No comments yet. Be the first to share your thoughts!