text-optimize — deep token optimization

Caution

Verbose prompts waste tokens and blur Claude’s focus. Every filler sentence, passive construction, and prose paragraph that could be a table adds cost and dilutes instruction signal. There is no native “compress this” in Claude Code — you get the full context or nothing.

Tip

52 validated rules, five modes, parallel processing. The skill reads your files, applies the right rule set for the mode, verifies references, and reports exact before/after metrics. For humanizing AI-generated text, see /brewtools:text-human.

Quick reference

FieldValue
Command/brewtools:text-optimize
Arguments[-l|-s|-d|-x|--max] [file|folder|path1,path2]
Modelsonnet
Contextsession
ToolsRead, Write, Edit, Bash, Grep, Glob, Task, AskUserQuestion

When to use

  • Shrink CLAUDE.md — deep mode compresses with a DICT header; LLM-only output, 2-3x reduction
  • Clean up agent/skill definitions — medium mode strips filler, converts prose to tables, merges redundant sections
  • Prepare user-facing docs — standard mode targets 30-50% compression while staying human-readable
  • Light pass on production prompts-l removes filler and fixes tone; structure untouched, safe to apply without review

Modes

ModeFlagTargetCompressionHuman-readableVerification
Light-lAny fileMinimalYesSub-gate only
Medium(default)Any fileModerateYesSelf-check (fact inventory)
Standard-sDocs, README30-50%Yes1 round (≥98% match)
Deep-dCLAUDE.md, system prompts, agent/skill defs, KNOWLEDGE2-3xNo — LLM-only1-2 rounds (≥95% match)
Max-x/--maxCLAUDE.md, system prompts, KNOWLEDGE3-4xNo — LLM-only2 mandatory rounds (≥95% + 100% sub-gate)

Auto-detection: when no flag is given, the skill reads the file path and header. CLAUDE.md, .claude/rules/*.md, KNOWLEDGE.* → deep candidate. README.md, docs/ → standard candidate. Ambiguous → asks via AskUserQuestion. Max is never auto-selected — requires an explicit -x/--max flag or an explicit max/extreme compress hint.

Every mode is guarded on disk. Before any file is touched, the skill snapshots each target byte-for-byte into a private run directory under .claude/reports/ — the git tree over the targets must be clean first, or the run refuses. After the optimization pass, a mechanical script diffs the snapshot against the result and checks that every number, version, file path, != prohibition, and ALL-CAPS modal keyword (NEVER, ALWAYS, MUST NOT, REQUIRED, MANDATORY) is still present. A single miss restores the file to its pre-edit bytes and reports exactly what vanished — this applies to every mode, light included, not just standard/deep/max. Standard, deep, and max additionally run an independent semantic gate on top (see Verification in the flow below).

Examples

# Single file, medium mode (default)
/brewtools:text-optimize CLAUDE.md

# Light mode — safe, minimal changes, no restructuring
/brewtools:text-optimize -l .claude/agents/reviewer.md

# Standard mode — 30-50% reduction, stays readable
/brewtools:text-optimize -s README.md

# Deep mode — max compression for CLAUDE.md, review diff after
/brewtools:text-optimize -d CLAUDE.md

# Max mode — 3-4x compression (opt-in)
/brewtools:text-optimize -x CLAUDE.md

# Multiple files in parallel
/brewtools:text-optimize agents/planner.md, agents/executor.md, CLAUDE.md

# All markdown files in a directory
/brewtools:text-optimize -d agents/

Natural language works as arguments after the command — the skill is user-invoked only and never fires on its own: /brewtools:text-optimize compress this for context → deep mode, /brewtools:text-optimize slim down the README → standard mode.

Flow

  1. Parse input

    Extracts mode flag, file paths, and any prompt hints (“deep compress”, “for LLM”). No args → optimizes ALL: CLAUDE.md, .claude/agents/*.md, .claude/skills/**/SKILL.md.

  2. Auto-detect mode

    If no flag, classifies each file by path and header. Confident classification runs silently with a short notice. Ambiguous files trigger a single AskUserQuestion with mode options.

  3. Snapshot before editing

    Requires a clean git tree over the target files, then scripts/text-guard.sh snapshot copies every target byte-for-byte into a private RUN_DIR under .claude/reports/ (created with umask 077) before any edit happens. A dirty target or a non-git root refuses with an error — nothing is written until the snapshot exists. Every mode goes through this step, not just deep and max.

  4. Analysis phase

    Parallel Explore agents scan each file for structure, cross-references, and redundancies. Results feed the optimization context.

  5. Optimization phase

    Parallel text-optimizer agents apply the rule set for the selected mode. Deep mode builds a DICT header for repeated terms and runs an aggressive lossy pass (A.1-A.4) — merges related lines, drops low-value words, paraphrases shorter, and elides generic facts the model already knows; A.2 word drops are ledgered but gate-neutral, A.4 elisions are labeled elided-known and count against the >=95% gate. Standard mode loads standard-compression.md reference. All modes verify file paths (R.1), URLs (R.2), and circular refs (R.3).

  6. Verification — restore, not warn

    Two gates run after every optimization pass, both comparing the on-disk snapshot against the on-disk result — never the agent’s own account of what it changed:

    Sub-gate, every modetext-guard.sh verify checks that every number, version, file path, != prohibition, and ALL-CAPS modal keyword from the snapshot still appears in the result. Any miss restores the file to its pre-edit bytes immediately and prints the exact missing tokens — a refusal, not a warning attached to a bad file.

    Semantic gate, standard/deep/max — a fresh agent that did not write the compression reads both files from disk, builds a numbered fact inventory, and scores it against the mode’s threshold:

    ModeRoundsGate
    Light / Mediumsub-gate only / self-check + sub-gate100% lossless
    Standard1≥98% match
    Deep1-2≥95% match + 100% sub-gate on numbers/names/negations/scope
    Max2 mandatory≥95% + 100% sub-gate on numbers/names/negations/scope

    A semantic FAIL restores the file the same way the sub-gate does. The run directory is never deleted automatically — it stays on disk so the diff or the loss list is available after the run.

  7. Report

    Each file produces a report: lines/tokens before and after, rules applied by ID, issues found and fixed, cross-reference verification status. Files are modified in-place; a restored file is reported as restored, with the missing tokens or the loss list and a suggested lighter mode.

