A nightly job calls a Hugging Face Inference Endpoint that scales to zero, and its first requests fail with HTTP 503 — what is happening and how do you fix it?
answer
- nobody is home after idle
- proxy answers while a replica boots
- a request header that makes it wait
- the Python client raises, the JS client re-sends
- pause and resume around a batch
basics
~20 sThe endpoint scaled to zero after its idle window, so the first request starts a cold replica and the proxy returns 503 until it is ready. Send X-Scale-Up-Timeout so the proxy waits, resume explicitly, or keep a replica up.
solid answer
~40 sWith scale-to-zero on, the endpoint drops to zero replicas after its idle window (the dashboard's default is 1 hour; in `huggingface_hub` it is `min_replica=0` plus `scale_to_zero_timeout` in minutes). The next request triggers a cold start that can take minutes, and the proxy answers `503` while the replica initializes. Fixes, cheapest first: send `X-Scale-Up-Timeout: 600` so the proxy holds each request up to 600 seconds; in Python pass it through `InferenceClient(headers=...)`, because in `huggingface_hub` 2.1.1 the client raises the 503 instead of retrying. For a scheduled batch, call `resume().wait()` before and `pause()` after. If callers cannot tolerate cold starts, keep `min_replica` at 1 or more and pay for the uptime.
code
python · 14 linesimport os
from huggingface_hub import InferenceClient
client = InferenceClient(
base_url=os.environ["ENDPOINT_URL"], # https://<id>.<region>.<vendor>.endpoints.huggingface.cloud
headers={"X-Scale-Up-Timeout": "600"}, # proxy holds requests up to 600 s instead of answering 503
timeout=660, # client waits a little longer than the proxy
)
reply = client.chat.completions.create(
messages=[{"role": "user", "content": "Summarise the overnight error log"}],
max_tokens=200,
)
print(reply.choices[0].message.content)go deeper
Recall that a scaled-to-zero endpoint has no replica running, so the next request must wait for one to start and may get a 503 meanwhile.
Explain the settings involved: the idle window, min_replica=0 with scale_to_zero_timeout in minutes, and the X-Scale-Up-Timeout header that makes the proxy hold requests.
Pick the fix by caller: header plus matching client timeout for batch calls, resume and pause around scheduled jobs, minimum replicas for interactive traffic, and know how each client treats a 503.
Treat cold starts as a product decision: which callers may see a multi-minute first response, and whether idle savings justify that against the cost of an always-on replica.
## What scale-to-zero does to the first request An Inference Endpoint with **scale-to-zero** enabled removes its last replica after a period with no requests. The dashboard's default idle window is **1 hour**; through `huggingface_hub`, `create_inference_endpoint` and `update_inference_endpoint` take `min_replica=0` together with `scale_to_zero_timeout`, measured in **minutes**. Note the library defaults: `min_replica` is 1 and `scale_to_zero_timeout` is `None`, so an endpoint created from code never scales to zero unless you ask for it. A scaled-to-zero endpoint keeps its URL and wakes on the next inference request, but waking is a full **cold start**: the platform allocates an instance, pulls the container and the model files, and starts the engine. The Endpoints docs warn this can take a few minutes depending on the model (why replica start-up is slow is a general serving question, not a Hugging Face setting). Until a replica is ready, the endpoint's **proxy answers `503`**. A nightly job that fires immediately after hours of silence therefore meets 503s on its first requests every single night. ## Fixes, cheapest first 1. **Ask the proxy to wait.** Add the request header `X-Scale-Up-Timeout: <seconds>`. While the endpoint is scaling, the proxy holds the request until a replica is ready or the timeout passes, so `X-Scale-Up-Timeout: 600` turns a 503 into a slow first response. Set the client's own timeout a little longer than the header value. 2. **Wake the endpoint on purpose.** For a scheduled batch, pause between runs instead of relying on scale-to-zero: `get_inference_endpoint(name).resume().wait()` before the job and `endpoint.pause()` after it. `wait()` polls the endpoint's status every few seconds and, once it reports `running`, checks the health route for a 200. 3. **Keep a replica up.** If interactive callers share the endpoint and cannot absorb a cold start, set `min_replica` to 1 or more. The Endpoints FAQ also advises at least two minimum replicas for an endpoint that must stay available, since a single replica can itself return 503 in production. ## How each client reacts to the 503 | Client | On a 503 | What to do | |---|---|---| | `InferenceClient` in `huggingface_hub` 2.1.1 | raises an HTTP error; the request path does not retry | pass `headers={"X-Scale-Up-Timeout": "600"}` and a matching `timeout` | | `InferenceClient` in `@huggingface/inference` 4.13 | re-sends the request at once by default (`retry_on_error`), with no delay | still send the header, or disable `retry_on_error` and back off yourself | | raw HTTP or another SDK | returns the 503 to your code | send the header, or retry with backoff | The JavaScript behaviour deserves attention: an immediate re-send loop during a multi-minute scale-up generates a burst of requests rather than a pause. ## Pause versus scale-to-zero - **Both cost nothing** while down; billing covers time spent initializing or running. - **A paused endpoint stays down** until something calls `resume()`; stray traffic cannot wake it, and calls fail until you resume. - **A scaled-to-zero endpoint wakes itself** on the next inference request, with the cold start and the 503 window. - **`wait()` does not wake anything.** It only polls status, so calling it on a scaled-to-zero endpoint without sending a request just waits until its timeout, or forever without one. Pause suits a job with a known schedule; scale-to-zero suits irregular traffic that tolerates a slow first call. ## Setting it up from code 1. **Create with scale-to-zero on purpose:** `create_inference_endpoint(..., min_replica=0, scale_to_zero_timeout=60)` scales to zero after 60 idle minutes; leave `min_replica` at its default of 1 and it never will. 2. **Warm it before a known spike:** `endpoint.update(min_replica=1)` keeps a replica up through a launch, and setting it back to 0 restores the savings afterwards. 3. **Script the batch lifecycle:** `hf endpoints resume <name>`, `hf endpoints pause <name>` and `hf endpoints scale-to-zero <name>` do the same from a shell step in a scheduler. 4. **Cap the ceiling too:** `max_replica` bounds how far traffic can scale the endpoint up, which is the other half of the cost control. ## Seeing it in the dashboard The endpoint's status moves through **Scaled to Zero**, **Initializing** and **Running**; its logs show the engine starting while the client sees 503s. If 500s rather than 503s appear during scale-up, the container is being treated as ready before the model loads: configure a health route that returns 200 only when the model can serve.
- Why is `endpoint.wait()` alone not enough to warm a scaled-to-zero endpoint?`wait()` polls the endpoint's status and health route; it sends no inference request, and a scaled-to-zero endpoint wakes only when an inference request arrives. Either send a request carrying `X-Scale-Up-Timeout`, or switch the job to `pause()` and `resume()`, after which `wait()` correctly blocks until the endpoint is running.
- When is scale-to-zero the wrong setting for an Inference Endpoint?When a user-facing caller cannot wait minutes for the first answer after idle. The Endpoints docs say scaling from 0 to 1 on a request is typically not recommended for responsive applications; keep `min_replica` at 1 or more, or two for high availability, and accept paying for idle hours.
- A scaled-to-zero endpoint keeps serving instantly because its URL stays live.
- The 503 means the endpoint was deleted or its token expired.
- huggingface_hub's InferenceClient automatically retries the 503 until the model loads.
- endpoint.wait() wakes a scaled-to-zero endpoint by itself.
- Endpoints created from huggingface_hub scale to zero after 1 hour by default.
Assumes huggingface_hub 2.1.1 and @huggingface/inference 4.13.30; how a 503 is handled is client-release behaviour, not an Endpoint setting.