Skip to content
FR
  1. LuzineDebug
  2. AI & LLMs
  3. OSError: You are trying to access a gated repo.…
AI & LLMs Beginner ≈ 10 min to fix

Fix "You are trying to access a gated repo" (401) on Hugging Face

Error message
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.
In short

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).

Be the first to confirm this fix

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

  1. No token sent: no hf auth login on this machine and no HF_TOKEN variable, especially on a server, in CI or in a container.
  2. License not accepted or request pending: access was never requested, is still being reviewed, or was denied (403 error).
  3. 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.
  4. Old token taking precedence: a stale or revoked HF_TOKEN variable overrides the token saved by hf auth login.

Step-by-step fix

  1. 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.

  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.

  3. Log in with the CLI

    hf auth login saves the token locally; every Hugging Face library then uses it automatically. hf auth whoami confirms the active account. The older huggingface-cli login command still works but is replaced by hf. In a notebook, use from huggingface_hub import login; login().

    bash
    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
  4. Use HF_TOKEN on a server, in CI or in Docker

    Without an interactive session, provide the token through the HF_TOKEN environment variable, stored in your CI or hosting secrets. Never write it in code, the Dockerfile or the Git repo. With Docker, -e HF_TOKEN passes the host value without writing it in the command.

    bash
    # 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
  5. Pass the token explicitly in code if needed

    The parameter is called token (the old use_auth_token is deprecated). Read it from the environment rather than pasting it into the script.

    python
    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)
  6. Tell 401 and 403 apart

    401: the request is not authenticated (no token, invalid or revoked token); check hf auth whoami and remove a stale HF_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.

    bash
    # 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

All AI & LLMs errors →