token-optimizer
Token Optimizer audits Claude Code and Codex setups to identify wasted context tokens, then implements targeted fixes like removing duplicate content, consolidating skills, and enabling smart compaction. Use this skill when your context window feels constrained or you want to recover 5 to 25 percent of available tokens through configuration cleanup and automated management.
git clone --depth 1 https://github.com/alexgreensh/token-optimizer /tmp/token-optimizer && cp -r /tmp/token-optimizer/skills/token-optimizer ~/.claude/skills/token-optimizerSKILL.md
# Token Optimizer
Audits a Claude Code or Codex setup, identifies context window waste, implements fixes, and measures savings.
**Target**: 5-15% context recovery through config cleanup, up to 25%+ with autocompact management.
---
## Codex Runtime
If `TOKEN_OPTIMIZER_RUNTIME=codex` or Codex environment is detected, read `references/codex-workflow.md` and follow its chat-first workflow instead of the Claude Code phases below.
---
## OpenCode Runtime
OpenCode loads `~/.claude/skills` by default, so it can invoke this skill even though you are working in OpenCode, not Claude Code. If OpenCode is detected — `TOKEN_OPTIMIZER_RUNTIME=opencode`, any `OPENCODE_*` environment variable, or you are running inside OpenCode — **STOP** and read `references/opencode-workflow.md`. Do **not** run the Claude Code phases below: they scan and modify `~/.claude`, which is the wrong target when the user is in OpenCode (issue #57).
Quick check before Phase 0:
```bash
python3 "$MEASURE_PY" report 2>/dev/null | head -1
# If it prints "Token Optimizer — OpenCode runtime detected.", follow references/opencode-workflow.md and stop.
```
---
## Phase 0: Initialize (Claude Code)
Resolve measure.py path:
```bash
MEASURE_PY=""
for f in "$HOME/.claude/skills/token-optimizer/scripts/measure.py" \
"$HOME/.claude/plugins/cache"/*/token-optimizer/*/skills/token-optimizer/scripts/measure.py; do
[ -f "$f" ] && MEASURE_PY="$f" && break
done
[ -z "$MEASURE_PY" ] && { echo "[Error] measure.py not found."; exit 1; }
```
Read `references/phase0-setup.md` for the full setup sequence: context window detection, pre-check, backup, coordination folder, hook checks, daemon setup, and smart compaction.
---
## Phase 0.5: Keep-Warm Consent (first run only, Claude Code)
Keep-Warm is opt-in and pays off only for API-key-billed Claude Code sessions. Ask once:
```bash
python3 "$MEASURE_PY" keepwarm-consent-status # JSON: {billing_mode, consent, should_ask}
```
If `should_ask` is `false`, skip this phase silently (subscription users are never asked; declined/enabled users keep their choice). If `should_ask` is `true`, first compute the user's own projection, then present the pitch:
```bash
python3 "$MEASURE_PY" keepwarm-backfill --json --no-fence # read modes."probe-only".net_usd
```
Read `net_usd` under `modes."probe-only"`. If it is a positive number, include it as the projection. If backfill errors, returns nothing, or `net_usd <= 0`, drop the dollar sentence entirely (do not invent a number) and use the no-data wording below.
> **Keep your prompt cache warm automatically?** When a Claude Code session pauses past its 1h cache window and resumes, the whole prefix is re-written at up to 2x input. Keep-Warm pings the cache just before expiry (~0.1x of the prefix, max 2 pings per pause) so a resume stays warm. A history-replay projection from your own last 30 days nets ~$<net_usd>/30d at the conservative probe-only setting. A tripwire auto-disables it if pings ever stop paying for themselves, and you can turn it off any time. Enable it?
No-data wording (when backfill yields no positive projection): drop the projection sentence and say "Your savings depend on your own pause-and-resume pattern; the dashboard will show your number once pings have fired."
Then record the answer (do this exactly once). **Record the yes/no FIRST**, so an interrupted run never strands an "asked" marker with no recorded answer:
```bash
# yes:
python3 "$MEASURE_PY" keepwarm-enable
# no:
python3 "$MEASURE_PY" keepwarm-disable
```
`keepwarm-enable` and `keepwarm-disable` are terminal states, so they already satisfy `should_ask`. Only if the user defers or ignores the question (records neither) run the shown-marker so they are not re-asked next run:
```bash
python3 "$MEASURE_PY" keepwarm-consent-asked # mark shown (sticky); use ONLY when no enable/disable was recorded
```
`keepwarm-enable` records consent and installs the scheduler (macOS); on other OSes the scheduler is pending, so it is watchdog-only. It refuses on subscription with an honest message. To confirm it is armed:
```bash
python3 "$MEASURE_PY" keepwarm-scheduler status # JSON: installed/loaded state (macOS)
python3 "$MEASURE_PY" keepwarm-tick --dry-run # JSON: what the next tick would decide
```
---
## Phase 0.6: Star the repo (first run only, once ever)
A one-time, no-pressure offer to star the repo. It is gated so it only ever surfaces for someone who has already gotten value (the gate checks `gh` is available, the repo is not already starred, and the user has session history). Check the gate:
```bash
python3 "$MEASURE_PY" star-status # JSON: {consent, gh_available, already_starred, has_value, should_ask}
```
If `should_ask` is `false`, skip this phase silently (already asked, already starred, no `gh`, no value yet, or disabled via `TOKEN_OPTIMIZER_STAR_ASK=0`). If `should_ask` is `true`, make the offer warmly and briefly, and make declining effortless:
> **Enjoying Token Optimizer?** If it's been saving you tokens, a GitHub star helps other people find it. Want me to star it for you? (One tap, and I won't ask again either way.)
Then record the answer exactly once:
```bash
# yes:
python3 "$MEASURE_PY" star-now # runs gh api -X PUT /user/starred/...; sets consent=starred
# no:
python3 "$MEASURE_PY" star-decline # terminal; never asked again
```
Only if the user defers or ignores the question (records neither) mark it shown so they are not re-asked:
```bash
python3 "$MEASURE_PY" star-consent-asked # mark shown (sticky); use ONLY when no star/decline was recorded
```
---
## Phase 1: Quick Audit (Parallel Agents)
Read `references/agent-prompts.md` for all prompt templates.
Dispatch 6 agents in parallel:
| Agent | Output File | Model | Task |
|-------|-------------|-------|------|
| CLAUDE.md Auditor | `audit/claudemd.md` | sonnet | Size, duplication, tiered content, cache structure |
| MEMORY.md Auditor | `audit/memorymd.Check running Claude Code or Codex sessions, find zombies, offer to clean up safely
Quick 10-second context health check with quality score and top issues
Audit token waste across agent systems (Claude Code, Codex, OpenClaw, Hermes, OpenCode). Detect idle burns, model misrouting, and config bloat with dollar savings.
|
Open the Token Optimizer dashboard. Collects latest session data, regenerates the dashboard, and opens it in your browser.