What is llama.cpp?

5 min readMarkdown

llama.cpp is an open-source inference engineInference engineThe layer that does the actual maths, such as llama.cpp or MLX.Read more written in C and C++. If a local AI app runs on Windows, Linux and Mac alike, there is a good chance llama.cpp or its core library is underneath.

What it is

llama.cpp started as a way to run Meta's first Llama model on a MacBook CPU. It grew into a general engine for almost every open model, with GPU support for every major vendor. The project lives at github.com/ggml-org/llama.cpp under the MIT licence.

Almost everything is built on it for three reasons. It has no Python dependency, so it ships as a small binary, and it runs on more hardware than any other engine. New models usually get support within days of release, often on day one.

LM Studio, Jan, KoboldCpp and many others bundle it. Ollama uses its core maths library, called ggml. When you use one of those apps, you are using llama.cpp with some settings chosen for you.

GGUF

GGUF is llama.cpp's file format. One .gguf file holds the weightsWeightsThe full set of a model's parameters, stored in a file.Read more, the tokenizer, the chat template and the model's settings. You download one file and you have everything.

GGUF files come in many quantizations of the same model, with labels such as Q4_K_M or Q8_0. Very large models are split into parts named like model-00001-of-00003.gguf. Point llama.cpp at the first part and it finds the rest.

Most GGUF files live on Hugging Face. Where do I download models? covers which uploaders to trust.

Backends

llama.cpp compiles to a different backendBackendThe hardware path an engine uses: Metal, CUDA, Vulkan, ROCm or CPU.Read more for each kind of chip. You pick it by downloading the right build, or by choosing it when you compile.

BackendRuns onNotes
MetalApple silicon MacsOn by default in Mac builds
CUDANVIDIA GPUsUsually the fastest path on NVIDIA
ROCm (HIP)AMD GPUsSupported cards only. See Do AMD and Intel work for local AI?
VulkanMost GPUs, any vendorThe easy path for AMD and Intel. Often a little slower than the vendor's own
CPUAnythingUses AVX on x86 and NEON on ARM. Slow for anything above a small model

You can split a model between GPU and CPU. Layers that do not fit in GPU memory run on the CPU from system RAM. That works, but each layer left on the CPU slows every tokenTokenA chunk of text, usually part of a word. Models read and write in tokens.Read more. Which NVIDIA card should I get? explains offloadingOffloadRunning part of a model on the CPU because it does not fit in VRAM.Read more.

llama-server and its web UI

llama-server is the part most people want. It loads one model, serves the OpenAI-compatible API and ships a simple chat page in the browser.

shell
llama-server -m ~/models/model-Q4_K_M.gguf -ngl 99 -c 16384 --port 8080

Open http://localhost:8080 for the chat page. Point any OpenAI client at http://localhost:8080/v1 for the API.

llama-server can also download a model from Hugging Face for you with -hf and a repository name. It caches the file so the next start is instant.

shell
llama-server -hf ggml-org/gemma-3-1b-it-GGUF

Two other binaries ship alongside it. llama-cli runs a chat in the terminal. llama-bench measures speed, covered in How do I benchmark my own setup?.

The six flags that matter

llama.cpp has well over a hundred options. These six cover nearly every case.

FlagWhat it setsWhere to start
-ngl, --n-gpu-layersHow many layers go on the GPU99 to put everything on the GPU. Lower it only if you run out of memory
-c, --ctx-sizeContext length in tokensWhat your task needs. Agents need 32K tokens or more
-t, --threadsCPU threads for work on the CPUYour number of performance cores. Matters only when layers run on the CPU
-b, --batch-sizeTokens processed at once during prefillLeave the default unless long prompts are slow
-np, --parallelNumber of request slots served at once1 for one user. Raise it for several agents
--cache-type-k, --cache-type-vPrecision of the KV cachef16 by default. q8_0 halves its memory for a longer context
Rule of thumb

Set -ngl 99 and the smallest -c your work needs. Those two flags decide most of your speed and memory.

The catch

With -np, the context is divided between the slots. -c 32768 -np 4 gives each slot about 8K tokens, not 32K. Raise -c when you raise -np.

A quantized V cache needs flash attention turned on. The flag is -fa or --flash-attn. How do I make it faster? covers when KV cacheKV cacheSaved work from earlier tokens, so the model does not reread the whole conversation for every new token.Read more quantizationQuantizationStoring each weight with fewer bits so the model takes less memory.Read more is worth it.

Build or download

Prebuilt binaries are the fast path. The GitHub releases page has builds for each OS and backend, updated many times a week. Pick the file that names your backend, such as CUDA or Vulkan.

shell
brew install llama.cpp
PowerShell
winget install llama.cpp
shell
brew install llama.cpp

Homebrew on Linux gives you a CPU or Vulkan build. For CUDA or ROCm, build from source.

Build from source when you want a backend the packages lack, or the newest fix today. You need CMake and a C++ compiler, plus the CUDA or ROCm toolkit for those backends.

shell
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release

Swap -DGGML_CUDA=ON for -DGGML_VULKAN=ON or -DGGML_HIP=ON for other backends. On a Mac, leave it out and Metal is on by default. The binaries land in build/bin.

Go deeperThat's enough

Why updates matter so much

llama.cpp has no slow release train. Each merged change becomes a numbered build, often several a day. New model architectures, faster kernels and fixes to chat templatesChat templateThe exact wrapper of tags a model expects around each message.Read more all arrive this way.

That is why an app's bundled copy matters. An app shipping a build from a few months back can be missing support for a new model, or run it slower. What is a runtime, and what sits under my app? covers why the same file runs at different speeds in different apps.

Tool calls and chat templates

For tool callingTool callingA model asking the app to run a function, such as a search or a shell command.Read more, start llama-server with --jinja. It then applies the chat template stored in the GGUF file, which is what most models need to format tool calls correctly. Without it, tool calls are hit and miss. Why does my model ramble or answer itself? explains what the template does.

Speculative decoding

llama-server can run a small draft model alongside the main one with -md or --model-draft. The draft guesses several tokens ahead and the main model checks them in one pass. Both models must share a tokenizer. How do I make it faster? covers when it pays off.

Memory mapping

By default llama.cpp memory-maps the GGUF file instead of reading it all in. The operating system pages the weights in as needed. That is why a model can start fast the second time: the file is still in the OS cache.