All posts

How model serving works

/AI/4 min read

Serving turns a trained model into an API that answers within a deadline at an affordable price. The same $2-an-hour GPU costs $11.11 per million tokens serving one request at a time and $0.28 when batching — and pushing it from 50% to 95% busy multiplies queueing delay nineteen-fold.

A trained model is a file of weights. Model serving is everything needed to turn that file into something that answers requests: reliably, quickly enough, for many users at once, at a cost that makes sense.

It is mostly not about the model. It is about queues, batches, memory and hardware.

The request path

A typical request to a served model goes through:

  1. A gateway — authentication, rate limits, routing to the right model and version.
  2. A queue — requests wait here when every worker is busy.
  3. A scheduler — groups waiting requests into batches and assigns them to workers.
  4. A model worker — a process holding the weights on one or more accelerators, running the forward passes.
  5. Pre- and post-processing — tokenising input, applying safety filters, formatting and streaming output.

Each step adds latency, and any of them can be the bottleneck.

Batching is the economics

A GPU running a model for one request at a time spends most of its time waiting for weights to arrive from memory, not computing. Serving many requests together lets each weight that is loaded be used for all of them. Throughput rises almost proportionally with batch size, until compute becomes the limit.

For a language model on a GPU rented at $2 an hour:

tokens per second (whole GPU)cost per million tokens
50 — one request at a time$11.11
500 — modest batching$1.11
2,000 — well-batched$0.28

A forty-fold difference in cost on the same hardware, from how requests are grouped. Continuous batching — adding new requests to a running batch the moment others finish, rather than waiting for a whole batch to complete — is what makes high utilisation possible for generation, where every request has a different length.

Utilisation is the latency trap

There is a catch. A busy server makes requests wait. With random arrivals and service times, queueing theory gives the average wait as ρ / (1 − ρ) times the service time, where ρ is utilisation. For a 100 ms service time:

utilisationaverage queue waittotal response time
50%100 ms200 ms
80%400 ms500 ms
90%900 ms1,000 ms
95%1,900 ms2,000 ms

From 50% to 95% busy, the wait grows nineteen-fold. The last few points of utilisation are where latency explodes. Cost pushes toward running hot; latency targets push back. Real serving systems run well below 100% and autoscale — adding workers as load rises — to stay on the flat part of the curve.

BUSY GPUs ARE CHEAP AND SLOW 2,000 ms0 50% · 20080% · 50090% · 1,00095% · 2,000 0%100% utilisation wait = ρ / (1 − ρ) × service time — the last few points cost the most
Service time 100 ms, random arrivals. Cost falls with utilisation; latency rises without bound.

Fitting the model in memory

The weights must fit on the accelerators, with room left for working memory — for language models, mostly the KV cache that grows with every active conversation.

  • Quantization stores weights in 8 or 4 bits instead of 16, halving or quartering their memory and speeding up memory-bound inference.
  • Tensor parallelism splits each layer across several GPUs that work on it together; pipeline parallelism gives different layers to different GPUs.
  • Paged KV caches allocate cache memory in small blocks, like virtual memory, so variable-length conversations do not waste space — which directly raises how many requests fit in a batch.

Latency targets for language models

Generation has two latencies that matter separately:

  • Time to first token (TTFT) — dominated by processing the prompt. Affected by prompt length, prompt caching and queueing.
  • Time per output token (TPOT) — the streaming speed once generation starts. Affected by batch size and model size.

Larger batches improve cost and throughput but slow each request's tokens. Serving systems set targets for both, often at the 95th or 99th percentile, and tune batch sizes to meet them.

Operating it

Versioning and rollout. Serve several model versions side by side; shift traffic gradually; roll back quickly.

Cold starts. Loading tens of gigabytes of weights takes time. Keep warm capacity and scale up before demand peaks rather than after.

Observability. Track queue depth, utilisation, TTFT and TPOT percentiles, error rates, and tokens per second per GPU. Cost per million tokens follows directly from the last.

Graceful overload. When demand exceeds capacity, reject or queue with a clear signal rather than letting every request slow down.

What to take away

Model serving is gateways, queues, schedulers and workers wrapped around a model. Batching is the economics: the same 2−an−hourGPUcosts2-an-hour GPU costs 11.11 per million tokens one request at a time and $0.28 when well batched.

Utilisation is the trade-off: queueing delay grows as ρ / (1 − ρ), nineteen-fold from 50% to 95% busy. Serving well means batching as much as latency targets allow, fitting more requests in memory, and autoscaling to stay off the steep part of the curve.