.claude/skills/speckit-optimize-tokens/SKILL.md
Track and report token usage across extensions and governance files.
npx skillsauth add pradeepmouli/lspeasy speckit-optimize-tokensInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
$ARGUMENTS
You MUST consider the user input before proceeding (if not empty).
Arguments: --diff to compare against the previous report, --extensions-only to skip governance files.
Measure the token footprint of every governance document and extension command that AI agents load during sessions. Produce a token usage report with per-file costs, per-extension rankings, session load estimates, and historical trends. Suggest optimizations but apply nothing without user consent.
This command answers: "How much of my AI context window is consumed by governance and tooling overhead before any actual work begins?"
.specify/ as the source of truth. Discover tool-specific files (CLAUDE.md, AGENTS.md, .github/copilot-instructions.md) by checking if they exist.chars_per_token (default: 4.0, configurable). Note this is approximate — actual tokenizer counts vary by model. Lower ratios (3.0–3.5) give more conservative estimates for code-heavy files.Scan for all files that AI agents may load on session start or command invocation:
Always-loaded files (loaded on every AI session):
CLAUDE.md (if present — Claude Code sessions)AGENTS.md (if present — generic agent sessions).github/copilot-instructions.md (if present — Copilot sessions)Constitution chain:
.specify/memory/constitution.md — read to check if it is a redirect or contains content.ai/rules/constitution.md)Supplementary governance files:
.ai/rules/*.md (if directory exists).specify/memory/*.md (beyond constitution)For each file: record path, exists (bool), size in bytes, size in characters, estimated tokens (chars ÷ chars_per_token).
For each extension listed in .specify/extensions.yml → installed::
.specify/extensions/<ext-id>/extension.ymlprovides.commands[]:
file: field points to the source)Produce a ranked list of extensions by total token footprint.
Estimate what gets loaded for different session types:
Baseline session (always loaded):
Constitution-aware session (baseline + constitution):
Command invocation (per command):
Present estimates for each context window size in context_window_sizes config (default: 8K, 32K, 128K, 200K, 1M).
### Per-Session Token Budget
| Session Type | Tokens | % of 8K | % of 32K | % of 128K | % of 200K | % of 1M |
|---|---|---|---|---|---|---|
| Baseline (governance only) | X | X% | X% | X% | X% | X% |
| + Constitution | X | X% | X% | X% | X% | X% |
| + Largest command | X | X% | X% | X% | X% | X% |
Check for a previous report at .specify/optimize/token-report.md.
If found:
file_growth_percent threshold (default: 20%)If not found:
Present the full report to the user:
## Token Usage Report
**Date**: <ISO date>
**Target Context Window**: <from config> tokens
### Governance Files
| File | Exists | Chars | Est. Tokens | Load Timing | Notes |
|---|---|---|---|---|---|
| CLAUDE.md | Yes/No | X | X | Always | — |
| .specify/memory/constitution.md | Yes/No | X | X | Always | Redirect to <path> |
| <actual constitution path> | Yes | X | X | Always | Actual content |
| AGENTS.md | Yes/No | X | X | Always | — |
| .github/copilot-instructions.md | Yes/No | X | X | Always | — |
| .ai/rules/<file>.md | Yes | X | X | On reference | — |
**Total governance tokens**: X (~Y% of <context_window>)
### Extension Commands (ranked by token cost)
| Extension | Commands | Total Tokens | Largest Command | Largest Tokens |
|---|---|---|---|---|
| <ext-id> | X | X | <cmd> | X |
| ... | ... | ... | ... | ... |
**Total extension tokens**: X (loaded per invocation, not per session)
### Per-Session Estimates
[Table from Step 3]
### Historical Trend
| File | Previous | Current | Change | Growth % | Flag |
|---|---|---|---|---|---|
| <path> | X | X | +/-X | X% | [!] if > threshold |
**Overall governance trend**: Growing / Stable / Shrinking (X% change)
### Optimization Suggestions
[Ranked by projected token savings — suggest only, do not apply]
1. **<suggestion>**: <description> — saves ~X tokens
2. ...
Ask the user: "Save this report to .specify/optimize/token-report.md for trend tracking?"
If approved:
.specify/optimize/token-report.md (create directory if needed)If declined:
Based on findings:
### Recommended Actions
- If governance budget exceeds threshold → suggest `/speckit.optimize.run` for full audit
- If specific extensions are oversized → suggest reviewing those command files for compression
- If CLAUDE.md duplicates constitution → suggest consolidation
- If growth trend is upward → suggest scheduling periodic token audits
This command reads and measures — it does not modify. The only write action is saving the report file, and only with explicit consent.
Token counts use chars ÷ chars_per_token (configurable, default: 4.0) throughout. This is an approximation — actual counts vary by tokenizer. Use 3.0–3.5 for code-heavy projects, 4.0 for prose-heavy. The approximation is consistent across runs, making trend analysis valid even if absolute numbers are approximate.
Every metric in the report is paired with a concrete action: "X tokens in version history → remove via /speckit.optimize.run". Raw numbers without actions are noise.
A single run provides a snapshot. Repeated runs provide a trend. The historical comparison is the most valuable output — it tells you whether your governance is growing, stable, or shrinking over time.
tools
Use for ANY rename, file-move, or move-symbol refactor — especially rename-heavy work across multiple files. Claude Code's built-in LSP tool is READ-ONLY (find references, but no rename / file-move / move-symbol). Hand-editing those refactors silently misses re-exports, aliased imports, type-only imports, and {@link} doc references. This skill drives a real language server via the `lspeasy` CLI to apply a correct WorkspaceEdit that catches every reference. Trigger when the user asks to rename a function/class/variable/type project-wide, move a file and fix its importers, or pull a symbol out into another module.
tools
Documentation site for lspeasy Use when: You are building a browser-based LSP client, a WebSocket-backed language....
tools
Documentation site for lspeasy Use when: You are implementing a custom client layer and need the same validation....
tools
Use when working with lspeasy (client, core, server). Covers: lsp, language-server-protocol, lsp-client, language-client, jsonrpc, transport, lsp-server, language-server.