Hugging Face

Access gated models (Llama, FLUX) and fix 401 errors

3 min read

Models like Llama and FLUX are gated: you must accept their licence and authenticate before any download works. Miss either step and every request returns a 401 or 403.

1. Accept the licence on the model page

Log in at huggingface.co, open the model, and click Agree and send request to access repo (some repos label it Acknowledge license). If approval is automatic you get access immediately; if the author reviews manually, you wait. Access is granted per user, never to a whole organization. The flow is described in the official gated-models guide.

2. Create a read or fine-grained token

Go to Settings, then Access Tokens, then New token. A read token downloads any repo you can read, including gated ones you have been granted. For production prefer a fine-grained token scoped to only the models you need. Token roles are explained in the security-tokens docs. Never print a token to logs or commit it.

3. Log in with hf auth login

hf auth login

Paste the token when prompted; it is stored under HF_HOME. Confirm which account it belongs to:

hf auth whoami

Prints your username. This must be the account that holds the licence approval.

4. Set HF_TOKEN for scripts and CI

macOS / Linux:

export HF_TOKEN=hf_xxx

Windows (PowerShell):

$env:HF_TOKEN = "hf_xxx"

HF_TOKEN overrides the stored login for that process, which is how you authenticate in CI without running hf auth login. In GitHub Actions put it in a repository secret, not the workflow file.

5. Use the token in Python

from huggingface_hub import login
login()  # reads HF_TOKEN, or prompts if unset

Logs the whole process in. Or authenticate one call only:

from huggingface_hub import hf_hub_download
hf_hub_download("meta-llama/Llama-3.1-8B-Instruct", "config.json", token="hf_xxx")

The token= argument works the same way on from_pretrained in transformers and diffusers.

6. Fix 401 and 403 errors

Verify it worked

hf download meta-llama/Llama-3.1-8B-Instruct config.json

Prints a local file path with no 401 or 403. Auth and licence are both in place.


Next: download models with the hf CLI · generate images with FLUX locally.

Open the full interactive version (with copy buttons) ↗

← All guides