Skip to content
mentoor

Topic 4 of 6 · 6 min read

Running GGUF files with llama.cpp

Goal: Write a llama-server command that serves a GGUF file with full GPU offload and an 8k context.

llama.cpp runs GGUF model files directly, and you control placement and memory with a few flags: how many layers go to the GPU, how long the context is and how many CPU threads help.

Check yourself · 1 of 3

Which llama-server flag sets the context size in tokens?

Choose one answer

Why it matters

Ollama and LM Studio are built on top of llama.cpp or use similar code. Using it directly gives you every option and the newest features first, at the price of reading flags. Knowing the main ones also helps you understand what the friendlier tools do for you.

The two programs

  • llama-cli runs a prompt or chat in the terminal.
  • llama-server serves an OpenAI-compatible HTTP API and a small web page, on port 8080 by default.

The flags that matter

Flag Meaning
-m model.gguf path to the model file
-ngl 99 layers to offload to the GPU; a number larger than the layer count means all
-c 8192 context size in tokens
-t 8 CPU threads for the layers that stay on the CPU
--port 8080 server port

A complete command:

llama-server -m ./models/qwen2.5-7b-instruct-q4_k_m.gguf -ngl 99 -c 8192 --port 8080

Check it with curl http://localhost:8080/health, then send a chat request to /v1/chat/completions.

Building for your hardware

llama.cpp is built with CMake, and the backend is a build option. A binary built without your backend runs on the CPU only, and nothing warns you loudly.

cmake -B build -DGGML_CUDA=ON      # NVIDIA
cmake -B build -DGGML_VULKAN=ON    # many AMD and Intel GPUs
cmake -B build                     # macOS: Metal is on by default
cmake --build build --config Release

Option names have changed over time, so read the build page for your version. Prebuilt release binaries also exist for common platforms.

Before and after

Weak: llama-server -m model.gguf on a machine with a 24 GB GPU. Nothing is offloaded, the 7B model generates a few tokens per second on the CPU, and the GPU sits at 0 percent.

Better: add -ngl 99 -c 8192, then read the startup log to confirm that all layers were offloaded.

What goes wrong

  • A CPU-only build. The flag -ngl is accepted but does nothing, because the build has no GPU backend.
  • Too many layers for the VRAM. Loading fails with an out-of-memory error. Lower -ngl or the context.
  • Context mismatch. Asking for a context longer than the model was trained for degrades answers.

Examples

Fully offloaded server

llama-server -m ./qwen2.5-7b-instruct-q4_k_m.gguf \
  -ngl 99 -c 8192 --port 8080

All layers on the GPU, an 8k context. If it runs out of memory, lower -c first, then -ngl.

Partial offload

llama-server -m ./big-model-q4_k_m.gguf -ngl 28 -c 4096 -t 8

A model too large for the GPU keeps 28 layers there and the rest on 8 CPU threads. It works, and it is slower than full offload.

Key points

  • llama-cli is for the terminal and llama-server exposes an OpenAI-compatible API, on port 8080 by default.
  • Use -m for the file, -ngl for GPU layers, -c for context size and -t for CPU threads.
  • Set -ngl to a number at least as large as the layer count to offload everything.
  • The GPU backend is chosen at build time: CUDA, Vulkan or Metal; a build without it runs on the CPU.
  • Always read the startup log to confirm what was actually offloaded.

Common mistakes

  • Forgetting -ngl and running a large model entirely on the CPU.
  • Using a build without the CUDA, Vulkan or Metal backend and expecting GPU speed.
  • Setting -c far above what the memory allows and getting an out-of-memory failure.
  • Treating -t as a speed knob for GPU work; threads only help the CPU part.

Check yourself · 2 of 3

llama-server starts with -ngl 99 on a machine with an NVIDIA GPU, yet the GPU stays idle. What is the most likely cause?

Choose one answer

Check yourself · 3 of 3

Which command serves model.gguf with all layers on the GPU and an 8192-token context?

Choose one answer

Practice items, not counted towards your mastery.

Start here