Drag to rotate the repo graph · orange = files touched during discovery · green = the actual target
What’s actually happening
The discovery loop
A typical agent turn: glob **/*.ts (file list into context) → grep "discount" (matches into context) → read pricing.ts (whole file into context) → wrong file, repeat. Every step pours tokens into the context window before any real reasoning begins.
Why it's expensive
Reading one 400-line file costs roughly 4,000–6,000 tokens. Five exploratory reads plus greps can consume 25,000+ tokens — and that context persists, crowding out room for the actual edit and raising per-turn cost on every subsequent call.
Fix 1: Code maps / indexes
Pre-build a compact map: file paths, exported symbols, one-line summaries, dependency edges. The agent reads a 1,500-token map, jumps straight to the right file, and reads only that. Discovery collapses from a loop to a lookup.
Fix 2: Retrieval & subagents
Alternatives: embedding search over symbols (return top-5 candidates, not full files); or delegating exploration to a cheap subagent that returns a one-paragraph answer instead of raw file dumps — the parent keeps the conclusion, not the noise.
Fix 3: Context hygiene
Truncate tool outputs aggressively, read line-ranges instead of whole files, and summarize-then-drop exploration results. The goal: tokens spent should scale with the edit, not with the size of the repo.
Worked numbers
- Grep-hunt on a 120-file repo: ~9 tool calls, ~28,000 tokens ≈ $0.084 per task at $3/M.
- Code-map lookup: ~3 calls, ~7,500 tokens ≈ $0.023.
- At 500 agent tasks/day, that's ~$30/day vs ~$11/day — before counting the quality gain from a cleaner context.