the-grid-cc 1.7.13 → 1.7.14
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/02-SUMMARY.md +156 -0
- package/agents/grid-accountant.md +519 -0
- package/agents/grid-git-operator.md +661 -0
- package/agents/grid-researcher.md +421 -0
- package/agents/grid-scout.md +376 -0
- package/commands/grid/VERSION +1 -1
- package/commands/grid/branch.md +567 -0
- package/commands/grid/budget.md +438 -0
- package/commands/grid/daemon.md +637 -0
- package/commands/grid/init.md +375 -18
- package/commands/grid/mc.md +103 -1098
- package/commands/grid/resume.md +656 -0
- package/docs/BUDGET_SYSTEM.md +745 -0
- package/docs/DAEMON_ARCHITECTURE.md +780 -0
- package/docs/GIT_AUTONOMY.md +981 -0
- package/docs/MC_OPTIMIZATION.md +181 -0
- package/docs/MC_PROTOCOLS.md +950 -0
- package/docs/PERSISTENCE.md +962 -0
- package/docs/RESEARCH_FIRST.md +591 -0
- package/package.json +1 -1
|
@@ -0,0 +1,745 @@
|
|
|
1
|
+
# The Grid - Budget and Metering System
|
|
2
|
+
|
|
3
|
+
## Technical Design Document
|
|
4
|
+
|
|
5
|
+
**Version:** 1.0
|
|
6
|
+
**Author:** Grid Program 2 (Budget System)
|
|
7
|
+
**Date:** 2026-01-23
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Executive Summary
|
|
12
|
+
|
|
13
|
+
The Grid Budget System provides cost tracking, budget enforcement, and usage reporting for Grid operations. Since The Grid operates within Claude Code (without direct API access), the system uses **estimation-based metering** derived from prompt sizes, agent type profiles, and historical patterns.
|
|
14
|
+
|
|
15
|
+
### Key Features
|
|
16
|
+
|
|
17
|
+
1. **Pre-Spawn Cost Estimation** - Estimate costs before agent spawns
|
|
18
|
+
2. **Budget Limits** - Set spending caps with configurable enforcement
|
|
19
|
+
3. **Real-time Tracking** - Monitor costs during cluster execution
|
|
20
|
+
4. **Usage Reporting** - Historical analysis and optimization recommendations
|
|
21
|
+
5. **Model Tier Integration** - Cost varies by model selection
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Architecture Overview
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
┌─────────────────────────────────────────┐
|
|
29
|
+
│ Master Control (MC) │
|
|
30
|
+
│ │
|
|
31
|
+
│ ┌─────────────────────────────────────┐│
|
|
32
|
+
│ │ Budget Check Gate ││
|
|
33
|
+
│ │ (runs before EVERY Task() spawn) ││
|
|
34
|
+
│ └─────────────────┬───────────────────┘│
|
|
35
|
+
└────────────────────┼────────────────────┘
|
|
36
|
+
│
|
|
37
|
+
┌────────────────────┼────────────────────┐
|
|
38
|
+
│ ▼ │
|
|
39
|
+
│ .grid/budget.json │
|
|
40
|
+
│ │
|
|
41
|
+
│ ┌─────────────────────────────────────┐│
|
|
42
|
+
│ │ budget_limit: 50.00 ││
|
|
43
|
+
│ │ current_session: ││
|
|
44
|
+
│ │ estimated_cost: 12.47 ││
|
|
45
|
+
│ │ spawns: [...] ││
|
|
46
|
+
│ │ history: ││
|
|
47
|
+
│ │ total_cost: 147.83 ││
|
|
48
|
+
│ └─────────────────────────────────────┘│
|
|
49
|
+
└─────────────────────────────────────────┘
|
|
50
|
+
│
|
|
51
|
+
┌───────────────┬───────────────┼───────────────┬───────────────┐
|
|
52
|
+
▼ ▼ ▼ ▼ ▼
|
|
53
|
+
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
|
|
54
|
+
│ Planner │ │Executor │ │Recogn- │ │ Visual │ │Persona │
|
|
55
|
+
│ │ │ │ │ izer │ │Inspector│ │Simulator│
|
|
56
|
+
│ ~$1.60 │ │ ~$2.10 │ │ ~$0.87 │ │ ~$0.87 │ │ ~$0.96 │
|
|
57
|
+
└─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
|
|
58
|
+
│ │ │ │ │
|
|
59
|
+
└───────────────┴───────────────┴───────────────┴───────────────┘
|
|
60
|
+
│
|
|
61
|
+
▼
|
|
62
|
+
┌─────────────────┐
|
|
63
|
+
│ Grid Accountant│
|
|
64
|
+
│ (on-demand) │
|
|
65
|
+
│ │
|
|
66
|
+
│ - Estimation │
|
|
67
|
+
│ - Reporting │
|
|
68
|
+
│ - Optimization │
|
|
69
|
+
└─────────────────┘
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Cost Estimation Model
|
|
75
|
+
|
|
76
|
+
### The Challenge
|
|
77
|
+
|
|
78
|
+
The Grid operates within Claude Code, which means:
|
|
79
|
+
- No direct access to Anthropic API usage metrics
|
|
80
|
+
- No token-level billing information
|
|
81
|
+
- No real-time cost feedback
|
|
82
|
+
|
|
83
|
+
### The Solution: Estimation-Based Metering
|
|
84
|
+
|
|
85
|
+
We estimate costs using:
|
|
86
|
+
|
|
87
|
+
1. **Prompt Size Analysis** - Characters in spawn prompts map to input tokens
|
|
88
|
+
2. **Agent Type Profiles** - Each agent has characteristic output ratios
|
|
89
|
+
3. **Model Pricing** - Published API rates from Anthropic
|
|
90
|
+
4. **Historical Calibration** - Refine estimates based on observed patterns
|
|
91
|
+
|
|
92
|
+
### Token Estimation
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
def estimate_tokens(text: str) -> int:
|
|
96
|
+
"""
|
|
97
|
+
Estimate token count from text.
|
|
98
|
+
|
|
99
|
+
Claude's tokenizer averages ~4 characters per token for mixed
|
|
100
|
+
English/code content. This is a simplification but provides
|
|
101
|
+
reasonable estimates for budgeting purposes.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
text: Input text to estimate
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
Estimated token count
|
|
108
|
+
"""
|
|
109
|
+
return len(text) // 4
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Accuracy Considerations:**
|
|
113
|
+
- English prose: ~4.5 chars/token
|
|
114
|
+
- Code: ~3.5 chars/token (more symbols)
|
|
115
|
+
- Mixed: ~4 chars/token (reasonable average)
|
|
116
|
+
- Error margin: +/- 15% typical
|
|
117
|
+
|
|
118
|
+
### Agent Type Profiles
|
|
119
|
+
|
|
120
|
+
Each agent type has characteristic input/output ratios based on their function:
|
|
121
|
+
|
|
122
|
+
| Agent | Input Profile | Output Ratio | Notes |
|
|
123
|
+
|-------|--------------|--------------|-------|
|
|
124
|
+
| Planner | Instructions + context | 1.3x | Produces detailed plans |
|
|
125
|
+
| Executor | Plan + state + files | 1.5x | Writes code, high output |
|
|
126
|
+
| Recognizer | Summaries + must-haves | 0.6x | Verification is concise |
|
|
127
|
+
| Visual Inspector | Instructions + URLs | 1.25x | Screenshots + analysis |
|
|
128
|
+
| E2E Exerciser | Instructions + flows | 1.25x | Click-by-click reports |
|
|
129
|
+
| Persona Simulator | Context + persona | 1.4x | Detailed feedback |
|
|
130
|
+
| Refinement Synth | All findings | 0.67x | Synthesizes, doesn't expand |
|
|
131
|
+
|
|
132
|
+
### Cost Calculation
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
def calculate_spawn_cost(
|
|
136
|
+
input_tokens: int,
|
|
137
|
+
output_tokens: int,
|
|
138
|
+
model: str
|
|
139
|
+
) -> float:
|
|
140
|
+
"""
|
|
141
|
+
Calculate estimated cost for a spawn.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
input_tokens: Estimated input token count
|
|
145
|
+
output_tokens: Estimated output token count
|
|
146
|
+
model: Model name (opus, sonnet, haiku)
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
Estimated cost in USD
|
|
150
|
+
"""
|
|
151
|
+
# Current Claude API pricing (per million tokens)
|
|
152
|
+
PRICING = {
|
|
153
|
+
'opus': {'input': 5.00, 'output': 25.00},
|
|
154
|
+
'sonnet': {'input': 3.00, 'output': 15.00},
|
|
155
|
+
'haiku': {'input': 1.00, 'output': 5.00},
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
rates = PRICING.get(model, PRICING['opus'])
|
|
159
|
+
|
|
160
|
+
cost = (
|
|
161
|
+
(input_tokens * rates['input']) +
|
|
162
|
+
(output_tokens * rates['output'])
|
|
163
|
+
) / 1_000_000
|
|
164
|
+
|
|
165
|
+
return round(cost, 4)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Budget Enforcement
|
|
171
|
+
|
|
172
|
+
### Enforcement Levels
|
|
173
|
+
|
|
174
|
+
The system supports multiple enforcement thresholds:
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
Budget Usage: 0% 75% 90% 100%
|
|
178
|
+
│ │ │ │
|
|
179
|
+
│ NORMAL │ WARNING │ CONFIRM │ EXCEEDED
|
|
180
|
+
│ │ │ │
|
|
181
|
+
│ Continue │ Show warn │ Ask user │ Block (hard)
|
|
182
|
+
│ silently │ continue │ to confirm│ Warn (soft)
|
|
183
|
+
▼ ▼ ▼ ▼
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Pre-Spawn Gate
|
|
187
|
+
|
|
188
|
+
Every spawn passes through the budget gate:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
def budget_gate(agent_type: str, model: str, prompt_chars: int) -> GateResult:
|
|
192
|
+
"""
|
|
193
|
+
Budget check gate - runs before EVERY spawn.
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
GateResult with:
|
|
197
|
+
- allowed: bool | 'confirm'
|
|
198
|
+
- message: Optional warning/error message
|
|
199
|
+
- estimated_cost: Cost for this spawn
|
|
200
|
+
"""
|
|
201
|
+
budget = load_budget_config()
|
|
202
|
+
|
|
203
|
+
# Unlimited budget
|
|
204
|
+
if budget.get('budget_limit') is None:
|
|
205
|
+
return GateResult(allowed=True, message=None, estimated_cost=0)
|
|
206
|
+
|
|
207
|
+
# Calculate costs
|
|
208
|
+
estimated_cost = estimate_spawn_cost(agent_type, model, prompt_chars)
|
|
209
|
+
current_usage = budget['current_session']['estimated_cost']
|
|
210
|
+
projected_usage = current_usage + estimated_cost
|
|
211
|
+
usage_ratio = projected_usage / budget['budget_limit']
|
|
212
|
+
|
|
213
|
+
# Check thresholds (highest to lowest)
|
|
214
|
+
if usage_ratio > 1.0:
|
|
215
|
+
if budget.get('enforcement', 'hard') == 'hard':
|
|
216
|
+
return GateResult(
|
|
217
|
+
allowed=False,
|
|
218
|
+
message=f"BUDGET EXCEEDED: Would be ${projected_usage:.2f} / ${budget['budget_limit']:.2f}",
|
|
219
|
+
estimated_cost=estimated_cost
|
|
220
|
+
)
|
|
221
|
+
else:
|
|
222
|
+
return GateResult(
|
|
223
|
+
allowed=True,
|
|
224
|
+
message=f"WARNING: Over budget (soft enforcement)",
|
|
225
|
+
estimated_cost=estimated_cost
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
if usage_ratio > budget.get('confirmation_threshold', 0.90):
|
|
229
|
+
return GateResult(
|
|
230
|
+
allowed='confirm',
|
|
231
|
+
message=f"Budget at {usage_ratio*100:.1f}% - confirm to proceed",
|
|
232
|
+
estimated_cost=estimated_cost
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
if usage_ratio > budget.get('warning_threshold', 0.75):
|
|
236
|
+
return GateResult(
|
|
237
|
+
allowed=True,
|
|
238
|
+
message=f"Budget warning: {usage_ratio*100:.1f}% used",
|
|
239
|
+
estimated_cost=estimated_cost
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
return GateResult(allowed=True, message=None, estimated_cost=estimated_cost)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### MC Integration
|
|
246
|
+
|
|
247
|
+
Master Control integrates budget checking into its spawn protocol:
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
# In MC spawn protocol (pseudocode)
|
|
251
|
+
def spawn_agent(agent_type, model, prompt):
|
|
252
|
+
# Step 1: Budget gate check
|
|
253
|
+
gate_result = budget_gate(agent_type, model, len(prompt))
|
|
254
|
+
|
|
255
|
+
if gate_result.allowed == False:
|
|
256
|
+
display_budget_exceeded(gate_result.message)
|
|
257
|
+
return None # Block spawn
|
|
258
|
+
|
|
259
|
+
if gate_result.allowed == 'confirm':
|
|
260
|
+
# I/O Tower confirmation
|
|
261
|
+
confirmed = ask_user_confirmation(gate_result.message)
|
|
262
|
+
if not confirmed:
|
|
263
|
+
return None # User declined
|
|
264
|
+
|
|
265
|
+
if gate_result.message:
|
|
266
|
+
display_warning(gate_result.message)
|
|
267
|
+
|
|
268
|
+
# Step 2: Record pre-spawn estimate
|
|
269
|
+
spawn_id = record_spawn_start(agent_type, model, len(prompt), gate_result.estimated_cost)
|
|
270
|
+
|
|
271
|
+
# Step 3: Execute spawn
|
|
272
|
+
result = Task(prompt=prompt, ...)
|
|
273
|
+
|
|
274
|
+
# Step 4: Record post-spawn data
|
|
275
|
+
record_spawn_complete(spawn_id, len(result))
|
|
276
|
+
|
|
277
|
+
return result
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Data Model
|
|
283
|
+
|
|
284
|
+
### Budget Configuration File
|
|
285
|
+
|
|
286
|
+
Location: `.grid/budget.json`
|
|
287
|
+
|
|
288
|
+
```json
|
|
289
|
+
{
|
|
290
|
+
"$schema": "https://thegrid.dev/schemas/budget.json",
|
|
291
|
+
"version": "1.0",
|
|
292
|
+
|
|
293
|
+
"budget_limit": 50.00,
|
|
294
|
+
"currency": "USD",
|
|
295
|
+
"enforcement": "hard",
|
|
296
|
+
"warning_threshold": 0.75,
|
|
297
|
+
"confirmation_threshold": 0.90,
|
|
298
|
+
|
|
299
|
+
"pricing": {
|
|
300
|
+
"opus": {"input": 5.00, "output": 25.00},
|
|
301
|
+
"sonnet": {"input": 3.00, "output": 15.00},
|
|
302
|
+
"haiku": {"input": 1.00, "output": 5.00}
|
|
303
|
+
},
|
|
304
|
+
|
|
305
|
+
"current_session": {
|
|
306
|
+
"id": "session-20260123-100000",
|
|
307
|
+
"started": "2026-01-23T10:00:00Z",
|
|
308
|
+
"cluster": "Auth System",
|
|
309
|
+
"model_tier": "quality",
|
|
310
|
+
"estimated_cost": 12.47,
|
|
311
|
+
"spawns": [
|
|
312
|
+
{
|
|
313
|
+
"id": "spawn-001",
|
|
314
|
+
"timestamp": "2026-01-23T10:05:00Z",
|
|
315
|
+
"agent": "planner",
|
|
316
|
+
"model": "opus",
|
|
317
|
+
"prompt_chars": 24000,
|
|
318
|
+
"est_input_tokens": 6000,
|
|
319
|
+
"est_output_tokens": 7800,
|
|
320
|
+
"est_cost": 1.60,
|
|
321
|
+
"actual_output_chars": 32400,
|
|
322
|
+
"reconciled_cost": 1.71
|
|
323
|
+
}
|
|
324
|
+
]
|
|
325
|
+
},
|
|
326
|
+
|
|
327
|
+
"history": {
|
|
328
|
+
"total_cost": 147.83,
|
|
329
|
+
"total_spawns": 84,
|
|
330
|
+
"total_input_tokens": 7388000,
|
|
331
|
+
"total_output_tokens": 1847000,
|
|
332
|
+
"sessions": [
|
|
333
|
+
{
|
|
334
|
+
"id": "session-20260122-090000",
|
|
335
|
+
"cluster": "Dashboard",
|
|
336
|
+
"spawns": 10,
|
|
337
|
+
"cost": 21.32
|
|
338
|
+
}
|
|
339
|
+
]
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Schema Definitions
|
|
345
|
+
|
|
346
|
+
#### SpawnRecord
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
interface SpawnRecord {
|
|
350
|
+
id: string; // Unique spawn identifier
|
|
351
|
+
timestamp: string; // ISO 8601 timestamp
|
|
352
|
+
agent: AgentType; // planner, executor, etc.
|
|
353
|
+
model: ModelType; // opus, sonnet, haiku
|
|
354
|
+
prompt_chars: number; // Characters in spawn prompt
|
|
355
|
+
est_input_tokens: number; // Estimated input tokens
|
|
356
|
+
est_output_tokens: number; // Estimated output tokens
|
|
357
|
+
est_cost: number; // Pre-spawn cost estimate
|
|
358
|
+
actual_output_chars?: number; // Actual output size (post-spawn)
|
|
359
|
+
reconciled_cost?: number; // Recalculated cost (post-spawn)
|
|
360
|
+
block_id?: string; // Associated block
|
|
361
|
+
description?: string; // What this spawn does
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
type AgentType =
|
|
365
|
+
| 'planner'
|
|
366
|
+
| 'executor'
|
|
367
|
+
| 'recognizer'
|
|
368
|
+
| 'visual_inspector'
|
|
369
|
+
| 'e2e_exerciser'
|
|
370
|
+
| 'persona_simulator'
|
|
371
|
+
| 'refinement_synth'
|
|
372
|
+
| 'accountant';
|
|
373
|
+
|
|
374
|
+
type ModelType = 'opus' | 'sonnet' | 'haiku';
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
#### SessionRecord
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
interface SessionRecord {
|
|
381
|
+
id: string; // Unique session identifier
|
|
382
|
+
started: string; // ISO 8601 start time
|
|
383
|
+
ended?: string; // ISO 8601 end time (when closed)
|
|
384
|
+
cluster: string; // Cluster name
|
|
385
|
+
model_tier: 'quality' | 'balanced' | 'budget' | 'custom';
|
|
386
|
+
spawns: number; // Total spawn count
|
|
387
|
+
est_input_tokens: number; // Total estimated input
|
|
388
|
+
est_output_tokens: number; // Total estimated output
|
|
389
|
+
est_cost: number; // Total estimated cost
|
|
390
|
+
reconciled_cost?: number; // Total reconciled cost
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
## Usage Reporting
|
|
397
|
+
|
|
398
|
+
### Report Types
|
|
399
|
+
|
|
400
|
+
1. **Session Report** - Current or past session details
|
|
401
|
+
2. **Historical Report** - Trends over time
|
|
402
|
+
3. **Optimization Report** - Cost reduction recommendations
|
|
403
|
+
|
|
404
|
+
### Session Report
|
|
405
|
+
|
|
406
|
+
```markdown
|
|
407
|
+
## SESSION COST REPORT
|
|
408
|
+
|
|
409
|
+
**Session:** session-20260123-100000
|
|
410
|
+
**Cluster:** Auth System
|
|
411
|
+
**Duration:** 4h 30m
|
|
412
|
+
**Model Tier:** quality
|
|
413
|
+
|
|
414
|
+
### Summary
|
|
415
|
+
| Metric | Estimated | Reconciled |
|
|
416
|
+
|--------|-----------|------------|
|
|
417
|
+
| Total Spawns | 8 | 8 |
|
|
418
|
+
| Input Tokens | 498,000 | 498,000 |
|
|
419
|
+
| Output Tokens | 124,500 | 131,200 |
|
|
420
|
+
| Total Cost | $14.86 | $15.23 |
|
|
421
|
+
|
|
422
|
+
### Spawn Timeline
|
|
423
|
+
| Time | Agent | Model | Est. Cost | Reconciled |
|
|
424
|
+
|------|-------|-------|-----------|------------|
|
|
425
|
+
| 10:05 | planner | opus | $1.60 | $1.71 |
|
|
426
|
+
| 10:20 | executor | opus | $2.10 | $2.15 |
|
|
427
|
+
| 10:45 | executor | opus | $2.10 | $2.08 |
|
|
428
|
+
| ... | ... | ... | ... | ... |
|
|
429
|
+
|
|
430
|
+
### Accuracy Analysis
|
|
431
|
+
- Average estimation error: +2.5%
|
|
432
|
+
- Largest over-estimate: -8% (recognizer)
|
|
433
|
+
- Largest under-estimate: +12% (executor-03)
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
### Historical Report
|
|
437
|
+
|
|
438
|
+
```markdown
|
|
439
|
+
## HISTORICAL COST REPORT
|
|
440
|
+
|
|
441
|
+
**Period:** 2026-01-01 to 2026-01-23
|
|
442
|
+
**Sessions:** 15
|
|
443
|
+
**Total Cost:** $183.47
|
|
444
|
+
|
|
445
|
+
### Weekly Trend
|
|
446
|
+
| Week | Sessions | Spawns | Cost | Avg/Session |
|
|
447
|
+
|------|----------|--------|------|-------------|
|
|
448
|
+
| Jan 20-26 | 4 | 32 | $58.40 | $14.60 |
|
|
449
|
+
| Jan 13-19 | 6 | 48 | $72.15 | $12.03 |
|
|
450
|
+
| Jan 6-12 | 3 | 24 | $38.72 | $12.91 |
|
|
451
|
+
| Jan 1-5 | 2 | 16 | $14.20 | $7.10 |
|
|
452
|
+
|
|
453
|
+
### Model Distribution
|
|
454
|
+
| Model | Spawns | % | Cost | % |
|
|
455
|
+
|-------|--------|---|------|---|
|
|
456
|
+
| Opus | 72 | 60% | $129.60 | 70.6% |
|
|
457
|
+
| Sonnet | 42 | 35% | $48.30 | 26.3% |
|
|
458
|
+
| Haiku | 6 | 5% | $5.57 | 3.1% |
|
|
459
|
+
|
|
460
|
+
### Cost by Agent Type
|
|
461
|
+
| Agent | Spawns | Avg Cost | Total |
|
|
462
|
+
|-------|--------|----------|-------|
|
|
463
|
+
| Executor | 45 | $1.95 | $87.75 |
|
|
464
|
+
| Planner | 15 | $1.52 | $22.80 |
|
|
465
|
+
| Recognizer | 30 | $0.92 | $27.60 |
|
|
466
|
+
| Others | 30 | $1.51 | $45.32 |
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
### Optimization Report
|
|
470
|
+
|
|
471
|
+
```markdown
|
|
472
|
+
## OPTIMIZATION RECOMMENDATIONS
|
|
473
|
+
|
|
474
|
+
Based on analysis of your last 15 sessions:
|
|
475
|
+
|
|
476
|
+
### High-Impact Recommendations
|
|
477
|
+
|
|
478
|
+
1. **Switch Recognizer to Haiku** (High confidence)
|
|
479
|
+
- Current: Sonnet ($0.87/spawn avg)
|
|
480
|
+
- Recommended: Haiku ($0.29/spawn)
|
|
481
|
+
- Savings: ~$17/month based on your usage
|
|
482
|
+
- Risk: Low - verification doesn't need deep reasoning
|
|
483
|
+
|
|
484
|
+
2. **Use Balanced tier for refinement** (Medium confidence)
|
|
485
|
+
- Current: Quality tier for all
|
|
486
|
+
- Recommended: Balanced for Visual/E2E/Synth
|
|
487
|
+
- Savings: ~$8/month
|
|
488
|
+
- Risk: Low - refinement quality maintained
|
|
489
|
+
|
|
490
|
+
3. **Increase block size** (Medium confidence)
|
|
491
|
+
- Current: Average 2.1 threads/block
|
|
492
|
+
- Recommended: 2.5-3 threads/block
|
|
493
|
+
- Savings: ~12% fewer Executor spawns
|
|
494
|
+
- Risk: Medium - watch for context degradation
|
|
495
|
+
|
|
496
|
+
### Patterns Detected
|
|
497
|
+
|
|
498
|
+
- **Peak usage:** Monday mornings (avg $18/session vs $12 otherwise)
|
|
499
|
+
- **Most expensive cluster type:** E-commerce projects ($28 avg)
|
|
500
|
+
- **Cheapest tier switch:** Quality → Balanced saves 40%
|
|
501
|
+
|
|
502
|
+
### ROI Summary
|
|
503
|
+
|
|
504
|
+
| Recommendation | Effort | Monthly Savings |
|
|
505
|
+
|---------------|--------|-----------------|
|
|
506
|
+
| Haiku for verification | Low | $17 |
|
|
507
|
+
| Balanced refinement | Low | $8 |
|
|
508
|
+
| Larger blocks | Medium | $6 |
|
|
509
|
+
| **Total** | - | **$31** |
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
---
|
|
513
|
+
|
|
514
|
+
## Integration Points
|
|
515
|
+
|
|
516
|
+
### With `/grid:model`
|
|
517
|
+
|
|
518
|
+
Model selection directly affects costs:
|
|
519
|
+
|
|
520
|
+
```
|
|
521
|
+
/grid:model quality → Opus everywhere ($$$)
|
|
522
|
+
/grid:model balanced → Sonnet most places ($$)
|
|
523
|
+
/grid:model budget → Haiku where safe ($)
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
Budget system reflects current model tier in estimates.
|
|
527
|
+
|
|
528
|
+
### With Cluster Planning
|
|
529
|
+
|
|
530
|
+
Pre-execution cost estimate:
|
|
531
|
+
|
|
532
|
+
```
|
|
533
|
+
CLUSTER COST ESTIMATE
|
|
534
|
+
=====================
|
|
535
|
+
|
|
536
|
+
Cluster: Auth System
|
|
537
|
+
Blocks: 3
|
|
538
|
+
Model Tier: quality
|
|
539
|
+
|
|
540
|
+
Breakdown:
|
|
541
|
+
Planning: 1 spawn × $1.60 = $1.60
|
|
542
|
+
Execution: 3 spawns × $2.10 = $6.30
|
|
543
|
+
Verification: 2 spawns × $1.45 = $2.90
|
|
544
|
+
Refinement: 5 spawns × $0.90 = $4.50
|
|
545
|
+
────────────────
|
|
546
|
+
Estimated Total: $15.30
|
|
547
|
+
|
|
548
|
+
Budget Status:
|
|
549
|
+
Limit: $50.00
|
|
550
|
+
Current: $12.47
|
|
551
|
+
After: $27.77 (55.5%)
|
|
552
|
+
|
|
553
|
+
Proceed? [Y/n]
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
### With Quick Mode
|
|
557
|
+
|
|
558
|
+
Quick mode skips Planner spawn, reducing cost:
|
|
559
|
+
|
|
560
|
+
```
|
|
561
|
+
QUICK MODE
|
|
562
|
+
==========
|
|
563
|
+
|
|
564
|
+
Estimated cost: ~$2.10 (1 Executor spawn)
|
|
565
|
+
|
|
566
|
+
vs Full Grid: ~$5.60 (Planner + Executor + Recognizer)
|
|
567
|
+
|
|
568
|
+
Savings: ~$3.50
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
### With Refinement Swarm
|
|
572
|
+
|
|
573
|
+
Refinement can be expensive; budget check shows impact:
|
|
574
|
+
|
|
575
|
+
```
|
|
576
|
+
REFINEMENT SWARM
|
|
577
|
+
================
|
|
578
|
+
|
|
579
|
+
This will spawn 5 agents:
|
|
580
|
+
- Visual Inspector ~$0.87
|
|
581
|
+
- E2E Exerciser ~$0.87
|
|
582
|
+
- 2x Persona Sim ~$1.92
|
|
583
|
+
- Refinement Synth ~$0.87
|
|
584
|
+
────────
|
|
585
|
+
Total: ~$4.53
|
|
586
|
+
|
|
587
|
+
Budget: $7.21 remaining → $2.68 after refinement
|
|
588
|
+
|
|
589
|
+
[!] Budget will be at 94.6% after refinement
|
|
590
|
+
|
|
591
|
+
Options:
|
|
592
|
+
1. Proceed with full swarm
|
|
593
|
+
2. Skip personas (saves $1.92)
|
|
594
|
+
3. Skip refinement entirely
|
|
595
|
+
4. Increase budget: /grid:budget set $60
|
|
596
|
+
|
|
597
|
+
Choice [1-4]:
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
---
|
|
601
|
+
|
|
602
|
+
## Command Reference
|
|
603
|
+
|
|
604
|
+
### `/grid:budget` - Status
|
|
605
|
+
|
|
606
|
+
Shows current budget status and session costs.
|
|
607
|
+
|
|
608
|
+
### `/grid:budget set <amount>` - Set Limit
|
|
609
|
+
|
|
610
|
+
```bash
|
|
611
|
+
/grid:budget set $50 # Set $50 limit
|
|
612
|
+
/grid:budget set 100 # Set $100 limit ($ optional)
|
|
613
|
+
/grid:budget set 0 # Set $0 (effectively paused)
|
|
614
|
+
```
|
|
615
|
+
|
|
616
|
+
### `/grid:budget estimate` - Pre-Estimate
|
|
617
|
+
|
|
618
|
+
Estimates cost for pending work (requires active plan).
|
|
619
|
+
|
|
620
|
+
### `/grid:budget report` - Detailed Report
|
|
621
|
+
|
|
622
|
+
```bash
|
|
623
|
+
/grid:budget report # Current session
|
|
624
|
+
/grid:budget report history # Last 30 days
|
|
625
|
+
/grid:budget report optimize # Optimization recommendations
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
### `/grid:budget reset` - Reset Counters
|
|
629
|
+
|
|
630
|
+
Resets current session counters (keeps limit and history).
|
|
631
|
+
|
|
632
|
+
### `/grid:budget unlimited` - Remove Limit
|
|
633
|
+
|
|
634
|
+
```bash
|
|
635
|
+
/grid:budget unlimited
|
|
636
|
+
|
|
637
|
+
WARNING: This removes all spending limits.
|
|
638
|
+
Spawns will continue without cost checks.
|
|
639
|
+
Type "confirm" to proceed:
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
---
|
|
643
|
+
|
|
644
|
+
## Future Enhancements
|
|
645
|
+
|
|
646
|
+
### Phase 2: Improved Estimation
|
|
647
|
+
|
|
648
|
+
- **Calibration Learning** - Adjust estimates based on actual patterns
|
|
649
|
+
- **Codebase Complexity Factor** - Larger files = more tokens
|
|
650
|
+
- **Context Carryover** - Track cumulative context growth
|
|
651
|
+
|
|
652
|
+
### Phase 3: Cost Alerts
|
|
653
|
+
|
|
654
|
+
- **Email/Slack notifications** at thresholds
|
|
655
|
+
- **Daily digest** of spending
|
|
656
|
+
- **Anomaly detection** for unusual spending
|
|
657
|
+
|
|
658
|
+
### Phase 4: Team Features
|
|
659
|
+
|
|
660
|
+
- **Per-user budgets** for team accounts
|
|
661
|
+
- **Project-level budgets** across sessions
|
|
662
|
+
- **Cost allocation** by tag/label
|
|
663
|
+
|
|
664
|
+
### Phase 5: Direct API Integration
|
|
665
|
+
|
|
666
|
+
If Anthropic provides usage APIs:
|
|
667
|
+
- **Real metering** instead of estimation
|
|
668
|
+
- **Exact costs** per spawn
|
|
669
|
+
- **Usage dashboards** with API data
|
|
670
|
+
|
|
671
|
+
---
|
|
672
|
+
|
|
673
|
+
## Appendix: Pricing History
|
|
674
|
+
|
|
675
|
+
Track pricing changes to maintain accurate estimates:
|
|
676
|
+
|
|
677
|
+
| Date | Model | Input (per 1M) | Output (per 1M) |
|
|
678
|
+
|------|-------|----------------|-----------------|
|
|
679
|
+
| 2026-01 | Opus 4.5 | $5.00 | $25.00 |
|
|
680
|
+
| 2026-01 | Sonnet 4.5 | $3.00 | $15.00 |
|
|
681
|
+
| 2026-01 | Haiku 4.5 | $1.00 | $5.00 |
|
|
682
|
+
| 2025-11 | Opus 4 | $15.00 | $75.00 |
|
|
683
|
+
| 2025-11 | Sonnet 4 | $3.00 | $15.00 |
|
|
684
|
+
|
|
685
|
+
*Note: Opus 4.5 brought significant cost reduction (~67% vs Opus 4)*
|
|
686
|
+
|
|
687
|
+
---
|
|
688
|
+
|
|
689
|
+
## Appendix: Error Handling
|
|
690
|
+
|
|
691
|
+
### Budget File Corruption
|
|
692
|
+
|
|
693
|
+
If `.grid/budget.json` is corrupted:
|
|
694
|
+
|
|
695
|
+
```python
|
|
696
|
+
def load_budget_config():
|
|
697
|
+
try:
|
|
698
|
+
return json.loads(read('.grid/budget.json'))
|
|
699
|
+
except (FileNotFoundError, json.JSONDecodeError):
|
|
700
|
+
# Return safe defaults
|
|
701
|
+
return {
|
|
702
|
+
'budget_limit': None, # Unlimited
|
|
703
|
+
'enforcement': 'soft',
|
|
704
|
+
'current_session': {
|
|
705
|
+
'estimated_cost': 0,
|
|
706
|
+
'spawns': []
|
|
707
|
+
},
|
|
708
|
+
'history': {
|
|
709
|
+
'total_cost': 0,
|
|
710
|
+
'total_spawns': 0,
|
|
711
|
+
'sessions': []
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
### Session Recovery
|
|
717
|
+
|
|
718
|
+
If session ends unexpectedly:
|
|
719
|
+
|
|
720
|
+
```python
|
|
721
|
+
def recover_session():
|
|
722
|
+
"""Attempt to recover incomplete session data."""
|
|
723
|
+
budget = load_budget_config()
|
|
724
|
+
session = budget.get('current_session', {})
|
|
725
|
+
|
|
726
|
+
if session.get('started') and not session.get('ended'):
|
|
727
|
+
# Mark session as incomplete
|
|
728
|
+
session['ended'] = datetime.now().isoformat()
|
|
729
|
+
session['status'] = 'incomplete'
|
|
730
|
+
|
|
731
|
+
# Archive to history
|
|
732
|
+
budget['history']['sessions'].append({
|
|
733
|
+
'id': session['id'],
|
|
734
|
+
'cluster': session.get('cluster', 'unknown'),
|
|
735
|
+
'spawns': len(session.get('spawns', [])),
|
|
736
|
+
'cost': session.get('estimated_cost', 0),
|
|
737
|
+
'status': 'incomplete'
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
save_budget_config(budget)
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
---
|
|
744
|
+
|
|
745
|
+
*End of Line.*
|