How to Estimate AI Coding Cost Before You Start a Task
By Eric Bush · July 21, 2026 · 8 min read
The most expensive AI coding tasks are the ones you never estimated. Before you point an agent at a task, you can predict its token cost within a reasonable range using four inputs: how much context you feed in, how much code comes out, how many times the agent loops, and the per-token rate of your model. This pre-flight framework takes about two minutes and saves you from surprise bills.
The math is not complicated. What trips people up is forgetting that agents iterate — they read, edit, run tests, then read again — so a task that looks like one request is really six or ten round trips. Here is how to estimate each piece.
Step 1 — Estimate Your Input Tokens
Input is everything you send the model: your prompt, the files you attach, the system prompt, and — in an agent — the accumulated conversation. Three rules of thumb get you close:
- ~750 words ≈ 1,000 tokens for English prose and comments.
- ~4 characters ≈ 1 token is a decent approximation for source code.
- A typical 200-line source file ≈ 2,000–3,000 tokens.
So if you plan to feed the agent five source files of ~200 lines each plus a paragraph of instructions, you are looking at roughly 10,000–15,000 input tokens per pass before the agent adds anything of its own.
Step 2 — Estimate Your Output Tokens
Output is everything the model generates: the code it writes, plus its reasoning, explanations, and any tool-call arguments. Output is where the money is — most models charge 4–5x more per output token than per input token. A focused edit might be 500–1,500 output tokens; a new module with explanation can run 3,000–5,000. If the model thinks out loud or writes verbose commentary, output balloons fast.
Step 3 — Account for Agent Iterations
This is the step everyone skips. A coding agent does not answer once. It reads the files, proposes an edit, runs the tests, reads the failure, and edits again. Each loop re-sends context and generates new output. For a trivial task the multiplier might be 1–2x. For a non-trivial task — anything involving tests, multiple files, or debugging — plan on 3–10x.
Multiply your single-pass estimate by an honest iteration factor. If you guess "one pass" for a real feature, your estimate will be off by an order of magnitude.
Step 4 — Multiply by the Model Rate
Now apply your model's price. Per-pass cost = (input tokens × input rate) + (output tokens × output rate), then multiply by the number of iterations. Rates are quoted per million tokens, so convert your token counts to millions first (15,000 tokens = 0.015M) and the arithmetic stays clean. Cheaper tiers change the whole equation, which is exactly why estimating first lets you pick the right model instead of defaulting to the priciest one.
Worked Example — Adding Auth to an Express App
Say you ask an agent to add authentication to an Express app. A realistic per-pass profile is ~15,000 input tokens (the routes, middleware, user model, and your instructions) and ~4,000 output tokens (the new auth middleware, route changes, and explanation). The agent needs about 6 iterations: scaffold, wire the middleware, add the login route, fix a failing test, handle the token-expiry edge case, and clean up.
Totals across 6 passes: 90,000 input tokens (0.09M) and 24,000 output tokens (0.024M). Here is the cost on three models:
- Haiku 4.5 (in $1 / out $5): (0.09M × $1) + (0.024M × $5) = $0.09 + $0.12 = $0.21
- Sonnet 5 (in $2 / out $10): (0.09M × $2) + (0.024M × $10) = $0.18 + $0.24 = $0.42
- GPT-5.6 Terra (in $2.5 / out $15): (0.09M × $2.5) + (0.024M × $15) = $0.225 + $0.36 = $0.59
The same task lands between about $0.21 and $0.59 depending on the model — a 2.8x spread driven entirely by model choice. That is your estimate: roughly 20 to 60 cents for a clean run.
Pad for Retries and Failed Branches
Your first estimate assumes the agent takes a clean path. Real runs rarely do. The model goes down a wrong branch, you interrupt and redirect, a test harness misbehaves, or context grows as history accumulates — later passes are bigger than early ones because the whole conversation rides along. Pad the clean estimate by 1.3–2x for anything non-trivial. Our $0.21–$0.59 auth task becomes a more honest $0.30–$1.20 once you account for the messy middle.
The padding is not pessimism — it is the difference between an estimate and a fantasy. Budget for the version of the task where two of the six iterations get wasted, because sometimes they will.
You don't have to do this arithmetic by hand for every model. Drop your per-pass input, output, and iteration estimates into the AI cost calculator and compare the same task across every tier before you commit a single token.
Want to calculate exact costs for your project?
Frequently Asked Questions
How many tokens is a typical source file?
For code, roughly 4 characters equals 1 token, so a 200-line file is about 2,000-3,000 tokens. For prose and comments, about 750 words equals 1,000 tokens. Attaching five 200-line files is therefore around 10,000-15,000 input tokens before your prompt.
Why is my actual cost higher than my estimate?
Usually iterations and context growth. Agents loop more than you expect, and each pass re-sends the accumulated conversation, so later passes cost more than early ones. Wrong branches and interrupted runs add more. Pad a clean estimate by 1.3-2x for anything non-trivial.
What iteration factor should I use?
Use 1-2x for trivial single-file edits, and 3-10x for anything involving tests, multiple files, or debugging. The iteration factor is the single biggest source of estimation error, so err on the higher side for unfamiliar tasks.
Do input or output tokens matter more for cost?
Output, usually. Most models charge 4-5x more per output token than per input token, so verbose explanations and large code generations dominate the bill even when you send more input than you get back. Asking the model to be terse is a direct cost lever.
Can I really estimate cost before writing any code?
Yes. Count the files and context you will attach for input, estimate the size of the change for output, pick an iteration factor, and multiply by the model rate. Two minutes of estimating up front prevents most surprise bills and helps you pick the cheapest model that can do the job.
Related Articles
AI Coding Cost Calculator: How to Estimate Your Project Budget Before You Start
Learn how to estimate AI coding costs before starting a project. Covers token usage formulas, complexity multipliers, model selection impact, and hidden costs like retries and context resets.
AI Coding Cost Per Debugging Session: How Many Iterations Before You Should Rewrite From Scratch?
AI debugging sessions have compounding costs — each retry burns tokens. We give a decision framework for when to keep iterating vs abandon and rewrite, with the specific iteration threshold that flips the math.
How to Count Tokens Before You Code: Estimating AI Coding Costs Accurately
You can't budget what you can't measure. Learn how tokens map to code, how to estimate token counts before a project starts, and how to turn that estimate into a real dollar figure.