nadimtuhin/claude-token-optimizer

Cut the tokens Claude Code loads at session start. CLI that sets up a small CLAUDE.md with on-demand docs, measures startup tokens, and keeps them small.

Claude Token Optimizer – What It Is

A small Node‑based CLI that reorganises the markdown files Claude Code reads at the start of each session. By moving large notes, old session logs and task histories into on‑demand files and keeping a concise CLAUDE.md cheat‑sheet, it cuts the number of tokens Claude has to load from ~11 000 to ~800 in the author’s example. The tool is aimed at developers who use Claude Code (or similar LLM coding agents) and want to stay within token limits and reduce cost.


How It Works

  1. cto init – Detects the project’s framework (e.g., Next.js, Express) and creates a standard Claude‑specific layout:
    • CLAUDE.md – the short file Claude reads first.
    • .claudeignore – tells Claude which files to skip.
    • .claude/ with several markdown files (COMMON_MISTAKES.md, QUICK_START.md, ARCHITECTURE_MAP.md, etc.) that are only opened when Claude needs them.
    • docs/ sub‑folders for learning notes and archived docs.
  2. cto measure – Counts how many tokens each file would load using the Claude 2 tokenizer, giving a before/after view.
  3. cto prune – Moves completed tasks and old session notes out of CLAUDE.md into the .claude/ folder, optionally keeping recent notes (--days N).
  4. cto compress – Applies deterministic markdown reductions (removing blank lines, trimming long lists, stripping HTML comments, etc.) to shrink files further.
  5. Hooks & CI – Optional hook scripts can enforce token limits during Claude tool usage, and cto audit provides 19 structural checks that can be run in CI pipelines.

Getting Started (Zero‑Install Option)

# 0️⃣ Back up your current notes
cp CLAUDE.md CLAUDE.md.mine

# 1️⃣ Measure current token load
npx -y claude-token-optimizer measure

# 2️⃣ Reduce it automatically
npx -y claude-token-optimizer prune --yes

If you prefer a global command:

npm install -g claude-token-optimizer   # or use the provided install.sh script
cto init   # creates the layout
cto measure
cto prune --yes

Key Commands (quick reference)

Command What it does
cto init Detects framework, creates the Claude‑specific file structure.
cto measure Shows token count per file (uses Claude 2 tokenizer).
cto prune Archives old session notes and completed tasks; reduces startup tokens.
cto compress Deterministically shrinks a markdown file (removes blanks, long lists, comments, etc.).
cto audit Runs structural checks; can output JSON for CI.
cto watch Live dashboard that refreshes token counts when files change.
cto hooks install --all Copies hook templates that can warn/block large reads, inject context, log token usage, etc.
cto companions Lists other token‑saving tools (rtk, lean‑ctx, caveman, ponytail) and how to install them.

Who Might Use This

  • Developers using Claude Code (or any Claude‑compatible coding agent) who hit token limits or want to lower usage costs.
  • Teams with CI pipelines that want to enforce a lightweight Claude context and fail builds if token budgets are exceeded.
  • Anyone building AI‑assisted coding agents – the repo even provides an AGENTS.md for non‑interactive setup.

Limitations & Caveats

  • The token counts are estimates based on the Claude 2 tokenizer; actual billing may differ.
  • compress is lossy – it truncates long lists and removes HTML comments.
  • The tool only affects Claude Code startup loading; it does not change Claude’s runtime behaviour.
  • Running cto init on a project that already has a CLAUDE.md will only append missing sections; without one it will replace existing .claude* files unless you back them up.

License

MIT – free to use, modify, and distribute.

Related

  • Dispatch
  • Dispatch
  • Dispatch
  • Project
  • Project