Token-Doctor
Measure and cut the token cost of the files that load into every prompt.
When to run
- A
CLAUDE.md / AGENTS.md / memory file has grown large
- Prompts feel expensive or context fills up fast
- Before committing context files (keep them lean)
- As a CI gate so no context file silently bloats
Input
A single file, or a directory (scans *.md, *.txt, CLAUDE.md, AGENTS.md,
llms.txt, etc.; skips code, node_modules, .git).
How to run
The engine lives in scripts/ (Python 3.8+ stdlib, zero dependencies).
# Audit (read-only) — see token cost + savings
python3 -m scripts /path/to/project
# Audit a single file
python3 -m scripts CLAUDE.md
# Apply the reductions (keeps a .bak backup per file)
python3 -m scripts CLAUDE.md --apply
# CI gate: fail if any context file exceeds a token budget
python3 -m scripts /path/to/project --fail-over 1500
# Machine-readable
python3 -m scripts /path/to/project --format json --out audit.json
What it does
- Measures estimated tokens per file (offline heuristic; % saved is reliable).
- Splits each file into prose vs protected regions (code, inline code, URLs,
paths, tables, frontmatter).
- Reduces ONLY prose: drops filler, shortens verbose phrases, collapses blank
bloat, removes duplicate adjacent lines.
- Reports before/after tokens, % saved, biggest-savings files, and waste findings.
- Applies safely on
--apply (writes <file>.bak first).
Guarantees
- Never edits fenced code, inline
code, URLs, markdown links, file paths,
env vars, tables, or YAML frontmatter — these survive byte-for-byte.
- Read-only by default.
--apply always writes a .bak backup.
- Meaning is preserved; only token-wasteful wording is trimmed.
Waste codes
| Code |
Severity |
Meaning |
LARGE_FILE |
🔴 |
File exceeds the token budget (taxes every prompt) |
FILLER_HEAVY |
🟡 |
Many removable filler words |
VERBOSE_PHRASES |
🟡 |
Wordy phrases that have short equivalents |
DUPLICATE_LINES |
🟡 |
Repeated adjacent lines |
HUGE_CODE_BLOCK |
🔵 |
A code/log block dominates the file |
BLANK_BLOAT |
🔵 |
Runs of blank lines |
Workflow for the agent
- Run the audit on the user's project or file → read the report.
- Summarize total % savings and the biggest-savings files.
- Offer
--apply (explain the .bak backups) before changing anything.
- For LARGE_FILE / HUGE_CODE_BLOCK, suggest moving detail into
references/
loaded on demand rather than the always-loaded file.
- Re-run to confirm the new token total.
See references/methodology.md for the estimator and references/preservation.md
for exactly what is never touched.
1---2name: token-doctor3description: Audits and reduces token usage in context files (CLAUDE.md, AGENTS.md, memory files, docs). Measures token cost per file, finds waste (filler words, verbose phrases, blank bloat, oversized blocks), and safely compresses prose while preserving all code, inline code, URLs, file paths and tables. Use when your context files feel bloated, prompts are expensive, or you want to trim CLAUDE.md without losing meaning. Trigger: "reduce tokens", "token usage", "trim my CLAUDE.md", "compress context", "shrink memory file", "context too big", "save tokens", "audit token cost", /token-doctor. For skill description collisions, see skill-doctor.4---56# Token-Doctor78Measure and cut the token cost of the files that load into every prompt.910## When to run1112- A `CLAUDE.md` / `AGENTS.md` / memory file has grown large13- Prompts feel expensive or context fills up fast14- Before committing context files (keep them lean)15- As a CI gate so no context file silently bloats1617## Input1819A single file, or a directory (scans `*.md`, `*.txt`, `CLAUDE.md`, `AGENTS.md`,20`llms.txt`, etc.; skips code, `node_modules`, `.git`).2122## How to run2324The engine lives in `scripts/` (Python 3.8+ stdlib, zero dependencies).2526```bash27# Audit (read-only) — see token cost + savings28python3 -m scripts /path/to/project2930# Audit a single file31python3 -m scripts CLAUDE.md3233# Apply the reductions (keeps a .bak backup per file)34python3 -m scripts CLAUDE.md --apply3536# CI gate: fail if any context file exceeds a token budget37python3 -m scripts /path/to/project --fail-over 15003839# Machine-readable40python3 -m scripts /path/to/project --format json --out audit.json41```4243## What it does44451. **Measures** estimated tokens per file (offline heuristic; % saved is reliable).462. **Splits** each file into prose vs protected regions (code, inline code, URLs,47 paths, tables, frontmatter).483. **Reduces** ONLY prose: drops filler, shortens verbose phrases, collapses blank49 bloat, removes duplicate adjacent lines.504. **Reports** before/after tokens, % saved, biggest-savings files, and waste findings.515. **Applies** safely on `--apply` (writes `<file>.bak` first).5253## Guarantees5455- **Never edits** fenced code, inline `code`, URLs, markdown links, file paths,56 env vars, tables, or YAML frontmatter — these survive byte-for-byte.57- **Read-only by default.** `--apply` always writes a `.bak` backup.58- Meaning is preserved; only token-wasteful wording is trimmed.5960## Waste codes6162| Code | Severity | Meaning |63|------|----------|---------|64| `LARGE_FILE` | 🔴 | File exceeds the token budget (taxes every prompt) |65| `FILLER_HEAVY` | 🟡 | Many removable filler words |66| `VERBOSE_PHRASES` | 🟡 | Wordy phrases that have short equivalents |67| `DUPLICATE_LINES` | 🟡 | Repeated adjacent lines |68| `HUGE_CODE_BLOCK` | 🔵 | A code/log block dominates the file |69| `BLANK_BLOAT` | 🔵 | Runs of blank lines |7071## Workflow for the agent72731. **Run** the audit on the user's project or file → read the report.742. **Summarize** total % savings and the biggest-savings files.753. **Offer** `--apply` (explain the `.bak` backups) before changing anything.764. **For LARGE_FILE / HUGE_CODE_BLOCK**, suggest moving detail into `references/`77 loaded on demand rather than the always-loaded file.785. **Re-run** to confirm the new token total.7980See `references/methodology.md` for the estimator and `references/preservation.md`81for exactly what is never touched.