← Blog
Published on
2026-11-27

Run a llama.cpp server with a GGUF model and Hivenet Router

Use a llama.cpp server when you want to run a GGUF model on your own machine and call it over HTTP. For a developer connecting an application on another machine, Hivenet Router provides a stable client address even if the model host changes. You’ll load the file, check the backend directly, and send the same model ID to Router. Comparing the two responses tells you whether to investigate llama.cpp, the Router agent, or the client’s credentials.

The examples use Bash, curl 7.76 or later, a chat-capable GGUF file, and a running Hivenet Router. Run a Router v0.1.7 agent on the same host as llama.cpp. For the final check, have a client key allowed to call this model and an HTTPS address for Router’s client API, typically provided by a reverse proxy.

Install the server binary or build it from source

The llama.cpp server is an HTTP server for running large language models locally. The executable is named llama-server. If llama-server --version works in your shell, skip to the model file. Otherwise, download a prebuilt release for your operating system and hardware, or build the project from source.

For a Linux CPU build, start in a working directory with Git, CMake, and a C++ compiler installed. The git clone command downloads the source from the ggml-org/llama.cpp repository. CMake then builds the server target.

git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release --target llama-server
./build/bin/llama-server --version

If you built from source, use ./build/bin/llama-server in the launch command below from the repository root. For NVIDIA GPUs, the upstream build instructions describe configuring CMake with -DGGML_CUDA=ON and the CUDA toolkit. A CPU build is sufficient for the API checks when the model fits in system memory.

Choose the model file and start the llama.cpp server

GGUF stores model tensors and metadata in a format llama.cpp can read for local LLM inference. If you download a model from Hugging Face, choose a chat or instruction-tuned variant supported by your llama.cpp build. Read the model card for the architecture, quantization, and file size. Keep the original filename in your model folder so you can trace which weights the alias refers to. Quantized models can use less memory for weights. Leave room for the key-value cache, which stores state for tokens in the context. If the server runs out of memory as it loads, try a smaller context with --ctx-size or choose a smaller model. See the Hugging Face guide to GGUF files.

On the model host, replace /srv/models/support-instruct.gguf with the path to your file. Choose an alias that is unused in your Router deployment so the first request is easy to trace. If you choose another alias, use the ID that Router returns in both the curl and Python examples below.

llama-server \
  -m /srv/models/support-instruct.gguf \
  --alias support-chat \
  --host 127.0.0.1 \
  --port 8888

Leave the server running. The -m argument selects the file to load. The --alias argument sets the name advertised by the API. Binding to loopback works here because the agent runs alongside the server. Port 8888 is an explicit choice. If that port is occupied, pick a free port and change it in the launch command, the local checks, and the agent’s backend URL. See the llama.cpp server reference for these options.

Run the same server in Docker

If you use Docker on Linux, run this command instead of the native server command. Here the GGUF file lives in /srv/models on the host. Mount that directory read-only and pass the file’s path inside the container to -m.

docker run --rm \
  -v /srv/models:/models:ro \
  -p 127.0.0.1:8888:8888 \
  ghcr.io/ggml-org/llama.cpp:server \
  -m /models/support-instruct.gguf --alias support-chat \
  --host 0.0.0.0 --port 8888

The process listens on the container’s interfaces, but Docker publishes port 8888 only on host loopback. The Router agent in this walkthrough runs directly on the host, so its backend URL stays the same. The official CPU server image supports Linux on x86-64 and ARM64. If you need GPU acceleration, check the upstream Docker image list for a CUDA image compatible with your NVIDIA setup.

Check readiness and read the backend model ID

In a second shell on the same host, check the server and its model catalog. The first command includes the HTTP status in the output. The second reads the model details returned to API clients.

curl -sS -i http://127.0.0.1:8888/health
curl --fail-with-body -sS http://127.0.0.1:8888/v1/models

A 503 with “Loading model” means the file is still loading. Once /health returns 200, look for "id": "support-chat" in the data array from /v1/models. If you see a filename or another ID, check the alias and port on the running process before starting the agent.

For a quick test of the model directly, open http://127.0.0.1:8888/ in a browser on the model host if the built-in chat interface is enabled. Send a short message. This exercises local inference before Router is involved. For a remote machine, run the HTTP checks in its shell. A browser on your laptop interprets loopback as your laptop.

Register the llama.cpp server with Router

Use the second shell to start the agent. The command assumes the Router agent binary is at ./bin/hivenet-agent relative to this shell’s working directory. Substitute the path where you installed it. Replace 192.168.1.100:50051 with your Router’s gRPC address. The JWT secret file must contain the secret used by that Router, and the directory for the agent’s identity file must exist and be writable by the agent. For a remote Router, the agent must also be able to reach Router’s advertised peer address. The Router llama.cpp agent guide covers binary and container setup.

