claudify-agent 1.0.1 → 1.0.2

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 (47) hide show
  1. package/.claude/QUICK-REFERENCE.md +213 -213
  2. package/.claude/cost-dashboard.md +162 -162
  3. package/.claude/docs/COST-OPTIMIZATION.md +402 -403
  4. package/.claude/docs/MCP-INTEGRATION-GUIDE.md +348 -348
  5. package/.claude/docs/PLAN-MODE-OPTIMIZATION.md +835 -836
  6. package/.claude/hooks/auto-learning-review.ts +200 -195
  7. package/.claude/hooks/auto-lint-fix.ts +60 -104
  8. package/.claude/hooks/auto-security-review.ts +314 -354
  9. package/.claude/hooks/auto-validate.ts +249 -348
  10. package/.claude/hooks/lib/claude-hook.ts +168 -0
  11. package/.claude/hooks/plan-quality-tracker.ts +598 -719
  12. package/.claude/rules/anti-patterns.md +22 -22
  13. package/.claude/rules/context-tier-1.md +41 -41
  14. package/.claude/rules/context-tier-2.md +285 -285
  15. package/.claude/rules/general.md +25 -25
  16. package/.claude/rules/mcp-auto-use.md +46 -46
  17. package/.claude/rules/parallel-exploration-auto.md +30 -31
  18. package/.claude/rules/plan-mode-execution.md +289 -289
  19. package/.claude/rules/plan-mode-research.md +34 -34
  20. package/.claude/rules/plan-mode-structure.md +166 -166
  21. package/.claude/rules/plan-mode-validation.md +129 -130
  22. package/.claude/rules/security-critical.md +19 -19
  23. package/.claude/rules/tool-auto-selection.md +45 -45
  24. package/.claude/rules/tool-strategy.md +22 -22
  25. package/.claude/scripts/security-scan.js +1 -1
  26. package/.claude/security-dashboard.md +142 -142
  27. package/.claude/settings.json +50 -0
  28. package/.claude/skills/debug-optimizer/SKILL.md +420 -399
  29. package/.claude/skills/learning-loop/SKILL.md +313 -308
  30. package/.claude/skills/mcp-mastery/SKILL.md +218 -214
  31. package/.claude/skills/nextjs-guide/SKILL.md +687 -687
  32. package/.claude/skills/parallel-exploration/SKILL.md +197 -193
  33. package/.claude/skills/plan-mode-mastery/SKILL.md +268 -263
  34. package/.claude/skills/python-guide/SKILL.md +822 -822
  35. package/.claude/skills/react-guide/SKILL.md +762 -762
  36. package/.claude/skills/{security.md → security-guide/SKILL.md} +10 -0
  37. package/.claude/skills/solidity-guide/SKILL.md +5 -0
  38. package/.claude/skills/tool-selection/SKILL.md +433 -428
  39. package/.claude/skills/vite-guide/SKILL.md +445 -445
  40. package/CLAUDE.md +1 -0
  41. package/README.md +74 -35
  42. package/README.vi.md +106 -0
  43. package/bin/cli.js +13 -4
  44. package/package.json +9 -1
  45. package/.claude/skills/debug-optimizer.md +0 -12
  46. package/.claude/skills/nextjs.md +0 -13
  47. package/.claude/skills/react.md +0 -12
