Sub topic 8
Cost and Resource Utilization
This chapter assumes you know basic machine learning vocabulary, can read Python, and understand scheduled jobs and HTTP APIs. It builds on the previous chapter on latency and throughput. Here you will see why the same model can cost very different amounts to serve, depending on how you run it.
Paying for capacity, not for predictions
Serving cost mostly comes from compute that you reserve or keep running. The number of predictions matters less than how busy that compute is.
- Batch jobs start, score a large table, and stop. You pay for the machine only while the job runs. Throughput is high because the job can fill every CPU core or GPU with large chunks of rows.
- Real-time services stay up all day. You pay for them even when no requests arrive. They must also have spare capacity for traffic peaks, because a request that waits too long breaks the latency target.
Utilization is the share of paid capacity that does useful work. A batch job can run near 80 to 95 percent utilization while it is active. A real-time service often averages 10 to 30 percent, because it is sized for the peak rather than the average.
A worked comparison
Suppose you score 1,000,000 customers.
| Item | Batch | Real time |
|---|---|---|
| Compute pattern | One 20-minute job per day | Two instances running 24 hours |
| Instance price | 1 instance at $0.50 per hour | 2 instances at $0.50 per hour |
| Daily compute cost | About $0.17 | $24.00 |
| Idle time paid for | Almost none | Most of the day |
The prices are made up, but the shape is typical. The real-time service costs far more here because it holds capacity for requests that may never come. If only 5,000 customers need a score each day, batch scoring all 1,000,000 wastes work in the other direction.
Wasted work in batch
Batch is not automatically cheap. Scoring every row means you compute predictions that nobody reads. The next code shows one way to cut that waste: score only the rows that changed or are needed.
import pandas as pd
def rows_to_score(customers: pd.DataFrame, last_scored: pd.DataFrame) -> pd.DataFrame:
merged = customers.merge(
last_scored[["customer_id", "scored_at"]], on="customer_id", how="left"
)
never_scored = merged["scored_at"].isna()
changed = merged["updated_at"] > merged["scored_at"]
return merged[never_scored | changed]
customers = pd.DataFrame(
{
"customer_id": [1, 2, 3],
"updated_at": pd.to_datetime(["2026-11-27", "2026-11-20", "2026-11-28"]),
}
)
last_scored = pd.DataFrame(
{
"customer_id": [1, 2],
"scored_at": pd.to_datetime(["2026-11-26", "2026-11-25"]),
}
)
print(rows_to_score(customers, last_scored)["customer_id"].tolist())This prints [1, 3]. Customer 1 changed after the last score, customer 3 was never scored, and customer 2 is unchanged.
Ways to improve utilization
For real-time services, the main tools are:
- Autoscaling. Add instances when traffic rises and remove them when it falls. Scaling takes time, so keep a minimum count to avoid slow starts.
- Dynamic batching. Group requests that arrive within a few milliseconds and score them together. This raises throughput per instance at a small latency cost.
- Smaller models. A lighter model may fit on cheaper hardware with little accuracy loss.
- Sharing hardware. Host several models on one instance when each model's traffic is low.
For batch jobs, use temporary or interruptible machines where your platform allows it, and make the job restartable so an interruption does not force a full rerun.
Common errors
- Sizing a real-time service for the average load instead of the peak, which causes slow responses at busy times.
- Comparing batch and real-time prices per prediction without adjusting for utilization.
- Running a full nightly batch when only a small share of records ever gets read.
- Choosing real time for a use case where users would accept an answer that is hours old.
Exercise
A model scores 2,000,000 records. A nightly batch job takes 30 minutes on one $1.00 per hour instance. A real-time service needs 3 instances at $1.00 per hour running all day. Compute the daily compute cost of each. Then name one reason the real-time option could still be the right choice.
Show answer
Batch costs 0.5 hours × $1.00 = $0.50 per day. Real time costs 3 × 24 × $1.00 = $72.00 per day. Real time can still be right when the use case needs a score from live data within seconds, such as fraud checks during a payment, because a nightly score would be too stale to be useful.