You want a language model on your own machine, and your tools should talk to it like they talk to the OpenAI API. Two commands get you there: install llama, start llama serve, and a local server answers on 127.0.0.1:9931. This guide shows how to run a local LLM with llama.cpp server, query it with curl and Python, and lock it down with an API key. I tested with the new llama command, version 1.1.0-dev (build 12826), and the Qwen3.5-4B model on a Linux box without a GPU. The traps I fell into along the way are included.
What the llama.cpp server is and what it needs
llama.cpp is an inference engine written in C/C++ that runs large language models (LLMs) locally. The llama.cpp server wraps it in an HTTP service. It speaks the OpenAI chat completions API, ships with a built-in chat interface called Llama Chat, and runs on a plain CPU. A GPU speeds things up but is not required, which makes local inference possible on most laptops. With version 1.0.0 the project committed to a stable API. Our news item llama.cpp v1.0.0: A Stable API for Local AI Inference has the background.
One rename trips people up when they follow older guides: the program used to be called llama-server. The new package has a single command, llama, with subcommands, and the server starts with llama serve. The options I use below (-c, -ngl, --host, --port, --api-key-file) will look familiar from the old tool, so most older llama-server advice carries over. Models come as GGUF files, a single-file format for quantized models. Quantization means the weights are stored with fewer bits, which saves memory and costs a little quality.
Install llama.cpp on Linux, macOS and Windows
The project provides an install script: one command on Linux and macOS, one PowerShell command on Windows. Both are in the project README and on llama.app:
# Linux and macOS
curl -LsSf https://llama.app/install.sh | sh
# Windows (PowerShell)
irm https://llama.app/install.ps1 | iex
I ran the Linux script. It puts llama into ~/.local/bin and downloads the program files from a Hugging Face bucket of the project. If you would rather not pipe a script into your shell, download it first and read it; it is short. Then make sure ~/.local/bin is on your PATH and check the version:
llama --version
I did not test the Windows command, Docker or the prebuilt binaries from the releases page. They are listed that way in the README. If you prefer not to download anything through PowerShell, take the binaries from the GitHub releases page or build from source; the guide is in the repository under docs/build.md. If older Windows guides with CUDA, CMake and Visual Studio build tools put you off, you can skip all of that for CPU use.
Run a local LLM with llama.cpp server: load a model and start it
One command does the real work. The -hf option downloads a model straight from Hugging Face and keeps it in a cache. I use Qwen3.5-4B because it stays usable on an ordinary CPU:
llama serve -hf ggml-org/Qwen3.5-4B-GGUF
On the first start llama downloads a little over 3 GB (quantization Q4_K_M, plus a file for image input) and starts from the cache afterwards. The server listens on port 9931 by default. I checked with ss -ltn: it binds to 127.0.0.1 only, so other machines cannot reach it. Open http://127.0.0.1:9931 in a browser and Llama Chat appears. --no-webui turns the interface off if you only need the API. You pick another quantization with a colon, for example -hf ggml-org/Qwen3.5-4B-GGUF:Q8_0, provided the repository offers it. The model page on Hugging Face lists the available files.
A word on what this model can do. I asked it in German what a GGUF model is. The answer was fluent and wrong: it called GGUF a form of quantization “developed by the Quantel algorithm”. That algorithm does not exist. The same question in English got a correct answer, but two samples prove little. A 4B model is fine for trying things out, for summaries and for structured output. For factual questions it is unreliable.
Test the server: web interface, curl and health check
The health endpoint tells you whether the server is up. The first real request goes to the chat completions route, exactly as with OpenAI:
curl -s localhost:9931/health
curl -s localhost:9931/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Name three advantages of a local language model, briefly."}],"max_tokens":200}'
The health endpoint answers {"status":"ok"}. Besides the text, the chat response contains a timings field with the speed. /v1/models shows the model name the server uses for the API, in my case ggml-org/Qwen3.5-4B-GGUF. With -a you set your own alias.
Trap: thinking models return empty text
Qwen3.5 thinks before it answers by default. With max_tokens set to 100 and a short question, I got finish_reason: length and an empty content. The whole token budget had gone into the reasoning_content field, which started with “Thinking Process: Analyze the Request”. If you read only content, you see an empty reply and look for the bug in the wrong place. There are two ways out. You can switch thinking off for the whole server:
llama serve -hf ggml-org/Qwen3.5-4B-GGUF --reasoning off
Or you control it per request by adding "chat_template_kwargs": {"enable_thinking": false} to the JSON. I tested both, and the text arrived in content afterwards. Thinking costs time and tokens but gives better results on calculations. For summaries and extraction, the variant without it is usually enough.
Call it from Python: OpenAI client, streaming and JSON schema
Because the server mimics the OpenAI API, the official library works. You only change the base_url. Install it with pip install openai. The following script streams the answer word by word; we set the API key in the next section:
import os
from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:9931/v1",
api_key=os.environ["LLAMA_API_KEY"],
)
stream = client.chat.completions.create(
model="local",
messages=[{"role": "user", "content": "Explain in two sentences what a GGUF model is."}],
stream=True,
temperature=0.2,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
The model name does not matter as long as the server holds a single model. I used local and the answer came back without an error. For your own applications, structured output is more interesting. With response_format and a JSON schema, the server forces valid JSON that matches your schema:
import json
import os
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:9931/v1", api_key=os.environ["LLAMA_API_KEY"])
schema = {
"type": "object",
"properties": {
"sentiment": {"type": "string", "enum": ["positive", "neutral", "negative"]},
"reason": {"type": "string"},
},
"required": ["sentiment", "reason"],
}
r = client.chat.completions.create(
model="local",
messages=[{"role": "user", "content": "Rate: 'Switching to the local server was bumpy, but it runs stably now.'"}],
response_format={"type": "json_schema", "json_schema": {"name": "rating", "schema": schema}},
temperature=0,
)
print(json.loads(r.choices[0].message.content))
Output from my run, reason shortened: {'sentiment': 'positive', 'reason': "The phrase 'runs stably now' indicates a successful resolution …"}. The schema held, and sentiment was one of the three allowed values. In my German test the same sentence came back as “neutral”, so the rating itself depends on the model and the wording. The schema guarantees the shape, not the content.
Secure the server: API key and network
As long as the server listens on 127.0.0.1, only your own machine can reach it. Even then, any local program can send requests. With a key file, the server demands a bearer token:
openssl rand -hex 16 > key.txt
llama serve -hf ggml-org/Qwen3.5-4B-GGUF --reasoning off --api-key-file key.txt
export LLAMA_API_KEY=$(cat key.txt)
Without a header the server then answered HTTP 401, and with -H "Authorization: Bearer $(cat key.txt)" it answered 200. To share the service on your home network, start it with --host 0.0.0.0. Never do that without a key. For access over the internet, add encryption in front, either a proxy with TLS or the options --ssl-key-file and --ssl-cert-file. I did not test the TLS options.
Hardware, memory and speed
Memory is the first question with local models. The model file was a little over 3 GB, yet the server process used about 6.5 to 7 GB of RAM on my machine, slightly less with context size 4096 and one slot than with the defaults. The rest goes to the context cache, compute buffers and the image component. Plan for roughly twice the file size until you have measured your own numbers.
On speed: on 8 vCPUs without a GPU, shared with other processes, the server generated between 11 and 23 tokens per second. The value swung noticeably with the load on the machine. Reading short prompts ran at 20 to 94 tokens per second. That is enough for interactive chat and sluggish for long documents. You offload to a GPU with -ngl (the number of layers kept in video memory). On my machine llama serve --list-devices reported no devices, so I could not try -ngl. Other knobs are -c for the context size and -np for the number of parallel slots. Without them the server picked 4 slots with 8192 tokens of context each. If you work alone, -np 1 saves memory.
Common mistakes at a glance
If content comes back empty and finish_reason says length, the model is thinking for too long: use --reasoning off or raise max_tokens. If the server answers 401, the bearer token is missing. If curl prints garbage when you fetch the web interface, add --compressed, because the page arrives compressed. And if llama is not found after the install, ~/.local/bin is not on your PATH.
Frequently asked questions
Do I need a GPU to run an LLM with the llama.cpp server? No. All tests above ran on the CPU. A GPU makes larger models comfortable to use, but it is not a requirement.
How does it differ from Ollama? Both run models locally. Ollama brings its own model management and interface, while llama.cpp gives you more switches and runs without an extra service. I did not test Ollama for this article, so I will not claim a comparison.
Does this work on Windows? According to the README, yes; the PowerShell installer and the binaries are meant for it. I tested Linux only.
How do I choose a model? A rule of thumb: take the largest model whose file size stays at about half of your free RAM. The rule comes from my measurement above, not from the documentation. How demanding large open models get is shown in our article on Amazon Ergo.
Where to go from here
The server is one building block. You can use it as a local backend for chat clients and your own scripts that accept an OpenAI-compatible address, and combine it with the tools from the tutorial Build an MCP Server in Python. The full list of server options is in the server documentation, and the OpenAI site describes the fields of the chat completions API. If you would rather work from Python without a server, llama-cpp-python offers its own bindings; I did not try them here. I also did not test the interactive llama cli command, because my environment had no terminal for it.




