Sub topic 8
Cost, latency, and operational burden
This chapter assumes you have read the earlier chapters on non-AI baselines and AI-fit signals. It also assumes you can read short Python snippets and know that hosted language models bill by usage. A feature can fit AI on paper and still fail because it costs too much per request, responds too slowly, or needs more upkeep than your team can give it. Here you will estimate those three costs before you commit.
Why this check comes after fit
Fit signals tell you whether AI could solve the problem. Cost, latency, and operational burden tell you whether it can solve it at your scale, at your speed, and with your staff. A rule or a search index costs almost nothing per request, responds in milliseconds, and rarely changes behavior without a code change. An AI component usually loses on all three counts. It must win on quality by enough to pay for them.
The three costs are separate questions:
- Unit cost: what one request costs, and what the monthly bill is at expected volume.
- Latency: how long the user waits, including slow outliers.
- Operational burden: the ongoing work to keep the feature correct, safe, and available.
Unit cost and monthly cost
Hosted language models usually bill per token. A token is a chunk of text, roughly a short word or word fragment. Input tokens (your prompt plus any context you attach) and output tokens (the model's reply) are priced separately, and output tokens usually cost more.
Cost grows with three things you control:
- Prompt size. Long instructions, examples, and retrieved documents are paid for on every call.
- Output length. A one-word label is cheap. A three-paragraph summary is not.
- Call count. Retries, multi-step chains, and "check the answer with a second call" patterns multiply the cost per user action.
The function below turns those inputs into a monthly estimate. The prices are made up for the example. Replace them with your provider's current rates.
def cost_per_request(in_tokens, out_tokens, in_price, out_price):
"""Prices are dollars per one million tokens."""
return in_tokens * in_price / 1_000_000 + out_tokens * out_price / 1_000_000
def monthly_cost(requests, per_request, retry_rate=0.0):
return requests * per_request * (1 + retry_rate)
per_request = cost_per_request(
in_tokens=1200, out_tokens=300, in_price=3.0, out_price=15.0
)
total = monthly_cost(500_000, per_request, retry_rate=0.05)
accuracy = 0.90
print(f"per request: ${per_request:.4f}")
print(f"per month: ${total:.2f}")
print(f"per correct result: ${per_request * 1.05 / accuracy:.5f}")The output is $0.0081 per request, $4252.50 per month, and $0.00945 per correct result.
The last line matters most. A wrong answer still costs money, so the cost of a useful result is the cost per request divided by the share of requests that are correct. If accuracy is 90 percent, each good result costs about 11 percent more than the sticker price. If a wrong answer also triggers a human fix, add that labor cost too.
Compare against value, not against zero
A $4,000 monthly bill is not good or bad by itself. Compare it with one of these:
- The cost of the non-AI baseline, including staff time it leaves undone.
- The value of each success, such as a support ticket resolved without an agent or a conversion that would not otherwise happen.
- The budget the feature is allowed to consume, set before you build.
If the feature serves free-tier users who generate no revenue, even small per-request costs can become a problem at scale. Check the unit economics against your pricing model.
Costs that are easy to miss
- Fixed costs: hosting for self-run models, vector databases, and monitoring tools charge whether or not anyone uses the feature.
- Growth in context: prompts tend to get longer as teams add instructions to fix edge cases. Cost drifts upward without any traffic change.
- Price and model changes: providers retire models and change prices. A forced migration is an unplanned project.
- Evaluation costs: running a test set against the model on every change uses tokens too, and so does labeling time.
Latency
Latency is the time between a user action and a usable result. For language models it has two parts: time to the first token and time to finish generating. Output length drives the second part directly, since models produce tokens one after another.
Averages hide the problem. Users remember the slow requests. Track percentiles: p50 is the median, and p95 is the value that 95 percent of requests beat. This snippet shows how one slow outlier separates them.
import statistics
latencies = [0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.1, 6.5] # seconds
p50 = statistics.median(latencies)
p95 = statistics.quantiles(latencies, n=20, method="inclusive")[18]
print(f"mean: {statistics.mean(latencies):.2f}s")
print(f"p50: {p50:.2f}s")
print(f"p95: {p95:.2f}s")The mean is 1.79s, the p50 is 1.25s, and the p95 is 4.52s. A team that watched only the mean would think the feature feels fine while one user in twenty waits more than four seconds.
Match latency to the interaction
Different interactions tolerate different delays.
| Interaction | Typical tolerance | Implication for AI |
|---|---|---|
| Autocomplete while typing | Under about 200 ms | Usually needs a small, fast model or a non-AI method |
| Chat reply | A few seconds, if text streams | Streaming makes waiting feel shorter |
| Search results page | Under about 1 second | A model call in the critical path is risky |
| Report or document generation | Tens of seconds | Acceptable with a progress indicator |
| Overnight batch labeling | Hours | Cheapest and easiest case for AI |
Several design moves reduce the pain without changing the model:
- Stream output so users see text as it arrives.
- Move work out of the request path: precompute results, or run it in a background job and notify the user.
- Cache responses for repeated identical inputs.
- Use a smaller model for easy cases and escalate hard ones. This is called routing.
- Set a timeout and fall back to the non-AI path when it expires.
Every one of these adds code to maintain, which feeds the next section.
Latency and cost trade against quality
Larger models are usually slower, more expensive, and more accurate. Longer prompts with more examples may raise quality and raise both other numbers. You are choosing a point on a three-way trade-off. Write down which of the three the feature can least afford to lose, then evaluate options against that.
Operational burden
Operational burden is the recurring work after launch. Deterministic code stays correct until someone changes it. An AI component can change behavior when the provider updates the model, when user inputs shift, or when you edit a prompt.
Typical recurring tasks:
- Evaluation upkeep: keep the test set current and rerun it after every prompt, model, or data change.
- Monitoring: track quality, cost, latency, error rates, and refusals, not only uptime.
- Prompt and model versioning: treat prompts as code, with review and rollback.
- Incident handling: decide who responds when the model produces harmful or wrong output, and how to turn the feature off quickly.
- Provider dependency: rate limits, outages, and deprecations need handling, such as retries with backoff and a fallback path.
- Data handling: logging prompts for debugging may store sensitive user data. The risk chapter covers this in detail.
- Human review capacity: if the design relies on people checking outputs, staff time is a running cost.
The relative weight differs by approach.
| Approach | Money | Latency control | Operational burden |
|---|---|---|---|
| Rules or heuristics | Very low | Full | Low, but rules grow tangled over time |
| Hosted model API | Per use | Limited | Moderate: prompts, evals, vendor changes |
| Self-hosted model | Fixed infrastructure | Good, if tuned | High: GPUs, scaling, updates, security |
| Custom trained model | Training plus hosting | Good | Highest: data pipelines, retraining, drift |
The build-versus-buy chapter uses this table as input.
A quick feasibility check
Run these steps before building a prototype.
- Estimate peak and average requests per month.
- Draft a realistic prompt and measure its token count. Estimate output length.
- Compute monthly cost with the function above, plus a retry allowance.
- Compare it with the baseline cost and the value per success.
- State the latency budget and test a prototype's p95 against it.
- List the recurring tasks above and name an owner for each.
If a step has no answer, record it as an open risk in the decision scorecard.
Common mistakes
- Costing the demo, not production. A prototype with ten short prompts says nothing about long real inputs at full volume.
- Judging latency by the mean. Use p95 or worse, measured under realistic load.
- Ignoring retries and chains. A feature that makes four model calls per action costs four times the estimate.
- Forgetting the baseline's cost. Compare total cost of ownership on both sides, including the maintenance of tangled rules.
- Budgeting build time only. Most of the effort comes after launch.
Connecting to the scorecard
Cost, latency, and operational burden become scorecard rows. Record the estimated monthly cost, the p95 latency against its budget, and a named owner for operations. A feature that fits all three keeps moving toward a prototype. A feature that fails one needs a design change, such as a smaller model, a background job, or a non-AI path. If none of those fixes works, the answer is not to use AI.
Exercise
A feature sends 2,000 input tokens and gets 500 output tokens per request. Input costs $2 per million tokens and output costs $10 per million. The feature handles 200,000 requests a month. Accuracy is 80 percent. What is the monthly cost without retries, and what is the cost per correct result? Name one latency and one operational risk you would check before launch.
Show answer
Cost per request is 2,000 × 2 / 1,000,000 + 500 × 10 / 1,000,000 = $0.004 + $0.005 = $0.009. Monthly cost is 200,000 × $0.009 = $1,800. Cost per correct result is $0.009 / 0.8 = $0.01125. For latency, measure p95 and check it against the interaction's budget, since the mean can hide slow outliers. For operations, check that someone owns re-running the evaluation set when the provider updates the model or the prompt changes. Provider outages and deprecations are also valid answers.
Exercise
Your p50 latency is 1.2 seconds and your p95 is 7 seconds. The feature sits in the critical path of a search results page that should load in under one second. Name two design changes that could help without switching models.
Show answer
Move the model call out of the request path, for example by precomputing results in a background job or caching responses for repeated inputs. Set a short timeout that falls back to the non-AI search result when the model is slow. Streaming alone would not help here, because the page needs a full result quickly.