# What is llama.cpp?

> llama.cpp is the open-source engine under most local AI apps. It reads GGUF files and runs on almost any hardware.

llama.cpp is an open-source inference engine 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](https://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](https://localagi.co/lm-studio), Jan, KoboldCpp and many others bundle it. [Ollama](https://localagi.co/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 weights, the tokenizer, the [chat template](https://localagi.co/chat-templates) and the model's settings. You download one file and you have everything.

GGUF files come in many [quantizations](https://localagi.co/quantization) 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?](https://localagi.co/where-models-come-from) covers which uploaders to trust.

## Backends

llama.cpp compiles to a different backend for each kind of chip. You pick it by downloading the right build, or by choosing it when you compile.

| Backend | Runs on | Notes |
| --- | --- | --- |
| Metal | Apple silicon Macs | On by default in Mac builds |
| CUDA | NVIDIA GPUs | Usually the fastest path on NVIDIA |
| ROCm (HIP) | AMD GPUs | Supported cards only. See [Do AMD and Intel work for local AI?](https://localagi.co/amd-and-intel) |
| Vulkan | Most GPUs, any vendor | The easy path for AMD and Intel. Often a little slower than the vendor's own |
| CPU | Anything | Uses 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 token. [Which NVIDIA card should I get?](https://localagi.co/nvidia-gpus) explains offloading.

## llama-server and its web UI

`llama-server` is the part most people want. It loads one model, serves the [OpenAI-compatible API](https://localagi.co/local-api) and ships a simple chat page in the browser.

```bash
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.

```bash
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?](https://localagi.co/benchmarking).

## The six flags that matter

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

| Flag | What it sets | Where to start |
| --- | --- | --- |
| `-ngl`, `--n-gpu-layers` | How many layers go on the GPU | `99` to put everything on the GPU. Lower it only if you run out of memory |
| `-c`, `--ctx-size` | Context length in tokens | What your task needs. Agents need 32K tokens or more |
| `-t`, `--threads` | CPU threads for work on the CPU | Your number of performance cores. Matters only when layers run on the CPU |
| `-b`, `--batch-size` | Tokens processed at once during prefill | Leave the default unless long prompts are slow |
| `-np`, `--parallel` | Number of request slots served at once | `1` for one user. Raise it for several agents |
| `--cache-type-k`, `--cache-type-v` | Precision of the KV cache | `f16` 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?](https://localagi.co/tuning-for-speed) covers when KV cache quantization 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.

**macOS**

```bash
brew install llama.cpp
```

**Windows**

```powershell
winget install llama.cpp
```

**Linux**

```bash
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.

```bash
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 deeper

### 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 templates 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?](https://localagi.co/the-stack) covers why the same file runs at different speeds in different apps.

### Tool calls and chat templates

For tool calling, 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?](https://localagi.co/chat-templates) 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?](https://localagi.co/tuning-for-speed) 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.
