llama-server has no authentication by default, so anyone who can reach its port can use your GPU. Starting it with an API key makes every endpoint except /health require a Bearer token, which is the minimum before the port is reachable beyond localhost.
What you need
- llama.cpp built or installed, with llama-server available
- A GGUF model file on disk
- A secret key string you generate yourself
Step 1: Start the server with a key
Run llama-server -m model.gguf –port 8080 –api-key <your-secret-key>. For several keys, use –api-key-file with a file holding one key per line; lines starting with # are comments. The server warns that the default port may move in a future release, so pin –port explicitly.
Step 2: Confirm the lock works
Run curl http://127.0.0.1:8080/v1/models without a key. The server should answer 401 with an error object: Invalid API Key, type authentication_error, code 401. The /health endpoint still answers without a key, which suits a health check.
Step 3: Call it with the key
Repeat the call with the header Authorization: Bearer <your-secret-key>. The models list should now return HTTP 200. The OpenAI Python SDK accepts the same key as its api_key argument with base_url pointed at the server.
Worked example: keyed server on port 8080
The table below is a synthetic example. The flags and the 401 response come from the cited llama-server guide; the key value is a placeholder for your own secret.
| Request | Key sent | Expected result |
|---|---|---|
| GET /v1/models | None | 401 Invalid API Key |
| GET /health | None | 200 status ok |
| GET /v1/models | Bearer <your-secret-key> | 200 with model list |
Expected result (UNTESTED). requests without a key get a 401, /health still answers, and requests with the Bearer key succeed. This check was not run live; the flags and responses come from the cited llama-server guide.
Limits
- The key travels in plain HTTP on localhost; use HTTPS or a tunnel beyond the machine.
- A key in shell history is visible to anyone with the history file; prefer –api-key-file.
- The server serves one model per process outside router mode.
- Binding the port to 0.0.0.0 exposes it to the local network; keep it on 127.0.0.1 unless needed.
