AI engineering and inferenceLoading progress…

Autoscaling

Calculate cost per token, estimate stable concurrency and define operational metrics tied to user-facing objectives.

Autoscaling is late by design

Adding a replica means finding a machine, loading the weights and warming up, so it helps only after a delay. Decisions should use queue and arrival signals and count replicas that are already starting. Removing replicas is risky too: in-flight requests and cached state are lost and a quick re-scale pays the whole startup again, so remove slowly and only when idle.

weights to load (GB)14load speed (GB/s)2warm-up (s)5arrivals per second12capacity now (per s)8
a new replica serves after 14/2 + 5 = 12 s while you wait, the backlog grows by (12 − 8) × 12 = 48 requests replicas needed for 12/s at 8/s each: 2

Autoscaling reacts late: loading weights takes real time. Use queue length and arrival signals, count replicas that are already starting so you do not order twice, and scale down slowly (one at a time, only when idle) because reloading is expensive and in-flight requests need to finish.

Change the model size, load speed and demand.

Predict first. Does a newly requested replica necessarily serve immediately?

Chapter context

An efficient service delivers useful requests within latency and quality targets at a sustainable cost. Hardware utilization, application traffic and failure handling determine whether an attractive benchmark becomes an economical deployment.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For hourly cost H and useful sustained output rate r:

Cmillion=106H3600r.C_{million}=\frac{10^6H}{3600r}.

For a stable system under compatible averages, Little's law is L=λWL=\lambda W: average in-system requests equal arrival rate times average time in system. It does not make an unstable queue stable.

Worked example

At a fictional 2/hourand100usefuloutputtokens/s,costis2×1,000,000/(3600×100)≈2/hour and 100 useful output tokens/s, cost is 2×1,000,000/(3600×100)≈5.5556 per million output tokens, excluding other costs. If useful throughput falls to 50, cost doubles to about $11.1111. At four requests/s and two seconds average in-system time, average concurrent requests are eight.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
cost=lambda rate: 1_000_000*2/(3600*rate)
assert isclose(cost(100),5.555555555555555)
assert isclose(cost(50),2*cost(100))
assert 4*2 == 8

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Fictional arithmetic prices are not current provider quotes. Little's law requires a stable, consistently measured system. Counting failed or late tokens as useful throughput can hide poor service.