← Experiments

Code view

LLM cold start

Where the cold-start fixes live in the code: the 7B's Helm values, the chart template that keeps the CUDA driver cache, and the gateway that wakes the model and holds the cold request.

The series took the 7B from 145 s to Ready down to 35–36 s, and a cold caller's first token from 151 s down to 36.5 s. Most of that is a handful of lines of configuration. The excerpts are from two private repos, trimmed to the code; comments are left out, and line numbers are the files' own.

01Off until someone asks

The model starts at zero replicas. Argo CD ignores replicas, so when the gateway scales it to 1, that sticks. An idle reaper scales it back down an hour after its last request.

charts/vllm-serving/values-llm.yaml
replicas: 0idleReaper:  scaleDownAfter: 60m

02Pin the KV cache, skip the profiling pass

On its own, vLLM profiles GPU memory at startup to size the KV cache. Giving it the size directly, with --kv-cache-memory, skips that pass. That took engine start from 110 s to 99.5 s, at almost the same token capacity.

charts/vllm-serving/values-llm.yaml
  gpuMemoryUtilization: "0.76"  maxModelLen: "8192"  extraArgs:    - "--quantization"    - "fp8"    - "--enforce-eager"    - "--kv-cache-memory=1848756736"

03No CUDA graphs

--enforce-eager drops CUDA graph capture. Once the driver cache was warm, graphs cost about 10 s of startup (51 s vs 40 s) for under 2% of decode speed. The series adopted eager mode because the goal was the shortest possible cold start.

charts/vllm-serving/values-llm.yaml
    - "--enforce-eager"

04The kernel caches that didn't matter

Step 6 moved vLLM's compile caches onto the persistent volume, expecting a win, and measured none. The setting stayed because the FlashInfer autotune cache still lives there.

charts/vllm-serving/values-llm.yaml
    VLLM_CACHE_ROOT: /data/vllm-cache

05The driver cache was the big one

The CUDA driver JIT-compiles PTX for the 5060 Ti's sm_120 on first use. By default it caches the result in the container's ephemeral layer, so every pod recompiled on every start. Pointing CUDA_CACHE_PATH at the persistent volume cut start from 80 s to 40 s. It was the largest single gain in the series. Step 13 moved the setting from the 7B's values into the chart template, so every vLLM instance with a volume gets it.

charts/vllm-serving/templates/deployment.yaml
            {{- if .Values.persistence.enabled }}            - name: CUDA_CACHE_PATH              value: {{ printf "%s/jit/nv" .Values.persistence.mountPath | quote }}            - name: CUDA_CACHE_MAXSIZE              value: "4294967296"

06Fork the engine, don't spawn it

89% of the time before the engine starts was Python imports, and they were paid twice. vLLM's CLI spawns the engine process, which re-imports torch, transformers and vLLM. With fork, the engine reuses the API server's imports, which saved 5.7 s and brought Ready to 35–36 s.

charts/vllm-serving/values-llm.yaml
    VLLM_WORKER_MULTIPROC_METHOD: fork

07The gateway wakes it, once

The caller's side starts in the gateway. A refused connection to vLLM means the model is at zero. _ensure_wake_in_flight scales the Deployment up, single-flight behind a lock: ten concurrent cold requests make one API call, and the rest see "starting".

