A coding agent never tells you the price of the next step. You find out when the session is over.
A single-file bug fix and a four-hour “refactor the whole module” session can look identical in the moment. One costs cents; the other costs more than lunch. Nothing in the interface separates them, because the expensive part is invisible: input tokens you re-send on every turn.
An agent does not remember your code “for free.” Each turn it re-reads the whole conversation — your instructions, the files it opened, the tool outputs it collected, the errors it hit — and bills you for all of it again. Work for an hour and the same context can be paid for dozens of times. This is why “my agent got expensive” almost never means “I wrote expensive prompts.” It is a control-loop problem: nothing is bounding the work.
Here is the arithmetic, the two failure modes that cause most of the blowup, and the controls that keep a session in the range you expect.
The bill is (turns × context) plus waste
Input cost is roughly:
cost ≈ turns × average_context_tokens × price_per_token
Two levers hide in that line. You can shrink the context (read less per turn), or you can stop re-paying for the part that never changes — caching. Most people reach for the first and ignore the second, which is a mistake, because caching attacks the multiplier rather than the base.
Where the tokens actually go
Four things inflate the bill, in roughly this order of magnitude:
1. Re-reading an unchanged prefix. If the beginning of your context is stable — system prompt, the same three files, the same task brief — and the provider supports prompt caching, those tokens can be served from cache at a fraction of the price. But caching only works on a byte-stable prefix. Change one character near the top and the cache misses for everything after it. A lot of “helpful” context tools quietly break this: they “shorten” the conversation in the middle of the prefix, invalidating the cache and replacing a cheap hit with a full-priced miss.
2. Big reads that never get reasoned about. The classic: you ask the agent to map a repo, and it pulls in .git, dist, node_modules, and generated files alongside the source that matters. You paid full price for text that never influenced the answer.
4. Loops and retries. An agent that retries a failing command, or keeps “checking” a condition, will happily do it forty times. Each attempt re-sends the context. Left unbounded, a loop is the single fastest way to turn a cheap task into an expensive one.
Offloading to nowhere
The natural reaction to a full context window is to summarise or delete. Both lose information: the moment the agent needs the dropped detail (the stack trace, the config value, the raw JSON it fetched), it re-fetches it or guesses — and you pay again, with worse answers. “Offload that removes content” and “compaction that shortens the prefix” are two of the most reliable ways to make a session more expensive while feeling tidier.
1. Estimate the bill before you send
You do not need a monitoring dashboard to know the cost of a turn. You need three numbers: the size of the context you are about to send, the price per million input tokens for your model, and how many turns you expect. The arithmetic is trivial:
context : 42,000 tokens
price (input) : $3.00 / 1,000,000 tokens
cost of one turn : 42,000 / 1,000,000 * 3.00 = $0.126
expected turns : 30
--------------------------------------------------
session estimate : 30 * 0.126 = $3.78
Now change one variable. If a naive read doubled the context to 84,000 tokens, that same session is $7.56 — the answer is no better. If half of the prefix is cacheable at a fraction of the price, the estimate drops again. Seeing the number before you run is the difference between choosing your context and discovering its cost afterward.
The discipline is simple: know the size of what you are sending, and the rate at which you send it. Everything else is arithmetic.
2. Make the cache work for you, not against you
Prompt caching rewards exactly one thing: a byte-stable prefix. Same bytes at the front, turn after turn, and the provider can reuse the computation instead of re-doing it. Break the prefix — reorder files, edit an early system instruction, let a “summarize the conversation so far” step rewrite the top — and you pay full price again for everything downstream.
That has a counter-intuitive consequence: the wrong optimization costs more than no optimization.
- Naive compaction that rewrites the early turns looks efficient and quietly guarantees cache misses on every subsequent turn.
- Appending new context to the end, and leaving the front untouched, keeps the prefix stable and keeps the cache warm.
So the rule is: compact the tail, never the stable head. Summarize what just happened; do not rewrite what already happened.
3. Put a gate on the loop
The most expensive failure mode is also the easiest to stop: an agent retrying the same failing step. A budget gate is three lines of policy:
max_turns : 40 # hard stop
max_tokens : 250,000 # session ceiling
on_exceed : STOP and ask the human
When the ledger crosses the ceiling, the loop stops and hands control back. No “one more attempt.” No silent escalation. The point is not to be stingy — it is to turn an open-ended session into a bounded one, so the worst case is a known number instead of a surprise.
4. Offload, don’t drop
When a session fills with tool output — a 3,000-line log, a JSON dump, a stack trace — the right move is not to delete it or summarize it away. It is to move it out of the context and onto disk, and leave behind a one-line pointer:
{"ts":"2026-10-17T09:14:02Z","path":"./artifacts/build.log","tokens_offloaded":3120,"ref":"build.log#1247-1602"}
The agent gets a cheap handle (build.log#1247-1602) and can re-open exactly the slice it needs, later, if it needs it. You paid to move 3,120 tokens out of the re-sent prefix, and you kept the data. Delete it and you pay to rediscover it.
The four moves
Put together, capping a session is not a trick — it is four small policies:
- Estimate the session’s bill before you run it (size × rate × turns).
- Keep the prefix byte-stable and compact only the tail, so the cache stays warm.
- Gate the loop with a hard turn/token ceiling that stops and asks.
- Offload to disk with a pointer, instead of dropping context you will need again.
None of these require a smarter model. They require treating the session as something with a budget.
The packaged version
I got tired of reassembling these four guards by hand on every long run, so I packaged them as Token-Guard — a caching-and-autonomy optimizer for Claude Code, Cursor, Gemini CLI, Codex, and any SKILL.md-compatible agent. It ships three cooperating sub-skills (compaction/, offload/, loop-guard/), emits structured manifests and a token ledger, and makes the human-in-the-loop checkpoint mandatory before anything leaves the context. It is fully local: no network calls, no shell execution, and the only optional helper script is one you choose to run.
If the cost is coming from what you read rather than how many turns you take, the free SKILL.md validator and the related tools below plug the rest of the leak:
- lean-context — AST-aware compression: keep the interfaces, drop the bodies (Python).
- Context Offload Hint — a FITS / TRIM / OFFLOAD verdict before you paste 40k tokens.
- SKILL.md Snippet Generator, Markdown Link Checker, YAML Frontmatter Fixer, Git Changed-Scope Lister — the $5 micro-skills that keep a skill healthy between runs.
All one-time purchases. 30-day refund, no questions. AI assistance was used in authoring this article and in the tools it describes.