kerf-cli 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -169,8 +169,8 @@ echo 'alias kerf="npx kerf-cli"' >> ~/.zshrc
169
169
  Contributions welcome! Please open an issue first to discuss what you'd like to change.
170
170
 
171
171
  ```bash
172
- git clone https://github.com/dhanushkumarsivaji/kerf.git
173
- cd kerf
172
+ git clone https://github.com/dhanushkumarsivaji/kerf-cli.git
173
+ cd kerf-cli
174
174
  npm install
175
175
  npm test
176
176
  ```
package/USAGE.md ADDED
@@ -0,0 +1,644 @@
1
+ # kerf-cli User Guide
2
+
3
+ A complete guide to using kerf-cli for Claude Code cost intelligence.
4
+
5
+ ---
6
+
7
+ ## Table of Contents
8
+
9
+ - [Installation](#installation)
10
+ - [Getting Started](#getting-started)
11
+ - [Monitoring a Live Session](#monitoring-a-live-session)
12
+ - [Estimating Costs Before You Start](#estimating-costs-before-you-start)
13
+ - [Setting Up Budgets](#setting-up-budgets)
14
+ - [Auditing Your Setup](#auditing-your-setup)
15
+ - [Viewing Historical Reports](#viewing-historical-reports)
16
+ - [Using With an Active Claude Code Session](#using-with-an-active-claude-code-session)
17
+ - [Automating With Hooks](#automating-with-hooks)
18
+ - [Tips & Best Practices](#tips--best-practices)
19
+ - [Troubleshooting](#troubleshooting)
20
+
21
+ ---
22
+
23
+ ## Installation
24
+
25
+ ### Quick (no install needed)
26
+
27
+ ```bash
28
+ npx kerf-cli@latest --help
29
+ ```
30
+
31
+ ### Global install (recommended for frequent use)
32
+
33
+ ```bash
34
+ npm install -g kerf-cli
35
+ kerf-cli --help
36
+ ```
37
+
38
+ ### Shell alias (convenience)
39
+
40
+ Add this to your `~/.zshrc` or `~/.bashrc`:
41
+
42
+ ```bash
43
+ alias kerf="npx kerf-cli@latest"
44
+ ```
45
+
46
+ Then use `kerf watch`, `kerf audit`, etc.
47
+
48
+ ---
49
+
50
+ ## Getting Started
51
+
52
+ ### First-time setup
53
+
54
+ Run `init` to create the database and optionally install hooks:
55
+
56
+ ```bash
57
+ npx kerf-cli init
58
+ ```
59
+
60
+ This will:
61
+ 1. Create `~/.kerf/` directory for data storage
62
+ 2. Initialize the SQLite database at `~/.kerf/kerf.db`
63
+ 3. Detect compatible tools (RTK, ccusage)
64
+ 4. Optionally install hooks for automatic tracking
65
+
66
+ ### Verify it works
67
+
68
+ ```bash
69
+ npx kerf-cli audit
70
+ ```
71
+
72
+ You should see a context window health report showing your ghost token overhead.
73
+
74
+ ---
75
+
76
+ ## Monitoring a Live Session
77
+
78
+ ### The scenario
79
+
80
+ You have Claude Code running in one terminal tab. You want to see how much it's costing you in real time.
81
+
82
+ ### How to do it
83
+
84
+ **Step 1:** Open a second terminal tab (Cmd+T / Ctrl+Shift+T).
85
+
86
+ **Step 2:** Run the watch dashboard:
87
+
88
+ ```bash
89
+ npx kerf-cli watch
90
+ ```
91
+
92
+ kerf-cli automatically finds the most recently active Claude Code session and starts monitoring it.
93
+
94
+ ### What you'll see
95
+
96
+ ```
97
+ ┌──────────────────────────────────────────────────────────────┐
98
+ │ kerf-cli watch | session: a3f8c2d1... | 12 messages │
99
+ ├──────────────────────────────────────────────────────────────┤
100
+ │ >> $2.34 / ~$15.00 window | $0.12/min | ~1h 45m remaining │
101
+ │ [████████░░░░░░░░░░░░░░░░░░░░░░] 28% | 56K / 200K tokens │
102
+ │ system(14K) + tools(15K) + mcp(0K) + claude.md(0K) │
103
+ │ │
104
+ │ Recent Messages: │
105
+ │ 10:45:02 3.2K tok $0.12 │
106
+ │ 10:47:18 5.1K tok $0.24 │
107
+ │ 10:49:55 2.8K tok $0.09 │
108
+ └──────────────────────────────────────────────────────────────┘
109
+ ```
110
+
111
+ - **Top bar:** Current spend vs projected window cost, burn rate, time remaining
112
+ - **Context bar:** How much of the 200K context window is used
113
+ - **Ghost token breakdown:** Where your overhead goes (system prompt, tools, MCP, CLAUDE.md)
114
+ - **Recent messages:** Token count and cost per message
115
+
116
+ ### Keyboard shortcuts
117
+
118
+ | Key | Action |
119
+ |-----|--------|
120
+ | `q` | Quit the dashboard |
121
+ | `b` | Toggle budget view |
122
+
123
+ ### Options
124
+
125
+ ```bash
126
+ # Watch a specific session
127
+ npx kerf-cli watch --session abc123
128
+
129
+ # Watch sessions for a specific project
130
+ npx kerf-cli watch --project /path/to/project
131
+
132
+ # Change refresh interval (default: 2000ms)
133
+ npx kerf-cli watch --interval 5000
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Estimating Costs Before You Start
139
+
140
+ ### The scenario
141
+
142
+ You're about to ask Claude Code to refactor a module. You want to know roughly how much it'll cost before you start.
143
+
144
+ ### How to do it
145
+
146
+ ```bash
147
+ npx kerf-cli estimate 'refactor the auth module'
148
+ ```
149
+
150
+ ### What you'll see
151
+
152
+ ```
153
+ ╭──────────────────────────────────────────────────────────╮
154
+ │ kerf-cli estimate: 'refactor the auth module' │
155
+ │ │
156
+ │ Model: sonnet │
157
+ │ Estimated turns: 15-40 (expected: 25) │
158
+ │ Files: 12.4K tokens │
159
+ │ Context overhead: 62.7K tokens (ghost tokens) │
160
+ │ │
161
+ │ Estimated Cost: │
162
+ │ Low: $2.10 │
163
+ │ Expected: $5.80 │
164
+ │ High: $12.40 │
165
+ │ │
166
+ │ Window Usage: ~15% of 5-hour window │
167
+ │ -> Using Opus would cost ~$29.00 (5x more) │
168
+ ╰──────────────────────────────────────────────────────────╯
169
+ ```
170
+
171
+ ### How it works
172
+
173
+ kerf-cli classifies your task by complexity:
174
+
175
+ | Complexity | Keywords | Estimated Turns |
176
+ |-----------|----------|-----------------|
177
+ | Simple | typo, rename, delete, update version | 2-5 turns |
178
+ | Medium | fix, add, update, change | 5-15 turns |
179
+ | Complex | refactor, rewrite, build, implement, migrate | 15-40 turns |
180
+
181
+ It then calculates cost based on:
182
+ - Context overhead (ghost tokens)
183
+ - File sizes that will be touched
184
+ - Conversation growth per turn
185
+ - Cache hit rate (90% after turn 2)
186
+ - Model pricing
187
+
188
+ ### Options
189
+
190
+ ```bash
191
+ # Estimate for a specific model
192
+ npx kerf-cli estimate 'add rate limiting' --model opus
193
+
194
+ # Specify which files will be touched
195
+ npx kerf-cli estimate 'add rate limiting' --files 'src/auth/*.ts'
196
+
197
+ # Compare costs across all models
198
+ npx kerf-cli estimate 'add rate limiting' --compare
199
+
200
+ # Get JSON output (for scripting)
201
+ npx kerf-cli estimate 'add rate limiting' --json
202
+ ```
203
+
204
+ ### Example: compare models
205
+
206
+ ```bash
207
+ npx kerf-cli estimate --compare 'build a new dashboard'
208
+ ```
209
+
210
+ This shows side-by-side estimates for Sonnet, Opus, and Haiku so you can pick the right model for the job.
211
+
212
+ ---
213
+
214
+ ## Setting Up Budgets
215
+
216
+ ### The scenario
217
+
218
+ You want to limit spending to $50/week on a project so you don't blow through your Claude Code plan.
219
+
220
+ ### Set a budget
221
+
222
+ ```bash
223
+ # Set $50/week budget for current project
224
+ npx kerf-cli budget set 50 --period weekly
225
+
226
+ # Set $10/day budget
227
+ npx kerf-cli budget set 10 --period daily
228
+
229
+ # Set $200/month budget
230
+ npx kerf-cli budget set 200 --period monthly
231
+
232
+ # Set budget for a specific project
233
+ npx kerf-cli budget set 50 --period weekly --project /path/to/project
234
+ ```
235
+
236
+ ### Check your budget
237
+
238
+ ```bash
239
+ npx kerf-cli budget show
240
+ ```
241
+
242
+ ```
243
+ kerf-cli budget
244
+
245
+ Period: weekly (2026-03-31 to 2026-04-06)
246
+ Budget: $50.00
247
+ Spent: $42.30
248
+ [████████████████░░░░] 84.6%
249
+ ```
250
+
251
+ ### List all project budgets
252
+
253
+ ```bash
254
+ npx kerf-cli budget list
255
+ ```
256
+
257
+ ### Remove a budget
258
+
259
+ ```bash
260
+ npx kerf-cli budget remove
261
+ ```
262
+
263
+ ### How budget enforcement works
264
+
265
+ If you've run `kerf-cli init` and installed hooks:
266
+ - At **80% budget used:** You get a warning message in Claude Code
267
+ - At **100% budget used:** You get an over-budget alert
268
+ - Budget enforcement runs automatically via the Stop hook
269
+
270
+ ---
271
+
272
+ ## Auditing Your Setup
273
+
274
+ ### The scenario
275
+
276
+ Claude Code seems to be using a lot of tokens and you're not sure why. You want to find hidden overhead.
277
+
278
+ ### Run a full audit
279
+
280
+ ```bash
281
+ npx kerf-cli audit
282
+ ```
283
+
284
+ ### What you'll see
285
+
286
+ ```
287
+ kerf-cli audit report
288
+
289
+ Context Window Health: B (62% usable)
290
+
291
+ Ghost Token Breakdown:
292
+ System prompt: 14,328 tokens (7.2%)
293
+ Built-in tools: 15,000 tokens (7.5%)
294
+ MCP tools (3 srv): 8,400 tokens (4.2%)
295
+ CLAUDE.md: 2,100 tokens (1.1%)
296
+ Autocompact buffer: 33,000 tokens (16.5%)
297
+ ----------------------------------------
298
+ Total overhead: 72,828 tokens (36.4%)
299
+ Effective window: 127,172 tokens (63.6%)
300
+
301
+ CLAUDE.md Analysis:
302
+ Lines: 245 (over 200 limit)
303
+ Tokens: 2,100
304
+ Critical rules in dead zone: 3
305
+
306
+ Recommendations:
307
+ 1. [HIGH] Reorder CLAUDE.md — 3 critical rules in dead zone
308
+ Impact: improved rule adherence
309
+ 2. [HIGH] CLAUDE.md is 245 lines (limit: 200). Trim or move to skills.
310
+ Impact: 45 lines over limit
311
+ 3. [MED] MCP server 'playwright' has 12 tools. Consider disabling.
312
+ Impact: -7,200 tokens/session
313
+ ```
314
+
315
+ ### Understanding the grades
316
+
317
+ | Grade | Usable Window | Meaning |
318
+ |-------|--------------|---------|
319
+ | A | >70% | Healthy — minimal overhead |
320
+ | B | 50-70% | Good — some optimization possible |
321
+ | C | 30-50% | Concerning — significant overhead |
322
+ | D | <30% | Critical — most of your window is ghost tokens |
323
+
324
+ ### Understanding ghost tokens
325
+
326
+ Ghost tokens are context window space consumed before your conversation even starts:
327
+
328
+ - **System prompt (14,328):** Claude Code's built-in instructions. Fixed, can't change.
329
+ - **Built-in tools (15,000):** Read, Write, Edit, Bash, etc. Fixed, can't change.
330
+ - **MCP tools (varies):** Each MCP tool costs ~600 tokens. More servers = more overhead.
331
+ - **CLAUDE.md (varies):** Your project instructions. Loaded every session.
332
+ - **Autocompact buffer (33,000):** Reserved space for context compression. Fixed.
333
+
334
+ ### Understanding the CLAUDE.md attention curve
335
+
336
+ Claude's attention follows a U-shaped curve:
337
+ - **Position 0-30%:** HIGH attention (top of file) — put critical rules here
338
+ - **Position 30-70%:** LOW attention (middle) — the "dead zone"
339
+ - **Position 70-100%:** HIGH attention (bottom of file) — good for critical rules
340
+
341
+ kerf-cli flags critical rules (NEVER, ALWAYS, MUST, CRITICAL) that are stuck in the dead zone.
342
+
343
+ ### Options
344
+
345
+ ```bash
346
+ # Only audit CLAUDE.md
347
+ npx kerf-cli audit --claude-md-only
348
+
349
+ # Only audit MCP servers
350
+ npx kerf-cli audit --mcp-only
351
+
352
+ # JSON output
353
+ npx kerf-cli audit --json
354
+ ```
355
+
356
+ ---
357
+
358
+ ## Viewing Historical Reports
359
+
360
+ ### The scenario
361
+
362
+ You want to see how much you've spent today, this week, or on a specific project.
363
+
364
+ ### Today's report
365
+
366
+ ```bash
367
+ npx kerf-cli report
368
+ ```
369
+
370
+ ```
371
+ kerf-cli report -- Sat, Apr 4, 2026
372
+
373
+ Total Cost: $8.42
374
+ Total Tokens: 1.2M in / 8.2K out
375
+ Cache Hit Rate: 94.2%
376
+ Sessions: 4
377
+ ```
378
+
379
+ ### Weekly report
380
+
381
+ ```bash
382
+ npx kerf-cli report --period week
383
+ ```
384
+
385
+ ### Monthly report
386
+
387
+ ```bash
388
+ npx kerf-cli report --period month
389
+ ```
390
+
391
+ ### All time
392
+
393
+ ```bash
394
+ npx kerf-cli report --period all
395
+ ```
396
+
397
+ ### Detailed breakdowns
398
+
399
+ ```bash
400
+ # Per-model breakdown
401
+ npx kerf-cli report --model
402
+
403
+ # Per-session breakdown
404
+ npx kerf-cli report --sessions
405
+
406
+ # Both
407
+ npx kerf-cli report --model --sessions
408
+ ```
409
+
410
+ ### Export data
411
+
412
+ ```bash
413
+ # CSV (for spreadsheets)
414
+ npx kerf-cli report --period week --csv > costs.csv
415
+
416
+ # JSON (for scripts)
417
+ npx kerf-cli report --period week --json
418
+ ```
419
+
420
+ ---
421
+
422
+ ## Using With an Active Claude Code Session
423
+
424
+ This is the most common workflow. Here's exactly how to use kerf-cli alongside Claude Code.
425
+
426
+ ### Workflow 1: Quick check before starting
427
+
428
+ ```bash
429
+ # 1. Before asking Claude to do something expensive:
430
+ npx kerf-cli estimate 'refactor the database layer'
431
+
432
+ # 2. Check your remaining budget:
433
+ npx kerf-cli budget show
434
+
435
+ # 3. If it looks good, start Claude Code and work
436
+ ```
437
+
438
+ ### Workflow 2: Live monitoring
439
+
440
+ ```
441
+ Terminal Tab 1 (Claude Code): Terminal Tab 2 (kerf-cli):
442
+ ┌──────────────────────────┐ ┌──────────────────────────┐
443
+ │ $ claude │ │ $ npx kerf-cli watch │
444
+ │ │ │ │
445
+ │ > Fix the auth bug in │ │ >> $1.20 / ~$8.00 window │
446
+ │ src/auth/login.ts │ │ [████░░░░░░] 18% │
447
+ │ │ │ │
448
+ │ I'll fix the auth bug... │ │ 10:30:02 2.1K $0.08 │
449
+ │ │ │ 10:30:45 3.4K $0.15 │
450
+ └──────────────────────────┘ └──────────────────────────┘
451
+ ```
452
+
453
+ 1. Open Terminal Tab 1 — start Claude Code as normal
454
+ 2. Open Terminal Tab 2 — run `npx kerf-cli watch`
455
+ 3. kerf-cli auto-detects the active session and shows live costs
456
+ 4. Press `q` to quit the dashboard when done
457
+
458
+ ### Workflow 3: End-of-day review
459
+
460
+ ```bash
461
+ # See what you spent today
462
+ npx kerf-cli report
463
+
464
+ # See per-session costs
465
+ npx kerf-cli report --sessions
466
+
467
+ # Check if you're on track with your budget
468
+ npx kerf-cli budget show
469
+ ```
470
+
471
+ ### Workflow 4: Optimize your setup
472
+
473
+ ```bash
474
+ # Run audit to find waste
475
+ npx kerf-cli audit
476
+
477
+ # Common findings:
478
+ # - CLAUDE.md is too long → trim it
479
+ # - MCP servers you don't use → disable them
480
+ # - Critical rules in dead zone → reorder CLAUDE.md
481
+ ```
482
+
483
+ ---
484
+
485
+ ## Automating With Hooks
486
+
487
+ ### What are hooks?
488
+
489
+ Claude Code hooks are shell scripts that run automatically at specific events. kerf-cli uses two hooks:
490
+
491
+ 1. **Notification hook:** Logs token usage to `~/.kerf/session-log.jsonl` after each message
492
+ 2. **Stop hook:** Checks your budget and warns you when you're approaching the limit
493
+
494
+ ### Installing hooks
495
+
496
+ ```bash
497
+ # Install hooks for current project
498
+ npx kerf-cli init
499
+
500
+ # Install hooks globally (applies to all projects)
501
+ npx kerf-cli init --global
502
+ ```
503
+
504
+ ### What the hooks do
505
+
506
+ **Notification hook** (runs after each Claude response):
507
+ - Records timestamp, session ID, and token metrics
508
+ - Writes to `~/.kerf/session-log.jsonl`
509
+ - Zero impact on Claude Code performance
510
+
511
+ **Stop hook** (runs when Claude finishes a response):
512
+ - Checks your budget via `kerf-cli budget show --json`
513
+ - At 80%: Adds a warning to Claude's context
514
+ - At 100%: Alerts that budget is exceeded
515
+ - Does NOT block Claude Code by default (configurable)
516
+
517
+ ### Skipping hooks during setup
518
+
519
+ ```bash
520
+ # Set up database only, no hooks
521
+ npx kerf-cli init --no-hooks
522
+ ```
523
+
524
+ ---
525
+
526
+ ## Tips & Best Practices
527
+
528
+ ### 1. Use Sonnet for implementation, Opus for planning
529
+
530
+ ```bash
531
+ # Check the price difference before switching models
532
+ npx kerf-cli estimate --compare 'your task here'
533
+ ```
534
+
535
+ Opus typically costs 5x more than Sonnet. Use it only when you need the extra reasoning.
536
+
537
+ ### 2. Keep CLAUDE.md under 200 lines
538
+
539
+ ```bash
540
+ # Check your CLAUDE.md health
541
+ npx kerf-cli audit --claude-md-only
542
+ ```
543
+
544
+ Move detailed instructions to Claude Code skills instead of bloating CLAUDE.md.
545
+
546
+ ### 3. Disable MCP servers you don't use
547
+
548
+ Each MCP tool costs ~600 tokens of context. A server with 10 tools = 6,000 tokens of overhead every message.
549
+
550
+ ```bash
551
+ # See which MCP servers are costing you
552
+ npx kerf-cli audit --mcp-only
553
+ ```
554
+
555
+ ### 4. Monitor cache hit rate
556
+
557
+ High cache hit rates (>80%) mean Claude is efficiently reusing context. Low rates mean you're paying full price for repeated context.
558
+
559
+ ```bash
560
+ npx kerf-cli report --period today
561
+ ```
562
+
563
+ ### 5. Set weekly budgets
564
+
565
+ Weekly budgets give you enough flexibility for busy and quiet days while preventing runaway costs.
566
+
567
+ ```bash
568
+ npx kerf-cli budget set 50 --period weekly
569
+ ```
570
+
571
+ ### 6. Review costs regularly
572
+
573
+ Make it a habit to check your daily report:
574
+
575
+ ```bash
576
+ npx kerf-cli report
577
+ ```
578
+
579
+ ---
580
+
581
+ ## Troubleshooting
582
+
583
+ ### "No active Claude Code session found"
584
+
585
+ kerf-cli looks for JSONL files in `~/.claude/projects/` modified in the last 5 hours.
586
+
587
+ **Fixes:**
588
+ - Make sure Claude Code is running and has sent at least one message
589
+ - Check that `~/.claude/projects/` exists: `ls ~/.claude/projects/`
590
+ - Try specifying the project: `npx kerf-cli watch --project ~/.claude/projects/`
591
+
592
+ ### Dashboard shows no data
593
+
594
+ The JSONL log file may not have usage data yet.
595
+
596
+ **Fixes:**
597
+ - Send a message in Claude Code and wait for a response
598
+ - Check the session file exists: `ls -la ~/.claude/projects/*/`
599
+
600
+ ### Costs seem wrong
601
+
602
+ kerf-cli uses a token heuristic (characters / 3.5) for estimates. Actual costs come from JSONL logs.
603
+
604
+ **Notes:**
605
+ - If `total_cost_usd` is present in the JSONL, kerf-cli uses that (it's authoritative)
606
+ - Otherwise, it calculates from token counts and model pricing
607
+ - Pricing is based on published Anthropic rates as of May 2025
608
+
609
+ ### "Command not found: kerf-cli"
610
+
611
+ If you installed globally:
612
+ ```bash
613
+ npm install -g kerf-cli
614
+ ```
615
+
616
+ Or use npx:
617
+ ```bash
618
+ npx kerf-cli@latest --help
619
+ ```
620
+
621
+ ### Database errors
622
+
623
+ Reset the database:
624
+ ```bash
625
+ rm ~/.kerf/kerf.db
626
+ npx kerf-cli init
627
+ ```
628
+
629
+ ---
630
+
631
+ ## Quick Reference
632
+
633
+ | Command | What it does |
634
+ |---------|-------------|
635
+ | `npx kerf-cli init` | First-time setup |
636
+ | `npx kerf-cli watch` | Live cost dashboard |
637
+ | `npx kerf-cli estimate '<task>'` | Pre-flight cost estimate |
638
+ | `npx kerf-cli estimate --compare '<task>'` | Compare Sonnet vs Opus vs Haiku |
639
+ | `npx kerf-cli budget set 50 --period weekly` | Set weekly budget |
640
+ | `npx kerf-cli budget show` | Check budget status |
641
+ | `npx kerf-cli audit` | Find ghost token waste |
642
+ | `npx kerf-cli report` | Today's cost report |
643
+ | `npx kerf-cli report --period week --csv` | Export weekly costs |
644
+ | `npx kerf-cli --help` | Show all commands |