Fix "You are trying to access a gated repo" (401) on Hugging Face
OSError: You are trying to access a gated repo. Make sure to have access to it at https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct. 401 Client Error.
- huggingface_hub.errors.GatedRepoError: 401 Client Error. (Request ID: Root=1-...)
- Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/resolve/main/config.json.
- Access to model meta-llama/Llama-3.1-8B-Instruct is restricted. You must have access to it and be authenticated to access it. Please log in.
- 403 Client Error. Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/resolve/main/config.json. Access to model meta-llama/Llama-3.1-8B-Instruct is restricted and you are not in the authorized list. Visit https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct to ask for access.
The model is gated: you must accept its license on its Hugging Face page, then authenticate with a token. Request access on the model page, create a read token, log in with hf auth login or set HF_TOKEN, then try again. A 403 error means you are authenticated but access has not been granted (yet).
What this error means
Some authors, such as Meta for Llama, Google for Gemma or Mistral for some models, protect their repos: you must be logged in and have accepted the terms of use to download their files. When transformers, diffusers or huggingface_hub request config.json or the weights without a valid token, the Hub answers HTTP 401 and the library raises GatedRepoError, which transformers turns into OSError: You are trying to access a gated repo. With a valid token but no access granted to the repo, the answer becomes 403 “not in the authorized list”. Access is granted per account and per repo.
Common causes
- No token sent: no
hf auth loginon this machine and noHF_TOKENvariable, especially on a server, in CI or in a container. - License not accepted or request pending: access was never requested, is still being reviewed, or was denied (403 error).
- Token without the right permissions: a fine-grained token lacking read access to gated repos, or a token from a different account than the one granted access.
- Old token taking precedence: a stale or revoked
HF_TOKENvariable overrides the token saved byhf auth login.
Step-by-step fix
-
Request access on the model page
Log in on huggingface.co, open the model page and accept the license (filling in the form if there is one). Some repos grant access immediately, others after a manual review. Track your requests in your account settings, under Gated Repositories. Each repo is separate: access to Llama 3.1 does not grant access to Llama 3.2.
-
Create an access token
In your account settings, under Access Tokens, create a “Read” token. If you prefer a fine-grained token, enable the permission to read the contents of public gated repos you can access. Treat this token like a password.
-
Log in with the CLI
hf auth loginsaves the token locally; every Hugging Face library then uses it automatically.hf auth whoamiconfirms the active account. The olderhuggingface-cli logincommand still works but is replaced byhf. In a notebook, usefrom huggingface_hub import login; login().python -m pip install -U huggingface_hub hf auth login hf auth whoami # quick test: download a single file hf download meta-llama/Llama-3.1-8B-Instruct config.json -
Use HF_TOKEN on a server, in CI or in Docker
Without an interactive session, provide the token through the
HF_TOKENenvironment variable, stored in your CI or hosting secrets. Never write it in code, the Dockerfile or the Git repo. With Docker,-e HF_TOKENpasses the host value without writing it in the command.# macOS / Linux export HF_TOKEN=hf_your_token # Windows PowerShell: $env:HF_TOKEN = "hf_your_token" docker run --rm -e HF_TOKEN my-image python app.py -
Pass the token explicitly in code if needed
The parameter is called
token(the olduse_auth_tokenis deprecated). Read it from the environment rather than pasting it into the script.import os from transformers import AutoModelForCausalLM, AutoTokenizer repo = "meta-llama/Llama-3.1-8B-Instruct" token = os.environ["HF_TOKEN"] tokenizer = AutoTokenizer.from_pretrained(repo, token=token) model = AutoModelForCausalLM.from_pretrained(repo, token=token) -
Tell 401 and 403 apart
401: the request is not authenticated (no token, invalid or revoked token); check
hf auth whoamiand remove a staleHF_TOKEN. 403: you are authenticated but have no access; wait for approval, and check that the token belongs to the account that requested access and has the gated-repo permission.# macOS / Linux: drop a stale variable, then check again unset HF_TOKEN hf auth whoami # Windows PowerShell: Remove-Item Env:HF_TOKEN
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use a dedicated token per machine or pipeline, with minimal (read) permissions, and revoke it as soon as it is no longer used.
- Request access to gated models before depending on them in a deployment: approval can take several hours.
- Download the weights once into the cache (
HF_HOME) or into the image, so production does not need a token at every start.
Frequently asked questions
How long does it take to get access to Llama?
It depends on the author: often a few minutes to a few hours. While the request is pending, the Hub answers 403 even with a valid token.
Where is the token stored after hf auth login?
In the Hugging Face cache folder, by default ~/.cache/huggingface/token. The HF_TOKEN variable takes precedence over that file.
Do I need a paid account for gated models?
No. Access to gated repos is free: you only need an account, to accept the license and to use a token.
Useful tools for this error
More AI & LLMs errors
- Fix error 429 "Rate limit reached" on OpenAI, Claude and Gemini APIs
- Fix "maximum context length" and "prompt is too long" (context_length_exceeded)
- Fix "CUDA out of memory" in PyTorch
- Fix error 401 "Incorrect API key provided" / "invalid x-api-key"
- Fix "Torch not compiled with CUDA enabled" and torch.cuda.is_available() returning False
- Fix "could not connect to ollama" and connection refused on port 11434
- Fix "JSONDecodeError: Expecting value" when parsing an LLM response