llama-cpp-python includes a small web server that serves a local GGUF model through an API that copies OpenAI's. That means apps and scripts written for the OpenAI API can talk to a model on your own PC instead, usually by changing just the base URL. 3.38).
Step 1: Install the server package
The server is an optional extra, so install it with:
pip install "llama-cpp-python[server]"
To use your graphics card, install a GPU-enabled build in the same way as the main package. For example, set CMAKE_ARGS="-DGGML_CUDA=on" for NVIDIA, or use one of the project's pre-built CUDA, HIP Radeon, Vulkan or Metal wheels. Our llama-cpp-python GPU install guide covers each option.
Step 2: Start the server with a model
Point the server at a GGUF file:
python -m llama_cpp.server --model models/your-model.gguf --n_gpu_layers -1
In the server settings, n_gpu_layers defaults to 0, meaning everything runs on the CPU. Setting it to -1 moves all layers to the GPU. Set a lower number if the model doesn't fit in your VRAM. Run python -m llama_cpp.server --help to see every option. Each one can also be set as an environment variable, for example MODEL instead of --model.
The README also suggests setting the prompt format, for example --chat_format chatml, if a model's chat template isn't picked up correctly.
Step 3: Check it's running
By default the server listens on localhost port 8000. Open http://localhost:8000/docs in your browser to see the interactive OpenAPI documentation, where you can try requests. Use --port to change the port. Use --host 0.0.0.0 only if you want other devices on your network to connect, as that exposes the server beyond your PC.
Step 4: Connect an app or script
Any OpenAI-compatible client can now use your local model. With the official OpenAI Python library, set the base URL to your server. The server's documentation uses a placeholder API key:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="sk-xxx")
resp = client.chat.completions.create(
model="local", # or the model_alias from your config file
messages=[{"role": "user", "content": "Give me three tips for cooling a small PC case."}],
)
print(resp.choices[0].message.content)
The same approach works with tools that let you set a custom OpenAI endpoint. If you've used Ollama's equivalent feature, our guide to Ollama's OpenAI-compatible API works in a similar way.
Serving several models with a config file
Instead of command-line options, you can pass a JSON config file with --config_file. It can list several models, each with its own model_alias, n_gpu_layers, n_ctx and chat_format. Requests are routed by matching the model name in the request against those aliases. According to the docs, only one model is loaded into memory at a time, and the server loads and unloads models as needed. So switching between them takes time, but you don't need VRAM for all of them at once.
The server documentation also covers:
Function calling compatible with the OpenAI API, using functionary models with the functionary-v1 or functionary-v2 chat format.
Vision models from the LLaVA 1.5 family and others such as moondream2, which need a separate CLIP model file (--clip_model_path) and the llava-1-5 chat format.
Code completion for GitHub Copilot in VS Code via a proxy setting. The docs warn this is unlikely to be fast enough without GPU acceleration, and recommend a much larger context (--n_ctx 16192).
A Docker image on GitHub Container Registry; the README's example maps port 8000 and sets the model with the MODEL environment variable.
Should you use it?
If you already run Ollama or LM Studio, they also provide OpenAI-compatible endpoints with less setup. See llama.cpp vs Ollama for how the options compare. llama-cpp-python's server makes most sense if you're already using the library in Python and want the same model settings available over an API, or want a lightweight server you can configure entirely from one JSON file.