25  Recipe: serve a llama.cpp scan target

Field Value
Tool llama-cpp (llama-server)
Section Ops
Level medium
Time ~20 min + model download
Layer/Category OPS / o-04
Tags ops, serving, target-endpoint, llama.cpp, gguf

25.1 Problem

The task: a model target without a heavy model-server runtime footprint. The shape: one binary that loads a GGUF file and answers HTTP. llama.cpp’s llama-server is that shape. Its GGUF/quant toolchain packages a model artefact, verified elsewhere, for serving. On a Kailash host, the catalogue row is the llama.cpp package (native, safety safe, category o-04 with o-02 as secondary). The task: the server’s real invocation shapes, plus the pin honesty about the packaging alias.

25.2 Solution

Serve a GGUF model on loopback. Gate it with an API key. Prove health before the scanner touches it:

# 0. Packaging honesty (the pin fact, checked in the nix repl):
#    the catalogue row is pkgs.llama-cpp (v0.6.0 at the packages
#    pin). There is NO `pkgs.llama.cpp` dotted alias at this pin —
#    that alias is guard-dropped, so never script against it.

# 1. Serve a model from -hf by repo tag (quant default Q4_K_M),
#    bound to loopback; the port default is 9931
llama-server -hf ggml-org/gemma-3-4b-it-GGUF \
      --host 127.0.0.1 --port 9931 -ngl 99 -c 8192

# 2. Same shape, local GGUF file, with an API key on the routes
llama-server -m ./model.gguf --host 127.0.0.1 --port 9931 \
      --api-key <lab-key> -c 8192

# 3. Prove health BEFORE scanning: /health is public (no key check;
#    /v1/health also works), API routes are not
curl http://127.0.0.1:9931/health
curl -H 'Authorization: Bearer <lab-key>' \
     -H 'Content-Type: application/json' \
     -d '{"model":"<alias>","messages":[{"role":"user","content":"Reply OK."}]}' \
     http://127.0.0.1:9931/v1/chat/completions

The quant flow — verify a model, convert it, quantise it:

# 4. Convert a verified HF checkpoint to GGUF
python3 convert_hf_to_gguf.py <model-dir> --outfile model-f16.gguf

# 5. Quantise it (the lab-favorite 4-bit shape)
./build/bin/llama-quantize model-f16.gguf model-Q4_K_M.gguf Q4_K_M

# 6. Optional imatrix flow for better low-bit quality (the
#    tools/quantize/README.md path); GGUF-my-repo automates the
#    whole conversion + quantise + upload pipeline

Expected output shape (abridged, shapes not transcripts):

# server banner
main: server is listening on 127.0.0.1:9931(?)
# health
{"status":"ok"}
# chat completion
{"choices":[{"message":{"role":"assistant","content":"OK…"}}],…}

25.3 Discussion

  • The alias fact, stated once and honestly: at the current packages pin the live attribute is pkgs.llama-cpp (0.6.0). The pkgs.llama.cpp dotted alias is guard-dropped. If a release bumps the packager and an alias appears, re-read the row before scripting against it. The pin is the contract.
  • Default bind is already safe, but say it: --host defaults to 127.0.0.1 and --port to 9931. Loopback is therefore the boot posture by default — keep it. A model endpoint is a deliberate attack surface. Upstream is explicit that the loopback default is convenience, not a security setting. Treat the port as a local dev/scan socket, never as a perimeter control.
  • -c is the context window, not a free setting: scanners push long prompts and multi-attempt conversations. A small -c keeps RAM bounded on a lab box but truncates. Budget it against the probe families.
  • -ngl is the layer-offload switch: the switch offloads layers to the GPU. auto (the default) fills VRAM. -ngl 99 is the lab shorthand for “as many layers as fit”. On a CPU-only box, skip it.
  • --api-key / --api-key-file: one or more bearer keys (comma-separated keys, or one-per-line in a file with # comment lines) gate the API routes. An unauthenticated request returns 401. /health stays public by design so boot-wait health polling (the pattern the pair recipes use) works without a key.
  • The Anthropic-compatible route: upstream documents an Anthropic Messages-compatible chat route alongside the OpenAI API surface. The route helps when a client stack already speaks that shape. The scanner families in this book speak OpenAI/anthropic via their generator configs.
  • Why the quant flow lives in an Ops recipe: the conversion + quantise loop turns a vetted model (supply-chain-scan-models) into a servable artefact. The servable artefact is what the scan records should name.
  • No GGUF-helper tool ships: the pre-#210 manifest carried a planned-wave-5 helper row (phantom). The os#210 audit proved its claimed kailash-os upstream was NEVER created and the row is REMOVED (os#210). The manual convert_hf_to_gguf.py + llama-quantize shapes above ARE the working path (llama.cpp ships quantisation itself).

25.4 See also


Recipe provenance. Manifest row: llama.cpp — packaging native, safety safe, category o-04 with o-02 secondary (packages manifest, verified 2026-10-14). Packaging alias fact: at the pin the attr is pkgs.llama-cpp (0.6.0), guard-dropped as llama.cpp (re-checked at this write). Server surfaces (-m/-c/-ngl/--api-key(-file), --host/--port defaults 127.0.0.1/9931, public /health, /v1/chat/completions, Anthropic-compatible route, -hf repo[:quant] default Q4_K_M) verified against upstream tools/server/README.md at main, 2026-10-14. Quant flow (convert_hf_to_gguf.py, llama-quantize Q4_K_M, imatrix, GGUF-my-repo) per tools/quantize/README.md + upstream READMEs. ref: llama.cpp tools/server + tools/quantize READMEs - verified 2026-10-14. Hands-on smoke of the packaged llama-cpp row on a Kailash boot PENDING (wave-2 ops lane).