From token-optimizer
Find the ghost tokens. Audit Claude Code or Codex setup, see where context goes, fix it. Use when context feels tight.
How this skill is triggered — by the user, by Claude, or both
Slash command
/token-optimizer:token-optimizerThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Audits a Claude Code or Codex setup, identifies context window waste, implements fixes, and measures savings.
assets/active-compression-hero.svgassets/automated-flow.svgassets/bash-compression.svgassets/before-after.svgassets/dashboard-overview.pngassets/dashboard.htmlassets/delta-mode.svgassets/fleet-demo.htmlassets/hero-terminal.svgassets/how-it-works.svgassets/logo.svgassets/logo.txtassets/quality-example.svgassets/quality-nudges-loops.svgassets/real-savings.svgassets/session-database-flow.svgassets/status-bar.svgassets/user-profiles.svgexamples/claude-md-optimized.mdexamples/hooks-starter.jsonAudits 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.
Runtime pre-gate (environment only — touches no
~/.claudepath). Before resolving any script path, check the environment directly. This keeps non-Claude runtimes from ever resolving a~/.claudepath (issue #57):# OpenCode / Copilot set these; detect them WITHOUT touching ~/.claude. # Explicit TOKEN_OPTIMIZER_RUNTIME is authoritative and checked first (matches detect_runtime()). # An explicit override to a Claude/Codex runtime is authoritative (matches detect_runtime); proceed. # Claude plugin env vars (CLAUDE_PLUGIN_ROOT/CLAUDE_PLUGIN_DATA) are checked BEFORE # OPENCODE_* env signals so a genuine Claude session with a stray OPENCODE_* export # is NOT stopped here — it falls through to measure.py, which resolves correctly # (detect_runtime step 3 beats step 4). This mirrors the Python priority order. if [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "claude" ] || [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "codex" ]; then : # fall through to the measure.py resolver + authoritative gate below elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "opencode" ]; then echo "Token Optimizer — OpenCode runtime detected." elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "copilot" ]; then echo "Token Optimizer — GitHub Copilot runtime detected." elif [ -n "${CLAUDE_PLUGIN_ROOT:-}${CLAUDE_PLUGIN_DATA:-}" ]; then : # genuine Claude Code session; fall through to measure.py (step 3 beats step 4) elif [ -n "${OPENCODE_BIN:-}${OPENCODE_CONFIG_DIR:-}${OPENCODE_DATA_DIR:-}${OPENCODE_CONFIG:-}${OPENCODE_CLIENT:-}" ]; then echo "Token Optimizer — OpenCode runtime detected." elif [ -n "${COPILOT_HOME:-}${TOKEN_OPTIMIZER_COPILOT_HOME:-}" ]; then echo "Token Optimizer — GitHub Copilot runtime detected." fi
- Prints "… OpenCode runtime detected." → STOP. Do not resolve
measure.py, do not run any phase below. Readreferences/opencode-workflow.md(bundled with this skill) and follow it. On OpenCode, Token Optimizer runs as a native plugin; the Claude audit must not run.- Prints "… GitHub Copilot runtime detected." → STOP and follow the Copilot guidance for the same reason.
- Prints nothing → continue to resolve
$MEASURE_PYbelow. This env-only pre-gate does NOT check the process tree, so OpenCode launched without exportingOPENCODE_*env vars (e.g. a bareopencodebinary ornode /path/to/opencode) prints nothing here. Themeasure.py reportruntime gate that follows is the authoritative second check — it runsdetect_runtime()which includes the ancestor-process scan and will catch those cases.
Resolve the script path once, before any phase or runtime decision. Every
command below — including the runtime gate — depends on $MEASURE_PY, so it
must be set first:
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. Is Token Optimizer installed?"; exit 1; }
With $MEASURE_PY resolved, run the runtime gate as the first executed
command. Its output is a hard stop, not a hint:
python3 "$MEASURE_PY" report 2>&1 | head -1
references/opencode-workflow.md and follow it.
The Claude Code phases scan and mutate ~/.claude, which is the wrong target
when the user is in OpenCode (issue #57).TOKEN_OPTIMIZER_RUNTIME=codex or a Codex environment
is detected, read references/codex-workflow.md and follow its chat-first
workflow instead of the phases below. Genuine Claude Code proceeds to Phase 0.MEASURE_PY was already resolved in Step 0 — do not re-resolve it.
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.
Keep-Warm is opt-in and pays off only for API-key-billed Claude Code sessions. Ask once:
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:
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:
# 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:
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:
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
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:
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:
# 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:
python3 "$MEASURE_PY" star-consent-asked # mark shown (sticky); use ONLY when no star/decline was recorded
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.md | sonnet | Size, overlap with CLAUDE.md |
| Skills Auditor | audit/skills.md | sonnet | Count, frontmatter overhead, duplicates |
| MCP Auditor | audit/mcp.md | sonnet | Deferred tools, broken/unused servers |
| Commands Auditor | audit/commands.md | haiku | Count, menu overhead |
| Settings & Advanced | audit/advanced.md | sonnet | Hooks, rules, settings, @imports, caching |
Pass COORD_PATH to each. Wait for all to complete. If any output file is missing, note the gap and proceed.
Read the Synthesis Agent prompt from references/agent-prompts.md. Dispatch with model="opus" (fallback: sonnet). It reads all audit files and writes {COORD_PATH}/analysis/optimization-plan.md. If missing, present raw audit files instead.
Read references/presentation-workflow.md for the findings template, dashboard generation, and URL presentation logic. Generate the dashboard:
python3 $MEASURE_PY dashboard --coord-path $COORD_PATH
Wait for user decision before proceeding.
Read references/implementation-playbook.md for detailed steps. Available actions: 4A-4P covering CLAUDE.md, MEMORY.md, Skills, File Exclusion, MCP, Hooks, Cache, Rules, Settings, Descriptions, Compact Instructions, Model Routing, Smart Compaction, Quality Check, Version-Aware Optimizations, and Smart Routing. Templates in examples/. Always backup before changes. Present diffs for approval.
Read the Verification Agent prompt from references/agent-prompts.md. Dispatch with model="haiku". Re-measures everything and calculates savings. Present before/after comparison and behavioral next steps.
Reopen a forgotten/cold session cheaply, no --resume, no command. On a fresh
session, when the user naturally asks to continue prior work ("continue the X
work, check what we discussed last session"), the continuity hook reconstructs a
lean context for the right same-project prior session and injects it.
session_log only (no LLM,
no subprocess). The only cost is the fresh session's normal first turn.resume_lean event (avoided cold-resume
cache-rewrite minus the lean block), idempotent per target session, shown in the
Savings view. Realized tier, same as checkpoint_restore.python3 $MEASURE_PY resume-lean lists cold sessions;
resume-lean <#|session_id> --print emits the block for claude "$(...)".| Context | Read |
|---|---|
| Codex runtime | references/codex-workflow.md |
| Phase 0 setup details | references/phase0-setup.md |
| Phase 1-2 agent prompts | references/agent-prompts.md, references/token-flow-architecture.md |
| Phase 3 presentation | references/presentation-workflow.md |
| Phase 4 implementation | references/implementation-playbook.md, examples/ |
| CLI commands | references/cli-reference.md |
| Phase 3 checklist | references/optimization-checklist.md |
| Error handling | references/error-recovery.md |
npx claudepluginhub danikdanik/token-optimizerGuides collaborative design exploration before implementation: explores context, asks clarifying questions, proposes approaches, and writes a design doc for user approval.
Creates structured, bite-sized implementation plans from specs or requirements before writing code. Useful for breaking down multi-step tasks into testable steps with file structure and task boundaries.
Implements work from a spec or tickets using TDD at agreed seams, with regular typechecking and test runs, followed by code review.