Token Engineering · ~8 min
The cheapest token is the one you never generate. Cut waste at three sources — tool output, generated code, and the codebase the agent reads.
Right Model cuts the price per token. Right Token cuts the count. They multiply: a cheaper model that also handles half the tokens is a 4× win. The leverage lives in three places — what tools return, what the agent writes, and what it has to read.
Every tool call's output enters the context window. A tool that returns a 10,000-token API response when 200 tokens would do consumes 10% of a 100k window on a single call. The fix is one question: what does the agent need to know to decide its next action? Return that, and nothing else.
"3 checks passed, 1 failed: lint" — about 20 tokens —
not the full Actions run object with 40+ fields at ~400 tokens. Same decision, 20× fewer tokens, and no parsing for
the agent.The mechanism that punishes bloat is attention dilution. Self-attention scores every token against every other, so noise competes with signal for the model's focus. Liu et al. (2023) found accuracy drops sharply when the relevant document sits in the middle of a long context — the "lost in the middle" effect. Oversized output doesn't just cost money; it buries the field that matters.
Two more levers compound the savings:
A summary that drops "unimportant" fields will eventually drop the one a rare-but-valid path needs — and the agent cannot ask for data it doesn't know exists. A useful heuristic: tool output should fit in a paragraph. If it can't, filter, structure it carefully, or write it to a file — but validate that you haven't silently amputated an edge case.
The obvious move — telling the agent "write concise code" — is the wrong one. It creates a competing objective: the agent does less work, not better work. Cursor reported that a token-preservation instruction made GPT-5-Codex refuse a task: "I'm not supposed to waste tokens, and I don't think it's worth continuing." Prompt-level conciseness is fragile.
These are ordinary Python idioms (PEP 8) applied systematically: a comprehension instead of a loop-plus-append, an
f-string instead of concatenation, dict.get(k, default) instead of an if key in dict check.
And the way to get them is to show, not tell — agents pattern-match from examples far more reliably
than they follow abstract directives.
The win compounds: every time generated code re-enters the context window on a later turn, the shorter version costs less again. The saving repeats for the life of the session.
ShortCoder found that reductions past ~30% correlated with an 18.7% drop in unit test pass rates in DeepSeek Code experiments — aggressive compression collapses multi-step logic into single expressions that fail edge cases. Frontier models already write idiomatically, so measure the real saving before bolting on transforms, and always validate against a test suite.
The third source is the codebase itself. In a controlled minimal-pair study, Trivedi & Schmitt (SonarSource, 2026) built six pairs of Java repos — identical architecture and behavior, differing only on rule violations and cognitive complexity — and ran Claude Code across 660 trials, cleaning messy code and degrading clean code in both directions.
| Metric | Clean vs messy |
|---|---|
| Task pass rate | Statistically indistinguishable |
| Tokens per task | 7–8% lower on cleaner code |
| File revisitations | 34% lower on cleaner code |
7–8% on a $1,200/month spend is ~$1,080/year — an 80-hour refactoring sprint pays back in ~11 years. Worse, agents re-introduce mess: a longitudinal study of 806 Cursor-adopting repos (He et al., MSR 2026) found warnings rise ~30% and complexity ~42% after adoption. Capture the saving from maintainability work you'd do anyway — linters, complexity budgets, CI quality gates — not a sprint aimed at the agent.
Retrieval practice — recall, don't peek
Question 1The right rule for sizing a tool's output is to return…
Question 2Adding "write concise code" to the agent prompt tends to…
Question 3In the minimal-pair study, cleaner code left pass rate unchanged but cut…
Question 4Oversized tool output hurts quality, not just cost, because of…
Question 5 · spaced recall from an earlier lessonCost-aware routing says default to…