./bin/hivenet-agent \
  --engine llamacpp \
  --backend-url http://127.0.0.1:8888 \
  --router-grpc 192.168.1.100:50051 \
  --jwt-secret-file /opt/hivenet-router/jwt.secret \
  --identity-path /var/lib/hivenet-router/agent/gguf-chat.key

The engine flag is spelled llamacpp. With --model omitted, the agent queries the backend’s /v1/models endpoint and registers the first model returned. Here the server exposes just support-chat. Leave the agent running while you check it from the client.

Send a chat request using the discovered ID

Open Bash on the client. Enter Router’s HTTPS base URL and your inference API key. The URL here belongs to the service that receives application requests. Port 8888 in the earlier commands belongs to llama.cpp, and port 50051 is for agent authentication.

read -rp 'Router HTTPS base URL (without /v1): ' ROUTER_URL
read -rsp 'Router inference API key: ' ROUTER_API_KEY
printf '\n'
curl --fail-with-body -sS "${ROUTER_URL%/}/v1/models" \
  -H "Authorization: Bearer ${ROUTER_API_KEY}"

Find support-chat in the Router model catalog. In that entry, look for llamacpp in agents.engines and a positive agents.healthy count. If the backend lists the alias but your client cannot see it, check agent registration and the model permissions on that client key.

In the same client shell, send a short prompt to Router. Use the model ID you just read from its catalog.

curl --fail-with-body -sS "${ROUTER_URL%/}/v1/chat/completions" \
  -H "Authorization: Bearer ${ROUTER_API_KEY}" \
  -H 'Content-Type: application/json' \
  --data '{
    "model": "support-chat",
    "messages": [{"role": "user", "content": "Suggest a short title for a guide to reading a map."}],
    "stream": false
  }'

Look for an assistant reply in choices[0].message.content. With stream set to false, the answer arrives in one JSON response. A catalog entry confirms discovery. The reply confirms the client key can complete a chat request through Router. To attribute that reply to this specific server, use an alias served only by this agent with fallback disabled for the test, or add -i to the curl command, match the X-Request-ID response header to request_id, and read agent_id in the Router audit record. The chat completions reference shows the response format.

Use the same model ID from a Python application

After curl succeeds, use the same address, key, and alias in application code. For example, the OpenAI Python client expects a base URL ending in /v1. In the Bash shell used above, run export ROUTER_URL ROUTER_API_KEY so a Python process started there can read both values. The example assumes the openai package is installed in that Python environment.

import os
from openai import OpenAI

client = OpenAI(
    base_url=os.environ["ROUTER_URL"].rstrip("/") + "/v1",
    api_key=os.environ["ROUTER_API_KEY"],
)
reply = client.chat.completions.create(
    model="support-chat",
    messages=[{"role": "user", "content": "Suggest a title for a hiking map."}],
)
print(reply.choices[0].message.content)

The output should be the assistant’s reply. If curl works but this call fails, check for a duplicated /v1 in the base URL and verify that the Python process received the client key. Other languages can send the same JSON over HTTP. See Router’s client examples.

What if the API responds but the chat output looks wrong?

If a completion contains unexpected role markers, test a short conversation on the backend first. The llama.cpp server normally reads the chat template from the model’s metadata to format the messages before inference. Compare your GGUF file and its template with the model card. If the application expects structured output, test its JSON schema separately after plain chat works. A valid text reply checks a different behavior from a reply constrained to a schema.

Check the server under concurrent requests

Once the first request works, llama.cpp can serve several clients with parallel decoding and continuous batching. The --parallel option sets the number of server slots. If latency rises when a second client sends a prompt, check whether requests are waiting before changing the model or moving it to a GPU.

To collect engine metrics, add --metrics when starting llama-server. From the model host, read http://127.0.0.1:8888/metrics. Router v0.1.7 can collect the server’s metrics through its agent, including running and waiting request counts. Check /health for readiness and /metrics for workload counters. A missing metrics endpoint can coexist with a working chat endpoint. The llama.cpp metrics reference lists the available signals.

Why does health pass while discovery returns 401?

Check which server returned the error. On a llama.cpp server configured with --api-key, /health is public. A successful health check can therefore be followed by a 401 from the backend’s model catalog. For a direct catalog check, send the llama.cpp key in an Authorization: Bearer header. For agent discovery, add --backend-api-key-file /opt/hivenet-router/llamacpp.key to the agent command, using a protected file containing the backend key.

A 401 from Router’s client URL means you should verify the inference key sent by curl. The agent’s shared JWT secret belongs between the agent and Router. Keeping the failing URL in view tells you which credential to inspect.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.