hale-api / docs /token-optimization-guide.md
Raunak211006's picture
Deploy HALE API v3.1
cc38116 verified
|
Raw
History Blame Contribute Delete
5.06 kB

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

β–‘ Do I know my current budget usage?
β–‘ Have I tried searching before loading?
β–‘ Am I loading files I've already understood?

During Execution

β–‘ 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

β–‘ 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: