Estimating LLM API Costs Before You Ship
Most LLM bills don't explode because of one big call. They explode because of a thousand small ones — retries, long context windows, and output tokens nobody counted. I learned this the hard way while building with LLM APIs, and it changed how I plan every AI feature. Here's the practical method I use now, plus the free calculator I built for it.
1. Price the whole call, not just the prompt
Every LLM call has three cost parts: input tokens, output tokens, and the calls you didn't plan for. Output tokens usually cost several times more than input tokens, so a chatty model that writes long answers is far more expensive than one that writes short ones — even on the same prompt.
Before I ship anything, I write down: how many tokens go in, how many come out, and how many calls happen per user action. If one user action triggers five agent steps, that's five bills, not one.
2. Context windows are the silent multiplier
This is the one most people miss. In a multi-turn conversation or an agent loop, the model re-reads the entire conversation history on every turn. Turn 1 costs a little. Turn 20 costs twenty times as much for the same question, because the history keeps growing.
Two fixes that actually work: summarize old turns instead of carrying the full transcript, and cap how much history you send. Both cut the bill without hurting quality much.
3. Retries quietly double your bill
A failed call that retries automatically is a second full-price call. Add a timeout, a retry that fails, and a fallback model, and one user request can easily cost three times what you estimated. Count retries in your estimate from day one — assume at least one retry in ten calls fails or times out.
4. Do the math before you write the code
Here's the formula I use:
Cost per call = (input tokens x input price) + (output tokens x output price)
Then: monthly cost = cost per call x calls per user x users x retries factor
Run it for your cheapest realistic case and your worst case. If the worst case scares you, your design needs a cheaper model, shorter prompts, or caching — not optimism.
I got tired of doing this math in a spreadsheet, so I built a free calculator for it: the LLM Token & API Pricing Calculator on DevMaple. You type in your token counts and your provider's per-token prices, and it shows the cost per call and per month — no signup, everything runs in your browser. You can find it at https://www.devmaple.com/tools/llm-cost-calculator, along with 40 free developer tools at https://www.devmaple.com — including an HTML to React converter at https://www.devmaple.com/tools/html-to-react.
5. Three habits that keep bills predictable
- Set a per-user budget. Cap tokens or calls per user per day. It's the cheapest insurance there is.
- Log real token usage from day one. Estimates are guesses; production logs are facts. Compare them weekly for the first month.
- Cache what doesn't change. System prompts, repeated instructions, and common answers don't need to be re-generated every time.
The bottom line
LLM pricing isn't complicated — it's just easy to underestimate. Count input and output separately, remember that context grows, budget for retries, and do the math before you ship. A five-minute estimate now saves a very unpleasant invoice later.
Top comments (0)