Using llama-server with curl and Python as a local API

The llama.cpp project ships llama-server, an HTTP server that offers an OpenAI-compatible chat completions route. This tutorial loads a local GGUF model and queries it from curl and Python, so prompts stay on your own machine.

Niklas Schult4 min read

Illustration: A small wooden house is linked by a brass pipe to a speaking tube holding an envelope, a message kept at home.

What you will have at the end and what you need

At the end you have a running llama-server that serves one local model on your own computer and answers chat requests over HTTP. You test it first with curl and then with the OpenAI Python client. The llama.cpp server README lists chat completions, responses and embeddings routes that follow the OpenAI API.

You need the llama-server binary from llama.cpp and a model file in GGUF format on disk. For the Python part you also need the openai package. The README describes inference of F16 and quantized models on GPU and CPU, so a machine without a graphics card can work as well, depending on the model size.

Because the model is loaded from a local file, the server needs no download when it starts. The README also offers options that fetch models from Hugging Face or a URL, and this tutorial leaves them out. Companies often prefer the local route for internal material, since the request path stays inside their own network. Our page on local AI covers the wider picture.

Step 1: start llama-server with a local model

The command below starts the server with a model file given by -m. The option -a sets an alias, which is the model name that API clients use in their requests. The option -c sets the prompt context size, and the README states that the default of 0 reads the size from the model.

Shell
# Start the server with a local GGUF file and an alias for the API
llama-server -m ./modelle/mein-modell.gguf -a firmenmodell -c 8192

According to the README, the server listens on 127.0.0.1 by default and uses port 9931. In that state only programs on the same machine can reach it. The README also describes the built-in chat app Llama Chat, which is enabled by default and can be switched off with --no-ui.

Step 2: test the endpoint with curl

The request below sends one user message to the route /v1/chat/completions. The field model carries the alias from step 1, and messages holds the conversation in the usual OpenAI format. The prompt is German on purpose, since many company texts are.

Shell
# Send one chat message to the local server
curl http://127.0.0.1:9931/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "firmenmodell",
    "messages": [
      {"role": "user", "content": "Fassen Sie den Satz in einem Wort zusammen: Das Wetter ist gut."}
    ]
  }'

The server answers with a JSON object in the OpenAI response shape. The generated text sits in the first entry of choices, inside message and content. If the model supports thinking, the option --reasoning-format decides whether the thoughts appear in message.reasoning_content or stay in the content.

Step 3: connect the Python client

The script below uses the OpenAI Python client but points base_url at the local server. The client library requires an api_key, and the README says the server only checks keys if you start it with --api-key, so the value here is a plain placeholder. The model name is again the alias.

Python
# Call the local server through the OpenAI Python client
from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:9931/v1", api_key="lokal")

antwort = client.chat.completions.create(
    model="firmenmodell",
    messages=[{"role": "user", "content": "Nennen Sie drei Vorteile lokaler Modelle."}],
)
print(antwort.choices[0].message.content)

Existing applications that already use the OpenAI client can often switch to the local server by changing only the base URL and the model name. Whether a given feature works depends on the model and the server options, so test each one before you rely on it.

Checking, pitfalls and fit in a company

The setup works if curl returns a JSON answer and the Python script prints text. If the server rejects the model name, check that it matches the alias given with -a. The option --list-devices shows which devices the server can use, and the README says the default for GPU layers is auto.

Several README points deserve attention before wider use. The default host is 127.0.0.1, and changing it with --host opens the server to the network. For that case the README recommends an API key via --api-key or --api-key-file and offers --ssl-key-file and --ssl-cert-file for encrypted connections. The experimental options --tools and --agent are marked as unsuitable for untrusted environments. The --embd-gemma-default style presets and the -hf options can download weights from the internet, so use a local file if outbound traffic is restricted. The option --offline forces use of the cache and prevents network access.

In a company, someone needs to own the server: updating llama.cpp, choosing models and managing keys. Data protection teams commonly check who can reach the port, what the logs contain and whether --log-prompts-dir, which the README marks as debugging only, is off. For a managed variant, souveraen.ai offers an OpenAI-compatible developer API and an on-premise option described under Private Spark. Our overview of security and data protection explains the hosting side.

Sources

More articles

All articles

BSI urges quick move to quantum-safe cryptography

On 3 December 2026 the German Federal Office for Information Security (BSI) repeated its advice to switch to quantum-safe cryptography without delay. Companies that run VPNs, certificates and long-lived archives should check what this means for their own systems.

Regulation5 min

GDPR fines in November 2026 and what they mean for you

In November 2026 European data protection authorities fined companies from social media, banking, energy and dating, with amounts from 675,000 euros to 805 million euros. The cases point to three duties that any company using software or AI with personal data should secure first.

Data protection5 min

AI Act nudification ban: what it means for companies

Since 2 December 2026, the EU AI Act prohibits AI systems that generate non-consensual intimate images of identifiable people and child sexual abuse material. Companies that allow staff to use image generators should check which tools are in use and under what rules.

Regulation4 min

Test it with your own documents.

Free for 3 days, then from €24.90 per user per month. Cancellable monthly.