/token-efficient-scripts:token-efficient-scriptsfrom Token-Efficient Scripts
Writes disposable bash/Python commands for one-off searching, counting, filtering, or aggregation over files, logs, CSV, or JSON. Prioritizes minimal output tokens and recovers from failed shell invocations cheaply.
Ships in Token-Efficient Scripts. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
For agent-written one-off scripts, context use is often dominated by output tokens (what the command returns to the harness), not the code. Optimize in this order. A 10-task validation found the same answer in every experiment, with a 90.4% mean output reduction across seven output-focused tasks. See references/benchmark-findings.md when evaluating the evidence or quoting results.
grep/find don't exist. Do not shell out to grep/find/cat/sed when a native tool covers the job.awk, jq, multi-stage pipelines.Then apply the tiers below to whatever you write.
Treat every printed token as context cost. Harnesses may truncate, compact, or cache tool output differently, but unbounded output can still dwarf a short command.
grep -c/rg -c for matching-line counts; use wc -l, uniq -c, or awk sums/counts to aggregate instead of dumping rows.| head -N, SQL LIMIT, jq '.[:N]'. Never let a listing run unbounded.jq -r '.[].id' not jq '.'; awk '{print $1}' not the whole line. (~83% on record dumps.)jq -c or jq -r '…|@tsv' instead of jq '.'. Complete compact JSON saved 40.64% in the validation; savings depend on structure.cmd > /tmp/out.txt; echo "$(wc -l </tmp/out.txt) rows -> /tmp/out.txt"; head -3 /tmp/out.txt (137k tokens → 84.)awk '$4==500{print $1}' log | sort -u beats sort log | awk … — the filter shrinks what later stages process. This also fixes slow sort-heavy pipelines.jq 'select(...)' beats jq '.[]' | grep. (1.5×.)rg/fd when installed, but treat speed as workload-dependent; fall back to grep/find if absent. In the validation workload, rg -c was 6.99× faster than grep -c, not a universal ratio.find … | head stops early. Worth it only when the source is large or each item is expensive; negligible on small trees.cat and redundant pipe stages.awk, jq, sort, uniq, wc, counts, sums, group-by. (Content search and file-finding go to the native Grep/Glob tools, not shell grep/find — see Tool selection order above.) Fall back to Python only when a one-liner can't do it cleanly: complex/quoted-CSV parsing, multi-key grouping, or cross-platform logic.argparse (use sys.argv), no main()/if __name__, no try/except (a traceback is acceptable for a disposable script). Add any of these back only if the script will live on or if error handling actually protects correctness or prevents a flood of failure output.count and c both cost 1 token; keep normal spacing. Golfing saves characters, not tokens, and hurts correctness review.awk command used 40% fewer command tokens but ran 3.29× slower than its Python baseline.Tests, builds, git inspection and archive extraction print a running log; you need did it pass, what broke, or which files. Most tools have a quiet form that returns just that. Use it when it answers the question you are asking.
pytest -q, not -v (-v was 11× plain pytest). Only need pass/fail? pytest -q >/tmp/t.log 2>&1 && echo ok || tail -5 /tmp/t.log. Test runners print their summary last. Warnings can be most of the output: add --disable-warnings when they are not the question.2>&1 | grep -m5 'error:', not | head or | tail. Rust: cargo build -q --message-format=short.git log --oneline -N. Which files changed: git show --name-only --format= <rev>, git diff --name-only A B (--stat adds line counts). Only when the question is which files, not what changed.v: tar -xf, unzip -q. Success is the exit status; if the harness does not show it, append ; echo rc=$?.A failed command is a fork in the road, and the default reflex — read the man page — is the worst option measured: ~3,290 tokens per lookup, and it still cannot answer when the flag does not exist on this platform. Escalate cheapest-first and stop at the first hit.
cmd --help (~58 tokens; solved 4/8). On BSD it usually exits with the same usage block,
which is exactly what you want.man cmd | col -b | grep -nE -m3 -B2 -A3 '<semantic phrase>'
depth, size of file, in-place, null.-B2 matters — flag names sit above their prose, so an -A-only window describes the
behaviour without ever naming the flag.-m3, not | head -N — a head cap gets eaten by early false-positive matches before
it reaches the definition.WebSearch) — but only for what local docs
structurally cannot say: "this flag/tool does not exist on this platform, use X instead."
Search-first scores worse than a targeted grep (1,678 vs 145 tokens per answer), and
community cheatsheets are GNU-centric enough to hand you back the invocation that just failed.Never run man cmd unpiped. Full pages measured 1,462–6,385 tokens each; the tight grep
returned the same 6/8 answers for 1/30th of the tokens (870 vs 26,316).
This ladder buys tokens and answer rate, not speed — every ladder measured within a second of the others, and it can add up to 2 tool round trips. See references/cli-failure-recovery.md.
This skill is designed to learn from its own use. When a run produces a measurement or a failure mode not already recorded:
references/benchmark-findings.md (helper: python3 references/log-run.py "<title>" < body.md). Installed as a plugin: the shipped file is a read-only cache, so runtime findings go to ${CLAUDE_PLUGIN_DATA}/findings-log.md (the /bench command does this for you). Record the environment (machine, tools present, tokenizer used) and whether the optimized command gave the same answer.wc -l counts newlines, not lines — off by one per file lacking a trailing newline.cat file1 file2 glues the last token of one file to the first of the next when files lack a trailing newline. Process files separately (awk over the file list, grep -h) when boundaries matter.sort before uniq — uniq only dedupes adjacent lines.head/tail cut can drop the answer from diagnostic output. On a C++ template error, | head -20 kept one of two root causes and | tail -20 kept neither; grep -m5 'error:' kept both. Cut by content, not by line count.man timeout on macOS returns ncurses curs_inopts(3X) — Xcode's SDK ships a timeout(3x) C-function page, and man resolves the name to it even though no timeout(1) command exists. The page is correct and irrelevant. Check its header names your command before trusting it.date -d, stat -c, du --max-depth, grep -P, xargs -d, sed -i (bare), find -printf and timeout all fail on macOS. Use the platform table in the cheat sheet.Ten deterministic throwaway tasks were tested on Darwin arm64. Every optimized command produced the same task answer as its baseline.
Seven output-focused tasks averaged 90.4% fewer output tokens; the median reduction was 99.97%.
Answer/preview/spill tasks, excluding a case that required the complete JSON value, produced a 99.16% combined reduction.
Compact JSON saved 40.64%, below the earlier 50–79% estimate.
Filter-before-sort ran 1.68× faster; rg -c ran 6.99× faster than grep -c on the tested workload.
Spilling complete results to a file saved 99.97% of returned tokens but ran 1.64× slower.
Shortening a Python command to awk cut command tokens 40% but ran 3.29× slower.
Recovering from a failed command: the full-man-page reflex cost 4,386 tokens per answer and solved only 6/8; an error -> --help -> tight-man|grep -> web ladder solved 8/8 for 3,166 tokens (8.3x fewer). It saves tokens and answer rate, not wall clock. See references/cli-failure-recovery.md.
Commands run only for a verdict (tests, builds, git, archives): the quiet form returned 97.1% fewer tokens across ten scenarios (31,031 → 893), kept every answer in two runs, and ran no faster. See references/verification-output.md.
Interpret token counts as directional: the benchmark used cl100k_base for continuity with the original measurements, not the private tokenizer or billing behavior of every current agent harness. Do not turn these measurements into a universal monthly cost claim without a representative workload, current pricing, and harness-specific accounting. Read references/benchmark-findings.md for the full experiment table, methodology, and limitations.
For plain content search or file-finding, prefer the native Grep/Glob tools; the rows below are for the data work that has no native tool.
| Task | Instead of Python | Use |
|---|---|---|
| Count matching lines | print matches | grep -c / rg -c |
| Count files recursively | os.walk | find root -type f | wc -l |
| Sum/avg a simple unquoted CSV column | csv module | awk -F, '{s+=$2}END{print s}' |
| Group-by aggregate | defaultdict | awk '{s[$k]+=$v}END{...}' |
| Filter JSON array | json.load+loop | jq '.[] | select(...)' |
| Top-N frequency | Counter (small) | sort|uniq -c|sort -rn|head |
| Files by mtime/size | os.walk+stat | find -mtime/-size |
Use these directly instead of paying for a lookup. Every replacement is verified to run
correctly on macOS except gtimeout, which needs brew install coreutils and is untested
here — so the perl fallback leads that row.
| Failed with (GNU) | On BSD/macOS use |
|---|---|
date -d '2 days ago' | date -v-2d |
stat -c %s f | stat -f %z f |
du --max-depth=N | du -d N |
grep -P '\d+' | grep -E '[0-9]+' |
xargs -d, | tr ',' '\0' | xargs -0 |
sed -i s/a/b/ f | sed -i '' s/a/b/ f |
find … -printf '%f\n' | find … -mindepth 1 -exec basename {} ';' (-mindepth 1 drops the start dir, which basename would otherwise emit) |
timeout N cmd | perl -e 'alarm N; exec @ARGV' cmd (verified; nothing to install), or gtimeout after brew install coreutils (untested here) |
Prior art: this generalizes Infracost's "predicate pushdown" (push filtering close to the data) to script authoring, adding the output-frugality and correctness dimensions.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub macetenth/token-efficient-scripts --plugin token-efficient-scriptsOther plugins' skills for the same job