@alexgreensh/token-optimizer 0.0.0-stage → 5.13.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +76 -0
- package/.claude-plugin/plugin.json +21 -0
- package/LICENSE +130 -0
- package/LICENSE-SMALL-BUSINESS.md +62 -0
- package/PRIVACY.md +200 -0
- package/README.md +13 -2
- package/hooks/codex-hooks.json +4 -0
- package/hooks/hooks.json +182 -0
- package/hooks/module_runner.py +149 -0
- package/hooks/posttooluse_runner.py +1110 -0
- package/hooks/python-launcher.sh +816 -0
- package/hooks/run.py +776 -0
- package/hooks/sessionstart_runner.py +629 -0
- package/hooks/stop_runner.py +408 -0
- package/hooks/userpromptsubmit_runner.py +722 -0
- package/hooks/windows-launcher.py +163 -0
- package/install-cloud.sh +23 -0
- package/package.json +29 -4
- package/skills/fleet-auditor/SKILL.md +150 -0
- package/skills/fleet-auditor/references/fleet-systems.md +167 -0
- package/skills/fleet-auditor/references/report-template.md +50 -0
- package/skills/fleet-auditor/references/waste-patterns.md +126 -0
- package/skills/fleet-auditor/scripts/fleet.py +3612 -0
- package/skills/fleet-auditor/scripts/shared.py +277 -0
- package/skills/resume-checkpoint/SKILL.md +52 -0
- package/skills/resume-checkpoint/scripts/pull_checkpoint.py +188 -0
- package/skills/token-coach/SKILL.md +129 -0
- package/skills/token-coach/assets/coach-dashboard-tab.html +45 -0
- package/skills/token-coach/examples/coaching-session-agentic.md +62 -0
- package/skills/token-coach/examples/coaching-session-heavy-setup.md +46 -0
- package/skills/token-coach/examples/coaching-session-new-project.md +38 -0
- package/skills/token-coach/references/agentic-systems.md +147 -0
- package/skills/token-coach/references/coach-patterns.md +120 -0
- package/skills/token-coach/references/coaching-scripts.md +146 -0
- package/skills/token-coach/references/deterministic-stages.md +102 -0
- package/skills/token-coach/references/keepwarm-consent.md +48 -0
- package/skills/token-coach/references/quick-reference.md +155 -0
- package/skills/token-dashboard/SKILL.md +72 -0
- package/skills/token-optimizer/SKILL.md +150 -0
- package/skills/token-optimizer/assets/dashboard.html +7251 -0
- package/skills/token-optimizer/examples/claude-md-optimized.md +40 -0
- package/skills/token-optimizer/examples/hooks-starter.json +179 -0
- package/skills/token-optimizer/examples/permissions-deny-template.json +24 -0
- package/skills/token-optimizer/pricing/prices.json +695 -0
- package/skills/token-optimizer/references/agent-prompts.md +687 -0
- package/skills/token-optimizer/references/cli-reference.md +92 -0
- package/skills/token-optimizer/references/codex-workflow.md +188 -0
- package/skills/token-optimizer/references/cursor-workflow.md +74 -0
- package/skills/token-optimizer/references/error-recovery.md +37 -0
- package/skills/token-optimizer/references/implementation-playbook.md +416 -0
- package/skills/token-optimizer/references/keepwarm-consent.md +48 -0
- package/skills/token-optimizer/references/opencode-workflow.md +72 -0
- package/skills/token-optimizer/references/optimization-checklist.md +714 -0
- package/skills/token-optimizer/references/phase0-setup.md +142 -0
- package/skills/token-optimizer/references/presentation-workflow.md +72 -0
- package/skills/token-optimizer/references/session-continuity.md +19 -0
- package/skills/token-optimizer/references/token-flow-architecture.md +629 -0
- package/skills/token-optimizer/scripts/activity_tracker.py +143 -0
- package/skills/token-optimizer/scripts/antigravity_doctor.py +548 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +511 -0
- package/skills/token-optimizer/scripts/antigravity_install.py +376 -0
- package/skills/token-optimizer/scripts/antigravity_proto.py +413 -0
- package/skills/token-optimizer/scripts/antigravity_session.py +346 -0
- package/skills/token-optimizer/scripts/antigravity_state.py +486 -0
- package/skills/token-optimizer/scripts/archive_result.py +1725 -0
- package/skills/token-optimizer/scripts/bash_compress.py +2490 -0
- package/skills/token-optimizer/scripts/bash_compress_hook.py +766 -0
- package/skills/token-optimizer/scripts/bash_hook.py +61 -0
- package/skills/token-optimizer/scripts/bash_whitelist.py +243 -0
- package/skills/token-optimizer/scripts/benchmark.py +1765 -0
- package/skills/token-optimizer/scripts/build_output_compress.py +776 -0
- package/skills/token-optimizer/scripts/cloud_kit.py +991 -0
- package/skills/token-optimizer/scripts/codex_command_compress.py +434 -0
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +273 -0
- package/skills/token-optimizer/scripts/codex_doctor.py +454 -0
- package/skills/token-optimizer/scripts/codex_hook_bridge.py +311 -0
- package/skills/token-optimizer/scripts/codex_install.py +810 -0
- package/skills/token-optimizer/scripts/codex_io.py +119 -0
- package/skills/token-optimizer/scripts/codex_log_index.py +219 -0
- package/skills/token-optimizer/scripts/codex_models.py +34 -0
- package/skills/token-optimizer/scripts/codex_session.py +1186 -0
- package/skills/token-optimizer/scripts/codex_state.py +707 -0
- package/skills/token-optimizer/scripts/codex_statusline.py +303 -0
- package/skills/token-optimizer/scripts/command_filters.py +386 -0
- package/skills/token-optimizer/scripts/compression_backfill.py +600 -0
- package/skills/token-optimizer/scripts/compression_log.py +230 -0
- package/skills/token-optimizer/scripts/context_intel.py +331 -0
- package/skills/token-optimizer/scripts/context_pressure.py +115 -0
- package/skills/token-optimizer/scripts/copilot_doctor.py +406 -0
- package/skills/token-optimizer/scripts/copilot_hook_bridge.py +709 -0
- package/skills/token-optimizer/scripts/copilot_install.py +277 -0
- package/skills/token-optimizer/scripts/copilot_session.py +496 -0
- package/skills/token-optimizer/scripts/copilot_state.py +569 -0
- package/skills/token-optimizer/scripts/copilot_vscode.py +832 -0
- package/skills/token-optimizer/scripts/cowork_doctor.py +270 -0
- package/skills/token-optimizer/scripts/cowork_install.py +380 -0
- package/skills/token-optimizer/scripts/credential_patterns.py +1514 -0
- package/skills/token-optimizer/scripts/cursor_doctor.py +602 -0
- package/skills/token-optimizer/scripts/cursor_hook_bridge.py +843 -0
- package/skills/token-optimizer/scripts/cursor_install.py +423 -0
- package/skills/token-optimizer/scripts/cursor_session.py +303 -0
- package/skills/token-optimizer/scripts/cursor_state.py +348 -0
- package/skills/token-optimizer/scripts/delta_diff.py +110 -0
- package/skills/token-optimizer/scripts/detectors/__init__.py +2 -0
- package/skills/token-optimizer/scripts/detectors/bad_decomposition.py +78 -0
- package/skills/token-optimizer/scripts/detectors/cache_instability.py +504 -0
- package/skills/token-optimizer/scripts/detectors/looping.py +88 -0
- package/skills/token-optimizer/scripts/detectors/output_waste.py +143 -0
- package/skills/token-optimizer/scripts/detectors/overpowered.py +116 -0
- package/skills/token-optimizer/scripts/detectors/pdf_ingestion.py +52 -0
- package/skills/token-optimizer/scripts/detectors/registry.py +65 -0
- package/skills/token-optimizer/scripts/detectors/respond_to_bash.py +76 -0
- package/skills/token-optimizer/scripts/detectors/retry_churn.py +83 -0
- package/skills/token-optimizer/scripts/detectors/tool_cascade.py +62 -0
- package/skills/token-optimizer/scripts/detectors/wasteful_thinking.py +75 -0
- package/skills/token-optimizer/scripts/detectors/weak_model.py +55 -0
- package/skills/token-optimizer/scripts/detectors/websearch_routing.py +72 -0
- package/skills/token-optimizer/scripts/deterministic_candidates.py +1458 -0
- package/skills/token-optimizer/scripts/devin_compress.py +790 -0
- package/skills/token-optimizer/scripts/devin_doctor.py +403 -0
- package/skills/token-optimizer/scripts/devin_hooks.py +453 -0
- package/skills/token-optimizer/scripts/devin_install.py +1086 -0
- package/skills/token-optimizer/scripts/devin_source.py +119 -0
- package/skills/token-optimizer/scripts/devin_state.py +330 -0
- package/skills/token-optimizer/scripts/devin_verify.py +67 -0
- package/skills/token-optimizer/scripts/grok_doctor.py +560 -0
- package/skills/token-optimizer/scripts/grok_hook_bridge.py +699 -0
- package/skills/token-optimizer/scripts/grok_install.py +397 -0
- package/skills/token-optimizer/scripts/grok_session.py +317 -0
- package/skills/token-optimizer/scripts/grok_state.py +299 -0
- package/skills/token-optimizer/scripts/hermes_doctor.py +479 -0
- package/skills/token-optimizer/scripts/hermes_hook_bridge.py +285 -0
- package/skills/token-optimizer/scripts/hermes_install.py +668 -0
- package/skills/token-optimizer/scripts/hermes_session.py +691 -0
- package/skills/token-optimizer/scripts/hermes_state.py +448 -0
- package/skills/token-optimizer/scripts/hook_io.py +120 -0
- package/skills/token-optimizer/scripts/hook_runtime.py +713 -0
- package/skills/token-optimizer/scripts/injection.py +156 -0
- package/skills/token-optimizer/scripts/install_reconcile.py +952 -0
- package/skills/token-optimizer/scripts/measure.py +58739 -0
- package/skills/token-optimizer/scripts/outline.py +53 -0
- package/skills/token-optimizer/scripts/pipeline_analyzer.py +723 -0
- package/skills/token-optimizer/scripts/plugin_env.py +562 -0
- package/skills/token-optimizer/scripts/py_trust.py +80 -0
- package/skills/token-optimizer/scripts/quality_cache_gate.py +320 -0
- package/skills/token-optimizer/scripts/read_cache.py +2489 -0
- package/skills/token-optimizer/scripts/recovery_output.py +110 -0
- package/skills/token-optimizer/scripts/refetch_fingerprint.py +236 -0
- package/skills/token-optimizer/scripts/refetch_guard.py +244 -0
- package/skills/token-optimizer/scripts/routing_advisor.py +389 -0
- package/skills/token-optimizer/scripts/runtime_env.py +1893 -0
- package/skills/token-optimizer/scripts/session_store.py +979 -0
- package/skills/token-optimizer/scripts/spawn_utils.py +122 -0
- package/skills/token-optimizer/scripts/statusline.js +660 -0
- package/skills/token-optimizer/scripts/structure_map.py +2880 -0
- package/skills/token-optimizer/scripts/structure_map_ts.py +676 -0
- package/skills/token-optimizer/scripts/structure_replay.py +1556 -0
- package/skills/token-optimizer/scripts/thrash_guard.py +604 -0
- package/skills/token-optimizer/scripts/token_estimate.py +80 -0
- package/skills/token-optimizer/scripts/tool_search_diagnostic.py +61 -0
- package/skills/token-optimizer/scripts/utf8_io.py +219 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
# Implementation Playbook
|
|
2
|
+
|
|
3
|
+
Detailed implementation steps for Phase 4 of the Token Optimizer. The orchestrator dispatches to these based on user choice.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Contents
|
|
8
|
+
|
|
9
|
+
- [4A: CLAUDE.md Consolidation](#4a-claudemd-consolidation)
|
|
10
|
+
- [4B: MEMORY.md Deduplication](#4b-memorymd-deduplication)
|
|
11
|
+
- [4C: Skill Archival](#4c-skill-archival)
|
|
12
|
+
- [4D: File Exclusion Rules](#4d-file-exclusion-rules)
|
|
13
|
+
- [4E: MCP Server Guidance](#4e-mcp-server-guidance)
|
|
14
|
+
- [4F: Hooks Configuration](#4f-hooks-configuration)
|
|
15
|
+
- [4G: CLAUDE.md Cache Structure](#4g-claudemd-cache-structure)
|
|
16
|
+
- [4H: Rules Cleanup](#4h-rules-cleanup)
|
|
17
|
+
- [4I: Settings Tuning](#4i-settings-tuning)
|
|
18
|
+
- [4J: Skill Description Tightening](#4j-skill-description-tightening)
|
|
19
|
+
- [4K: Compact Instructions Setup](#4k-compact-instructions-setup)
|
|
20
|
+
- [4L: Model Routing Setup](#4l-model-routing-setup)
|
|
21
|
+
- [4O: Version-Aware Optimizations](#4o-version-aware-optimizations)
|
|
22
|
+
- [Quality Checklist](#quality-checklist)
|
|
23
|
+
- [Anti-Patterns](#anti-patterns)
|
|
24
|
+
- [Error Handling](#error-handling)
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 4A: CLAUDE.md Consolidation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# Backup first
|
|
32
|
+
cp ~/.claude/CLAUDE.md ~/.claude/_backups/CLAUDE.md.pre-optimization-$(date +%Y%m%d)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Steps**:
|
|
36
|
+
1. Read current CLAUDE.md
|
|
37
|
+
2. Apply tiered architecture pattern:
|
|
38
|
+
- **Tier 1 (always loaded, ~300 lines / ~4,500 tokens — internal heuristic, not Anthropic's under-200-lines guidance)**: Identity, critical rules, key paths
|
|
39
|
+
- **Tier 2 (skill/command, loaded on-demand)**: Workflows, domain docs, tool configs
|
|
40
|
+
- **Tier 3 (file reference, explicit only)**: Full guides, templates, detailed standards
|
|
41
|
+
3. Move Tier 2/3 content to skills or reference files
|
|
42
|
+
4. Output optimized version to `{COORD_PATH}/plan/CLAUDE.md.optimized`
|
|
43
|
+
5. Present diff to user for approval before overwriting
|
|
44
|
+
|
|
45
|
+
**Targets**:
|
|
46
|
+
- Remove content that belongs in skills/commands (workflows, detailed configs)
|
|
47
|
+
- Remove content that duplicates MEMORY.md
|
|
48
|
+
- Move reference content to on-demand files
|
|
49
|
+
- Condense personality/voice specs to 1-2 lines (full spec can live in MEMORY.md)
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 4B: MEMORY.md Deduplication
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Backup first (handles multiple projects)
|
|
57
|
+
for memfile in ~/.claude/projects/*/memory/MEMORY.md; do
|
|
58
|
+
[ -f "$memfile" ] || continue
|
|
59
|
+
projname=$(basename "$(dirname "$(dirname "$memfile")")")
|
|
60
|
+
cp "$memfile" "$HOME/.claude/_backups/MEMORY-${projname}.pre-optimization-$(date +%Y%m%d).md"
|
|
61
|
+
done
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Steps**:
|
|
65
|
+
1. Read current MEMORY.md
|
|
66
|
+
2. Remove content that duplicates CLAUDE.md (choose ONE source of truth)
|
|
67
|
+
3. Condense verbose operational history to current rule only
|
|
68
|
+
4. Keep only: learnings, corrections, habit tracking
|
|
69
|
+
5. Output to `{COORD_PATH}/plan/MEMORY.md.optimized`
|
|
70
|
+
6. Present diff for approval
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 4C: Skill Archival
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# Create backup location
|
|
78
|
+
mkdir -p ~/.claude/_backups/skills-archived-$(date +%Y%m%d)
|
|
79
|
+
|
|
80
|
+
# Move identified skills
|
|
81
|
+
mv ~/.claude/skills/[skill-name] ~/.claude/_backups/skills-archived-$(date +%Y%m%d)/
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
A subfolder such as `_archived/` inside `skills/` still loads as a namespace, so move archived skills outside `skills/` entirely.
|
|
85
|
+
|
|
86
|
+
List what will be archived, ask for confirmation before moving.
|
|
87
|
+
|
|
88
|
+
**DEPENDENCY CHECK (mandatory before archival)**:
|
|
89
|
+
Before archiving any skill, search for references to it:
|
|
90
|
+
1. `grep -r "[skill-name]" ~/.claude/CLAUDE.md ~/.claude/rules/ ~/.claude/skills/` to find @imports or instructions that reference it
|
|
91
|
+
2. Check if any MCP server tools depend on the skill (e.g., a skill that configures API keys used by MCP tools)
|
|
92
|
+
3. Warn the user: "Archiving [skill] may break [dependent] which references it. Archive anyway?"
|
|
93
|
+
If dependencies are found, list them explicitly and get confirmation.
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 4D: File Exclusion Rules
|
|
98
|
+
|
|
99
|
+
If missing, add `permissions.deny` rules to `.claude/settings.json` (project-level) or `~/.claude/settings.json` (global). See `examples/permissions-deny-template.json` for a starter template.
|
|
100
|
+
|
|
101
|
+
Start with the **security-critical** excludes only. These are files Claude has no reason to read, so it never bumps into the rule, and they protect secrets:
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"permissions": {
|
|
106
|
+
"deny": [
|
|
107
|
+
"Read(./.env)",
|
|
108
|
+
"Read(./.env.*)",
|
|
109
|
+
"Read(./secrets/**)"
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Add noise excludes ONLY if they apply to your repo, and prefer narrow paths:**
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"permissions": {
|
|
120
|
+
"deny": [
|
|
121
|
+
"Read(./node_modules/**)",
|
|
122
|
+
"Read(./dist/**)",
|
|
123
|
+
"Read(./**/*.log)"
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Why**: Files matching deny patterns are excluded from file discovery, search, and read operations. This is the officially supported approach (replaces deprecated `ignorePatterns`). Deny rules are enforced *locally* by Claude Code's permission engine and are NOT injected into the prompt, so the rules themselves cost zero tokens per turn.
|
|
130
|
+
|
|
131
|
+
**TOKEN-COST WARNING (the counterintuitive one)**:
|
|
132
|
+
Deny rules save tokens when Claude *never tries* the denied path (e.g. `.env`, `secrets/`). But a **broad deny rule on a path Claude actively wants to explore can cost tokens instead of saving them.** When Claude attempts a denied read, the denial comes back as tool feedback, and that feedback **accumulates in the conversation and is re-sent on every subsequent turn.** A broad glob plus an exploring agent can stack up dozens of "blocked" messages.
|
|
133
|
+
|
|
134
|
+
To stay on the saving side of the line:
|
|
135
|
+
- **Prefer narrow, specific paths** over broad globs. `Read(./logs/**)` beats `Read(./**/*.log)`.
|
|
136
|
+
- **Only deny what Claude wouldn't need anyway.** Excluding `node_modules/` is safe because Claude rarely reads it; denying a source directory Claude keeps reaching into is not.
|
|
137
|
+
- **If you see Claude repeatedly hit "permission denied" on the same path, that rule is costing you, not saving you.** Narrow it or remove it.
|
|
138
|
+
|
|
139
|
+
**SIDE EFFECT WARNING (mandatory before applying)**:
|
|
140
|
+
Deny rules affect ALL tools in ALL sessions. Before applying:
|
|
141
|
+
1. **Database deny rules** (`*.db`, `*.sqlite`): Will break any skill or MCP server that reads SQLite databases (e.g., session memory tools, local search indexes, WhatsApp MCP). Only add these at project level, never globally, unless you are certain no tools need database access.
|
|
142
|
+
2. **Credential deny rules** (`.env`, `*.key`, `*.pem`): Will prevent Claude from reading these files. If any skill reads API keys from `.env` at runtime, it will fail silently. This is usually DESIRED for security, but warn the user.
|
|
143
|
+
3. **Global vs project-level**: Recommend project-level (`./claude/settings.json`) over global (`~/.claude/settings.json`). Global rules affect every project and are harder to debug when something breaks.
|
|
144
|
+
|
|
145
|
+
Always tell the user: "These deny rules will prevent Claude from reading matching files in ALL sessions. If any of your skills or MCP servers need access to these file types, they will stop working. Apply at project level first to test."
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 4E: MCP Server Guidance
|
|
150
|
+
|
|
151
|
+
Don't auto-disable MCP servers (requires manual config edits). Instead, provide:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
To disable these MCP servers:
|
|
155
|
+
|
|
156
|
+
1. Edit config file:
|
|
157
|
+
- Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json
|
|
158
|
+
- Claude Code: ~/.claude/settings.json
|
|
159
|
+
|
|
160
|
+
2. Remove or comment out these entries:
|
|
161
|
+
- [server1]: [reason]
|
|
162
|
+
- [server2]: [reason]
|
|
163
|
+
|
|
164
|
+
3. Restart Claude
|
|
165
|
+
|
|
166
|
+
Estimated savings: ~X tokens
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**CONSEQUENCE CHECK (mandatory before suggesting MCP disabling)**:
|
|
170
|
+
1. Search CLAUDE.md, skills, and rules for references to the server's tool names (e.g., `mcp__[server]__*`)
|
|
171
|
+
2. If ANY skill or instruction references the server's tools, warn: "Disabling [server] will break [skill/instruction] which uses [tool_name]. Disable anyway?"
|
|
172
|
+
3. For servers with deferred tool loading: even if no hardcoded references exist, the user may invoke them conversationally. Ask: "Do you ever use [server] tools directly in conversation?" before recommending removal.
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## 4F: Hooks Configuration
|
|
177
|
+
|
|
178
|
+
### SessionEnd (measure.py collect)
|
|
179
|
+
|
|
180
|
+
Handled in Phase 0 setup. If the user skipped it there, offer again here:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
python3 $MEASURE_PY check-hook
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
If not installed, run `setup-hook --dry-run` to show the proposed change, then `setup-hook` after confirmation. The hook runs `measure.py collect --quiet` once per session close (~1 second, zero background processes).
|
|
187
|
+
|
|
188
|
+
### PreCompact
|
|
189
|
+
|
|
190
|
+
Guides what Claude preserves during context compaction (prevents loss of critical context). Offer `examples/hooks-starter.json` template for the PreCompact hook.
|
|
191
|
+
|
|
192
|
+
### PostToolUse
|
|
193
|
+
|
|
194
|
+
Triggers auto-formatters on file writes, saving output tokens on style explanations. Show the JSON template, explain the hook, and ask user before creating.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## 4G: CLAUDE.md Cache Structure
|
|
199
|
+
|
|
200
|
+
If CLAUDE.md has volatile content mixed with stable content, restructure for prompt caching.
|
|
201
|
+
|
|
202
|
+
See `examples/claude-md-optimized.md` for the pattern.
|
|
203
|
+
|
|
204
|
+
**Why**: Prompt caching caches prefixes. If stable content comes first, it stays cached (90% cheaper). Volatile content at the end doesn't break the cache prefix.
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## 4H: Rules Cleanup
|
|
209
|
+
|
|
210
|
+
Scan `.claude/rules/` directory and optimize rule files.
|
|
211
|
+
|
|
212
|
+
**Steps**:
|
|
213
|
+
1. List all files in `~/.claude/rules/` (if directory exists)
|
|
214
|
+
2. For each rule file:
|
|
215
|
+
- Measure token cost (lines x ~15 for prose, ~8 for YAML)
|
|
216
|
+
- Check for `paths:` frontmatter (scoped vs always-loaded)
|
|
217
|
+
- Compare content against other rules for duplication
|
|
218
|
+
3. Present findings:
|
|
219
|
+
```
|
|
220
|
+
Rules Directory: X files, ~Y tokens total
|
|
221
|
+
Always-loaded (no path scope): X files, ~Y tokens
|
|
222
|
+
Path-scoped: X files
|
|
223
|
+
|
|
224
|
+
Optimization opportunities:
|
|
225
|
+
- [rule1.md] and [rule2.md]: 60% content overlap, merge candidate
|
|
226
|
+
- [rule3.md]: No path scope but only applies to tests/ (add paths: ["tests/**"])
|
|
227
|
+
- [rule4.md]: Stale (references deprecated tool)
|
|
228
|
+
```
|
|
229
|
+
4. Generate merge plan for duplicates
|
|
230
|
+
5. Execute after user approval (backup originals to `~/.claude/_backups/rules-$(date +%Y%m%d)/`)
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## 4I: Settings Tuning
|
|
235
|
+
|
|
236
|
+
Audit settings.json env block and help user tune token-relevant variables.
|
|
237
|
+
|
|
238
|
+
**Steps**:
|
|
239
|
+
1. Read `~/.claude/settings.json` (and `settings.local.json` if exists)
|
|
240
|
+
2. Check env block for token-relevant variables (items 23-30 from checklist)
|
|
241
|
+
3. Present current vs default values with tradeoff explanations:
|
|
242
|
+
```
|
|
243
|
+
Settings Audit:
|
|
244
|
+
| Variable | Current | Default | Recommendation |
|
|
245
|
+
|---------------------------------|---------|---------|----------------|
|
|
246
|
+
| CLAUDE_AUTOCOMPACT_PCT_OVERRIDE | user choice | unset | Percent of the compact window used when compaction fires; lower compacts earlier. Reported, never changed |
|
|
247
|
+
| MAX_THINKING_TOKENS | not set | unset (adaptive models pick their own depth) | Default is fine |
|
|
248
|
+
| ENABLE_TOOL_SEARCH | unset | unset (MCP tools deferred) | Good (active) |
|
|
249
|
+
```
|
|
250
|
+
4. Apply user-chosen changes to settings.json env block
|
|
251
|
+
5. Verify changes don't conflict with settings.local.json overrides
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## 4J: Skill Description Tightening
|
|
256
|
+
|
|
257
|
+
Flag verbose skill frontmatter and generate tighter descriptions.
|
|
258
|
+
|
|
259
|
+
Claude Code truncates combined `description` + `when_to_use` text at 1,536 characters (since v2.1.105). Descriptions over this limit waste the overflow tokens. Descriptions under 1,536 are NOT truncated but shorter is still more efficient since they load every session.
|
|
260
|
+
|
|
261
|
+
**Steps**:
|
|
262
|
+
1. Scan all skill SKILL.md files in `~/.claude/skills/` and plugin-bundled skills
|
|
263
|
+
2. Extract frontmatter `description:` and `when_to_use:` fields from each
|
|
264
|
+
3. Flag descriptions with combined length >1,536 chars as TRUNCATED (correctness bug)
|
|
265
|
+
4. Flag descriptions >200 characters as verbose (efficiency opportunity, not a bug)
|
|
266
|
+
5. Generate tighter alternatives for verbose descriptions:
|
|
267
|
+
```
|
|
268
|
+
Truncated Descriptions (high priority):
|
|
269
|
+
- my-mega-skill (1,842 chars): Combined description + when_to_use exceeds 1,536 limit
|
|
270
|
+
Action: Split content between description (core purpose) and SKILL.md body (detailed usage)
|
|
271
|
+
|
|
272
|
+
Verbose Descriptions (efficiency):
|
|
273
|
+
- morning (312 chars): "Your comprehensive daily briefing that covers email, calendar..."
|
|
274
|
+
Suggested: "Daily briefing: email, calendar, tasks, partner updates"
|
|
275
|
+
```
|
|
276
|
+
6. Apply approved changes to SKILL.md frontmatter (backup first)
|
|
277
|
+
|
|
278
|
+
**Note**: Only modify `description:` and `when_to_use:` fields in frontmatter. Never touch skill body content.
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 4K: Compact Instructions Setup
|
|
283
|
+
|
|
284
|
+
Generate and add a compact instructions section to CLAUDE.md.
|
|
285
|
+
|
|
286
|
+
**Steps**:
|
|
287
|
+
1. Read current CLAUDE.md content
|
|
288
|
+
2. Identify what should survive compaction:
|
|
289
|
+
- Current task context
|
|
290
|
+
- Key file paths being modified
|
|
291
|
+
- Active decisions and constraints
|
|
292
|
+
- Error states and test results
|
|
293
|
+
3. Generate a compact instructions section:
|
|
294
|
+
```markdown
|
|
295
|
+
## Compact Instructions
|
|
296
|
+
When compacting this conversation, always preserve:
|
|
297
|
+
- Current task context and progress
|
|
298
|
+
- File paths being modified and their state
|
|
299
|
+
- Test results and error messages
|
|
300
|
+
- Active constraints and decisions made
|
|
301
|
+
- User preferences expressed in this session
|
|
302
|
+
```
|
|
303
|
+
4. Present to user for customization
|
|
304
|
+
5. Add to CLAUDE.md after approval (place near end, volatile section)
|
|
305
|
+
|
|
306
|
+
**Why**: Without compact instructions, compaction is generic and may lose critical session context. This is especially valuable for long sessions with complex multi-step work.
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## 4L: Model Routing Setup
|
|
311
|
+
|
|
312
|
+
Add or improve model routing instructions in CLAUDE.md.
|
|
313
|
+
|
|
314
|
+
**Steps**:
|
|
315
|
+
1. Check if CLAUDE.md already has model routing instructions:
|
|
316
|
+
- Grep for "haiku", "sonnet", "opus", "model" in CLAUDE.md
|
|
317
|
+
- If found, assess specificity (detailed table vs vague "use appropriate models")
|
|
318
|
+
2. If no routing instructions exist, present the recommended snippet:
|
|
319
|
+
```markdown
|
|
320
|
+
## Agent Model Selection
|
|
321
|
+
Default subagents to haiku. Upgrade only when task requires judgment:
|
|
322
|
+
- haiku: file reading, data gathering, counting, scanning, formatting
|
|
323
|
+
- sonnet: analysis, code review, writing, moderate reasoning
|
|
324
|
+
- opus: architecture decisions, novel debugging, cross-cutting synthesis
|
|
325
|
+
```
|
|
326
|
+
3. If routing instructions exist, compare against actual model_mix usage:
|
|
327
|
+
- Run `python3 $MEASURE_PY trends --json --days 30` if trends DB exists
|
|
328
|
+
- Are subagents actually following the routing? (check model_mix percentages)
|
|
329
|
+
- Is the instruction specific enough? ("default to haiku" vs detailed task-to-model table)
|
|
330
|
+
- If >70% of tokens go to Opus despite routing instructions, the instructions may be too vague
|
|
331
|
+
4. Present routing snippet for user approval
|
|
332
|
+
5. Add to CLAUDE.md under the heading `## Agent Model Selection`. Place in the stable
|
|
333
|
+
(static) section of CLAUDE.md, not the volatile tail (routing instructions rarely change,
|
|
334
|
+
so they benefit from prompt caching). If no clear section exists, add after identity/rules.
|
|
335
|
+
|
|
336
|
+
**Targets**:
|
|
337
|
+
- Lower cost on multi-agent workflows for API users (the lighter tier bills less per token)
|
|
338
|
+
- Rate limit conservation for subscription users (lighter models use fewer quota units)
|
|
339
|
+
- Faster responses on data-gathering agents
|
|
340
|
+
- No context window impact (routing doesn't change system prompt size)
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## 4O: Version-Aware Optimizations
|
|
345
|
+
|
|
346
|
+
Check the user's Claude Code version (`claude --version`) and apply version-specific guidance:
|
|
347
|
+
|
|
348
|
+
**v2.1.86+: Read tool native deduplication**
|
|
349
|
+
- Claude Code deduplicates unchanged re-reads natively (compact line-number format)
|
|
350
|
+
- If Token Optimizer's `read_cache.py` PreToolUse hook is installed, it is redundant on v2.1.86+
|
|
351
|
+
- Check: run `python3 $MEASURE_PY read-cache-stats --session current`. If "blocks: 0" for recent sessions, native dedup is handling it.
|
|
352
|
+
- Action: Disable the read-cache hook (`TOKEN_OPTIMIZER_READ_CACHE=0` in settings.json env block) to eliminate hook process overhead
|
|
353
|
+
|
|
354
|
+
**v2.1.86+: @ file mentions no longer JSON-escaped**
|
|
355
|
+
- Raw string content in file mentions saves tokens automatically
|
|
356
|
+
- No action needed. Note in findings that file-heavy sessions save tokens for free
|
|
357
|
+
|
|
358
|
+
**v2.1.84+: Idle-return /clear prompt conflicts with Smart Compaction**
|
|
359
|
+
- Claude Code prompts to /clear after 75+ min idle
|
|
360
|
+
- /clear destroys Smart Compaction checkpoints. /compact preserves them.
|
|
361
|
+
- Action: If Smart Compaction is active, tell the user to choose /compact instead of /clear when prompted. Add to CLAUDE.md: "After idle, use /compact not /clear (preserves checkpoints)."
|
|
362
|
+
|
|
363
|
+
**v2.1.83+: Auto-compact circuit breaker**
|
|
364
|
+
- Auto-compaction stops after 3 consecutive failures and does not retry until a new session
|
|
365
|
+
- Smart Compaction's PreCompact hook will not fire after the circuit breaker trips
|
|
366
|
+
- Action: If user reports "compaction stopped working mid-session", explain the circuit breaker. Workaround: run `/compact` manually (resets the counter)
|
|
367
|
+
|
|
368
|
+
**v2.1.85+: Conditional hooks with `if` field**
|
|
369
|
+
- Token Optimizer's hooks can use `if` filters to skip execution when not needed
|
|
370
|
+
- Action: Add `"if": "tool_uses"` to the quality-cache PostToolUse hook to skip on messages with no tool calls
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## Quality Checklist
|
|
375
|
+
|
|
376
|
+
- [ ] Coordination folder created with manifest
|
|
377
|
+
- [ ] All 6 audit agents dispatched in parallel
|
|
378
|
+
- [ ] Synthesis completed with tiered plan
|
|
379
|
+
- [ ] Findings presented clearly (no jargon)
|
|
380
|
+
- [ ] User consent before any file changes
|
|
381
|
+
- [ ] Backups created before modifications
|
|
382
|
+
- [ ] Verification run after changes
|
|
383
|
+
- [ ] Results quantified (tokens + cost)
|
|
384
|
+
- [ ] Hooks configuration offered (PreCompact, PostToolUse)
|
|
385
|
+
- [ ] CLAUDE.md cache structure checked (static first, volatile last)
|
|
386
|
+
- [ ] Model routing instructions checked/added (4L)
|
|
387
|
+
- [ ] Version-aware optimizations checked (4O)
|
|
388
|
+
- [ ] Token monitoring tools recommended (measure.py trends + SessionEnd hook, /context, /cost)
|
|
389
|
+
|
|
390
|
+
---
|
|
391
|
+
|
|
392
|
+
## Anti-Patterns
|
|
393
|
+
|
|
394
|
+
| DON'T | DO |
|
|
395
|
+
|-------|-----|
|
|
396
|
+
| Make changes without user approval | Ask before implementing |
|
|
397
|
+
| Delete files | Always archive to `~/.claude/_backups/` |
|
|
398
|
+
| Claim "this might save tokens" | MEASURE IT (use scripts/measure.py) |
|
|
399
|
+
| Skip verification step | Run Phase 5 after every change |
|
|
400
|
+
| Use opus for simple file reading | Match model to task: haiku for counting, sonnet for judgment, opus for synthesis |
|
|
401
|
+
| Present findings without next steps | Quantify everything (X tokens, Y%) |
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
## Error Handling
|
|
406
|
+
|
|
407
|
+
| Issue | Response |
|
|
408
|
+
|-------|----------|
|
|
409
|
+
| CLAUDE.md not found | "No global CLAUDE.md found. This is unusual but means zero overhead from it. Skip to skills audit." |
|
|
410
|
+
| MEMORY.md not found | "No MEMORY.md found. Skip this optimization." |
|
|
411
|
+
| No skills directory | "No skills found. Setup is minimal (good for tokens). Focus on CLAUDE.md + MCP." |
|
|
412
|
+
| Can't measure MCP tools | "Deferred tools not visible in this session. Skip MCP audit or check Desktop config manually." |
|
|
413
|
+
| User says 'skip verification' | "Noted. Skipping verification. Recommend running /cost before and after to measure actual savings." |
|
|
414
|
+
| Backup is empty/missing | "Backup directory appears empty. This could mean a fresh setup or a write failure. Proceeding without backup, but will not modify any files without explicit user confirmation for each change." |
|
|
415
|
+
| File write fails mid-implementation | "Write failed for [file]. Restoring from backup. Check disk space and permissions." Present the restore command from the Restoring Backups section. |
|
|
416
|
+
| Synthesis output missing | "Synthesis agent did not produce output. Showing raw audit findings instead." Present the individual audit files from `$COORD_PATH/audit/` for user review. |
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Keep-Warm consent
|
|
2
|
+
|
|
3
|
+
Keep-Warm pings the prompt cache before it expires so a paused session resumes warm. It pays off only for API-billed Claude Code sessions, it is off by default, and it refuses on subscription auth. Ask once.
|
|
4
|
+
|
|
5
|
+
## 1. Check whether to ask
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
python3 "$MEASURE_PY" keepwarm-consent-status # JSON: {billing_mode, consent, should_ask}
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`should_ask` false: say nothing. True (API-billed, not yet asked): continue.
|
|
12
|
+
|
|
13
|
+
## 2. Compute the user's own projection
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
python3 "$MEASURE_PY" keepwarm-backfill --json --no-fence # read modes."probe-only".net_usd
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 3. Pitch
|
|
20
|
+
|
|
21
|
+
When a session pauses past its cache window and resumes, the whole prefix is written again at up to 2x input. Keep-Warm pings just before expiry (about 0.1x of the prefix, at most 2 pings per pause), and a tripwire switches it off if the pings stop paying for themselves.
|
|
22
|
+
|
|
23
|
+
- `net_usd` positive: add "a replay of your own last 30 days nets about $<net_usd> per 30 days at the conservative probe-only setting".
|
|
24
|
+
- Backfill failed, returned nothing, or `net_usd <= 0`: leave the dollar sentence out and say the saving depends on their own pause-and-resume pattern, and the dashboard shows it once pings have fired. Never supply a number of your own.
|
|
25
|
+
|
|
26
|
+
## 4. Record the answer, yes or no first
|
|
27
|
+
|
|
28
|
+
Recording the answer before anything else means an interrupted run never leaves an "asked" marker with no answer.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
python3 "$MEASURE_PY" keepwarm-enable # yes: records consent and installs the scheduler (macOS)
|
|
32
|
+
python3 "$MEASURE_PY" keepwarm-disable # no
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Both are final, so the user is not asked again. Only when the user neither accepts nor declines:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
python3 "$MEASURE_PY" keepwarm-consent-asked # marks the question as shown
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 5. Confirm it is armed (after a yes)
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
python3 "$MEASURE_PY" keepwarm-scheduler status # installed and loaded state (macOS)
|
|
45
|
+
python3 "$MEASURE_PY" keepwarm-tick --dry-run # what the next tick would decide
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
On Linux and Windows the scheduler is not shipped yet: Keep-Warm runs watchdog-only until the user wires `keepwarm-tick` to their own cron or timer. Tell them that plainly.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# OpenCode Runtime
|
|
2
|
+
|
|
3
|
+
You reached this file because the token-optimizer skill was invoked from inside
|
|
4
|
+
**OpenCode**. OpenCode loads `~/.claude/skills` by default, so it can pick up
|
|
5
|
+
this Claude Code skill even though the user is not in Claude Code.
|
|
6
|
+
|
|
7
|
+
**Do not run the Claude Code audit/fix phases.** They scan and modify `~/.claude`
|
|
8
|
+
(skills, plugins, settings, MEMORY.md). When the user is working in OpenCode,
|
|
9
|
+
mutating `~/.claude` is the wrong target.
|
|
10
|
+
|
|
11
|
+
## What to tell the user
|
|
12
|
+
|
|
13
|
+
Token Optimizer already supports OpenCode through a dedicated, native plugin —
|
|
14
|
+
not this Python audit. On OpenCode it works automatically:
|
|
15
|
+
|
|
16
|
+
- **Live tracking** of context quality, realized savings, and session continuity
|
|
17
|
+
via OpenCode session hooks (no audit run needed).
|
|
18
|
+
- **Dashboard / status**: invoke the `token_dashboard` tool (HTML dashboard) or
|
|
19
|
+
the `token_status` tool (in-chat snapshot).
|
|
20
|
+
|
|
21
|
+
Install / verify the OpenCode plugin:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
// opencode.json
|
|
25
|
+
{
|
|
26
|
+
"plugin": ["token-optimizer-opencode"]
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or drop a local build into `~/.config/opencode/plugins/` (OpenCode auto-loads it).
|
|
31
|
+
|
|
32
|
+
## Why the Python skill stops here
|
|
33
|
+
|
|
34
|
+
- The Python skill's audit is Claude Code / Codex specific: it scans
|
|
35
|
+
`~/.claude` structure (skills, plugin cache, `settings.json`, `MEMORY.md`).
|
|
36
|
+
- OpenCode stores config under `~/.config/opencode` and data under
|
|
37
|
+
`~/.local/share/opencode` (XDG), with a different layout. A Claude-shaped audit
|
|
38
|
+
would either error or, worse, mutate the wrong home.
|
|
39
|
+
- Token Optimizer's own data, when this skill does run any runtime-data command
|
|
40
|
+
under OpenCode, now resolves to OpenCode's data dir (`~/.local/share/opencode`),
|
|
41
|
+
never `~/.claude` — see `runtime_env.opencode_data_home()`.
|
|
42
|
+
|
|
43
|
+
## Escape hatch
|
|
44
|
+
|
|
45
|
+
If the user genuinely wants to audit a Claude Code setup *from* OpenCode (rare),
|
|
46
|
+
they can force the runtime:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
TOKEN_OPTIMIZER_RUNTIME=claude python3 "$MEASURE_PY" report
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This is opt-in and explicit. The default under OpenCode is to never touch
|
|
53
|
+
`~/.claude`.
|
|
54
|
+
|
|
55
|
+
## Refreshing the skill tree (OpenCode-only users)
|
|
56
|
+
|
|
57
|
+
OpenCode loads skill content from `~/.claude/skills`. If SKILL.md or reference
|
|
58
|
+
files look stale, the skill symlink can be refreshed by re-running the
|
|
59
|
+
installer. **However**, the default `install.sh` also installs Claude Code
|
|
60
|
+
hooks into `~/.claude/settings.json` (via `measure.py setup-all-hooks`).
|
|
61
|
+
|
|
62
|
+
- If you use **both** Claude Code and OpenCode on this host: `bash install.sh`
|
|
63
|
+
is safe — the hooks are needed for Claude Code anyway.
|
|
64
|
+
- If you use **OpenCode only**: the Claude hooks are unnecessary. After
|
|
65
|
+
running `install.sh`, remove them with:
|
|
66
|
+
```bash
|
|
67
|
+
python3 ~/.claude/token-optimizer/skills/token-optimizer/scripts/measure.py cleanup-duplicate-hooks
|
|
68
|
+
```
|
|
69
|
+
Or simply re-create the symlink manually:
|
|
70
|
+
```bash
|
|
71
|
+
ln -sfn ~/.claude/token-optimizer/skills/token-optimizer ~/.claude/skills/token-optimizer
|
|
72
|
+
```
|