Hugging Face: Hub, routed/hosted inference, and transformers
Hugging Face is three surfaces, and you should always know which one you are on:
- The Hub — versioned git repos for models, datasets, and Spaces. You search it, you
hf download / hf upload, you read and write model cards.
- Inference — three ways to actually run a model: the Inference Providers router
(serverless, you own nothing), a dedicated Inference Endpoint (you own a deployment
that autoscales), or local
transformers (you own the machine).
- The catalog — 1M+ open models you choose from by task, license, and size.
The whole skill is choosing the right surface for the job and proving it works: a 200 router
response, a live endpoint URL, a pushed repo commit. If the model is open and the workflow
lives on huggingface.co, you are in the right place. Operating the GPU box yourself is
../ollama/SKILL.md (your machine) or ../runpod/SKILL.md
(a rented box); training weights is ../finetuning/SKILL.md.
Decision: how should I run this model?
Pick the row before you write a line of code. The cheapest mistake is standing up infra you
did not need.
| Situation |
Use |
Why |
| Try a model now, low/dev volume, own no infra |
Inference Providers router (InferenceClient) |
Fastest path; monthly credits cover dev. |
| CPU task: embeddings, text-ranking, text-classification, small BERT/GPT-2 |
provider="hf-inference" |
That is exactly its remaining niche as of July 2025. |
| Big LLM (8B, 70B, 405B) through HF |
router with a partner provider (Together/Fireworks/Cerebras/DeepInfra…) |
hf-inference does not serve big LLMs — it will 404 or stall. |
| Steady prod traffic, need fixed latency/SLA |
dedicated Inference Endpoint + scale-to-zero |
Predictable, autoscaling, billed per minute. |
| Interactive demo or shareable GPU app |
Space (Gradio + ZeroGPU) |
Free-ish, public URL, GPU only while a call runs. |
| One-off GPU job (eval, batch convert) |
hf jobs run |
No standing infra; PRO feature. |
| Offline, data-private, or already on a GPU box |
local transformers pipeline() |
No network, no per-call cost. |
Auth & install
pip install "huggingface_hub[inference]" # 1.17.0; needs Python >=3.10
pip install transformers # 5.x line, PyTorch-first, optional/local
hf auth login # stores a token; or export HF_TOKEN=...
- The CLI is
hf now, shaped hf <resource> <action> (hf auth login, hf download,
hf upload, hf repo create, hf jobs run). huggingface-cli still runs but prints a
deprecation warning — do not write it into new scripts.
- Never hardcode a
hf_... token in code — tokens leak the moment the file hits git. Read
from the environment instead:
import os
from huggingface_hub import InferenceClient
client = InferenceClient(api_key=os.environ["HF_TOKEN"]) # never api_key="hf_xxx"
- Token scopes: read to pull public/gated repos and run inference, write to push,
fine-grained to scope to specific repos/orgs — why: a leaked read token cannot overwrite
your models.
Inference Providers — the default path
One router reaches 200+ models across partner providers plus hf-inference; HF passes provider
cost through with no markup. Two equivalent entry points:
# Native client — task methods, NOT the removed .post()
from huggingface_hub import InferenceClient
client = InferenceClient(api_key=os.environ["HF_TOKEN"])
out = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=[{"role": "user", "content": "One sentence on diffusion models."}],
provider="together", # name a partner; or omit for auto-routing
)
print(out.choices[0].message.content)
# OpenAI-compatible — same router, drop-in for existing OpenAI code
from openai import OpenAI
client = OpenAI(
base_url="https://router.huggingface.co/v1", # this exact host, nothing else
api_key=os.environ["HF_TOKEN"],
)
InferenceClient.post() was removed (dropped in hub v0.31.0). Use the task methods:
chat.completions.create(), text_generation(), feature_extraction() (embeddings),
text_to_image(), automatic_speech_recognition().
- Credits are real and small: Free $0.10/mo, PRO $2.00/mo, Team/Enterprise $2.00 per seat
(shared). Past that you are pay-as-you-go and must buy credits. Budget accordingly — why: a
chat loop on a 70B model burns the free tier in minutes.
- A Custom Provider Key bypasses HF billing entirely (the provider bills you; HF credits do
not apply). For org billing, pass
bill_to="org-name" (header X-HF-Bill-To).
- Full recipes (embeddings, image, ASR, streaming, rate-limit handling, the provider list) live
in
references/inference-providers.md.
Hub ops
hf download meta-llama/Llama-3.1-8B-Instruct --include "*.safetensors"
hf repo create my-org/my-model --repo-type model
hf upload my-org/my-model ./out --commit-message "v1 weights"
from huggingface_hub import snapshot_download
path = snapshot_download("BAAI/bge-small-en-v1.5") # full repo, cached, resumable
- Gated models (Llama, Gemma, many others) need you to accept terms on the model page first,
then a token with read access — otherwise the download 403s.
- A model card is a
README.md with YAML front-matter (license, pipeline_tag, tags,
base_model). Ship one on every upload — why: an uncarded repo is unsearchable and unusable by
anyone but you. Command map and hf jobs run details in references/hub-and-cli.md.
Choosing a model
Filter the Hub by task + license + size + recent downloads, then read the card before you
commit. Match the model to your constraint; do not grab whatever is trending.
- Check the license: Apache-2.0/MIT are permissive; Llama/Gemma carry commercial terms and
are gated; "non-commercial"/"research-only" cards mean you cannot ship them.
- Check size vs target: a 70B will not fit a single A10G; an embedding model belongs on CPU.
- Check context length and intended use in the card — the headline number is not always
the usable one.
Dedicated Inference Endpoints — when to graduate
Move off the router when you need fixed latency/SLA, or the router's PAYG cost stops being
predictable. An Endpoint is your own autoscaling deployment.
- Pricing: CPU from ~$0.032/core/hr, GPU from ~$0.50/hr (A10G ~$1.00/hr, H100 ~$6.40–8.00/hr),
billed per minute even though shown hourly.
- Enable scale-to-zero for bursty traffic — it parks at $0 when idle and cold-starts on the
next request. A bursty 100–1000 req/day workload typically lands at $20–60/mo.
- Deploy from the UI or with
huggingface_hub (create_inference_endpoint(...)). Config and a
cost worksheet are in references/endpoints-and-spaces.md.
Spaces + ZeroGPU
A Space hosts a demo app with a public URL. ZeroGPU grabs an H200 MIG slice (~70GB) only
while a decorated function runs, then releases it.
import spaces
@spaces.GPU # GPU acquired for this call only
def generate(prompt: str) -> str:
...
- ZeroGPU is Gradio-SDK only — Streamlit/Docker/static Spaces cannot use it. PRO ($9/mo)
gives 8x daily quota, queue priority, and up to 10 owned ZeroGPU Spaces. Details in
references/endpoints-and-spaces.md.
Local transformers
from transformers import pipeline
pipe = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct",
device_map="auto", torch_dtype="auto")
print(pipe("Hello", max_new_tokens=64)[0]["generated_text"])
pipeline("task", model=...) for quick use; AutoModelForCausalLM.from_pretrained(...) when
you need control over generation/quantization. Set device_map/torch_dtype explicitly.
- Use local only when you are offline, data-private, or already on a GPU. Otherwise the
router is far less ops than babysitting CUDA and weights.
Anti-patterns
| Anti-pattern |
Why it bites |
Do instead |
InferenceClient.post(...) |
Removed in hub v0.31.0; raises |
Task methods: chat.completions.create(), feature_extraction() |
provider="hf-inference" for a 70B/405B LLM |
CPU niche; 404s or stalls |
Route to a partner provider (Together/Fireworks/Cerebras) |
api_key="hf_abc123..." in code |
Token leaks in git history |
Read os.environ["HF_TOKEN"] |
| Spin up a dedicated Endpoint just to try a model |
Burns money idle |
Use the router first; graduate only on real traffic |
| Assuming router calls are free/unlimited |
Free tier is $0.10/mo |
Budget credits; expect PAYG |
| ZeroGPU under Streamlit/Docker SDK |
Unsupported, silently no GPU |
Use the Gradio SDK |
huggingface-cli ... in new scripts |
Deprecated, warns |
Use hf ... |
OpenAI base URL other than https://router.huggingface.co/v1 |
Won't reach the HF router |
Use that exact host |
verify.sh
scripts/verify.sh [TARGET] is a static, read-only linter (no network, no token). It flags the
hard violations above — .post(, hardcoded hf_ tokens, big-LLM-to-hf-inference, wrong router
host — and warns on legacy huggingface-cli. It exits 0 on a clean or empty target.
1---2name: huggingface3description: Use when running open models or working on the Hugging Face platform — the Inference Providers router or InferenceClient, Hub repos via the hf CLI, a dedicated Inference Endpoint with scale-to-zero, a Gradio Space with ZeroGPU, picking an open model by task/license/size, or loading one locally with transformers. NOT serving locally on your own machine (that is `ollama`), NOT renting your own GPU box (that is `runpod`), NOT hosted creative image APIs (that is `replicate-images`), NOT fine-tuning with trl/peft (that is `finetuning`).4---56# Hugging Face: Hub, routed/hosted inference, and transformers78Hugging Face is three surfaces, and you should always know which one you are on:9101. **The Hub** — versioned git repos for models, datasets, and Spaces. You search it, you11 `hf download` / `hf upload`, you read and write model cards.122. **Inference** — three ways to actually run a model: the **Inference Providers** router13 (serverless, you own nothing), a **dedicated Inference Endpoint** (you own a deployment14 that autoscales), or **local `transformers`** (you own the machine).153. **The catalog** — 1M+ open models you choose from by task, license, and size.1617The whole skill is choosing the right surface for the job and proving it works: a 200 router18response, a live endpoint URL, a pushed repo commit. If the model is open and the workflow19lives on huggingface.co, you are in the right place. Operating the GPU box yourself is20[`../ollama/SKILL.md`](../ollama/SKILL.md) (your machine) or [`../runpod/SKILL.md`](../runpod/SKILL.md)21(a rented box); training weights is [`../finetuning/SKILL.md`](../finetuning/SKILL.md).2223## Decision: how should I run this model?2425Pick the row before you write a line of code. The cheapest mistake is standing up infra you26did not need.2728| Situation | Use | Why |29|---|---|---|30| Try a model now, low/dev volume, own no infra | **Inference Providers router** (`InferenceClient`) | Fastest path; monthly credits cover dev. |31| CPU task: embeddings, text-ranking, text-classification, small BERT/GPT-2 | `provider="hf-inference"` | That is exactly its remaining niche as of July 2025. |32| Big LLM (8B, 70B, 405B) through HF | router with a **partner provider** (Together/Fireworks/Cerebras/DeepInfra…) | `hf-inference` does not serve big LLMs — it will 404 or stall. |33| Steady prod traffic, need fixed latency/SLA | **dedicated Inference Endpoint** + scale-to-zero | Predictable, autoscaling, billed per minute. |34| Interactive demo or shareable GPU app | **Space** (Gradio + ZeroGPU) | Free-ish, public URL, GPU only while a call runs. |35| One-off GPU job (eval, batch convert) | `hf jobs run` | No standing infra; PRO feature. |36| Offline, data-private, or already on a GPU box | local `transformers` `pipeline()` | No network, no per-call cost. |3738## Auth & install3940```bash41pip install "huggingface_hub[inference]" # 1.17.0; needs Python >=3.1042pip install transformers # 5.x line, PyTorch-first, optional/local43hf auth login # stores a token; or export HF_TOKEN=...44```4546- **The CLI is `hf` now**, shaped `hf <resource> <action>` (`hf auth login`, `hf download`,47 `hf upload`, `hf repo create`, `hf jobs run`). `huggingface-cli` still runs but prints a48 deprecation warning — do not write it into new scripts.49- **Never hardcode a `hf_...` token in code** — tokens leak the moment the file hits git. Read50 from the environment instead:5152```python53import os54from huggingface_hub import InferenceClient55client = InferenceClient(api_key=os.environ["HF_TOKEN"]) # never api_key="hf_xxx"56```5758- Token scopes: **read** to pull public/gated repos and run inference, **write** to push,59 **fine-grained** to scope to specific repos/orgs — why: a leaked read token cannot overwrite60 your models.6162## Inference Providers — the default path6364One router reaches 200+ models across partner providers plus `hf-inference`; HF passes provider65cost through with **no markup**. Two equivalent entry points:6667```python68# Native client — task methods, NOT the removed .post()69from huggingface_hub import InferenceClient70client = InferenceClient(api_key=os.environ["HF_TOKEN"])71out = client.chat.completions.create(72 model="meta-llama/Llama-3.1-8B-Instruct",73 messages=[{"role": "user", "content": "One sentence on diffusion models."}],74 provider="together", # name a partner; or omit for auto-routing75)76print(out.choices[0].message.content)77```7879```python80# OpenAI-compatible — same router, drop-in for existing OpenAI code81from openai import OpenAI82client = OpenAI(83 base_url="https://router.huggingface.co/v1", # this exact host, nothing else84 api_key=os.environ["HF_TOKEN"],85)86```8788- **`InferenceClient.post()` was removed** (dropped in hub v0.31.0). Use the task methods:89 `chat.completions.create()`, `text_generation()`, `feature_extraction()` (embeddings),90 `text_to_image()`, `automatic_speech_recognition()`.91- **Credits are real and small**: Free $0.10/mo, PRO $2.00/mo, Team/Enterprise $2.00 per seat92 (shared). Past that you are pay-as-you-go and must buy credits. Budget accordingly — why: a93 chat loop on a 70B model burns the free tier in minutes.94- A **Custom Provider Key** bypasses HF billing entirely (the provider bills you; HF credits do95 not apply). For org billing, pass `bill_to="org-name"` (header `X-HF-Bill-To`).96- Full recipes (embeddings, image, ASR, streaming, rate-limit handling, the provider list) live97 in `references/inference-providers.md`.9899## Hub ops100101```bash102hf download meta-llama/Llama-3.1-8B-Instruct --include "*.safetensors"103hf repo create my-org/my-model --repo-type model104hf upload my-org/my-model ./out --commit-message "v1 weights"105```106107```python108from huggingface_hub import snapshot_download109path = snapshot_download("BAAI/bge-small-en-v1.5") # full repo, cached, resumable110```111112- **Gated models** (Llama, Gemma, many others) need you to accept terms on the model page first,113 then a token with read access — otherwise the download 403s.114- A model card is a `README.md` with YAML front-matter (`license`, `pipeline_tag`, `tags`,115 `base_model`). Ship one on every upload — why: an uncarded repo is unsearchable and unusable by116 anyone but you. Command map and `hf jobs run` details in `references/hub-and-cli.md`.117118## Choosing a model119120Filter the Hub by **task + license + size + recent downloads**, then read the card before you121commit. Match the model to your constraint; do not grab whatever is trending.122123- Check the **license**: Apache-2.0/MIT are permissive; Llama/Gemma carry commercial terms and124 are gated; "non-commercial"/"research-only" cards mean you cannot ship them.125- Check **size vs target**: a 70B will not fit a single A10G; an embedding model belongs on CPU.126- Check **context length** and **intended use** in the card — the headline number is not always127 the usable one.128129## Dedicated Inference Endpoints — when to graduate130131Move off the router when you need fixed latency/SLA, or the router's PAYG cost stops being132predictable. An Endpoint is your own autoscaling deployment.133134- Pricing: CPU from ~$0.032/core/hr, GPU from ~$0.50/hr (A10G ~$1.00/hr, H100 ~$6.40–8.00/hr),135 **billed per minute** even though shown hourly.136- Enable **scale-to-zero** for bursty traffic — it parks at $0 when idle and cold-starts on the137 next request. A bursty 100–1000 req/day workload typically lands at **$20–60/mo**.138- Deploy from the UI or with `huggingface_hub` (`create_inference_endpoint(...)`). Config and a139 cost worksheet are in `references/endpoints-and-spaces.md`.140141## Spaces + ZeroGPU142143A Space hosts a demo app with a public URL. **ZeroGPU** grabs an H200 MIG slice (~70GB) only144while a decorated function runs, then releases it.145146```python147import spaces148@spaces.GPU # GPU acquired for this call only149def generate(prompt: str) -> str:150 ...151```152153- **ZeroGPU is Gradio-SDK only** — Streamlit/Docker/static Spaces cannot use it. PRO ($9/mo)154 gives 8x daily quota, queue priority, and up to 10 owned ZeroGPU Spaces. Details in155 `references/endpoints-and-spaces.md`.156157## Local transformers158159```python160from transformers import pipeline161pipe = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct",162 device_map="auto", torch_dtype="auto")163print(pipe("Hello", max_new_tokens=64)[0]["generated_text"])164```165166- `pipeline("task", model=...)` for quick use; `AutoModelForCausalLM.from_pretrained(...)` when167 you need control over generation/quantization. Set `device_map`/`torch_dtype` explicitly.168- Use local only when you are **offline, data-private, or already on a GPU**. Otherwise the169 router is far less ops than babysitting CUDA and weights.170171## Anti-patterns172173| Anti-pattern | Why it bites | Do instead |174|---|---|---|175| `InferenceClient.post(...)` | Removed in hub v0.31.0; raises | Task methods: `chat.completions.create()`, `feature_extraction()` |176| `provider="hf-inference"` for a 70B/405B LLM | CPU niche; 404s or stalls | Route to a partner provider (Together/Fireworks/Cerebras) |177| `api_key="hf_abc123..."` in code | Token leaks in git history | Read `os.environ["HF_TOKEN"]` |178| Spin up a dedicated Endpoint just to try a model | Burns money idle | Use the router first; graduate only on real traffic |179| Assuming router calls are free/unlimited | Free tier is $0.10/mo | Budget credits; expect PAYG |180| ZeroGPU under Streamlit/Docker SDK | Unsupported, silently no GPU | Use the Gradio SDK |181| `huggingface-cli ...` in new scripts | Deprecated, warns | Use `hf ...` |182| OpenAI base URL other than `https://router.huggingface.co/v1` | Won't reach the HF router | Use that exact host |183184## verify.sh185186`scripts/verify.sh [TARGET]` is a static, read-only linter (no network, no token). It flags the187hard violations above — `.post(`, hardcoded `hf_` tokens, big-LLM-to-`hf-inference`, wrong router188host — and warns on legacy `huggingface-cli`. It exits 0 on a clean or empty target.