llama.cpp --hf-repo: download the exact GGUF quantization and verify it before loading
TL;DR: llama.cpp can fetch a GGUF straight from Hugging Face with --hf-repo <user>/<model>[:quant], where the quant suffix is optional, case-insensitive, and defaults to Q4_K_M before falling back to the first file in the repository. The file it downloads is not self-describing on the command line, so the honest check is to read general.file_type out of the GGUF metadata and compare it with the quantization you asked for. One number in the file header tells you whether you are running the 4-bit file you planned or a different one.
What you’ll be able to do:
- Ask
llama.cppfor one named quantization instead of accepting whatever the repository lists first. - Read the quantization that actually landed on disk from the GGUF metadata, before the model eats your VRAM.
- Tell a truncated download or a saved HTML error page apart from a real model file in one command.
- Size a download against your card before you start it, using the file list instead of a guess.
Honest take:
--hf-repopicks a file. It does not verify that the file is the one you think you downloaded, and it does not check the bytes against a published checksum. The metadata read is what turns “I asked for Q4_K_M” into “this file is Q4_K_M”.
Why the quantization is the first decision, not the last
A GGUF file is a container of tensors, and the quantization decides how many bits each weight takes. At 16 bits per weight the file is roughly twice the size of the same model at 8 bits, and roughly four times the size at 4 bits. That single choice sets three things at once: how long the download takes, how much of the file has to sit in VRAM or RAM at load time, and how much quality the model loses.
The practical consequence for a home lab is that “download the model” is three decisions wearing one hat: which repository, which quantization, and which file inside that repository. llama.cpp has a documented flag for all three, and the failure mode when you skip the second one is a model that loads happily and behaves differently than the benchmark you read.
If you are choosing hardware based on a model’s size, work the numbers out first with the VRAM calculator before you start a 20 GB transfer.
What --hf-repo actually does
The upstream server documentation, checked November 16, 2026, defines the flag precisely. The relevant part, from tools/server/README.md in the llama.cpp repository:
-hf, -hfr, --hf-repo <user>/<model>[:quant]— Hugging Face model repository; quant is optional, case-insensitive, default to Q4_K_M, or falls back to the first file in the repo if Q4_K_M doesn’t exist. mmproj is also downloaded automatically if available. to disable, add —no-mmproj
Three rules hide in that sentence, and each one has a failure mode worth naming.
| Behaviour | Documented rule | What goes wrong if you do not know it |
|---|---|---|
| Quant suffix | Optional, case-insensitive, defaults to Q4_K_M | You get Q4_K_M whether you asked for it or not, which is the wrong file for a 24 GB card running a 70B model |
| Missing quant | Falls back to the first file in the repository | The “first file” is whatever the uploader sorted first, which is often a full-precision or an F16 build |
| Multimodal projector | mmproj is fetched automatically if the repository has one | The model loads but has no vision, and the error points at your prompt instead of the missing projector |
Two companion flags make the selection explicit. --hf-file FILE overrides the quant suffix, so you can name one exact file inside a repository that publishes several. --hf-token TOKEN supplies a Hugging Face token for gated repositories and defaults to the HF_TOKEN environment variable when it is not given. The upstream source in common/arg.cpp is where the download plan is assembled: it resolves the repository into a set of files, downloads the primary file first, and points the model path at it.
# a named quantization, with the projector
llama-server -hf ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF:Q8_0
# the same repository, but the file named explicitly
llama-server -hf ggml-org/Qwen2.5-Coder-7B-Q8_0-GGUF -hf-file qwen2.5-coder-7b-q8_0.gguf
# a gated repository
HF_TOKEN=hf_xxx llama-server -hf owner/private-model-repo:Q4_K_M
Note what the default does not do. llama.cpp will not warn you that the repository has no Q4_K_M file and that it fell back to something else. The fallback is silent, which is exactly why the next step is worth doing.
The file is not named after the quantization, so read the metadata
A GGUF file starts with a fixed 24-byte header: the magic value GGUF, a version number, the tensor count, and the count of metadata key-value pairs. The metadata that follows holds the model’s metadata, and one key is the one that answers “which quantization is this file?” — general.file_type, an unsigned 32-bit integer whose values are defined in the LlamaFileType enum in the upstream gguf-py package.
general.file_type | Quantization | Typical fit on a consumer card |
|---|---|---|
| 15 | Q4_K_M | The default; the best quality-per-byte compromise for most 24 GB builds |
| 17 | Q5_K_M | Noticeably larger; worth it only when quality matters more than the last gigabyte |
| 18 | Q6_K | Close to full precision, needs the memory to match |
| 7 | Q8_0 | Effectively lossless for most purposes, twice the size of Q4_K_M |
| 30 | IQ4_XS | i-quants need an importance matrix to be worth using |
| 1 | F16 | Twice the size of Q4_K_M; only for a card with room |
| 32 | BF16 | Full precision for hardware that supports it |
Writing that check is about thirty lines of Python, because the format is deliberately simple. The code below parses the header, walks the metadata pairs, and prints the quantization it found:
import struct
GGUF_MAGIC = 0x46554747 # "GGUF"
FILE_TYPES = {15: "Q4_K_M", 17: "Q5_K_M", 18: "Q6_K", 7: "Q8_0",
30: "IQ4_XS", 1: "F16", 32: "BF16"}
def read_file_type(path):
with open(path, "rb") as fh:
data = fh.read()
if len(data) < 24:
raise ValueError("file shorter than the 24-byte GGUF header")
magic, version, tensors, kv_count = struct.unpack("<IIQQ", data[:24])
if magic != GGUF_MAGIC:
raise ValueError("bad magic 0x%08X" % magic)
off = 24
for _ in range(kv_count):
(klen,) = struct.unpack_from("<Q", data, off); off += 8
key = data[off:off + klen].decode("utf-8", "replace"); off += klen
(vtype,) = struct.unpack_from("<I", data, off); off += 4
if vtype != 4: # only uint32 values are needed here
raise ValueError("unsupported value type %d for %s" % (vtype, key))
(val,) = struct.unpack_from("<I", data, off); off += 4
if key == "general.file_type":
return FILE_TYPES.get(val, "UNKNOWN(%d)" % val)
return "not set"
print(read_file_type("model.gguf"))
What the check returns when it is run
I ran this against synthetic GGUF fixtures built to the documented layout — a 24-byte header plus a metadata block — because the point of the check is the comparison, not the model. Six cases, six expected outcomes:
| Case | Result |
|---|---|
Asked for Q4_K_M, file metadata says Q4_K_M | MATCH |
Asked for Q8_0, file metadata says Q8_0 | MATCH |
Asked for Q4_K_M, file metadata says Q8_0 | MISMATCH |
Asked for Q5_K_M, file metadata says Q4_K_M | MISMATCH |
| File truncated after 10 bytes | UNREADABLE — shorter than the 24-byte header |
HTML error page saved with a .gguf name | UNREADABLE — bad magic 0x6F64213C |
The last two rows are the ones that save an evening. A 404 page written to disk as model.gguf does not fail with a network error later; it fails with a header mismatch inside the inference engine, and the message points at the model rather than at the download. If you want the full integrity story for Ollama’s own blob store, verify the SHA256 digest of each layer instead — that is a different mechanism and answers a different question.
How the file is loaded changes how much RAM you need
The same server documentation describes how the model file reaches memory, through -lm, --load-mode MODE:
| Mode | What the upstream documentation says | When it helps on a home box |
|---|---|---|
auto | Memory-map the model, unless the device does not support it | The default; the model pages in as the GPU needs it |
none | No special loading mode | Rarely what you want on a workstation |
mmap | Memory-map the model; slower load, but may reduce pageouts if not using mlock | A model that mostly fits in VRAM on a machine with plenty of RAM |
mlock | Force the system to keep the model in RAM rather than swapping or compressing | A model that does not fit in VRAM and would otherwise start paging mid-generation |
mmap+mlock | Both | The same, with the faster load path |
dio | Use DirectIO if available | Filesystem-dependent; not a setting to start with |
The environment variable LLAMA_ARG_LOAD_MODE sets the same thing. On a box where the model is larger than the card, mlock is the difference between a prompt that answers in seconds and one that stalls while the page cache thrashes. That is also the setting where system RAM becomes the budget that matters, which is the topic of how much system RAM local LLMs need.
The KV cache is a separate dial. -ctk, --cache-type-k TYPE and -ctv, --cache-type-v TYPE accept f32, f16, bf16, f8_e4m3, q8_0, q4_0, q4_1, iq4_nl, q5_0, q5_1, and default to f16 on both. Quantizing the cache is how you fit a long context into a card that could not hold it in f16; if you need the full picture, the KV cache offload guide covers where the memory goes.
Honest take: what --hf-repo does not promise
The flag selects a file from a repository. Everything security-shaped is still on you.
- No checksum in the documented behaviour. The upstream README describes selection and download, not digest verification. If integrity matters to you, verify the file separately, the way the safetensors header check does for the other format.
- A silent fallback. If the repository has no
Q4_K_Mfile, you get the first file instead, with no warning. The metadata check is how you find out. - The repository is the publisher.
--hf-repodownloads whatever that account publishes. Naming the publisher is part of the command, not an accident. - A matched quantization is not a matched model. The same quantization name exists across dozens of repositories for different base models. The metadata confirms the format, not the weights’ provenance; for that, compare digests or check the model card.
If the file you want is already on disk, none of this matters — point llama-server at the path. The value of --hf-repo is for the moment you are choosing between four quantizations of the same model and want the choice recorded in the command line rather than in your memory.
Sources
- llama.cpp server documentation:
--hf-repo,--hf-file,--hf-token,--load-mode,--cache-type-k/v— llama.cpp GitHub - llama.cpp argument handling and Hugging Face download plan —
common/arg.cpp - GGUF file type enum (
LlamaFileType) and thegeneral.file_typemetadata key —gguf-pyconstants - llama.cpp project README and build instructions — llama.cpp GitHub
- llama.cpp server documentation index — GitHub
Last updated November 16, 2026. The upstream flags and defaults above were read from the llama.cpp repository on this date. The metadata check was verified locally against synthetic GGUF fixtures built to the documented layout; no model weights were downloaded and no inference was run. Quantization sizes and fit change with every model and every driver — check the current listing before you download.
Was this article helpful?
Thanks for the feedback — it helps improve future articles.
Need hands-on help?
I offer 1-on-1 technical consulting for local AI setup, GPU selection, and AI coding tool configuration — same topics covered on this site.
Book a session — $49 / hour →Get the numbers before you buy
New GPU and mini-PC benchmarks, VRAM thresholds, and price checks — sent when there's something worth acting on, not on a schedule. No spam, unsubscribe anytime.