Delegation

Warning

A big task handed to one agent = an agent gone for an hour. You cannot observe it, you cannot correct it, and it usually drifts off-target. One text-optimizer agent = one file, ~10 steps. A folder or comma-separated multi-path run is split one file per agent, all spawned in a single message — never one agent for the whole folder.

Every spawn prompt carries six fields. A bare one-line task is never enough:

FieldContent
GOALcut token cost across the run without losing meaning; this agent owns one file
ROLEoptimize that file in place — no behavior change, no dropped names, numbers, paths, versions, prohibitions
SCOPEin — the one file; out — every other path, references/ are read-only inputs
CONTEXTmode already chosen, Phase 1 Explore findings supplied, siblings hold the other files
CONSUMERthe skill merges every Optimization Report into one summary; other files still link to this file’s headings
DONEdedup pass (D.1-D.6) → transformations → ref checks (R.1-R.3) → mode verification → Optimization Report

The CONSUMER field is what keeps a parallel run coherent: a heading renamed without care breaks a sibling agent’s cross-reference in the same batch.

Rule categories (52 rules)
CategoryRule IDsScope
Claude behaviorC.1-C.8Literal following, avoid “think”, positive framing, match style, descriptive instructions, overengineering, avoid ALL-CAPS, prompt format
Token efficiencyT.1-T.8, T.10Tables, bullets, one-liners, inline code, abbreviations, filler, comma lists, arrows, strip whitespace
StructureS.1-S.8XML tags, imperative, single source, context/motivation, blockquotes, progressive disclosure, consistent terminology, ref depth
DeduplicationD.1-D.6Exact-duplicate merge, near-duplicate merge (most specific variant wins), cross-format duplicate (prose vs table), emphasis cap ≤2/doc, cross-file dedup (canonical location + pointer), wrong-merge guard
Reference integrityR.1-R.3Verify file paths, check URLs, linearize circular refs
PerceptionP.1-P.6Examples near rules, hierarchy, bold keywords, standard symbols, instruction order, default over options
LLM ComprehensionL.1-L.8Critical info position, documents-first, conciseness, quote-first, add WHY, reiterate constraint, prompt repetition, preserve scope qualifiers
Aggressive lossyA.1-A.4Line fusion, low-value word drop, aggressive paraphrase, known-fact elision (deep/max only)

A dedup pass runs in every mode during analysis, before compression. Dedup-merged facts count as preserved: match % = (kept + merged) / total.

Mode-to-rules mapping:

ModeAppliesNotes
LightC.1-C.8, T.6, D.1, R.1-R.3, P.1-P.4, L.1-L.8Text cleanup only — no restructuring
MediumAll rules (C + T + S + D + R + P + L)Balanced transformations
StandardAll rules + standard-compression.md30-50% compression, 1 verification round
DeepAll rules + A.1-A.4 + deep-compression.mdDICT header, symbol substitutions, aggressive lossy pass, 1-2 verification rounds
MaxAll rules + A.1-A.4 + deep-compression.md + max-compression.md (opt-in)3-4x compression, 2 mandatory verification rounds + 100% sub-gate

Text Human

Remove AI artifacts and humanize content — the complement to text-optimize.

🧠

text-optimizer agent

The subagent that handles single-file optimization in parallel batches.

🔗

GitHub source

Source code, rule references, and compression guides.

📄

Brewtools overview

All brewtools skills — text, SSH, deploy, plugin management.

Updating plugins

Use /brewtools:plugin-update to check and update the brewcode plugin suite in one command. See the FAQ for details.