costclaw 0.12.2 → 0.12.4

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.
Files changed (3) hide show
  1. package/README.md +327 -326
  2. package/dist/index.js +770 -270
  3. package/package.json +47 -47
package/README.md CHANGED
@@ -1,326 +1,327 @@
1
- # costclaw
2
-
3
- A local cost and setup audit for [Claude Code](https://claude.com/claude-code).
4
- Point it at your session logs and it shows where token spend leaks, scores your
5
- setup across six pillars, and ranks fixes by the dollars they recover.
6
-
7
- Your prompts never leave your machine. The audit parses the logs already on your
8
- disk and prints a report. It uploads nothing.
9
-
10
- ## Use it
11
-
12
- Requires Node 20+.
13
-
14
- ```bash
15
- npx costclaw audit
16
- ```
17
-
18
- That reads `~/.claude/projects` (override with `--path <dir>`) and prints:
19
-
20
- ```
21
- CostClaw audit
22
- Source: ~/.claude/projects (N sessions across M projects)
23
- CLAUDE.md: ~/.claude/CLAUDE.md
24
-
25
- Recoverable spend (cache-miss exposure): $XXX.XX
26
-
27
- Spend analyzed: $X,XXX.XX Cache hit: 96.3% Active hours: XXX.X
28
- Model spend: opus 71% / sonnet 24% / haiku 5%
29
-
30
- Top projects by spend:
31
- C--Projects-demo $XXX.XX 312 sessions cache 96.4% opus
32
-
33
- Burn Clock (spend by day):
34
- Monday [###########.........] $XXX.XX
35
- ...
36
- Friday [####################] $X,XXX.XX
37
-
38
- Friday is your most expensive day ($X,XXX.XX), about 2.0x an average day across
39
- 42 active days. Peak stretch: Friday morning.
40
-
41
- Overall setup score: 91 / 100 (Dialed in)
42
- CLAUDE.md quality [#################...] 85
43
- Context hygiene [###################.] 95
44
- Prompting patterns [##################..] 92
45
- Session management [####################] 100
46
- Tool and MCP config [####################] 100
47
- Cost discipline [##############......] 70
48
-
49
- Top fixes by recoverable spend:
50
- - [$XXX.XX] Recover spend lost to cache misses
51
- Your longest sessions started at 71% cache reuse and finished at 9%.
52
- Compact or start a fresh session before the context outgrows the cache.
53
- Evidence: XX% of input tokens missed the cache across N sessions.
54
- ```
55
-
56
- The cache fix names the specific reason your cache is missing, picked from what
57
- your logs actually show: sessions whose cache decayed as they ran, a `CLAUDE.md`
58
- churning with dates and TODOs, or a project that keeps starting cold. Burn Clock
59
- needs at least 7 active days of logs; below that it is left out rather than
60
- guessed at.
61
-
62
- ## On a subscription? Declare your plan
63
-
64
- Every dollar the audit prints is an API-equivalent price: what those tokens
65
- would cost at pay-as-you-go rates. On a Claude subscription (Pro or Max) your
66
- marginal token cost is zero, so those dollars are not money back - they measure
67
- your rate-limit headroom.
68
-
69
- The first interactive run asks which one you are on and remembers the answer, so
70
- usually there is nothing to do. To set it directly, or to change it later:
71
-
72
- ```bash
73
- npx costclaw audit --plan max20x # or pro, max5x; --plan api switches back
74
- ```
75
-
76
- Piped runs, `--json` and CI are never asked and assume API rates, so a scripted
77
- audit never blocks waiting for an answer.
78
-
79
- The headline becomes recoverable usage - the share of your token consumption
80
- you could win back - with the dollars demoted to API-equivalent context:
81
-
82
- ```
83
- Recoverable usage (cache-miss exposure): 14.3% of analyzed usage ($1134.61 at API rates)
84
-
85
- Usage analyzed (API-equivalent): $7921.99 Cache hit: 96.2% Active hours: 1889.7
86
- Plan: max20x. Dollar figures are API-equivalent token value, not money back;
87
- recovering them buys rate-limit headroom (more work per usage window).
88
- Usage windows (5h, like your rate limit): median $8.10, busiest $34.20; busiest week $310.55 (API-equivalent)
89
- Recovering 14.3% buys about 17% more work per window: up to ~42 extra minutes in a window like your busiest.
90
- ```
91
-
92
- The usage windows are modeled from your own activity timestamps, mirroring how
93
- the limits actually work: a 5-hour window opens at your first activity, and a
94
- weekly cap sits above it. Nothing is assumed about Anthropic's actual budgets;
95
- everything is relative to your observed consumption ("busiest" is the 90th
96
- percentile window, so one freak outlier does not set the bar).
97
-
98
- The value is saved to `~/.costclaw/config.json`, so later runs (and `optimize`)
99
- remember it. The findings and their ranking are identical either way; only the
100
- unit is framed to match how you actually pay.
101
-
102
- ## Options
103
-
104
- ```
105
- costclaw audit [--path <dir>] [--claude-md <file>] [--plan <plan>] [--json] [--html [file]] [--no-html] [--card [file]]
106
- ```
107
-
108
- - `--path <dir>` audit a specific projects directory (default `~/.claude/projects`).
109
- - `--claude-md <file>` score a specific CLAUDE.md file's quality (by default the
110
- audit scores `~/.claude/CLAUDE.md` when it exists).
111
- - `--plan <api|pro|max5x|max20x>` declare how you pay for Claude (see above);
112
- persisted for later runs.
113
- - `--json` print the raw, derived `AuditRecord` instead of the human report.
114
- - The HTML report is written and opened in your browser **by default**
115
- (`costclaw-report.html` in the current directory). The file inlines
116
- everything, so nothing is fetched and nothing is uploaded; the terminal
117
- report still prints (in color on interactive terminals; set `NO_COLOR` to
118
- disable). `--html <file>` picks the path; `--no-html` skips the file;
119
- `--json` runs stay file-free unless `--html` is passed explicitly. Set
120
- `COSTCLAW_NO_OPEN=1` to write reports without launching a browser (CI and
121
- test runners are detected and never open one). The report ends with a
122
- "What to do next" ramp: the commands to run next, how the optimize license
123
- works, and a command reference.
124
- - `--card [file]` also write a shareable score card SVG (default
125
- `costclaw-card.svg`): your score, band, recoverable spend, cache hit rate, and
126
- strongest pillar. A static file, no fonts or network calls.
127
-
128
- Run `costclaw --version` (or `-v` / `costclaw version`) to print the installed
129
- CLI version.
130
-
131
- ## Diff
132
-
133
- Every audit run saves a snapshot of the derived record to `~/.costclaw/history`
134
- (local, nothing uploaded). Run the audit again after you have made changes, then:
135
-
136
- ```bash
137
- npx costclaw diff
138
- ```
139
-
140
- That compares your latest two audits and shows the movement, so you can verify
141
- the fixes actually recovered dollars:
142
-
143
- ```
144
- CostClaw diff
145
- Baseline: 2026-06-09 Current: 2026-07-09 (30 days)
146
-
147
- Since the baseline:
148
- New spend $842.10 across 210 new sessions
149
- Marginal cache hit 97.1% (lifetime 96.2%)
150
- Recoverable exposure +$96.40 (now $1131.61)
151
-
152
- Overall setup score: 83 -> 86 (+3, Tight)
153
- CLAUDE.md quality 69 -> 78 (+9)
154
- ...
155
-
156
- Fixes resolved since the baseline:
157
- - [$52.40] Use a cheaper model for 61 small sessions
158
- ```
159
-
160
- The marginal cache hit rate covers only the period between the two audits, which
161
- a lifetime average hides. You can also diff two explicit records saved with
162
- `costclaw audit --json`: `costclaw diff baseline.json current.json`. Add `--json`
163
- for the machine-readable diff. Free, like the audit. The history keeps the newest
164
- 50 snapshots and prunes older ones automatically.
165
-
166
- ## Badge
167
-
168
- ```bash
169
- npx costclaw badge
170
- ```
171
-
172
- Runs a fresh audit and writes a shields.io-style badge SVG (default
173
- `costclaw-badge.svg`): a "costclaw" segment and your overall score, colored by
174
- its band. Drop it in a README with a plain image tag:
175
-
176
- ```markdown
177
- ![costclaw score](./costclaw-badge.svg)
178
- ```
179
-
180
- ```
181
- costclaw badge [--path <dir>] [--claude-md <file>] [--out <file>]
182
- ```
183
-
184
- ## Optimize
185
-
186
- ```bash
187
- npx costclaw optimize
188
- ```
189
-
190
- Generates, for every project your audit saw, five artifacts: an optimized
191
- `CLAUDE.md` scaffold, a prioritized fix playbook, a conservative,
192
- project-derived `settings.suggested.json` permission allowlist, a
193
- `HARNESS-UPGRADE.md` work order for your own Claude Code, and `costclaw.md`
194
- (the generated sections as a standalone include). It never writes inside a
195
- project; only `costclaw apply` does that, and only on an explicit command.
196
-
197
- It also reviews the files themselves. The dashboard shows your global
198
- `~/.claude/CLAUDE.md`, plus every file it imports with an `@path` line, and each
199
- project's `CLAUDE.md` annotated line by line: what to remove (the generated
200
- include now carries it, or the global file already says it), what to move out
201
- (dated and status text, a paragraph that names only one project), and what to
202
- add (a short, evidence-backed block, never a placeholder). Above that sits one
203
- prompt, `UPGRADE-ALL.md`, covering the global file and every project at once:
204
-
205
- ```
206
- claude "Follow ~/.costclaw/optimize/UPGRADE-ALL.md"
207
- ```
208
-
209
- It states each file's per-turn token total before and after the proposed
210
- changes, and no target line count anywhere. A project with no `CLAUDE.md` of its
211
- own now reports no "before" instead of comparing against your global file, which
212
- was never that project's text.
213
-
214
- By default it runs globally: it ranks your audited projects by spend,
215
- generates for the top ones (cap 10; override with `--limit N`) into
216
- `~/.costclaw/optimize/<project>/`, writes `UPGRADE-ALL.md` and one
217
- `DASHBOARD.html` index at the root
218
- listing every project with its receipt and apply line, plus a local
219
- click-to-apply page you can use instead of the command line (Ctrl+C, the
220
- page's Done button, or `--no-serve` to skip it). `--project <dir>` keeps the
221
- single-project behavior, writing into `./costclaw-out` (or `--out <dir>`)
222
- instead. `--no-dashboard` skips the HTML.
223
-
224
- And it tells you what it actually did to the file that rides on every turn:
225
-
226
- ```
227
- Your CLAUDE.md: an estimated 5,266 tokens to 2,479 (52.9% smaller), paid on every turn.
228
- ```
229
-
230
- costclaw never calls a model, so it does not rewrite your prose: your own sections
231
- are carried through exactly as written. When that carried prose is most of what is
232
- left, the receipt says so rather than presenting a small percentage as a win:
233
-
234
- ```
235
- About 1,714 of those tokens are your own prose, carried through as written.
236
- costclaw does not rewrite prose, so trimming it is judgment work: HARNESS-UPGRADE.md
237
- ranks your heaviest sections and flags the dated lines to move into docs/.
238
- ```
239
-
240
- `HARNESS-UPGRADE.md` is the fourth artifact: a work order you hand to your own
241
- Claude Code (`claude "Follow costclaw-out/HARNESS-UPGRADE.md"`). It carries the
242
- merge and permission tasks plus evidence-conditional ones, and when your carried
243
- prose is heavy it ranks it so the trimming is a worklist instead of a guess:
244
-
245
- ```
246
- Heaviest carried sections, largest first:
247
-
248
- - Core Philosophy: est. 1,382 tokens.
249
- - How to Work: est. 1,267 tokens, 2 dated lines.
250
- - Communication and Output: est. 550 tokens, 3 dated lines.
251
-
252
- The remaining 6 carried sections account for the other 1,880 tokens.
253
- ```
254
-
255
- Those rows plus the remainder line add up to the stated total, the same way the
256
- playbook's per-project table reconciles. costclaw sets no target line count
257
- anywhere: it measures what your sections cost and which lines carry dates, and
258
- leaves the keep-or-move judgment to you.
259
-
260
- The playbook breaks the recoverable headline down by project, so you can see
261
- which repo owes which dollars and at what cache hit rate:
262
-
263
- ```
264
- ### Where that money is
265
-
266
- | Project | Recoverable | Cache hit | Main sessions |
267
- | --- | --- | --- | --- |
268
- | some-app | $490.43 | 98% | 229 |
269
- | another-app | $312.43 | 99% | 131 |
270
-
271
- 39 smaller projects account for the remaining $172.06.
272
- ```
273
-
274
- The rows plus that remainder line always add up to the headline figure above
275
- them, so the breakdown reconciles rather than approximating.
276
-
277
- ```
278
- costclaw optimize [--path <dir>] [--claude-md <file>] [--project <dir>] [--out <dir>] [--limit <n>] [--no-dashboard] [--no-serve] [--force]
279
- ```
280
-
281
- ## Apply
282
-
283
- ```bash
284
- npx costclaw apply
285
- ```
286
-
287
- The only command that writes inside a project, and only additively. Run it
288
- from inside the project, or pass a directory:
289
-
290
- ```
291
- costclaw apply [<dir>] [--remove] [--replace] [--apply-settings [file]] [--from <dir>] [--force]
292
- ```
293
-
294
- - Default mode writes the generated sections to `.claude/costclaw.md` and adds
295
- one `@.claude/costclaw.md` import line (Claude Code's memory import syntax)
296
- to your `CLAUDE.md`. Every line you wrote stays exactly as it was. Idempotent:
297
- running it again rewrites the include and leaves the import line alone.
298
- - `--remove` undoes it: deletes `.claude/costclaw.md` and `.claude/costclaw/`,
299
- and strips the import line (only that exact generated line).
300
- - `--replace` is the old wholesale rewrite. It requires a git repository (a
301
- `.git` directory in the project), so the undo is `git checkout -- CLAUDE.md`;
302
- outside git it refuses. It still keeps a `.bak` as a second net, refusing to
303
- overwrite an existing one without `--force`.
304
- - `--apply-settings [file]` additively merges the derived permission allowlist
305
- into your Claude Code settings. Opt-in: it only touches
306
- `permissions.allow` and `permissions.deny` (a deduped union that keeps your
307
- existing entries and orders them first) and never removes or changes any other
308
- key. The default target is the project's `.claude/settings.json`; pass
309
- `[file]` to target another. It backs up the previous file to `<target>.bak`,
310
- prints a diff, then writes pretty JSON. A missing target is created; a file it
311
- cannot `JSON.parse` (for example JSONC with comments) is refused and left
312
- untouched.
313
- - `--from <dir>` points at a specific artifacts folder instead of looking one
314
- up by project. Without it, `apply` finds artifacts from the last `optimize`
315
- run, or generates them cold (into `./costclaw-out`) if none exist yet.
316
- - `--force` allows overwriting an existing `.bak` backup for `--replace` and
317
- `--apply-settings`.
318
-
319
- `optimize` and `apply` need a one-time license (`costclaw activate <key>`);
320
- `audit` is free.
321
-
322
- ## Privacy
323
-
324
- The only thing the tool produces is a derived `AuditRecord`: totals and generated
325
- prose, no prompt text, no file paths, no secrets. A hosted dashboard is on the
326
- roadmap and would receive only that derived record, never your logs.
1
+ # costclaw
2
+
3
+ A local cost and setup audit for [Claude Code](https://claude.com/claude-code).
4
+ Point it at your session logs and it shows where token spend leaks, scores your
5
+ setup across six pillars, and ranks counterfactual opportunities by estimated
6
+ token value.
7
+
8
+ Your prompts are analyzed locally and never uploaded. The audit parses the logs
9
+ already on your disk and prints a report.
10
+
11
+ ## Use it
12
+
13
+ Requires Node 20+.
14
+
15
+ ```bash
16
+ npx costclaw audit
17
+ ```
18
+
19
+ That reads `~/.claude/projects` (override with `--path <dir>`) and prints a
20
+ local report. This concise example is synthetic, generated from synthetic inputs:
21
+
22
+ ```
23
+ CostClaw audit
24
+ Source: synthetic example (1 session across 1 project)
25
+ CLAUDE.md: synthetic input
26
+
27
+ Cache-miss exposure (estimate): $90.01
28
+
29
+ Estimated current API-equivalent valuation: $100.01 Cache hit: 0.0% Session hours (timestamp union): 1.0
30
+ Model spend: opus 0.01% / haiku 99.99%
31
+
32
+ Rule-based setup score: 74 / 100 (Solid)
33
+ CLAUDE.md quality 72
34
+ Context hygiene 50
35
+ Prompting patterns 75
36
+ Session management 100
37
+ Tool and MCP config 88
38
+ Cost discipline 67
39
+
40
+ Top opportunities by estimated exposure:
41
+ - [$90.01] Reduce cache-miss exposure
42
+ Synthetic cache-read counterfactual; cold and unique inputs may not be cacheable.
43
+ ```
44
+
45
+ The audit applies rules to observed cache and setup patterns. Its suggested causes
46
+ and actions are hypotheses to inspect, not diagnoses or proof that a change will save
47
+ money. Burn Clock needs at least 7 active days of logs; below that it is left out.
48
+
49
+ ## On a subscription? Declare your plan
50
+
51
+ Every dollar the audit prints is an API-equivalent price: what those tokens
52
+ would cost at pay-as-you-go rates. On a Claude subscription (Pro or Max) your
53
+ usage within an included allowance may have no marginal token charge. Extra Usage
54
+ can be billed, so API-equivalent figures are neither money back nor a statement of
55
+ your actual subscription charges.
56
+
57
+ The first interactive run asks which one you are on and remembers the answer, so
58
+ usually there is nothing to do. To set it directly, or to change it later:
59
+
60
+ ```bash
61
+ npx costclaw audit --plan max20x # or pro, max5x; --plan api switches back
62
+ ```
63
+
64
+ Piped runs, `--json` and CI are never asked and assume API rates, so a scripted
65
+ audit never blocks waiting for an answer.
66
+
67
+ The headline becomes cache-miss exposure - a counterfactual comparison of logged
68
+ token use with a cache-read scenario - with dollars as API-equivalent context. The
69
+ following uses the same synthetic example:
70
+
71
+ ```
72
+ Cache-miss exposure (counterfactual): 90.0% of analyzed usage ($90.01 at API rates)
73
+
74
+ Estimated current API-equivalent valuation: $100.01 Cache hit: 0.0% Session hours (timestamp union): 1.0
75
+ Plan: max20x. This synthetic example does not measure quota, billing, or promise
76
+ more work. Activity windows are omitted when there is insufficient history.
77
+ ```
78
+
79
+ Usage windows are fixed activity bins based on local timestamps. They summarize
80
+ observed activity only: CostClaw does not know Anthropic's actual quota, rate-limit
81
+ windows, subscription inclusion, or billing adjustments.
82
+
83
+ The value is saved to `~/.costclaw/config.json`, so later runs (and `optimize`)
84
+ remember it. The findings and their ranking are identical either way; only the
85
+ unit is framed to match how you actually pay.
86
+
87
+ ## Options
88
+
89
+ ```
90
+ costclaw audit [--path <dir>] [--claude-md <file>] [--plan <plan>] [--json] [--html [file]] [--no-html] [--card [file]]
91
+ ```
92
+
93
+ - `--path <dir>` audit a specific projects directory (default `~/.claude/projects`).
94
+ - `--claude-md <file>` score a specific CLAUDE.md file's quality (by default the
95
+ audit scores `~/.claude/CLAUDE.md` when it exists).
96
+ - `--plan <api|pro|max5x|max20x>` declare how you pay for Claude (see above);
97
+ persisted for later runs.
98
+ - `--json` print the raw, derived `AuditRecord` instead of the human report.
99
+ - The HTML report is written and opened in your browser **by default**
100
+ (`costclaw-report.html` in the current directory). The file inlines
101
+ everything, so nothing is fetched and nothing is uploaded; the terminal
102
+ report still prints (in color on interactive terminals; set `NO_COLOR` to
103
+ disable). `--html <file>` picks the path; `--no-html` skips the file;
104
+ `--json` runs stay file-free unless `--html` is passed explicitly. Set
105
+ `COSTCLAW_NO_OPEN=1` to write reports without launching a browser (CI and
106
+ test runners are detected and never open one). The report ends with a
107
+ "What to do next" ramp: the commands to run next, how the optimize license
108
+ works, and a command reference.
109
+ - `--card [file]` also write a shareable score card SVG (default
110
+ `costclaw-card.svg`): your score, band, cache-miss exposure estimate, cache hit rate, and
111
+ strongest pillar. A static file, no fonts or network calls.
112
+
113
+ Run `costclaw --version` (or `-v` / `costclaw version`) to print the installed
114
+ CLI version.
115
+
116
+ ## Accuracy boundary
117
+
118
+ The report estimates current API-equivalent list-price value from logged usage and a
119
+ dated rate table. It is not billing proof. Unknown model IDs, unavailable pricing
120
+ modifiers, and incomplete logs add uncertainty. Repeated cumulative request updates
121
+ are counted once using the highest coherent observed total; logged cache TTL splits are priced
122
+ when available. A top-level/split conflict retains the valid top-level count at the
123
+ conservative one-hour rate with a warning; legacy writes without a split use five-minute
124
+ pricing with a warning. Cache-miss exposure is a counterfactual, and the setup score uses
125
+ rules rather than a provider quality grade. The release retests these boundaries with
126
+ `npm test --workspace @costclaw/engine -- accuracy-regression` and
127
+ `npm test --workspace costclaw -- accuracy-report`.
128
+
129
+ ## Diff
130
+
131
+ Every audit run saves a snapshot of the derived record to `~/.costclaw/history`
132
+ (local, nothing uploaded). Run the audit again after you have made changes, then:
133
+
134
+ ```bash
135
+ npx costclaw diff
136
+ ```
137
+
138
+ That compares your latest two audits and shows movement in the estimates. It does not
139
+ verify money recovered. The numeric example below is illustrative:
140
+
141
+ ```
142
+ CostClaw diff
143
+ Baseline: 2026-06-09 Current: 2026-07-09 (30 days)
144
+
145
+ Since the baseline:
146
+ New spend $842.10 across 210 new sessions
147
+ Marginal cache hit 97.1% (lifetime 96.2%)
148
+ Cache-miss exposure (estimate) +$96.40 (now $1131.61)
149
+
150
+ Rule-based setup score: 83 -> 86 (+3, Tight)
151
+ CLAUDE.md quality 69 -> 78 (+9)
152
+ ...
153
+
154
+ Fixes resolved since the baseline:
155
+ - [$52.40] Review model fit for 61 small sessions
156
+ ```
157
+
158
+ The marginal cache hit rate covers only the period between the two audits, which
159
+ a lifetime average hides. You can also diff two explicit records saved with
160
+ `costclaw audit --json`: `costclaw diff baseline.json current.json`. Add `--json`
161
+ for the machine-readable diff. Free, like the audit. The history keeps the newest
162
+ 50 snapshots and prunes older ones automatically.
163
+
164
+ ## Badge
165
+
166
+ ```bash
167
+ npx costclaw badge
168
+ ```
169
+
170
+ Runs a fresh audit and writes a shields.io-style badge SVG (default
171
+ `costclaw-badge.svg`): a "costclaw" segment and your overall score, colored by
172
+ its band. Drop it in a README with a plain image tag:
173
+
174
+ ```markdown
175
+ ![costclaw score](./costclaw-badge.svg)
176
+ ```
177
+
178
+ ```
179
+ costclaw badge [--path <dir>] [--claude-md <file>] [--out <file>]
180
+ ```
181
+
182
+ ## Optimize
183
+
184
+ The numeric examples in this section are illustrative placeholders, not output from
185
+ an account or a promise of savings.
186
+
187
+ ```bash
188
+ npx costclaw optimize
189
+ ```
190
+
191
+ Generates, for every project your audit saw, five artifacts: an optimized
192
+ `CLAUDE.md` scaffold, a prioritized fix playbook, a conservative,
193
+ project-derived `settings.suggested.json` permission allowlist, a
194
+ `HARNESS-UPGRADE.md` work order for your own Claude Code, and `costclaw.md`
195
+ (the generated sections as a standalone include). It never writes inside a
196
+ project; only `costclaw apply` does that, and only on an explicit command.
197
+
198
+ It also reviews the files themselves. The dashboard shows your global
199
+ `~/.claude/CLAUDE.md`, plus every file it imports with an `@path` line, and each
200
+ project's `CLAUDE.md` annotated line by line: what to remove (the generated
201
+ include now carries it, or the global file already says it), what to move out
202
+ (dated and status text, a paragraph that names only one project), and what to
203
+ add (a short, evidence-backed block, never a placeholder). Above that sits one
204
+ prompt, `UPGRADE-ALL.md`, covering the global file and every project at once:
205
+
206
+ ```
207
+ claude "Follow ~/.costclaw/optimize/UPGRADE-ALL.md"
208
+ ```
209
+
210
+ It states each file's per-turn token total before and after the proposed
211
+ changes, and no target line count anywhere. A project with no `CLAUDE.md` of its
212
+ own now reports no "before" instead of comparing against your global file, which
213
+ was never that project's text.
214
+
215
+ By default it runs globally: it ranks your audited projects by spend,
216
+ generates for the top ones (cap 10; override with `--limit N`) into
217
+ `~/.costclaw/optimize/<project>/`, writes `UPGRADE-ALL.md` and one
218
+ `DASHBOARD.html` index at the root
219
+ listing every project with its receipt and apply line, plus a local
220
+ click-to-apply page you can use instead of the command line (Ctrl+C, the
221
+ page's Done button, or `--no-serve` to skip it). `--project <dir>` keeps the
222
+ single-project behavior, writing into `./costclaw-out` (or `--out <dir>`)
223
+ instead. `--no-dashboard` skips the HTML.
224
+
225
+ And it tells you what it actually did to the file that rides on every turn:
226
+
227
+ ```
228
+ Your CLAUDE.md: an estimated 5,266 tokens to 2,479 (52.9% smaller), paid on every turn.
229
+ ```
230
+
231
+ costclaw never calls a model, so it does not rewrite your prose: your own sections
232
+ are carried through exactly as written. When that carried prose is most of what is
233
+ left, the receipt says so rather than presenting a small percentage as a win:
234
+
235
+ ```
236
+ About 1,714 of those tokens are your own prose, carried through as written.
237
+ costclaw does not rewrite prose, so trimming it is judgment work: HARNESS-UPGRADE.md
238
+ ranks your heaviest sections and flags the dated lines to move into docs/.
239
+ ```
240
+
241
+ `HARNESS-UPGRADE.md` is the fourth artifact: a work order you hand to your own
242
+ Claude Code (`claude "Follow costclaw-out/HARNESS-UPGRADE.md"`). It carries the
243
+ merge and permission tasks plus evidence-conditional ones, and when your carried
244
+ prose is heavy it ranks it so the trimming is a worklist instead of a guess:
245
+
246
+ ```
247
+ Heaviest carried sections, largest first:
248
+
249
+ - Core Philosophy: est. 1,382 tokens.
250
+ - How to Work: est. 1,267 tokens, 2 dated lines.
251
+ - Communication and Output: est. 550 tokens, 3 dated lines.
252
+
253
+ The remaining 6 carried sections account for the other 1,880 tokens.
254
+ ```
255
+
256
+ Those rows plus the remainder line add up to the stated total, the same way the
257
+ playbook's per-project table reconciles. costclaw sets no target line count
258
+ anywhere: it measures what your sections cost and which lines carry dates, and
259
+ leaves the keep-or-move judgment to you.
260
+
261
+ The playbook breaks the cache-miss exposure estimate down by project, so you can see
262
+ which repository has modeled exposure and at what cache hit rate:
263
+
264
+ ```
265
+ ### Where that money is
266
+
267
+ | Project | Cache-miss exposure (estimate) | Cache hit | Main sessions |
268
+ | --- | --- | --- | --- |
269
+ | some-app | $490.43 | 98% | 229 |
270
+ | another-app | $312.43 | 99% | 131 |
271
+
272
+ 39 smaller projects account for the remaining $172.06.
273
+ ```
274
+
275
+ The rows plus that remainder line always add up to the headline figure above
276
+ them, so the breakdown reconciles rather than approximating.
277
+
278
+ ```
279
+ costclaw optimize [--path <dir>] [--claude-md <file>] [--project <dir>] [--out <dir>] [--limit <n>] [--no-dashboard] [--no-serve] [--force]
280
+ ```
281
+
282
+ ## Apply
283
+
284
+ ```bash
285
+ npx costclaw apply
286
+ ```
287
+
288
+ The only command that writes inside a project, and only additively. Run it
289
+ from inside the project, or pass a directory:
290
+
291
+ ```
292
+ costclaw apply [<dir>] [--remove] [--replace] [--apply-settings [file]] [--from <dir>] [--force]
293
+ ```
294
+
295
+ - Default mode writes the generated sections to `.claude/costclaw.md` and adds
296
+ one `@.claude/costclaw.md` import line (Claude Code's memory import syntax)
297
+ to your `CLAUDE.md`. Every line you wrote stays exactly as it was. Idempotent:
298
+ running it again rewrites the include and leaves the import line alone.
299
+ - `--remove` undoes it: deletes `.claude/costclaw.md` and `.claude/costclaw/`,
300
+ and strips the import line (only that exact generated line).
301
+ - `--replace` is the old wholesale rewrite. It requires a git repository (a
302
+ `.git` directory in the project), so the undo is `git checkout -- CLAUDE.md`;
303
+ outside git it refuses. It still keeps a `.bak` as a second net, refusing to
304
+ overwrite an existing one without `--force`.
305
+ - `--apply-settings [file]` additively merges the derived permission allowlist
306
+ into your Claude Code settings. Opt-in: it only touches
307
+ `permissions.allow` and `permissions.deny` (a deduped union that keeps your
308
+ existing entries and orders them first) and never removes or changes any other
309
+ key. The default target is the project's `.claude/settings.json`; pass
310
+ `[file]` to target another. It backs up the previous file to `<target>.bak`,
311
+ prints a diff, then writes pretty JSON. A missing target is created; a file it
312
+ cannot `JSON.parse` (for example JSONC with comments) is refused and left
313
+ untouched.
314
+ - `--from <dir>` points at a specific artifacts folder instead of looking one
315
+ up by project. Without it, `apply` finds artifacts from the last `optimize`
316
+ run, or generates them cold (into `./costclaw-out`) if none exist yet.
317
+ - `--force` allows overwriting an existing `.bak` backup for `--replace` and
318
+ `--apply-settings`.
319
+
320
+ `optimize` and `apply` need a one-time license (`costclaw activate <key>`);
321
+ `audit` is free.
322
+
323
+ ## Privacy
324
+
325
+ The audit saves a derived `AuditRecord`: totals and generated prose, with no raw
326
+ prompt text, file paths, or secrets. A hosted dashboard is on the roadmap and would
327
+ receive only that derived record, never your logs.