For one-off shell/python that searches, counts, filters, or aggregates data, the token bill is dominated by what the command prints back into context — not the code. Optimize output first, data flow second, code last.
The one number that matters
Same answer — "count the 404s in a 60k-line log" — measured as tokens returned to context:
| command | tokens into context |
|---|
| ❌ | grep ' 404 ' log | 137,136 |
| ✅ | grep -c ' 404 ' log | 3 |
Every token a command prints is injected into context and re-billed every turn it lingers in an agent loop. Controlling output is the whole game.
Tool selection order (decide this first)
- Native Claude Code tool if one fits — Grep (content search), Glob (find files), Read, Edit, Write. Optimized, output-capped, and cross-platform (they work on native Windows without Git Bash, where shell
grep/find don't exist). Don't shell out to grep/find/cat/sed when a native tool covers the job.
- Bash for what they don't do — aggregation & transforms:
awk, jq, counts, sums, group-by, multi-stage pipelines.
- Python only when a one-liner can't do it cleanly.
Then apply the three tiers below to whatever you write.
How it works — three tiers, by impact
| Tier | Lever | Billed as | Measured |
|---|
| 1 | Control output — return the answer, cap rows, project fields, spill big results to a file | input tokens | up to ~100% |
| 2 | Push filters early & use fast tools — predicate pushdown, rg/fd, filter inside jq | runtime | 1.5–2.5× |
| 3 | Trim the code — no argparse/main()/docstrings/try-except; shell over python | output tokens | ~25–40% |
…all gated by correctness guardrails: wc -l counts newlines not lines; cat glues newline-less file boundaries; sort before uniq; ties ≠ bug; judge equivalence on the data, not the formatting.
Benchmarks
All optimized commands were verified to return the same answer as their baseline. Token counts use cl100k (tiktoken) as a proxy — the percentages are the robust part; absolute dollars are ±20%.
Output-token reduction across 6 tasks (99.5% combined)
| Task | Technique | Output naive | Output opt | Saved | Speedup |
|---|
| Count 404s | dump matches → return the count | 137,136 | 3 | 100% | 1.1× |
| Unique IPs w/ 500 | sort-all → filter-then-sort | 4 | 4 | — | 1.7× |
| Largest files | dump all → head -10 | 16,181 | 515 | 97% | 1.0× |
| Return 100 records | pretty JSON → TSV rows | 2,402 | 500 | 79% | 1.0× |
| Return 100 records | pretty JSON → compact JSON | 2,402 | 1,202 | 50% | 1.0× |
| Category summary | dump rows → aggregate to 2 lines | 299,500 | 22 | 100% | 0.9× |
| Total | | 457,625 | 2,246 | 99.5% | 1.2× |
Refinement levers (spill-to-file, projection, tool choice)
| # | Technique | Metric | Result | Verdict |
|---|
| 1 | Spill big result to file + print summary | output tokens | 137,136 → 84 | ✅ Huge (100%) |
| 3 | Return only needed fields (ids vs whole objects) | output tokens | 2,402 → 400 | ✅ Strong (83%) |
| 4 | rg instead of grep | runtime | 2.5× faster | ✅ Strong (if rg installed) |
| 5 | Pushdown into jq (vs dump|grep|wc) | runtime | 1.5× faster | ✅ Good |
| 6 | Project + filter early, drop useless cat/stages | runtime | 1.2× faster | ✅ Modest but free |
| 2 | Early head short-circuit | runtime | 1.1× | ⚠️ Marginal at small scale |
Independent verification run — 10 experiments