@@ -1,403 +1,402 @@
1
- # Cost Optimization Guide
2
-
3
- ## Overview
4
- Track and optimize token consumption and execution costs for Cursor agent operations. This guide provides formulas, tracking methods, and optimization strategies.
5
-
6
- ## Tool Cost Hierarchy
7
-
8
- ### Tier 1: Cheapest (Always Try First)
9
- ```
10
- Tool | Est. Tokens | When to Use
11
- --------------|-------------|----------------------------------
12
- Read | 500 | Single file access
13
- Glob | 200 | Pattern-based file finding
14
- Grep | 300 | Exact text/symbol search
15
- Shell | 100 | Command execution
16
- Write | 1000 | Creating new files
17
- StrReplace | 500 | Small targeted edits (<50 lines)
18
- ```
19
-
20
- ### Tier 2: Moderate Cost
21
- ```
22
- Tool | Est. Tokens | When to Use
23
- -------------------|-------------|----------------------------------
24
- SemanticSearch | 1500 | Conceptual/pattern search
25
- ReadLints | 800 | Code quality validation
26
- EditNotebook | 1000 | Jupyter notebook edits
27
- ```
28
-
29
- ### Tier 3: Higher Cost
30
- ```
31
- Tool | Est. Tokens | When to Use
32
- --------------|-------------|----------------------------------
33
- Task | 3000 | Complex exploration (subagents)
34
- WebSearch | 2000 | Web search operations
35
- WebFetch | 2500 | Fetch web content
36
- ```
37
-
38
- ### Tier 4: Most Expensive (Use Sparingly)
39
- ```
40
- MCP Server | Est. Tokens | When to Use
41
- ------------------------|-------------|----------------------------------
42
- cursor-ide-browser | 5000+ | Visual verification, UI testing
43
- user-github | 2000 | Git/GitHub operations
44
- user-chrome-devtools | 3000 | Performance audits
45
- user-playwright | 4000 | Cross-browser E2E testing
46
- ```
47
-
48
- ## Cost Calculation Formulas
49
-
50
- ### Per-Tool Cost
51
- ```typescript
52
- function calculateToolCost(toolCalls: ToolCall[]): number {
53
- const costEstimates: Record<string, number> = {
54
- 'Shell': 100, 'Read': 500, 'Write': 1000,
55
- 'Glob': 200, 'Grep': 300, 'StrReplace': 500,
56
- 'ReadLints': 800, 'SemanticSearch': 1500,
57
- 'Task': 3000, 'WebSearch': 2000, 'WebFetch': 2500,
58
- };
59
-
60
- let totalCost = 0;
61
- for (const tool of toolCalls) {
62
- if (tool.mcp_server) {
63
- totalCost += 5000; // MCP tools estimated higher
64
- } else {
65
- totalCost += costEstimates[tool.name] || 1000;
66
- }
67
- }
68
-
69
- return totalCost;
70
- }
71
- ```
72
-
73
- ### Plan Efficiency Score
74
- ```typescript
75
- function calculateEfficiency(tools: ToolCall[]): number {
76
- const tierScores: Record<string, number> = {
77
- 'Shell': 100, 'Read': 100, 'Write': 100,
78
- 'Glob': 100, 'Grep': 100,
79
- 'ReadLints': 80, 'SemanticSearch': 80,
80
- 'Task': 60, 'WebSearch': 60, 'WebFetch': 60,
81
- };
82
-
83
- let totalScore = 0;
84
- let toolCount = 0;
85
-
86
- for (const tool of tools) {
87
- if (tool.mcp_server) {
88
- totalScore += 40; // MCP = lowest efficiency score
89
- toolCount++;
90
- } else {
91
- totalScore += tierScores[tool.name] || 75;
92
- toolCount++;
93
- }
94
- }
95
-
96
- return toolCount > 0 ? Math.round(totalScore / toolCount) : 75;
97
- }
98
- ```
99
-
100
- ### Cost Per Task Type
101
- ```
102
- Task Type | Avg Tokens | Optimization Target
103
- -----------------------|------------|--------------------
104
- Simple bug fix | 2,000-5,000 | <3,000
105
- Feature implementation | 10,000-20,000 | <15,000
106
- Complex refactor | 15,000-30,000 | <20,000
107
- New file creation | 3,000-8,000 | <5,000
108
- Research/exploration | 5,000-15,000 | <10,000
109
- ```
110
-
111
- ## Cost Tracking
112
-
113
- ### Automatic Tracking (Enabled by Default)
114
- The `plan-quality-tracker.ts` hook automatically tracks:
115
- - Tool usage per plan
116
- - Estimated token cost
117
- - Rolling averages (last 10 plans)
118
- - Cost efficiency scores
119
-
120
- ### Manual Tracking
121
- ```bash
122
- # View recent plan costs
123
- cat .cursor/plan-metrics.json | jq '.[-10:] | .[].cost_estimate'
124
-
125
- # Calculate average cost
126
- cat .cursor/plan-metrics.json | jq '[.[].cost_estimate] | add / length'
127
-
128
- # Find most expensive plans
129
- cat .cursor/plan-metrics.json | jq 'sort_by(-.cost_estimate) | .[0:5]'
130
- ```
131
-
132
- ### Cost Alerts
133
- Configure in `.cursor/hooks.config.json`:
134
- ```json
135
- {
136
- "planTracking": {
137
- "enableCostTracking": true,
138
- "costAlertThreshold": 20000,
139
- "efficiencyThreshold": 60
140
- }
141
- }
142
- ```
143
-
144
- ## Optimization Strategies
145
-
146
- ### Strategy 1: Tool Escalation Protocol
147
- **Rule**: Start with cheapest tool, escalate only if needed
148
-
149
- ```
150
- 1. Try Tier 1 first:
151
- - Need file? → Read (know path) or Glob (pattern)
152
- - Need text? → Grep (exact) or SemanticSearch (concept)
153
-
154
- 2. If Tier 1 fails → Escalate to Tier 2:
155
- - Complex search? → SemanticSearch
156
- - Small edit? → StrReplace
157
-
158
- 3. If Tier 2 inadequate → Escalate to Tier 3:
159
- - Broad exploration? → Task (subagent)
160
- - External info? → WebSearch/WebFetch
161
-
162
- 4. If Tier 3 insufficient → Escalate to Tier 4:
163
- - Visual verification? → cursor-ide-browser
164
- - Git operations? → user-github
165
- ```
166
-
167
- **Savings**: 30-50% reduction in token consumption
168
-
169
- ### Strategy 2: MCP Tool Optimization
170
- **Rule**: Use MCP tools only when essential
171
-
172
- **When to Use MCP**:
173
- - ✅ Visual verification (UI changes)
174
- - ✅ Git/GitHub operations
175
- - ✅ Performance audits
176
- - ✅ Documentation lookup
177
- - ❌ Simple file operations (use Read/Write)
178
- - ❌ Basic searches (use Grep/Glob)
179
- - ❌ Code edits (use StrReplace/Write)
180
-
181
- **Savings**: 40-60% reduction for typical tasks
182
-
183
- ### Strategy 3: Parallel Exploration
184
- **Rule**: Use parallel subagents for complex tasks (>10 steps)
185
-
186
- **Cost-Benefit**:
187
- - Cost: 3-4 subagents × 3000 tokens = 9,000-12,000 tokens
188
- - Benefit: 60-70% faster discovery, prevents wasted iterations
189
- - ROI: Positive for tasks >30 min estimated
190
-
191
- **Configuration**:
192
- ```typescript
193
- Task:
194
- description="Explore [area]"
195
- model="fast" // Cost optimization
196
- readonly=true // Safety
197
- run_in_background=true // Non-blocking
198
- ```
199
-
200
- ### Strategy 4: Research First, Plan Second
201
- **Rule**: Spend 2-3 min on research before planning
202
-
203
- **Research Tools** (cheap):
204
- - SemanticSearch: 1500 tokens
205
- - Grep: 300 tokens
206
- - Glob: 200 tokens
207
-
208
- **Benefit**:
209
- - Prevents wrong direction (-10,000+ tokens wasted)
210
- - Improves accuracy by 20-25%
211
- - Reduces iterations by 40-50%
212
-
213
- ### Strategy 5: Automatic Validation
214
- **Rule**: Always validate after substantive changes
215
-
216
- **Validation Sequence**:
217
- 1. ReadLints (800 tokens) - catches errors early
218
- 2. Shell tests (100 tokens) - verifies functionality
219
- 3. MCP validation (if used) - ensures tool success
220
-
221
- **Benefit**:
222
- - Catches 95% of issues before commit
223
- - Prevents expensive rework
224
- - Reduces revision cycles by 55%
225
-
226
- ## Cost Optimization Checklist
227
-
228
- ### Before Starting Task
229
- - [ ] Define clear success criteria
230
- - [ ] Estimate complexity (simple/medium/complex)
231
- - [ ] Identify cheapest tool sequence
232
- - [ ] Set cost budget based on task type
233
-
234
- ### During Execution
235
- - [ ] Follow tool escalation protocol
236
- - [ ] Use Tier 1 tools first
237
- - [ ] Avoid MCP unless essential
238
- - [ ] Track tool usage mentally
239
-
240
- ### After Completion
241
- - [ ] Review actual cost vs estimate
242
- - [ ] Check efficiency score (target: >70%)
243
- - [ ] Identify optimization opportunities
244
- - [ ] Log learnings for future reference
245
-
246
- ## Cost Reduction Examples
247
-
248
- ### Example 1: Bug Fix (Before/After)
249
-
250
- **Before** (No optimization):
251
- ```
252
- 1. Task (explore): "Find auth bug" - 3000 tokens
253
- 2. browser_snapshot - 5000 tokens
254
- 3. StrReplace - 500 tokens
255
- 4. ReadLints - 800 tokens
256
- Total: 9,300 tokens
257
- Efficiency: 52%
258
- ```
259
-
260
- **After** (Optimized):
261
- ```
262
- 1. Grep: "find login function" - 300 tokens
263
- 2. Read: auth code - 500 tokens
264
- 3. StrReplace: fix bug - 500 tokens
265
- 4. ReadLints: validate - 800 tokens
266
- Total: 2,100 tokens
267
- Efficiency: 88%
268
- Savings: 77% reduction
269
- ```
270
-
271
- ### Example 2: Feature Implementation
272
-
273
- **Before** (No optimization):
274
- ```
275
- 1. browser_snapshot - 5000 tokens
276
- 2. Task x3 (parallel) - 9000 tokens
277
- 3. Write x5 files - 5000 tokens
278
- 4. user-github (create PR) - 2000 tokens
279
- Total: 21,000 tokens
280
- Efficiency: 58%
281
- ```
282
-
283
- **After** (Optimized):
284
- ```
285
- 1. SemanticSearch: patterns - 1500 tokens
286
- 2. Read: 3 examples - 1500 tokens
287
- 3. Write x5 files - 5000 tokens
288
- 4. ReadLints - 800 tokens
289
- 5. Shell (tests) - 100 tokens
290
- 6. user-github (create PR) - 2000 tokens
291
- Total: 10,900 tokens
292
- Efficiency: 76%
293
- Savings: 48% reduction
294
- ```
295
-
296
- ### Example 3: Complex Refactor
297
-
298
- **Before** (No optimization):
299
- ```
300
- 1. Task x4 (exploration) - 12,000 tokens
301
- 2. browser_snapshot x5 - 25,000 tokens
302
- 3. StrReplace x10 - 5000 tokens
303
- 4. ReadLints x3 - 2400 tokens
304
- Total: 44,400 tokens
305
- Efficiency: 45%
306
- ```
307
-
308
- **After** (Optimized):
309
- ```
310
- 1. Task x3 (parallel, targeted) - 9000 tokens
311
- 2. Grep: find usages - 300 tokens
312
- 3. StrReplace x10 - 5000 tokens
313
- 4. ReadLints (auto) - 800 tokens
314
- 5. Shell (tests) - 100 tokens
315
- Total: 15,200 tokens
316
- Efficiency: 72%
317
- Savings: 66% reduction
318
- ```
319
-
320
- ## Monitoring & Alerts
321
-
322
- ### Weekly Cost Review
323
- ```markdown
324
- ## Weekly Cost Report
325
-
326
- ### Summary
327
- - Total plans: 15
328
- - Total cost: 145,000 tokens
329
- - Avg cost/plan: 9,667 tokens
330
- - Target: <8,000 tokens
331
-
332
- ### Most Expensive Plans
333
- 1. Feature X: 25,000 tokens (over budget)
334
- 2. Refactor Y: 18,000 tokens (over budget)
335
- 3. Bug fix Z: 12,000 tokens (on budget)
336
-
337
- ### Optimization Wins
338
- - Tool escalation: -35% avg cost
339
- - MCP reduction: -50% MCP usage
340
- - Research-first: +20% accuracy
341
-
342
- ### Action Items
343
- 1. Review Feature X approach (too expensive)
344
- 2. Document successful patterns
345
- 3. Adjust cost estimates
346
- ```
347
-
348
- ### Cost Alert Triggers
349
- Configure alerts for:
350
- - Single plan > 20,000 tokens
351
- - Daily total > 100,000 tokens
352
- - Weekly avg > 10,000 tokens/plan
353
- - Efficiency score < 60%
354
-
355
- ## Tools & Resources
356
-
357
- ### Cost Calculators
358
- - Built-in: `estimateCost()` in plan-quality-tracker.ts
359
- - Manual: Use formulas above
360
- - Dashboard: Future enhancement
361
-
362
- ### Cost Benchmarks
363
- ```
364
- Developer Level | Avg Cost/Plan | Efficiency
365
- ----------------|---------------|------------
366
- Junior | 12,000 | 55%
367
- Mid | 8,000 | 70%
368
- Senior | 6,000 | 80%
369
- Optimized | 4,000 | 90%
370
- ```
371
-
372
- ### ROI Calculation
373
- ```
374
- Time saved with optimization: 2 hours/week
375
- Token cost reduction: 40%
376
- Developer hourly rate: $50/hour
377
- Weekly savings: $100
378
- Monthly savings: $400
379
- Annual savings: $4,800 per developer
380
- ```
381
-
382
- ## Related Documentation
383
- - `AGENT-INSTRUCTION-BEST-PRACTICES.md` - Research backing
384
- - `.cursor/skills/tool-selection/SKILL.md` - Tool selection
385
- - `.cursor/plan-metrics.json` - Historical cost data
386
- - `.cursor/hooks.config.json` - Configuration
387
-
388
- ## Maintenance
389
-
390
- ### Weekly
391
- - Review cost metrics
392
- - Identify optimization opportunities
393
- - Adjust thresholds
394
-
395
- ### Monthly
396
- - Analyze cost trends
397
- - Update cost estimates
398
- - Refine optimization strategies
399
-
400
- ### Quarterly
401
- - Major strategy review
402
- - Update benchmarks
403
- - Integrate new tools/MCPs
1
+ # Cost Optimization Guide
2
+
3
+ ## Overview
4
+ Track and optimize token consumption and execution costs for Claude Code agent operations. This guide provides formulas, tracking methods, and optimization strategies.
5
+
6
+ ## Tool Cost Hierarchy
7
+
8
+ ### Tier 1: Cheapest (Always Try First)
9
+ ```
10
+ Tool | Est. Tokens | When to Use
11
+ --------------|-------------|----------------------------------
12
+ Read | 500 | Single file access
13
+ Glob | 200 | Pattern-based file finding
14
+ Grep | 300 | Exact text/symbol search
15
+ Bash | 100 | Command execution
16
+ Write | 1000 | Creating new files
17
+ Edit | 500 | Small targeted edits (<50 lines)
18
+ ```
19
+
20
+ ### Tier 2: Moderate Cost
21
+ ```
22
+ Tool | Est. Tokens | When to Use
23
+ -------------------|-------------|----------------------------------
24
+ Explore (quick) | 1500 | Conceptual/pattern search
25
+ Lint check | 800 | Code quality validation
26
+ NotebookEdit | 1000 | Jupyter notebook edits
27
+ ```
28
+
29
+ ### Tier 3: Higher Cost
30
+ ```
31
+ Tool | Est. Tokens | When to Use
32
+ --------------|-------------|----------------------------------
33
+ Agent | 3000 | Complex exploration (subagents)
34
+ WebSearch | 2000 | Web search operations
35
+ WebFetch | 2500 | Fetch web content
36
+ ```
37
+
38
+ ### Tier 4: Most Expensive (Use Sparingly)
39
+ ```
40
+ MCP Server | Est. Tokens | When to Use
41
+ ------------------------|-------------|----------------------------------
42
+ claude-in-chrome | 5000+ | Visual verification, UI testing
43
+ github | 2000 | Git/GitHub operations
44
+ chrome-devtools | 3000 | Performance audits
45
+ playwright | 4000 | Cross-browser E2E testing
46
+ ```
47
+
48
+ ## Cost Calculation Formulas
49
+
50
+ ### Per-Tool Cost
51
+ ```typescript
52
+ function calculateToolCost(toolCalls: ToolCall[]): number {
53
+ const costEstimates: Record<string, number> = {
54
+ 'Bash': 100, 'Read': 500, 'Edit': 800, 'MultiEdit': 1000, 'Write': 1000,
55
+ 'Glob': 200, 'Grep': 300,
56
+ 'LSP': 800,
57
+ 'Agent': 3000, 'Task': 3000, 'WebSearch': 2000, 'WebFetch': 2500,
58
+ };
59
+
60
+ let totalCost = 0;
61
+ for (const tool of toolCalls) {
62
+ if (tool.mcp_server) {
63
+ totalCost += 5000; // MCP tools estimated higher
64
+ } else {
65
+ totalCost += costEstimates[tool.name] || 1000;
66
+ }
67
+ }
68
+
69
+ return totalCost;
70
+ }
71
+ ```
72
+
73
+ ### Plan Efficiency Score
74
+ ```typescript
75
+ function calculateEfficiency(tools: ToolCall[]): number {
76
+ const tierScores: Record<string, number> = {
77
+ 'Bash': 100, 'Read': 100, 'Edit': 100, 'MultiEdit': 100, 'Write': 100,
78
+ 'Glob': 100, 'Grep': 100,
79
+ 'LSP': 80,
80
+ 'Agent': 60, 'Task': 60, 'WebSearch': 60, 'WebFetch': 60,
81
+ };
82
+
83
+ let totalScore = 0;
84
+ let toolCount = 0;
85
+
86
+ for (const tool of tools) {
87
+ if (tool.mcp_server) {
88
+ totalScore += 40; // MCP = lowest efficiency score
89
+ toolCount++;
90
+ } else {
91
+ totalScore += tierScores[tool.name] || 75;
92
+ toolCount++;
93
+ }
94
+ }
95
+
96
+ return toolCount > 0 ? Math.round(totalScore / toolCount) : 75;
97
+ }
98
+ ```
99
+
100
+ ### Cost Per Task Type
101
+ ```
102
+ Task Type | Avg Tokens | Optimization Target
103
+ -----------------------|------------|--------------------
104
+ Simple bug fix | 2,000-5,000 | <3,000
105
+ Feature implementation | 10,000-20,000 | <15,000
106
+ Complex refactor | 15,000-30,000 | <20,000
107
+ New file creation | 3,000-8,000 | <5,000
108
+ Research/exploration | 5,000-15,000 | <10,000
109
+ ```
110
+
111
+ ## Cost Tracking
112
+
113
+ ### Automatic Tracking (Enabled by Default)
114
+ The `plan-quality-tracker.ts` hook automatically tracks:
115
+ - Tool usage per plan
116
+ - Estimated token cost
117
+ - Rolling averages (last 10 plans)
118
+ - Cost efficiency scores
119
+
120
+ ### Manual Tracking
121
+ ```bash
122
+ # View recent plan costs
123
+ cat .claude/plan-metrics.json | jq '.[-10:] | .[].cost_estimate'
124
+
125
+ # Calculate average cost
126
+ cat .claude/plan-metrics.json | jq '[.[].cost_estimate] | add / length'
127
+
128
+ # Find most expensive plans
129
+ cat .claude/plan-metrics.json | jq 'sort_by(-.cost_estimate) | .[0:5]'
130
+ ```
131
+
132
+ ### Cost Alerts
133
+ Configure in `.claude/hooks.config.json`:
134
+ ```json
135
+ {
136
+ "planTracking": {
137
+ "enableCostTracking": true,
138
+ "costAlertThreshold": 20000,
139
+ "efficiencyThreshold": 60
140
+ }
141
+ }
142
+ ```
143
+
144
+ ## Optimization Strategies
145
+
146
+ ### Strategy 1: Tool Escalation Protocol
147
+ **Rule**: Start with cheapest tool, escalate only if needed
148
+
149
+ ```
150
+ 1. Try Tier 1 first:
151
+ - Need file? → Read (know path) or Glob (pattern)
152
+ - Need text? → Grep (exact) or Explore (quick) (concept)
153
+
154
+ 2. If Tier 1 fails → Escalate to Tier 2:
155
+ - Complex search? → Explore (quick)
156
+ - Small edit? → Edit
157
+
158
+ 3. If Tier 2 inadequate → Escalate to Tier 3:
159
+ - Broad exploration? → Agent (subagent)
160
+ - External info? → WebSearch/WebFetch
161
+
162
+ 4. If Tier 3 insufficient → Escalate to Tier 4:
163
+ - Visual verification? → claude-in-chrome
164
+ - Git operations? → github
165
+ ```
166
+
167
+ **Savings**: 30-50% reduction in token consumption
168
+
169
+ ### Strategy 2: MCP Tool Optimization
170
+ **Rule**: Use MCP tools only when essential
171
+
172
+ **When to Use MCP**:
173
+ - ✅ Visual verification (UI changes)
174
+ - ✅ Git/GitHub operations
175
+ - ✅ Performance audits
176
+ - ✅ Documentation lookup
177
+ - ❌ Simple file operations (use Read/Write)
178
+ - ❌ Basic searches (use Grep/Glob)
179
+ - ❌ Code edits (use Edit/Write)
180
+
181
+ **Savings**: 40-60% reduction for typical tasks
182
+
183
+ ### Strategy 3: Parallel Exploration
184
+ **Rule**: Use parallel subagents for complex tasks (>10 steps)
185
+
186
+ **Cost-Benefit**:
187
+ - Cost: 3-4 subagents × 3000 tokens = 9,000-12,000 tokens
188
+ - Benefit: 60-70% faster discovery, prevents wasted iterations
189
+ - ROI: Positive for tasks >30 min estimated
190
+
191
+ **Configuration**:
192
+ ```typescript
193
+ Agent:
194
+ description="Explore [area]"
195
+ model="haiku" // Cost optimization
196
+ run_in_background=true // Non-blocking
197
+ ```
198
+
199
+ ### Strategy 4: Research First, Plan Second
200
+ **Rule**: Spend 2-3 min on research before planning
201
+
202
+ **Research Tools** (cheap):
203
+ - Explore (quick): 1500 tokens
204
+ - Grep: 300 tokens
205
+ - Glob: 200 tokens
206
+
207
+ **Benefit**:
208
+ - Prevents wrong direction (-10,000+ tokens wasted)
209
+ - Improves accuracy by 20-25%
210
+ - Reduces iterations by 40-50%
211
+
212
+ ### Strategy 5: Automatic Validation
213
+ **Rule**: Always validate after substantive changes
214
+
215
+ **Validation Sequence**:
216
+ 1. Lint check (800 tokens) - catches errors early
217
+ 2. Bash tests (100 tokens) - verifies functionality
218
+ 3. MCP validation (if used) - ensures tool success
219
+
220
+ **Benefit**:
221
+ - Catches 95% of issues before commit
222
+ - Prevents expensive rework
223
+ - Reduces revision cycles by 55%
224
+
225
+ ## Cost Optimization Checklist
226
+
227
+ ### Before Starting Task
228
+ - [ ] Define clear success criteria
229
+ - [ ] Estimate complexity (simple/medium/complex)
230
+ - [ ] Identify cheapest tool sequence
231
+ - [ ] Set cost budget based on task type
232
+
233
+ ### During Execution
234
+ - [ ] Follow tool escalation protocol
235
+ - [ ] Use Tier 1 tools first
236
+ - [ ] Avoid MCP unless essential
237
+ - [ ] Track tool usage mentally
238
+
239
+ ### After Completion
240
+ - [ ] Review actual cost vs estimate
241
+ - [ ] Check efficiency score (target: >70%)
242
+ - [ ] Identify optimization opportunities
243
+ - [ ] Log learnings for future reference
244
+
245
+ ## Cost Reduction Examples
246
+
247
+ ### Example 1: Bug Fix (Before/After)
248
+
249
+ **Before** (No optimization):
250
+ ```
251
+ 1. Agent (Explore): "Find auth bug" - 3000 tokens
252
+ 2. claude-in-chrome screenshot - 5000 tokens
253
+ 3. Edit - 500 tokens
254
+ 4. Lint check - 800 tokens
255
+ Total: 9,300 tokens
256
+ Efficiency: 52%
257
+ ```
258
+
259
+ **After** (Optimized):
260
+ ```
261
+ 1. Grep: "find login function" - 300 tokens
262
+ 2. Read: auth code - 500 tokens
263
+ 3. Edit: fix bug - 500 tokens
264
+ 4. Lint check: validate - 800 tokens
265
+ Total: 2,100 tokens
266
+ Efficiency: 88%
267
+ Savings: 77% reduction
268
+ ```
269
+
270
+ ### Example 2: Feature Implementation
271
+
272
+ **Before** (No optimization):
273
+ ```
274
+ 1. claude-in-chrome screenshot - 5000 tokens
275
+ 2. Agent x3 (parallel) - 9000 tokens
276
+ 3. Write x5 files - 5000 tokens
277
+ 4. github (create PR) - 2000 tokens
278
+ Total: 21,000 tokens
279
+ Efficiency: 58%
280
+ ```
281
+
282
+ **After** (Optimized):
283
+ ```
284
+ 1. Explore (quick): patterns - 1500 tokens
285
+ 2. Read: 3 examples - 1500 tokens
286
+ 3. Write x5 files - 5000 tokens
287
+ 4. Lint check - 800 tokens
288
+ 5. Bash (tests) - 100 tokens
289
+ 6. github (create PR) - 2000 tokens
290
+ Total: 10,900 tokens
291
+ Efficiency: 76%
292
+ Savings: 48% reduction
293
+ ```
294
+
295
+ ### Example 3: Complex Refactor
296
+
297
+ **Before** (No optimization):
298
+ ```
299
+ 1. Agent x4 (exploration) - 12,000 tokens
300
+ 2. claude-in-chrome screenshot x5 - 25,000 tokens
301
+ 3. Edit x10 - 5000 tokens
302
+ 4. Lint check x3 - 2400 tokens
303
+ Total: 44,400 tokens
304
+ Efficiency: 45%
305
+ ```
306
+
307
+ **After** (Optimized):
308
+ ```
309
+ 1. Agent x3 (parallel, targeted) - 9000 tokens
310
+ 2. Grep: find usages - 300 tokens
311
+ 3. Edit x10 - 5000 tokens
312
+ 4. Lint check (auto) - 800 tokens
313
+ 5. Bash (tests) - 100 tokens
314
+ Total: 15,200 tokens
315
+ Efficiency: 72%
316
+ Savings: 66% reduction
317
+ ```
318
+
319
+ ## Monitoring & Alerts
320
+
321
+ ### Weekly Cost Review
322
+ ```markdown
323
+ ## Weekly Cost Report
324
+
325
+ ### Summary
326
+ - Total plans: 15
327
+ - Total cost: 145,000 tokens
328
+ - Avg cost/plan: 9,667 tokens
329
+ - Target: <8,000 tokens
330
+
331
+ ### Most Expensive Plans
332
+ 1. Feature X: 25,000 tokens (over budget)
333
+ 2. Refactor Y: 18,000 tokens (over budget)
334
+ 3. Bug fix Z: 12,000 tokens (on budget)
335
+
336
+ ### Optimization Wins
337
+ - Tool escalation: -35% avg cost
338
+ - MCP reduction: -50% MCP usage
339
+ - Research-first: +20% accuracy
340
+
341
+ ### Action Items
342
+ 1. Review Feature X approach (too expensive)
343
+ 2. Document successful patterns
344
+ 3. Adjust cost estimates
345
+ ```
346
+
347
+ ### Cost Alert Triggers
348
+ Configure alerts for:
349
+ - Single plan > 20,000 tokens
350
+ - Daily total > 100,000 tokens
351
+ - Weekly avg > 10,000 tokens/plan
352
+ - Efficiency score < 60%
353
+
354
+ ## Tools & Resources
355
+
356
+ ### Cost Calculators
357
+ - Built-in: `estimateCost()` in plan-quality-tracker.ts
358
+ - Manual: Use formulas above
359
+ - Dashboard: Future enhancement
360
+
361
+ ### Cost Benchmarks
362
+ ```
363
+ Developer Level | Avg Cost/Plan | Efficiency
364
+ ----------------|---------------|------------
365
+ Junior | 12,000 | 55%
366
+ Mid | 8,000 | 70%
367
+ Senior | 6,000 | 80%
368
+ Optimized | 4,000 | 90%
369
+ ```
370
+
371
+ ### ROI Calculation
372
+ ```
373
+ Time saved with optimization: 2 hours/week
374
+ Token cost reduction: 40%
375
+ Developer hourly rate: $50/hour
376
+ Weekly savings: $100
377
+ Monthly savings: $400
378
+ Annual savings: $4,800 per developer
379
+ ```
380
+
381
+ ## Related Documentation
382
+ - `AGENT-INSTRUCTION-BEST-PRACTICES.md` - Research backing
383
+ - `.claude/skills/tool-selection/SKILL.md` - Tool selection
384
+ - `.claude/plan-metrics.json` - Historical cost data
385
+ - `.claude/hooks.config.json` - Configuration
386
+
387
+ ## Maintenance
388
+
389
+ ### Weekly
390
+ - Review cost metrics
391
+ - Identify optimization opportunities
392
+ - Adjust thresholds
393
+
394
+ ### Monthly
395
+ - Analyze cost trends
396
+ - Update cost estimates
397
+ - Refine optimization strategies
398
+
399
+ ### Quarterly
400
+ - Major strategy review
401
+ - Update benchmarks
402
+ - Integrate new tools/MCPs