Skip to content

Local inference with llama.cpp

Last updated: 2026-11-24T13:10:14-03:00 (UTC-3)

llama.cpp serves GGUF models through llama-server. On a Mac with Apple Silicon it uses the GPU through Metal; anywhere it also runs on the CPU alone. This page sets up a local inference pool the gateway starts and supervises. The commands and numbers were run on a Mac Mini M4 Pro (24 GB, macOS 26) with llama.cpp 1.0.0 and Qwen2.5-0.5B-Instruct in the Q4_K_M quantization.

Run llama.cpp natively. Docker on macOS does not see the Apple GPU.

Terminal window
brew install llama.cpp
which llama-server # /opt/homebrew/bin/llama-server
mkdir -p /opt/models
curl -fL -o /opt/models/qwen2.5-0.5b-instruct-q4_k_m.gguf \
https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf

The gateway never downloads weights: put the .gguf file where you want it and name that directory in model_dirs.

features:
local_inference: true
local_inference_gpu: true # leave these two off to serve on the CPU
local_inference_metal: true
local_inference:
supervise: true
runtimes:
llama_cpp: /opt/homebrew/bin/llama-server
model_dirs: [/opt/models]
pools:
- id: qwen-metal
backend: llama_cpp
mode: supervise
model: qwen05
alias: local-qwen
weights: /opt/models/qwen2.5-0.5b-instruct-q4_k_m.gguf
replicas: 3
slots: 2
context_tokens: 2048
port_base: 14201
gpu: {mode: metal, share_device: true}
routing_strategy: least_busy

Add local-qwen to the allowed models of the keys that may call it, then start the gateway. supervise and runtimes are read at start.

What the gateway runs for replica 1, so you can reproduce it by hand:

Terminal window
/opt/homebrew/bin/llama-server -m /opt/models/qwen2.5-0.5b-instruct-q4_k_m.gguf \
--host 127.0.0.1 --port 14201 --alias qwen05 -ngl 99 --parallel 2 -c 4096 --metrics
  • -ngl 99 offloads every layer to the GPU. With gpu.mode: off, or with the GPU flags off, the gateway passes -ngl 0.
  • -c is context_tokens times slots: llama.cpp divides its context among the slots, and context_tokens is the window of one request.
  • --metrics is added unless scrape_metrics is false.
  • An NVIDIA build of llama.cpp uses gpu.mode: nvidia; each replica gets its device in CUDA_VISIBLE_DEVICES.
Terminal window
curl -s -H "Authorization: Bearer $GATEWAY_ADMIN_TOKEN" http://localhost:8080/api/v1/admin/local-inference
curl -s http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer $GATEWAY_KEY" -H "Content-Type: application/json" \
-d '{"model":"local-qwen","stream":true,"max_tokens":160,"messages":[{"role":"user","content":"Explain a hash map."}]}'

Each replica goes from starting to ready once the model is loaded: 7 to 8 seconds on the first start here, about 1 second on a restart.

Check that the GPU is really in use. A model server that fell back to the CPU still answers. Compare a GPU pool with a CPU pool (gpu.mode: off) of the same model, and watch the GPU while they decode:

Terminal window
ioreg -r -d 1 -w 0 -c IOAccelerator | grep -o '"Device Utilization %"=[0-9]*'

Streamed chat, 160 output tokens, measured through the gateway unless the row says direct:

Setup Concurrent streams First token (median) Decode per stream Total tokens/s GPU utilization
One Metal replica, direct 1 5 ms 247 tok/s 225 69% average, 81% peak
One CPU replica (8 threads), direct 1 7 ms 171 tok/s 159 8% average
Three Metal replicas of two slots, least_busy 1 17 ms 263 tok/s 236 76%
Three Metal replicas of two slots, least_busy 6 31 ms 103 tok/s 536 94%
Three Metal replicas of two slots, least_busy 12 1,552 ms 103 tok/s 602 99%
One Metal replica of two slots, direct 6 1,440 ms 223 tok/s 444 82%
One CPU replica of two slots 2 21 ms 118 tok/s 210 3%

How to read it:

  • Size the slots to your concurrency. Past replicas × slots concurrent streams, requests queue inside the model server and the first token waits (1.5 s at 12 streams on 6 slots).
  • Replicas on one Mac buy slots, not much throughput. The GPU is one shared budget: three replicas gave 536 tokens per second against 444 for one, and no queueing up to six streams. That is why this pool needs share_device: true and why the gateway warns about it.
  • The GPU gain is modest on Apple Silicon (247 against 171 tokens per second here): decoding is bound by memory bandwidth, which the CPU and the GPU share. Give the CPU pool enough threads before comparing (extra_args: ["-t", "8"] in the YAML).

A replica killed with kill -9 in the middle of 36 requests at 6 concurrent streams: every request answered HTTP 200, one that had not started moved to another replica, two streams already running were cut (reported to the client as stream_interrupted and counted in gateway_provider_errors_total). The gateway restarted the replica after its 2 second backoff and it was ready 3.2 seconds after the kill. An operator Stop under the same load drained in 0.9 seconds and cut nothing.

m7gtwcontact@m7gtw.com