← all blogs

How to cut AI token burn without losing code quality

noisy context clean slice faster + sharper
Every noisy log or filler paragraph stays in history and gets paid for again on every next turn.

The problem: why your quota disappears faster than expected

Most developers assume token burn happens because the model writes too much code. In real coding sessions, generated code is usually the smallest slice of the bill.

What actually eats your token budget is compounding history. Every time you send a new message in a thread, the entire conversation so far gets sent back to the model as input tokens.

Do the quick math on what happens when you paste a 600-line terminal log or read a whole 1,200-line file on turn 2:

  • Turn 2: you pay for those 600 lines once.
  • Turn 3: you pay for those same 600 lines again.
  • Turn 15: you have now paid for that exact same stale log 14 times over, even though you already fixed the bug ten minutes ago.

By turn 20 of a long session, 80 percent of your prompt is often dead weight: old stack traces, full file dumps, and polite three-paragraph summaries explaining what the agent is about to do next.

Why bloated context also makes answers worse

This is not only about cost or hitting rate limits. Models genuinely get sloppier when their context window is crowded.

When a prompt contains five old versions of a file plus three unrelated error logs, the model has to spend attention separating what is still true from what was true fifteen minutes ago. That is when it starts referencing deleted helper names or undoing an earlier fix.

Before: bloated window (hits limits fast) full files + 1,000 log lines bug After: clean slice (faster + sharper) exact slice free headroom for reasoning
Less noise leaves more room for the model to focus on the actual bug.

Four practical habits that fix it

You do not need a complex setup to cut token usage dramatically. Four simple habits handle most of the waste:

1. Search first, read slices second

Instead of telling an agent to read an entire 900-line file just to change one function, search for the symbol name first and read only the 50 to 80 lines around it.

# Wasteful: loads 900 lines into history forever
read src/styles/global.css

# Lean: grep line number first, read only the relevant block
grep ".sticky-note" src/styles/global.css
read src/styles/global.css lines 180-245

You get the exact same accuracy at a fraction of the context size.

2. Trim noisy shell output

Running a test suite or build command often prints hundreds of passing lines before the single failing assertion at the bottom. Whenever possible, run a targeted test file or filter output so only the failure enters context:

# Loud: dumps 400 lines of passing specs
npm test

# Quiet: runs one file and surfaces only what broke
npx vitest run src/utils/price.test.ts --reporter=dot

3. Drop conversational filler

Polite openers (“Sure! I would be happy to help you look into that!”) and long end-of-turn recaps of code you can already see in the diff add up quickly across a full workday. Asking your agent for terse, direct replies keeps code blocks and error messages 100 percent intact while cutting prose output in half.

4. Start fresh between tasks

Treat each chat session like a single git branch. Once a feature is merged or a bug is fixed, open a new session for the next task. If a single task runs past 15 or 20 turns, compact the conversation before starting the next phase.

The takeaway

Think of your context window like a physical desk. When only the two relevant files and the one failing test are sitting in front of the model, replies come back faster, bills drop, and the code gets noticeably cleaner.