Dev.to AI 🤖 Ai 👁 0 📖 3 min read

Your AI feature costs more in production than your estimate. Here is the arithmetic.

Most AI features are costed once, in a spreadsheet, before launch: "about 800 tokens per request, a few thousand requests a day, fine." Then the invoice arrives and it is several times the estimate. The gap is rarely a p

Most AI features are costed once, in a spreadsheet, before launch: "about 800 tokens per request, a few thousand requests a day, fine." Then the invoice arrives and it is several times the estimate. The gap is rarely a pricing surprise. It is arithmetic that the estimate skipped.

Here is where the money actually goes, in the order that usually matters.

1. The conversation grows on every turn

A chat feature that keeps history re-sends the whole history with each new message. Turn one sends 500 tokens. Turn ten sends those 500, plus nine replies and nine answers, plus the system prompt. By turn twenty you are paying for thousands of input tokens to ask one short question.

Do the sum for a support bot with a 300-token system prompt and 150-token turns:

  • Turn 1: about 450 input tokens
  • Turn 10: about 1,950
  • Turn 20: about 3,450

Twenty turns of conversation cost roughly 39,000 input tokens, not the 9,000 a per-message estimate assumes. The curve is quadratic in the number of turns. Two fixes, both cheap:

  • Cap the window. Send the last N turns plus a rolling summary of everything older.
  • Summarize on a schedule. Every ten turns, replace the history with a 200-token summary generated once.

2. Output tokens cost more than input

At every major vendor, generated tokens are priced several times higher than input tokens, because generation is the expensive part. An estimate that counts "tokens" as one number will be low whenever the feature produces long answers.

Count them separately. A 400-token answer can cost more than a 3,000-token prompt.

3. Retries are invisible in the estimate

Timeouts, 429s, malformed JSON that fails validation, and empty responses all get retried, and every retry is a full-price request. A 5 percent retry rate sounds harmless until you notice it applies to the expensive requests most often: long prompts and long outputs are the ones that time out.

Log retries as their own counter. If retries are above 2 percent, the problem is usually prompt size or an output format that the model keeps breaking, not the provider.

4. The p95 request is what you budget for

Averages hide the tail. If most requests are 800 tokens and the top 5 percent are 20,000 (a pasted document, a long thread, a table), the tail can be most of the bill. Budget on the 95th percentile length, not the mean, and put a hard cap on input size with a clear error message.

5. Caching changes the picture, but only for the stable part

Prompt caching discounts the repeated prefix: the system prompt, instructions, and any fixed reference material. It does nothing for the part that changes every request. So the saving depends on how you order the prompt: stable content first, variable content last. If your prompt puts the user's question at the top and the instructions at the bottom, caching saves you almost nothing.

6. What to measure before optimizing anything

Three numbers per feature, logged per request:

  • input tokens and output tokens, separately
  • retries and failures
  • turn count per conversation

With those three you can predict the bill within a few percent and see which fix pays. Without them you are guessing at which of the five effects above is eating the budget.

The short version

Cost per request is not a number you look up, it is a number you compute from turn count, output length, retries and the length of the tail. The vendors publish per-token prices; the multipliers are yours.

I write about AI tool pricing at AI Pulse Daily, where prices are checked against the vendor's own page before anything is published.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.