My account

Running LLM serving in production

Use a ready serving engine, and make every layer between it and the user stream well. Set speed targets, measure TTFT and TPOT at p95 from the user's side, and load test with real prompts to find the true capacity of one replica. Autoscale on waiting requests and KV cache use, plan for slow cold starts, and compute cost per million tokens with real utilization, not peak benchmarks.

Not reviewedWritten with AI helpReading time: 14 minShop assistant examplePythonNginx

Author: bezzad

Contents

The problem: everything works, except in production

The shop assistant passed its evals and the engine looked fast. Then, in the first busy evening:

  1. Answers appeared all at once after several seconds, although the engine streamed them.
  2. The autoscaler added replicas too late. Each new replica needed several minutes to start.
  3. Capacity was half of the plan. The plan came from a benchmark with one short prompt.
  4. The cost per month was higher than the spreadsheet, because the spreadsheet assumed the GPUs were full all day.

None of these were model problems. They were serving problems. This lesson is about running the whole system.

Choose a ready engine

Do not write your own serving loop for real traffic. Engines like vLLM, SGLang and TensorRT-LLM already do continuous batching, paged KV memory, prefix caching, quantized formats and parallelism. Most offer an OpenAI-compatible API.

When you compare them, check:

  1. Your hardware and your model are supported, with fast kernels for the formats you want.
  2. The features you need: prefix caching, chunked prefill, speculative decoding, structured output, multi-GPU.
  3. Operations: metrics, health checks, how it behaves when memory is full, how upgrades go.
  4. Your own numbers: run the same load test on each. Results differ by model and workload.

Tools like llama.cpp or Ollama are great for laptops and small use, but they are usually not the first choice for many parallel users.

The request path

UserProxystreaming, limitsRoutersame chat, same replicaReplica 1: engine on a GPUReplica 2: engine on a GPUReplica 3: engine on a GPUAutoscaler: waiting requests, KV cache useMeasure time to first tokenat the user, not only at the engine
Each layer between the engine and the user can add delay. Look at all of them.
  1. Proxy. It must stream: turn off response buffering for the streaming route, use HTTP/1.1 to the backend, and allow long enough read timeouts. Apply limits here: max prompt size and tokens per minute per user.
  2. Router. With prefix caching, send the same chat to the same replica, but also watch load so no replica is overloaded.
  3. Replicas. One serving engine per GPU or group of GPUs.
  4. Cancel. When the user leaves, the request must be aborted in the engine, so the GPU does not write tokens nobody reads.
  5. Fallback. If all replicas are full or down, have a plan: a smaller model, an outside API, or a clear “try again” message.

A streaming route in Nginx:

location /chat/stream {
    proxy_pass http://llm_backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    proxy_read_timeout 300s;
}

Targets and dashboards

Agree on targets (SLOs) with product first. For our chat: time to first token p95 under 1 second, time per output token p95 under 50 ms.

Put these on the dashboard:

Signal Why
TTFT and TPOT at p50, p95, p99 What users feel. Measure at the client too.
Waiting requests (queue length) The first sign of too little capacity.
KV cache usage and preemptions Memory pressure before users feel it.
Prefix cache hit rate Prompt order and routing are working.
Throughput and goodput Cost and how much of it is good service.
Errors and timeouts Broken requests, by type.
Cost per day No surprises at the end of the month.

Alert on the user-facing targets and on the queue, not on GPU usage alone. A GPU can show high usage while users are happy, or medium usage while a queue grows.

Load testing: find the real capacity

A useful load test looks like real traffic:

  1. Real prompts, anonymized, with real lengths for prompts and answers, in every language you serve.
  2. Growing load: step up the number of parallel chats or the request rate.
  3. The right numbers: TTFT, TPOT, end-to-end time and errors, at p95 and p99.
  4. Through the real path: proxy, router and network, not only the engine.
p95 time to first tokenTarget: 1 secondMeets the targetToo slowcapacity: about 120 chatsChats at the same time (example numbers)
The real capacity of one replica is where the latency target breaks, not where throughput peaks.

The point where p95 crosses the target is the capacity of one replica. Above it, latency grows very fast. Plan with that number, plus spare replicas for failures and deploys.

Use a ready tool rather than a loop: the benchmark scripts that come with your engine, or tools like GuideLLM or GenAI-Perf.

Autoscaling and cold starts

Traffic has a daily curve. Paying for the peak all night is expensive, so we scale. But GPU replicas are slow to start:

  1. Pulling a large container image, often many GB.
  2. Downloading or reading the weights, 16 GB for our 8B model, much more for big models.
  3. Loading them onto the GPU, and warm-up steps in the engine.

All together this can take minutes. So:

  1. Scale on the right signals: waiting requests and KV cache usage. CPU usage says almost nothing about an LLM server.
  2. Start early. Use the known daily curve to scale up before the evening peak.
  3. Keep a minimum of warm replicas.
  4. Make starts faster: keep weights on fast local disk or a cache close to the GPUs, and keep images small.
  5. Scale down slowly and gently. Stop sending new requests to a replica, let running streams finish, then stop it.

Cost per million tokens

The honest formula uses what you really get, not the peak:

def cost_per_million(gpu_hour_price, gpus, tokens_per_sec_at_slo,
                     utilization):
    tokens_per_hour = tokens_per_sec_at_slo * 3600 * utilization
    return gpu_hour_price * gpus / tokens_per_hour * 1_000_000

# Example numbers only.
print(round(cost_per_million(4.0, 1, 2_500, 0.35), 2))   # about 1.27
  1. Tokens per second at your SLO, from the load test, not the largest batch.
  2. Utilization over the whole day, including quiet hours.
  3. Add spare replicas and the cost of the people who run the system.
  4. Fill quiet hours with batch work, like nightly summaries, which can use big batches.

Then compare with an outside API at the same quality, for the same requests.

Changing things safely

New model versions, new engine versions and new settings all change speed and quality.

  1. Run the eval set and the load test before each change.
  2. Canary: send a small share of traffic to the new version, compare its numbers with the old one.
  3. Move step by step, and keep the old version ready for rollback.
  4. Pin versions of the engine, the model and the tokenizer, so a rebuild does not change behavior by surprise.

Common mistakes

Mistake Result Right way
Trusting only the engine’s latency Users wait while the dashboard looks good. Measure at the client too; fix proxy buffering.
Benchmark with one short prompt Capacity plan off by a lot. Real prompts, growing load, p95 and p99.
Autoscaling on CPU usage Too late or never. Waiting requests and KV cache usage.
Ignoring cold start time Peak arrives before new replicas are ready. Scale up early, keep warm replicas, cache weights.
Cost from peak throughput Monthly bill far above the plan. Throughput at SLO, real utilization, spare capacity.
Upgrading engine or model directly in production Quality or speed changes unseen. Evals, load test, canary, rollback.

Summary in six lines

  1. Use a ready serving engine, and compare engines with your own load test.
  2. Make every layer stream, apply limits, cancel abandoned requests, and have a fallback.
  3. Set targets and watch TTFT and TPOT at p95, the queue and KV cache usage.
  4. Find the real capacity of one replica where the latency target breaks.
  5. Autoscale on queue and memory signals, and plan for slow cold starts.
  6. Compute cost with throughput at SLO and real utilization, and change things with evals and canaries.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.