ansible/files/llm-gateway/app.py
async def _ensure_wake_in_flight() -> str:    async with _wake_state.lock:        now = time.monotonic()        if _wake_state.status == "starting" and (now - _wake_state.since) < WAKE_STALE_SECONDS:            logger.info(                "llm_gateway.wake decision=already-starting deployment=%s namespace=%s elapsed_s=%.1f",                WAKE_DEPLOYMENT,                WAKE_NAMESPACE,                now - _wake_state.since,            )            return _wake_state.status        logger.info(            "llm_gateway.wake decision=requested deployment=%s namespace=%s prior_status=%s",            WAKE_DEPLOYMENT,            WAKE_NAMESPACE,            _wake_state.status,        )        _wake_state.status = "cold"        try:            prior_replicas = await _scale_client.ensure_scaled_up(WAKE_NAMESPACE, WAKE_DEPLOYMENT, 1)        except Exception as exc:            logger.warning(                "llm_gateway.wake decision=scale-failed deployment=%s namespace=%s "                "elapsed_s=%.1f error=%s: %s",                WAKE_DEPLOYMENT,                WAKE_NAMESPACE,                time.monotonic() - now,                type(exc).__name__,                exc,            )            raise        if prior_replicas == 0:            logger.info(                "llm_gateway.wake decision=scale-patched deployment=%s namespace=%s "                "prior_replicas=%d elapsed_s=%.1f",                WAKE_DEPLOYMENT,                WAKE_NAMESPACE,                prior_replicas,                time.monotonic() - now,            )        else:            logger.info(                "llm_gateway.wake decision=already-starting deployment=%s namespace=%s prior_replicas=%d",                WAKE_DEPLOYMENT,                WAKE_NAMESPACE,                prior_replicas,            )        _wake_state.status = "starting"        _wake_state.since = time.monotonic()        return _wake_state.status

08A truthful Retry-After

Before step 14, every cold caller got an immediate 503 with a flat Retry-After: 150, a number sized for the old cold start. A client that obeyed it waited 150 s for a model that was Ready at 41. Now the hint counts down from how long the wake has actually been running.

ansible/files/llm-gateway/app.py
def _compute_retry_after_seconds() -> int:    if _wake_state.status == "starting":        remaining = WAKE_EXPECTED_SECONDS - (time.monotonic() - _wake_state.since)        return math.ceil(max(WAKE_RETRY_MIN_SECONDS, remaining))    return WAKE_EXPECTED_SECONDS

09Hold the cold request

Instead of answering 503 at once, the gateway now holds the request. It polls vLLM's own /health for up to 60 s and forwards the original request the moment the model is up. Each probe has its own timeout, so a blackholed connection can't hang the hold past its deadline.

ansible/files/llm-gateway/app.py
async def _wait_for_upstream_ready(hold_seconds: float, poll_seconds: float) -> bool:    deadline = time.monotonic() + hold_seconds    while True:        remaining = deadline - time.monotonic()        if remaining <= 0:            return False        probe_timeout = max(0.2, min(WAKE_HOLD_PROBE_TIMEOUT_SECONDS, remaining))        try:            response = await _client.get("/health", timeout=httpx.Timeout(probe_timeout))        except httpx.HTTPError:            response = None        if response is not None and response.status_code == 200:            return True        remaining = deadline - time.monotonic()        if remaining <= 0:            return False        await asyncio.sleep(min(poll_seconds, remaining))

10Putting it together

_handle_upstream_down joins the three pieces: wake, hold, then either forward the original request or, past the window, answer 503 with the counted-down hint. With the hold in place, a client that obeys the hint went from 150.3 s to 41.4 s, with no 503s.

ansible/files/llm-gateway/app.py
async def _handle_upstream_down(    exc: httpx.HTTPError, retry: Callable[[], Awaitable[Response]], upstream_path: str) -> Response:    if not WAKE_DEPLOYMENT:        return _upstream_error_response(exc)    try:        await _ensure_wake_in_flight()    except Exception as scale_exc:        return _wake_error_response(            "model_unavailable",            f"could not scale {WAKE_DEPLOYMENT} in namespace {WAKE_NAMESPACE}: "            f"{type(scale_exc).__name__}: {scale_exc}",        )    if WAKE_HOLD_SECONDS > 0:        hold_start = time.monotonic()        became_ready = await _wait_for_upstream_ready(WAKE_HOLD_SECONDS, WAKE_HOLD_POLL_SECONDS)        held_for = time.monotonic() - hold_start        if became_ready:            _mark_ready()            logger.info("wake: held %.1fs then proxied %s", held_for, upstream_path)            return await retry()        logger.info(            "wake: hold expired after %ds for %s; 503 Retry-After=%d",            round(held_for),            upstream_path,            _compute_retry_after_seconds(),        )    return _wake_error_response(        "model_starting",        f"{WAKE_DEPLOYMENT} is starting back up after being scaled to zero — retry shortly",    )