Spaces:
Sleeping
Sleeping
File size: 5,057 Bytes
cc38116 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | # Token Optimization Guide
> Practical strategies for reducing token consumption while maintaining quality.
---
## Why Token Optimization Matters
| Issue | Impact |
|-------|--------|
| Excessive file loading | Higher costs, slower responses |
| Context accumulation | Quality degradation after 50% |
| Re-reading files | Wasted tokens on understood content |
| Full files when snippets suffice | 10x token usage |
**Goal:** Maximize output quality per token spent.
---
## The Token Efficiency Stack
```
βββββββββββββββββββββββββββββββββββββββ
β 1. Search-First (context-fetch) β β Find before loading
βββββββββββββββββββββββββββββββββββββββ€
β 2. Budget Tracking (token-budget) β β Know your limits
βββββββββββββββββββββββββββββββββββββββ€
β 3. Compression (context-compressor)β β Minimize footprint
βββββββββββββββββββββββββββββββββββββββ€
β 4. Health Monitoring β β Prevent degradation
βββββββββββββββββββββββββββββββββββββββ
```
---
## Quick Reference: Token Costs
### By Content Type
| Type | Tokens/Line | 100 Lines = |
|------|-------------|-------------|
| Dense code | 6 | ~600 tokens |
| Standard code | 4 | ~400 tokens |
| Markdown | 3 | ~300 tokens |
| Sparse YAML | 2 | ~200 tokens |
### By File Size
| File Lines | Strategy |
|------------|----------|
| <50 | Load freely |
| 50-200 | Outline first |
| 200-500 | Search + snippets |
| 500+ | Never load fully |
---
## Optimization Patterns
### Pattern 1: Search β Outline β Target
```
Step 1: Search for "handlePayment"
β Found in: payment.ts:45, checkout.ts:120
Step 2: Get outline of payment.ts
β L45-80: handlePayment function
Step 3: Load only L45-80
β 35 lines (~140 tokens) vs 400 lines (~1600 tokens)
β Saved: ~90%
```
### Pattern 2: Summarize After Understanding
```
After reading auth.ts:
## Summary: auth.ts
- Exports: login, logout, validateToken
- Pattern: Express middleware
- DB: queries users table
- JWT: uses jose library
Next time: Reference summary, don't reload
```
### Pattern 3: Progressive Disclosure
```
Need: Understand login flow
Level 1: Outline
β "login at L45, uses validateCredentials at L67"
β Often sufficient
Level 2: Key function
β Load L45-65 only
β Understand core logic
Level 3: Dependencies
β Load validateCredentials (L67-85)
β Only if L2 insufficient
Level 4: Full file
β Last resort, re-compress after
```
---
## Anti-Patterns to Avoid
### β The "Context Dump"
```
BAD:
"Let me read all the files in src/ to understand the project"
β 50 files Γ 200 lines Γ 4 tokens = 40,000 tokens
GOOD:
"Let me search for 'main entry' and 'router'"
β 2 targeted searches, ~500 tokens
```
### β The "Just In Case" Load
```
BAD:
"Loading utils.ts in case I need it later"
β Probably won't need it, wasted tokens
GOOD:
"Noting utils.ts exists, will load if needed"
β Zero tokens until actually needed
```
### β The Re-Read
```
BAD:
"Reading config.ts again to check the port"
β Already read it twice = 1200 tokens
GOOD:
"From my earlier analysis, port is on L15"
β Zero additional tokens
```
---
## Budget Checkpoints
### Before Starting Work
```markdown
β‘ Do I know my current budget usage?
β‘ Have I tried searching before loading?
β‘ Am I loading files I've already understood?
```
### During Execution
```markdown
β‘ Am I at >50%? Time to compress.
β‘ Am I re-reading files? Use summaries.
β‘ Can I use outline instead of full file?
```
### After Each Wave
```markdown
β‘ Have I compressed context for next wave?
β‘ Are summaries documented in STATE.md?
β‘ Would a fresh session be more efficient?
```
---
## Integration with GSD
| GSD Workflow | Token Optimization |
|--------------|-------------------|
| `/map` | Generate outline, not full read |
| `/plan` | Budget estimate per task |
| `/execute` | Load minimal per task |
| `/verify` | Targeted evidence only |
| `/pause` | Compress and dump state |
---
## Metrics
Track these for improvement:
| Metric | Good | Poor |
|--------|------|------|
| Files fully loaded | <3 per wave | 10+ |
| Search:Load ratio | 3:1 | 1:3 |
| Re-reads | 0 | 3+ |
| Budget at wave end | <50% | >70% |
---
*See also:*
- *[.agents/skills/token-budget/SKILL.md](.agents/skills/token-budget/SKILL.md)*
- *[.agents/skills/context-compressor/SKILL.md](.agents/skills/context-compressor/SKILL.md)*
- *[PROJECT_RULES.md](PROJECT_RULES.md) β Token Efficiency Rules*
|