Sub topic 8
Cost, Latency, and Operational Burden
Assume you understand how software features are built, know the basic idea of machine learning and large language models, and can read short Python snippets. This chapter focuses on one question: can the product afford to run and maintain the AI feature?
Count the full cost
An AI feature has more than a model price. Estimate these cost categories:
- Inference cost: the charge for each model request.
- Infrastructure cost: servers, databases, queues, logs, and storage.
- Engineering cost: integration, testing, prompt or model changes, and incident response.
- Human cost: review, correction, labeling, and customer support.
- Opportunity cost: work that the team cannot do because it is maintaining this feature.
A hosted model can reduce infrastructure work, but it does not make the feature free. A self-hosted model may lower the price per request at high volume while increasing deployment and reliability work.
Use the same unit when comparing options. For example, calculate cost per completed task rather than cost per API call. If an AI system needs two attempts to complete one task, its effective cost is roughly twice the request cost.
def monthly_cost(requests, model_cost, fixed_cost, review_rate=0, review_cost=0):
model_total = requests * model_cost
review_total = requests * review_rate * review_cost
return model_total + fixed_cost + review_total
print(monthly_cost(
requests=100_000,
model_cost=0.003,
fixed_cost=800,
review_rate=0.10,
review_cost=0.25,
))This estimate includes model requests, fixed monthly infrastructure, and human review. Add retries, failed requests, storage, and monitoring when they are known.
Treat latency as part of the user experience
Latency is the time between a user action and a useful response. Measure more than the average. A few slow requests can make a feature feel unreliable, so track percentiles such as p95, the time under which 95% of requests finish.
Latency often comes from several steps:
- The application sends a request.
- The provider queues and processes it.
- The application may call tools or retrieve documents.
- The response is checked, stored, and displayed.
Streaming can show partial text sooner, but it does not reduce the total work. A background job can remove waiting from the main screen, but it requires status handling, retries, and notifications.
| Feature need | Useful latency target | Possible design |
|---|---|---|
| Inline autocomplete | Very low | Rules, cache, or small local model |
| Draft generated after a form submission | Seconds may be acceptable | Hosted model with progress state |
| Batch classification overnight | Minutes or hours may be acceptable | Queue and worker process |
Set a budget before choosing a model. If the user needs an answer in 300 milliseconds, a large remote model with retrieval may be the wrong tool even if its quality is higher.
Account for operational burden
Operational burden is the ongoing work required to keep the feature dependable. AI adds failure modes that ordinary application code may not have:
- Provider outages or rate limits.
- Model behavior changes after a provider update.
- Prompt or retrieval regressions.
- Unexpectedly long inputs and high bills.
- Outputs that fail formatting or safety checks.
- New privacy and audit requirements.
A fallback should be concrete. It might return a search result, use a deterministic rule, place the task in a queue, or ask a person to review it. “Try again” is not a complete reliability strategy.
Make the trade-off visible
Compare each candidate using the same measures. A simpler rule may have lower quality but predictable cost and latency. AI may improve coverage while adding variable bills and operational work. The right choice depends on whether the improvement pays for those differences.
A practical decision test
Reject an option if it exceeds a hard constraint, such as a privacy rule, maximum response time, or monthly budget. For softer constraints, compare the value of better outcomes with the added engineering and operating cost.
Exercise
Exercise
A support feature needs an answer within 500 milliseconds, but a remote large language model takes two seconds at p95. What should you investigate before shipping it?
Show answer
Test a smaller or local model, caching, precomputation, or a non-AI baseline. If none meets the latency budget, use a slower background workflow or do not use AI for the interactive feature.