llama.cpp and GGUF: Install, Quantize, Run llama-server
What is llama.cpp?
llama.cpp is an open-source (MIT) C/C++ library and a set of tools for running language models on CPU and GPU. It reads models from GGUF files, which hold the weights together with metadata. llama-server exposes a model over an OpenAI-compatible HTTP API, llama-quantize compresses weights, and llama-bench and llama-perplexity measure speed and loss.
TL;DR
- -As of November 29, 2026 the stable version is 1.1.0: there is now a llama command with subcommands and semver releases, the server listens on port 9931, Jinja templates are on from the start, and the --draft-max and --draft-min flags are removed.
- -Memory is the file plus the KV cache. With Qwen3.5-9B Q4_K_M, the llama-server process took 5.63 GiB at a context of 4,096, 6.51 GiB at 32,768 and 9.51 GiB at 131,072. The growth matched the calculation from the config: 32 KiB per token.
- -Perplexity on one text: Q8_0 3.6662, Q4_K_M 3.6966, Q3_K_M 3.8122. Q4 is 0.8% worse than Q8, Q3 is 4% worse. This is a rough check on 68.8 KB of text and doesn't replace a test on your own task.
- -In the test, llama-server with no extra flags returned a function call and JSON that follows a schema on Qwen3.5-9B.
- -The article has no speed numbers: on a busy machine, repeating the same run gives noticeably different results. Instead there is a llama-bench command and the signs of an invalid run.
llama.cpp is an open-source (MIT) C/C++ library and a set of tools for running language models on CPU and GPU. It reads models from GGUF files: one file contains the weights and the metadata. The llama.cpp engine works inside Ollama and LM Studio, but you can also run it directly. The minimal set of commands:
brew install llama.cpp # macOS and Linux
llama cli -hf unsloth/Qwen3.5-9B-GGUF:Q4_K_M # chat in the terminal
llama serve -hf unsloth/Qwen3.5-9B-GGUF:Q4_K_M -c 32768 # HTTP API, port 9931
What was tested and what wasn’t. The test ran on November 29, 2026: llama.cpp 1.1.0 from Homebrew (build 12774), a MacBook with an Apple M4 Max and 36 GB, macOS 27.2.1, the unsloth/Qwen3.5-9B-GGUF model in Q3_K_M, Q4_K_M and Q8_0 quants. The SHA-256 of the files matched those on Hugging Face. The files were built by Unsloth, not by the model’s authors. Server memory, perplexity, a function call and JSON by schema were tested, one run each. Windows, Linux, the router, speculative decoding, multimodality and making your own quant are described from the documentation for tag v1.1.0.
What changed in llama.cpp 1.0 and 1.1
Version 1.0.0 came out on November 13, 2026, and 1.1.0 on November 26. Semver tags started earlier, with 0.x releases; from 1.0.0 on, the major number goes up when the public C API changes incompatibly. Some older instructions no longer apply.
| In older instructions | In 1.1.0 |
|---|---|
llama-cli, llama-server | still work; a llama command with cli, serve, bench, quantize and perplexity subcommands now sits next to them |
http://localhost:8080 | default port 9931 |
--jinja for tool calling | on by default, turned off with --no-jinja |
--draft-max, --draft-min | removed, replaced by --spec-draft-n-max, --spec-draft-n-min or the --spec-ngram-mod-* parameters |
only bNNNN versions | semver for releases, bNNNN for nightly builds |
Prebuilt binaries on GitHub Releases are still in the nightly builds. Release 1.1.0 points to build b12774, and the Homebrew package showed the same number. According to the release process description, stable versions reach users through the installer, package managers and building from the tag.
Installing llama.cpp
| Method | Command | Where it works |
|---|---|---|
| Homebrew | brew install llama.cpp | macOS, Linux |
| Winget | winget install llama.cpp | Windows |
| conda-forge | conda install -c conda-forge llama.cpp | Windows, macOS, Linux |
| Installer from the README | curl -LsSf https://llama.app/install.sh | sh | macOS, Linux |
| Docker | ghcr.io/ggml-org/llama.cpp:server | Linux/amd64, arm64, s390x |
The channels are listed in docs/install.md and docs/docker.md. The installer downloads a prebuilt binary from storage on Hugging Face, and if HF_TOKEN is in the environment, it sends the token in its requests. Read the script before running it.
The Homebrew package weighed 22.2 MB in the test. It installs both llama and separate tools, among them llama-server, llama-bench, llama-quantize, llama-imatrix and llama-perplexity. llama has no imatrix subcommand, so call llama-imatrix.
Metal on macOS is on by default. The number of layers on the GPU is set with -ngl: the default is auto, and a number or all also works.
First run and the model cache
The -hf <author>/<model>[:quant] flag downloads a GGUF from Hugging Face. Without a quant it takes Q4_K_M, and if there is none, the first file in the repository. The image projector (mmproj) is downloaded automatically; for this model F16 and BF16 weigh 0.92 GB each. If you only need text, add --no-mmproj.
The files go to the Hugging Face cache (~/.cache/huggingface/hub); LLAMA_CACHE sets a different directory. llama cli -cl lists what has been downloaded. If Hugging Face is unavailable, a local file will do:
shasum -a 256 models/Qwen3.5-9B-Q4_K_M.gguf # compare with the hash on the file page
llama serve -m models/Qwen3.5-9B-Q4_K_M.gguf -c 32768
curl -s http://127.0.0.1:9931/health # {"status":"ok"}, 503 while loading
In the test the server was ready in 2 seconds. The file was already in the OS cache; a cold load takes longer.
GGUF: what’s inside the file and how to read the name
GGUF is a binary format with key-value metadata: architecture, layer sizes, tokenizer. Under the specification, one file is enough to load a model, and it can be memory-mapped with mmap instead of read in full.
| Part of the name | Meaning |
|---|---|
Qwen3.5-9B | model and parameter count |
Q4_K_M | quantization scheme |
mmproj-... | multimodal projector, loaded together with the main model |
mtp-... | Multi-Token Prediction heads for speculative decoding |
-00001-of-00004 | part of a model split into several files |
Quants with the UD- prefix in Unsloth repositories are the publisher’s own scheme; llama.cpp has no type with that name.
Quantization: Q3, Q4, Q8 and quality loss
Quantization reduces the number of bits per weight. The name rounds the real figure: in the llama-quantize table for Llama 3.1 8B, Q3_K_M has 4.00 bits per weight, Q4_K_M 4.89, Q6_K 6.56 and Q8_0 8.50. The “parameters × bits / 8” formula and a table of models for different amounts of memory are in the local LLM guide.
Perplexity shows how well a model predicts the next token; lower is better. In the test the same 68.8 KB English text went through three quants with -c 2048:
| Qwen3.5-9B file | Size | Model in memory | Perplexity | vs Q8_0 |
|---|---|---|---|---|
| Q8_0 | 9.53 GB | 8.86 GiB | 3.6662 ± 0.082 | - |
| Q4_K_M | 5.68 GB | 5.28 GiB | 3.6966 ± 0.082 | +0.8% |
| Q3_K_M | 4.67 GB | 4.34 GiB | 3.8122 ± 0.087 | +4.0% |
Q4_K_M saves 40% of the file size and barely changes the result on this text. Q3_K_M is another 18% smaller than Q4 but loses noticeably more.
The error after ± describes the spread across the text, and the intervals of all three rows overlap. It doesn’t follow that there’s no difference: the models were scored on the same tokens, so the difference has to be estimated pairwise. In the llama.cpp table for Llama 3 8B, the error of Q4_K_M perplexity itself is 0.039, while the error of its difference from FP16 is only 0.005. KL divergence mode does the pairwise comparison:
llama-perplexity -m m/Q8_0.gguf -f text.txt -c 2048 --kl-divergence-base q8.kld
llama-perplexity -m m/Q4_K_M.gguf -f text.txt -c 2048 --kl-divergence-base q8.kld --kl-divergence
The logit file is large: for Wikitext-2 the documentation gives 37 GiB for Llama 3. On a short text of your own it’s smaller. The author hasn’t run this mode. Don’t compare perplexity between different models: their tokenizers differ, and the documentation warns about this.
Your own quant and imatrix
Quantize from BF16 or F16. The conversion script is in the repository; the Homebrew package doesn’t include it:
git clone --depth 1 --branch v1.1.0 https://github.com/ggml-org/llama.cpp && cd llama.cpp
uv pip install -r requirements.txt --index-strategy unsafe-best-match
python convert_hf_to_gguf.py --outfile m-bf16.gguf --outtype bf16 --remote <author>/<model>
llama-imatrix -m m-bf16.gguf -f calibration.txt -o imatrix.gguf -ngl 99
llama-quantize --imatrix imatrix.gguf m-bf16.gguf m-Q4_K_M.gguf Q4_K_M
imatrix (importance matrix) is collected on a calibration text and indicates which weights to round more carefully. In the same Llama 3 8B table it cut the perplexity increase of Q3_K_M from 0.66 to 0.50 and of Q4_K_M from 0.18 to 0.15: the coarser Q3 gains more. In 1.1.0 llama-quantize got --target-bpw and --target-size, which choose tensor types for a given budget. --dry-run checks the plan without writing a file.
Context and KV cache: how much memory llama-server takes
Process memory (RSS) with Q4_K_M, -np 1 --fit off:
Context -c | llama-server RSS | Measured growth | Calculated growth |
|---|---|---|---|
| 4,096 | 5.63 GiB | - | - |
| 32,768 | 6.51 GiB | 0.88 GiB | 0.875 GiB |
| 131,072 | 9.51 GiB | 3.88 GiB | 3.875 GiB |
The calculation uses config.json. Qwen3.5-9B has hybrid attention: only 8 of its 32 layers keep a full KV cache. Size per token: 8 layers × 2 (K and V) × 4 KV heads × 256 × 2 bytes (f16) = 32 KiB. Calculation and measurement differ by less than a hundredth of a GiB, so for the full window of 262,144 you can expect about 13.5 GiB (a calculation, not a measurement).
A model with regular attention has a KV cache in every layer and uses more per token. Don’t carry these numbers over to another architecture; calculate from its config.
Flags that affect memory:
-c Nsets the context size. The value 0 (default) takes the model’s window.--fit on(default) adjusts the parameters you haven’t set to free memory. The--fit-targetmargin is 1,024 MiB per device, and the lower bound on context,--fit-ctx, is 4,096. It doesn’t touch an explicit-c.llama-fit-paramsprints the outcome in advance.-np Nis the number of slots. Without the flag, server 1.1.0 creates 4 slots with a unified KV cache. If you set-npby hand, the unified cache is off and the context is split:-c 32768 -np 4gives 8,192 tokens per request.-ctk,-ctvset the KV cache type, f16 by default; there are also q8_0, q4_0 and f8_e4m3, which appeared in 1.0.0. The documentation doesn’t estimate quality loss, and the author didn’t measure it.-cramis the prompt cache in RAM, up to 8,192 MiB. It fills up during operation and didn’t show in the startup measurement.
For a Mac with 36 GB the order is: the file, plus the KV cache, plus a margin for the system and open applications. In the measurement from the local LLM guide, Ollama sees 28.1 GiB on such a machine. This limit wasn’t measured separately for llama.cpp. What goes into the context and why a model forgets the start of a conversation is covered in the context window article.
llama-server: OpenAI API, tool calling and JSON
llama serve and llama-server are the same program. According to the documentation it serves /v1/chat/completions, /v1/responses and /v1/embeddings in OpenAI format, /v1/messages in Anthropic format and a built-in web chat. Reranking is enabled separately with --rerank. For a shared team interface you can connect the server to Open WebUI, and for a terminal agent, to OpenCode.
curl -s http://127.0.0.1:9931/v1/chat/completions \
-H 'Content-Type: application/json' -d '{
"messages": [{"role": "user", "content": "What is the weather in Kazan?"}],
"tools": [{"type": "function", "function": {
"name": "get_weather", "description": "Weather in a city",
"parameters": {"type": "object",
"properties": {"city": {"type": "string"}}, "required": ["city"]}}}]
}'
What the server returned in the test, with no extra flags. The test prompts were in Russian and the request above is a translation, so the values came back in Russian: Казань is Kazan, Токио is Tokyo.
| Check | Result |
|---|---|
| Function call | finish_reason=tool_calls, get_weather {"city":"Казань"} |
response_format: json_schema | {"capital": "Токио"}, reasoning separate from the answer |
Reasoning in the second request took 2,392 characters for a one-word answer. According to the documentation, "reasoning_effort": "none" in the request or --reasoning off at startup turns it off. One successful call shows that the mechanism works but says nothing about how often the model will get your schemas wrong. Validating arguments and deciding when to retry is covered in the article on function calling and structured output.
The function calling page lists native formats, and Qwen3.5 isn’t among them, although the call worked in the test. If the model prints the call as plain text, look for Chat format: Generic in the log and set a template with --chat-template-file. Parallel calls are enabled with "parallel_tool_calls": true.
Server security
- By default the server listens on
127.0.0.1and there is no key. Before changing--host, set a key throughLLAMA_API_KEYor--api-key-file: a key in the--api-keyargument is visible in the process list. The/healthendpoint is open even with a key. - By default CORS reflects any
Origin. If web pages don’t need access, set--cors-origins localhost. --toolsand--agentlet the model read and write files and run commands. The documentation asks not to enable them in an untrusted environment. Outside loopback without a key, a server with tools refuses to start; only--allow-unauthenticated-toolsbypasses this.- MCP servers from
--mcp-servers-configrun with the server’s own permissions.
Several models on one port
llama-server without a model starts as a router: it loads the model named in the request’s model field and unloads the least recently used ones. This is the closest counterpart to Ollama’s background server.
llama-server --models-dir ./models --models-max 2 -c 16384
curl -s http://127.0.0.1:9931/models # names of available models
By default up to 4 models are kept at once. A limit by count knows nothing about memory, so either fix -c and --models-max or set --models-memory-margin: then the router unloads idle models before loading, to make room. Per-model parameters go into an INI file for --models-preset. Put the projector file in the model’s subdirectory; its name must start with mmproj. --sleep-idle-seconds unloads a model when idle.
Speculative decoding and multimodality
With speculative decoding the server guesses several tokens ahead and verifies them in one pass. The type is set with --spec-type; it’s off by default.
| Type | What it needs | When to try it |
|---|---|---|
ngram-mod | nothing, a pool of about 16 MB | code edits, summarization, repetitive text |
draft-mtp | MTP heads in the model or an mtp-* file | models that have such a file |
draft-simple | a small draft model, -md | a large model and its smaller version |
draft-eagle3, draft-dflash* | a draft trained for the model | such a draft is published |
The simplest start is --spec-default: it enables ngram-mod. unsloth/Qwen3.5-9B-GGUF has no MTP file; ggml-org/Qwen3.8-27B-GGUF does (from 1.68 GB). The gain depends on the share of accepted tokens and wasn’t measured for this article.
llama-cli and llama-server accept images, audio and video. For a local file, specify the projector explicitly: -m model.gguf --mmproj mmproj.gguf. The documentation advises keeping the projector in BF16 or Q8: it takes little space and directly affects answer quality.
Speed: how to measure it and when not to trust a run
Speed depends on machine load: on a busy machine, repeating the same run gives noticeably different results, so there are no numbers here. The command below measures it on your machine.
uptime
llama-bench -m m/Q3_K_M.gguf -m m/Q4_K_M.gguf -m m/Q8_0.gguf \
-p 512,4096 -n 128 -r 5 --delay 10 -o md | tee bench-1.md
uptime
# repeat with the files in reverse order, output to bench-2.md
llama-bench reports prompt processing (pp) and generation (tg) separately; tokenization and sampling time isn’t included. What a token is and why non-English text takes more of them is explained in the tokens article.
Three signs give away an invalid run:
- The repeat doesn’t reproduce. The difference between runs is larger than the spread inside each of them. The
±value in thellama-benchtable refers to repetitions within a run and doesn’t show this error. - The order of quants is implausible. Generation on a GPU is limited by reading weights from memory, so a smaller file of the same model shouldn’t lose badly to a larger one. If it does, look for interference first, not for a property of the quant.
- Load average is rising. Something else was running during the test: a build, an emulator, indexing. Record
uptimebefore and after each run.
When the numbers can be compared
Close builds, emulators and heavy tabs, plug in power and check that swap isn’t growing. Run the whole set twice in a different order and keep the result if the runs agree within their internal spread. Record the llama.cpp version, file name, context and KV cache type: without them the numbers can’t be compared with anything. Application measurements on an M4 Max with 36 GB, with the method, are in the LM Studio and Ollama comparison. They can’t be carried over to plain llama-server.
llama.cpp, Ollama or LM Studio
- llama.cpp when you need a specific GGUF, control over the KV cache and slots, your own quant, loss measurement or a server without extra layers.
- Ollama when a library of ready models and a background service are enough. Installation and Modelfile are described in the Ollama guide.
- LM Studio when a graphical interface and a catalog are more convenient.
Don’t expect a speedup just from switching to llama.cpp: both applications run GGUF on the same engine, and the difference comes down to version and settings.
Common errors
| Symptom | Cause | What to do |
|---|---|---|
localhost:8080 doesn’t respond | the default port is 9931 | fix the address or set --port 8080 |
The --draft-max flag isn’t accepted | removed | --spec-draft-n-max |
| Context smaller than expected | -c not set and --fit kicked in, or the context was split between slots | set -c, check n_ctx_slot in the log |
-hf downloaded an extra file | the projector loads automatically | --no-mmproj |
| Memory grows during operation | prompt cache, -cram up to 8,192 MiB | reduce -cram, 0 turns it off |
| The model prints a function call as text | template not recognized | --chat-template-file, check Chat format |
| Your quant answers worse than expected | an already quantized file was quantized | take BF16 or F16 |
Limitations of the test
One run each, one model, one machine. Perplexity was computed on a short English text from the author’s own articles: the picture may differ for other languages, code and long context. The reference was Q8_0, not F16. RSS shows the process right after startup, without load or parallel requests. The function call and JSON were checked with one request each.
Where to start
- Install llama.cpp and check the version:
llama version. - Download the Q4_K_M of your model and compare the SHA-256.
- Calculate the KV cache from
config.jsonand set-cexplicitly. - Run
llama serve, then check/health, a function call and JSON on your schemas. - Compare Q4 with Q8 on 20-30 of your own requests, and through KL divergence if you need a figure.
- Measure speed twice on an idle machine.
- Open the port to the outside only with a key and behind an HTTPS proxy.