Skip to main content
Connect a llama.cpp server to Hivenet Router by running an agent beside the backend. The agent discovers the model served by llama.cpp, registers it with the router, forwards OpenAI-compatible chat-completion requests, and reports engine and hardware metrics.
A llama.cpp server runs one model per process, and each Hivenet Router agent registers one model. Run a separate server and agent pair for each model you want to expose.

Before you start

You need:
  • a running Hivenet Router router
  • a GGUF model file
  • llama.cpp built locally or available as a Docker image
  • network access between the agent host and router
  • the Hivenet Router agent binary or Docker image
  • the same JWT secret used by the router
  • an NVIDIA GPU if you want CUDA acceleration and GPU metrics
The examples use:
Replace these values with addresses and model names from your deployment.

Start llama.cpp

Use the -a option to give the model a stable alias. The alias becomes the model name registered with Hivenet Router and used by API clients. Start llama.cpp with --metrics if you want Hivenet Router to collect cache, queue, latency, and throughput metrics.
Place the GGUF file in a local model directory, such as:
Start the server:
Follow the startup logs:
For CPU-only inference, omit:
Wait for the model to load, then check the server:
A ready llama.cpp server returns HTTP status 200. List the model exposed by its OpenAI-compatible API:

Choose a stable model name

The alias passed with -a determines the model name returned by llama.cpp. Using an explicit alias makes client configuration and routing policies less dependent on the model filename.
Changing the alias changes the model name seen by Hivenet Router. Clients and routing policies must use the new value.

Prepare the Hivenet Router agent

From the Hivenet Router repository, build the agent image if you have not already done so:
Create a directory for the shared secret and persistent identity:
Place the router’s JWT secret at:
Protect it:

Start the agent

For CPU-only inference, omit:
The /data mount preserves the agent’s peer identity across container replacement and restarts.
The agent waits until llama.cpp is healthy and exposes a model before registering with the router. If the backend is still loading, the agent continues polling rather than exiting permanently.

Understand the agent settings

You can also add routing tags:

Model discovery

When --model is omitted, the agent requests:
It registers the first model returned by llama.cpp. Because a llama.cpp server normally serves one model, automatic discovery is usually sufficient. You can also pin the model explicitly:
The value must match the model alias returned by llama.cpp.

Set agent capacity

--capacity limits how many concurrent requests Hivenet Router may assign to the agent. It does not change llama.cpp’s own parallelism, slot count, context allocation, or batch configuration. Conservative starting values from the original deployment guidance are: Treat these as starting points rather than universal limits. Test with your model, quantization, context size, and hardware. When the agent reaches its declared capacity, Hivenet Router considers another matching agent or a configured fallback step.
For streaming responses, Hivenet Router releases the agent capacity slot when response headers arrive, while backend generation can continue. Treat --capacity as a routing-admission setting rather than a hard limit on ongoing streams, and verify llama.cpp concurrency under sustained streaming load.

Verify the connection

On the router, check agent health:
List the models available to clients:
Inspect llama.cpp agents:
Send a chat-completion request:
The model in the request must match the alias registered by the agent.

llama.cpp metrics

The agent scrapes llama.cpp’s /metrics endpoint every 500 milliseconds by default. Start llama.cpp with --metrics to make this data available. Metrics are sent to the router and re-exported through its Prometheus endpoint. The agent does not expose a separate Prometheus port. llama.cpp does not use the preemption mechanism measured for vLLM, so Hivenet Router does not populate a preemption metric for this engine. View all engine metrics through the router:
Useful exported metrics include:
View current values through the routing table:
The routing table exposes scalar cache, queue, TTFT, and ITL values. llama.cpp prompt and generation throughput are available through Prometheus, not the current routing-table response.

Use llama.cpp metrics in routing policies

Exclude agents whose KV cache is under pressure:
Exclude agents with queued requests:
Exclude agents whose average time to first token is too high:
When a metric is unavailable, Hivenet Router skips that gate for the agent rather than excluding it. See Policy YAML reference for the full schema.

Tune llama.cpp

Reduce the context size

A smaller context window reduces KV-cache memory use:

Change the quantization

Quantization affects model size, memory requirements, speed, and output quality. The source deployment guide uses:
as a practical balance for its example. Choose a quantization appropriate for your model and hardware.

Load layers on the GPU

For a CUDA build:
requests that llama.cpp offload model layers to the GPU. The number of layers that fit depends on the model, quantization, and available VRAM.

Enable flash attention

The example uses:
Support depends on the build and hardware. Remove it if the server fails to start with the option enabled.

Troubleshooting

llama.cpp is not ready

Check the health endpoint:
List the exposed model:
Inspect the logs:
The agent waits and retries while the backend is unavailable or still loading.

Metrics are missing

Confirm that llama.cpp was started with:
Check the endpoint directly:
Without --metrics, the endpoint returns HTTP 404 and Hivenet Router cannot collect engine-specific metrics. A metrics scrape failure does not stop the agent from forwarding requests.

The model name does not match

Check the name returned by llama.cpp:
Use -a to set a stable alias:
Clients must send the same name to Hivenet Router.

Responses are slow

Possible adjustments include:
  • reduce --capacity on the Hivenet Router agent
  • reduce llama.cpp’s context size
  • use a smaller model or quantization
  • increase GPU offloading
  • reduce parallel request pressure
Measure with representative prompts before changing several settings at once.

The agent does not register

Check the agent logs:
Test connectivity from the agent to the router:
Check that:
  • the router and agent use the same JWT secret
  • the router’s libp2p interface is reachable
  • the router advertises a libp2p address the agent can reach
  • llama.cpp is healthy and exposes a model

The peer ID changes after restart

Make sure --identity-path points to persistent storage. For Docker:
For bare metal:

Requests time out

The agent’s backend HTTP timeout defaults to two minutes. Increase it for long-running requests:
Also check the declared agent capacity and llama.cpp queue state.

GPU metrics are missing

Check the host:
For Docker, confirm the agent received GPU access:
The agent continues to operate without NVML. It reports CPU and memory metrics but omits GPU measurements.

Next steps

Infinity agent

Connect embedding and reranking models through Infinity.

Routing concepts

Learn how Hivenet Router filters and ranks matching agents.

Engine metrics

Understand the metrics collected from llama.cpp and other engines.