Skip to content

How-ToGPU

How to Run llama-cpp-python's OpenAI-Compatible Server on Your PC

llama-cpp-python includes an OpenAI-compatible web server that lets apps and scripts use a GGUF model on your own PC. Using the official docs, we cover installing the server extra, GPU offload, the default port, connecting the OpenAI Python client and serving several models from a config file.

By , Systems & Technical Editor

3 min read

How to Run llama-cpp-python's OpenAI-Compatible Server on Your PC

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.

Extra features

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.

The bottom line

llama-cpp-python's server turns any GGUF model into a local, OpenAI-compatible API with a single pip extra and one command. Install a GPU-enabled build, set n_gpu_layers so the model actually runs on your graphics card, then point your apps at http://localhost:8000/v1. Keep it bound to localhost unless you really need network access.

Sources

  1. 01llama-cpp-python official GitHub README
  2. 02llama-cpp-python OpenAI Compatible Server documentation
  3. 03llama-cpp-python server settings source code

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

How to Add an Exclusion to Microsoft Defender in Windows 11 (Safely)

How-To

How to Add an Exclusion to Microsoft Defender in Windows 11 (Safely)

Microsoft Defender keeps flagging a file you trust? Rather than switching protection off, you can exclude a file, folder, file type or process in Windows Security. Following Microsoft's guidance, we show how to add and remove exclusions, use wildcards safely and what exclusions don't cover.

Marcus Thorne4 min

The Best NVIDIA App Features in 2026: What's Worth Using on a GeForce PC

Best Picks

The Best NVIDIA App Features in 2026: What's Worth Using on a GeForce PC

The NVIDIA App does far more than update drivers. Based on NVIDIA's official pages, we pick the features most worth using on a GeForce PC, from DLSS Override and Smooth Motion to Instant Replay, the performance overlay, RTX HDR and one-click GPU tuning, and explain who each suits.

Hannah Vance4 min