My AI coding bill is much higher than expected
Any AI coding tool Last checked 2026-10-14
Almost always input tokens, not output. Every turn of a conversation resends the entire history, so cost grows with the square of the conversation length, and agents that read large files re-send those files on every subsequent step. Start new sessions often, enable prompt caching, and check usage per request before changing model.
What you saw
A bill several times what you estimated from the published per-token price, with no change in how much you feel you used it.
Or a single long agent session costing more than a whole day of short ones.
What it actually means
The arithmetic that surprises people:
- History is resent every turn. Turn 20 pays for turns 1–19 again. A conversation of n turns costs roughly n²/2 in input tokens, not n.
- Files are expensive and sticky. An agent that reads a 2,000-line file re-sends it on every subsequent step of that session.
- Reasoning tokens are billed. On models with extended thinking, the thinking is output you pay for even though you never see most of it.
- Failed and retried calls still bill. A retry loop costs full price every attempt.
- Input and output are priced differently, often by 3–5×, so a chatty agent with short answers can still be dominated by input cost.
The cost driver is session length and file size, far more than which model you chose.
The fix
Find out where it actually went before changing anything. Every response carries usage; log it:
usage: { input_tokens, output_tokens,
cache_creation_input_tokens,
cache_read_input_tokens }
Start a new session per task. This is the single biggest lever — it resets the quadratic growth to zero.
Turn on prompt caching for the stable prefix (system prompt, tool definitions, the large file you keep re-reading). Cache reads are billed at a large discount versus fresh input.
Stop attaching whole files. Ask for the relevant function or a grep result rather than the file:
Read only the function I named. Do not read the whole file.
Cap retries and cap max_tokens. An uncapped retry loop overnight is the classic way to produce a shocking bill.
Set a hard spend limit in the provider console today. It is a two-minute change and it bounds every mistake you have not thought of yet.
How to stop it happening again
Track cost per task, not per month. A monthly number tells you nothing about which workflow is expensive.
Route by difficulty: a small model for classification, summarising and routine edits; a large one only for genuinely hard work.
Keep files small — it reduces cost, wrong-file edits and whole-file rewrites at the same time.
For non-urgent bulk work, use the provider's batch API, which is substantially cheaper.
Alert on anomalies: a token spike per session is a bug (usually a loop) before it is a billing problem.
Related questions
Is switching to a cheaper model the fix?
Usually not first. If your cost is dominated by resending a long history, a cheaper model just makes the same waste cost somewhat less. Shorten sessions and cache, then reconsider the model.
Does prompt caching actually help an agent?
Yes, when the expensive part is a stable prefix — a long system prompt, tool definitions, or a file read repeatedly within a session. It does not help if the content changes every turn.
Why did one session cost so much more than the others?
Look for a retry loop or a very large file read early in the session. Both inflate every subsequent step of that session.