llama.cpp memory and GGUF
llama.cpp memory usage is weights (GGUF quant) plus KV cache (context length × batch). Queries like llama cpp fit target, llama.cpp quantization, and what is llamacpp often land here — GGUF format and --fit-target are the levers that keep edge inference usable.
Hub: llama.cpp.
GGUF quantization
Models ship as GGUF files with embedded quantization:
| Quant | Size vs FP16 | Quality | Typical use |
|---|---|---|---|
| Q4_K_M | ~4× smaller | Good for most tasks | Default starting point |
| Q5_K_M | ~3× smaller | Better than Q4 on code | IDE assistants |
| Q8_0 | ~2× smaller | Near full precision | Quality-sensitive local work |
| F16 | Baseline | Best | When VRAM allows |
Download from Hugging Face (TheBloke, bartowski, or official repos) or pull via Ollama which uses llama.cpp under the hood.
llama cpp and llama.cpp refer to the same runtime — GGUF is the model format it expects.
Context and RAM
| Variable | Effect |
|---|---|
-c / --ctx-size | Max context tokens — drives KV cache size |
| Model size (quant) | Weight memory in RAM/VRAM |
-ngl | Layers on GPU — rest stays in system RAM |
Rule: larger -c without more RAM increases swap thrashing on laptops. Cap per task — do not default to 128K for agent loops.
Datacenter VRAM tables: TGI hardware — same weights-vs-KV-cache tradeoff at different scale.
--fit and --fit-target
llama-server can auto-tune unset memory args to fit your GPU or unified memory:
| Flag | Purpose |
|---|---|
--fit on | Adjust unset args to fit device memory (default: on) |
--fit-target 1024 | Reserve margin in MiB per device (default 1024); comma-separated per GPU |
--fit-ctx 4096 | Minimum context --fit may shrink to (default 4096) |
Example: 24 GB RTX 4090 with 8B Q5 — leave headroom for desktop GPU workloads:
./llama-server \
-m models/llama-3.2-8b-instruct.Q5_K_M.gguf \
--host 0.0.0.0 --port 8080 \
-ngl 99 \
--fit on \
--fit-target 2048If you set -c explicitly, --fit respects your cap when possible. Lower --fit-target when other GPU workloads run alongside the server.
Sizing checklist
- Pick quant — start Q4_K_M, bump to Q5_K_M for code if RAM allows
- Set
-n(max output tokens) on every call - Set
-cto task need (4K–8K for most agent turns) - Use
--fit-targeton shared GPUs - Benchmark quality on your repo — do not assume Q2 is fine for code