Skip to content
mentoor

Topic 4 of 5 · 5 min read

Cutting token waste in long agent sessions

Goal: Find the habits in an agent transcript that waste tokens and propose changes that keep the result but cut the cost.

Every token in the context is resent on every later turn, so the cheapest token is the one that never entered the conversation. Waste in agent sessions is rarely one big mistake. It is a handful of habits repeated many times: a huge file read for one function, a full log printed for one error, and a session that carries yesterday's task into today's.

Check yourself · 1 of 3

When should you start a fresh agent session?

Choose one answer

Why it matters

Context also hurts quality. A window stuffed with old output makes the model slower to find the relevant part and more likely to drop an instruction. Cutting waste usually makes the agent cheaper and better at once.

How to cut it

  1. Start fresh between unrelated tasks. Use the agent's clear or new-session command (in Claude Code, /clear) when the topic changes. Old context is paid for on every turn and helps nothing.
  2. Compact when a task is long. A summary of what matters replaces thousands of tokens of back and forth. Do it at a natural break, before the window fills.
  3. Trim tool output at the source. Run the one failing test, not the suite. Use git log -n 10, not the whole history. Pipe long commands through head, tail or grep before the agent sees them.
  4. Point to the file and the part. Say "the refreshToken function in src/auth/session.ts" so the agent reads that range, not the whole directory.
  5. Load fewer tools. Each connected tool server adds its definitions to every request. Disable servers you do not use in this task.
  6. Keep memory files short. An AGENTS.md or CLAUDE.md is sent every turn. Move rarely needed detail into files the agent reads on demand.
  7. Stop retry loops. After two failed attempts at the same fix, ask the agent to explain what it knows and to propose a different approach.

Before and after

> run the tests
[4,200 lines of passing output]
> read src/
[reads 38 files, 90k tokens]
> fix the date bug

Better:

> run only tests/date.test.ts and show failures only
[14 lines]
> read formatDate in src/lib/date.ts
[40 lines]
> fix the date bug

Same fix. The second transcript carries a small fraction of the context into every later turn.

What goes wrong

  • Cutting too hard. Hiding the failure output the agent needs makes it guess. Trim the noise, keep the error.
  • Compacting mid-thought. Summarizing in the middle of a debugging chain can lose the detail you need next.
  • Never clearing. One long session for a week turns into the most expensive way to work.

Examples

Three changes from one transcript

A review of a 90-minute session found three patterns:

  • The full suite ran six times, each printing about 4,000 lines. Change: run the single affected test file, show failures only.
  • Five tool servers were connected, but only one was used. Change: disable the other four for this task.
  • The session began on an invoice bug and ended on an unrelated CSS fix. Change: clear between the two.

The code changes were identical. Each later turn carried far less context.

Key points

  • Start a fresh session when the task changes, because old context is resent and billed on every turn.
  • Trim large tool output at the source: run one test file, cap logs, and filter commands before the agent sees them.
  • Name the file and function so the agent reads a small range instead of exploring a whole directory.
  • Always-loaded items such as tool servers and long memory files cost tokens on every request, so keep them lean.
  • Stop after repeated failed retries and change approach instead of letting the agent loop.

Common mistakes

  • Letting one session run across unrelated tasks for days, so every turn pays for stale history.
  • Printing whole logs or test suites into the conversation when only the failing lines matter.
  • Cutting so much output that the agent no longer sees the real error and starts guessing.

Check yourself · 2 of 3

An agent runs a full test suite that prints 4,000 lines, when one test file fails. Best fix?

Choose one answer

Check yourself · 3 of 3

Why do connected tool servers add cost even when a task never uses them?

Choose one answer

Practice items, not counted towards your mastery.

Start here