OK.
All posts

July 18, 2026

Where Your AI Coding Agent's Tokens Actually Go

AI AgentsLLMDeveloper Tools

If you’ve used an AI coding agent like Claude Code, Codex, Cursor, Gemini CLI , you’ve probably had this moment: you ask it to fix a bug, it works for a while, it ‘fails’ and your usage bill goes up anyway.

Wait. It failed. Why did I pay?

That question sent me down a rabbit hole, and what I found changed how I use these tools every day. This post is the map: what your agent is actually doing with your tokens, why failure isn’t free, where the hidden leaks are — and seven practical habits that plug them.

No prior knowledge needed. If you know what a chatbot is, you’re ready.

First: what is your agent actually doing in there?

When you type “fix the login bug”, the agent doesn’t just answer. It runs a loop:

The Agent Loop

Figure 1: The Agent Loop.

The AI is consulted — many times per task — , not once. Every consultation costs tokens.

That’s the single most important thing to understand, and everything below follows from it.

What costs tokens — and what doesn’t

Figure 2: Free vs Billed Split. [Your Machine(free) , AI(billed)]

Here’s the part that surprises most people. The ‘local work’ is free:

  • Searching your files? Free. That’s your own CPU, like Ctrl+F.

  • Running your tests, compiling, git commands? Free. Ordinary programs.

But the moment the results of that free work are sent back into the AI — the search hits, the file contents, the error output — they become part of what the AI reads, and reading is exactly what tokens measure. A 500-line file costs nothing to open, and real money to show to the AI.

So your bill isn’t driven by how big your project is. It’s driven by “how much the agent reads, and how many times it has to think.”

Why a failed task still costs money

Here’s the mental model that fixes the “why did I pay for failure?” frustration:

You’re not paying for success. You’re paying for computation.

By the time your agent gave up, it had already searched your codebase, read a dozen files, planned an approach, written code, run tests, and read the failures. All of that was real work by the AI — thousands of tokens of reading and thinking. The provider bills the work, not the outcome. Like a plumber who spends two hours diagnosing before declaring the part unfixable: you still owe for the two hours.

Annoying? Sure. But once you accept it, the goal becomes obvious: make the work smaller, so both successes and failures cost less.

Fig : Dead Weight , Compaction , Amnesia

Beyond the visible loop, there are three subtler ways tokens drain. These are the ones almost nobody talks about.

Leak 1: Dead weight

Everything the agent reads stays in its working memory (the ‘context’) for the rest of the session — including all the material from failed attempts. And here’s the kicker: with every new step, the entire conversation so far is re-sent to the AI. Providers soften this with caching (re-sent history is billed at a steep discount), but it’s not free. Those five irrelevant files from attempt #1? You pay a small tax on them for the rest of the session. Dead weight in the truck, billed by the mile.

Leak 2: The compaction double-payment

Working memory has a size limit. When a long session fills it up, the agent summarizes its own history to make room — and details get squeezed out. If it later needs something that got summarized away, it re-reads the file. You’ve now paid for the same knowledge twice. Long, meandering sessions make this worse.

Leak 3: Amnesia between sessions

Close the session, and everything the agent learned about your project is gone.

Tomorrow it starts from zero and re-explores — re-paying the entire orientation cost.

Join The Writer's Circle event Every session. Forever. Unless you do something about it (you can — trick #3).


1. Point, don’t make it hunt

The single biggest saver. You know things the agent doesn’t — use them.

“There’s a login bug, fix it.” → Broad search, five candidate files opened, thousands of tokens spent orienting.

“Login fails after a timeout. The retry logic is in src/auth/login.ts. Here's the error: [paste]." → Opens one file. Starts working.

2. Paste the evidence

Error messages, stack traces, failing test output — paste them. A pasted error costs ~50 tokens. Letting the agent reproduce and diagnose it from scratch costs thousands.

❌ ”The app crashes when I upload a file, can you look into it?”

→ The agent has to find the upload code, run the app or tests, trigger the crash itself, and read the output — all billed.

✅ ”Uploading crashes with this — TypeError: Cannot read properties of undefined (reading ‘size’) at validateUpload (upload.ts:42)

→ The agent jumps straight to line 42 of upload.ts. The diagnosis phase you’d have paid for? You just did it for free by copy-pasting.

3. Give your repo a briefing file

Most agents automatically read a briefing file from your project root — CLAUDE.md for Claude Code, AGENTS.md for Codex — and most have a command (like /init) that generates one for you by studying the repo. Do it once. From then on, every session starts pre-briefed instead of re-exploring. This is the direct cure for Leak 3, and it’s the best ten minutes you’ll spend on your setup.

A briefing file doesn’t need to be fancy. Even this saves real money:

# MyShop — e-commerce site
- Frontend: React in `client/`, backend: Express API in `server/`
- Auth lives in `server/auth/` (JWT, refresh tokens in Redis)
- Run tests with `npm test`; DO NOT touch `legacy/` — it's scheduled for deletion
- Gotcha: the payment tests need the Stripe mock running (`npm run mock`)

Five lines — and the agent no longer spends its first two thousand tokens discovering any of it, in every single session.

4. Reset between tasks

Finished a feature and moving to something unrelated? Clear the session (/clear in Claude Code). Otherwise every file from the old task rides along as dead weight, billed with every message of the new one. One task, one session.

Concrete scenario: you spend an hour building a PDF-export feature — the agent has read fifteen files about PDFs, styling, and fonts. Then, same session, you ask “why is the login page slow?” Every one of those fifteen PDF files is still in its working memory, re-sent (at the discounted-but-not-free cache rate) with every message of the login investigation. They will never be useful again. /clear first, and the login task starts at a fraction of the cost.

(The reverse matters too: don’t clear mid-task — re-explaining costs more than continuing.)

5. Fence the work area

Add the export button — only touch the reports/ folder, don't modify the API layer." One sentence, and the agent never wanders into (or reads) the parts of your codebase that were never relevant.

6. Plan first, build second

For anything big, ask for a plan before allowing code changes (Claude Code has a dedicated plan mode). The most expensive token event that exists is a wrong implementation that gets thrown away and redone. A two-minute “here’s my plan — approve?” checkpoint is the cheapest insurance you can buy.

Example: “I want to add dark mode. Before writing any code, give me your plan: which files you’ll touch and how the theme will be stored.” The agent replies with a plan; you spot that it wants to hardcode colors in 30 components instead of using CSS variables; you correct course in one sentence. Without the checkpoint, you’d have paid for the 30-component version, the “actually, please use CSS variables” rework, and all the reading in between.

7. Make it take notes outside its own head

The pro move. Tell the agent: “as you investigate, record what you learn in notes.md." A file on disk survives both compaction (Leak 2) and the end of the session (Leak 3). The next session reads one compact file instead of re-discovering everything. You've given your amnesiac genius a diary.

What ends up in the diary is gold precisely because it was expensive to learn:

## Investigating the double-charge bug — findings so far
- Payments flow: checkout.tsPaymentService.charge() → Stripe SDK
- The retry wrapper in api/retry.ts does NOT check idempotency — suspicious
- Ruled out: webhook duplication (logs show single delivery)
- Next: check if charge() is called from both checkout AND order-confirm

Tomorrow’s session reads those four lines and continues exactly where this one stopped — instead of re-paying for the whole investigation that produced them.

The one-line summary

Tokens are spent on reading, thinking, and redoing. So: brief it (briefing file), aim it (name files, paste errors), fence it (scope), checkpoint it (plan first), and reset it between jobs. Treat your agent like a brilliant contractor who bills by the minute — the better your work order, the smaller the invoice.