What 'sleeping' actually means
A free-tier Hugging Face Space is a container that gets suspended after 48 hours of no incoming traffic. The image and any persistent storage stick around; only the running process and its RAM are torn down. The next request wakes the container back up, which involves rescheduling onto a host, restoring the filesystem, and re-running the entrypoint. This is the classic serverless cold-start pattern applied to long-running ML apps that were never really designed to boot on demand. Understanding what happens in each phase is the fastest path to making Hugging Face Spaces cold starts tolerable — or avoiding them entirely.
Cold start phases and where the seconds go
- Scheduling: 1–3 seconds to place the container on a host with capacity
- Filesystem restore: 2–10 seconds to hydrate persistent volumes
- Container start + Python interpreter: 3–8 seconds
- Model loading from disk into RAM/VRAM: 10–90 seconds, dominant for LLM demos
- First-token warmup (CUDA kernels, torch.compile, TGI prefill): 2–20 seconds
Why LLM Spaces feel especially slow
For a Gradio app that loads a 500MB image classifier into RAM, the whole cold start might be 10–30 seconds and users tolerate it. For a Docker Space with a big CUDA base image and a 7B model, cold start creeps to 60–120 seconds because weight loading and CUDA kernel compilation dominate. Every megabyte of PyTorch import time and every module resolved at first token adds to the wait. Quantization helps (int4/int8 halves the disk-to-VRAM time), safetensors helps (mmap is faster than pickle), and pre-compiling with `torch.compile` inside the entrypoint hides some CUDA warmup — but none of these eliminates the fundamental problem, which is that big models are big.
How to eliminate cold starts entirely
- Upgrade the Space hardware — any paid CPU/GPU tier (CPU upgrade, T4, A10G, A100) runs always-on until you pause or downgrade
- Use ZeroGPU (Pro subscription) — shared H100 pool with fast attach, cold start is measured in seconds, not minutes
- Move to an Inference Endpoint — dedicated infra, no sleep, per-second billing but zero surprise
- Ship a static Space — HTML/JS only, no Python runtime, no cold start at all
ZeroGPU: the middle ground
ZeroGPU is a shared H100 pool for Hugging Face Pro users. You decorate a function with `@spaces.GPU`, and the GPU is attached only for the duration of the call. Between calls the Space runs on CPU and the GPU is released to other users. This makes ZeroGPU dramatically cheaper than a dedicated H100 (which would be $8–10/hr always-on) for demos and low-traffic apps, at the cost of a small per-call attach latency (typically 1–3 seconds). It is the correct answer for community demos, model spotlight pages, and internal review apps where dedicated hardware is overkill.
The warm-ping approach, and why it's fragile
A common hack: run a cron job that hits the Space every 30 minutes to prevent the 48-hour idle from tripping. This works, but it undermines the shared-fairness model the free tier is built on and Hugging Face may throttle or block traffic that looks synthetic. If your Space needs to stay warm continuously it needs to be a paid Space, or a ZeroGPU Space, or an Endpoint — not a cron-warmed free tier. Treat warm-pings as a bridge, not a strategy.
Loader-side optimizations that actually help
- Convert PyTorch .bin weights to safetensors — mmap makes load 2–4x faster
- Enable torch_dtype=torch.float16 (or bfloat16) to halve weight size in VRAM
- Prefer AWQ/GPTQ quantized variants when quality allows — 4x smaller weights, 2x faster load
- Lazy-import heavy libraries inside handler functions, not at module top
- Warm CUDA kernels with a dummy 1-token generation in the entrypoint
When to give up on Spaces and use an Endpoint
Spaces are optimized for demos, model spotlights, and internal tools. If any of the following is true, cold starts on Spaces will fight you forever and an Inference Endpoint is the better answer: users are outside your org and expect sub-second first-response, the model is above 13B parameters, you need horizontal autoscaling, you need private networking or org-level ACLs, or you have an SLO. Endpoints cost more but eliminate the class of cold-start problems that Spaces will never fully solve.