Integration Guides

Self-Hosted OpenAI-Compatible API: Engines, Setup and Security (2026)

ROIBest AI

What a self-hosted OpenAI-compatible API is

A self-hosted OpenAI-compatible API is an inference server you run on your own machine or cloud GPU that answers the same HTTP routes as OpenAI: POST /v1/chat/completions, GET /v1/models, and usually /v1/completions and /v1/embeddings. Any client built for OpenAI (the official SDKs, Dify, Home Assistant add-ons, agents, chat apps) can use it by changing two settings: the base URL and the API key.

You do not write this API yourself. Every major open-source serving engine ships one. The real decisions are which engine fits your hardware, how you expose it safely, and what you must change on the client. Server behaviour below was checked on November 24, 2026 against the vLLM OpenAI-compatible server docs and security notes, the llama.cpp server README, Ollama's OpenAI compatibility docs and FAQ, and SGLang's server arguments.

If you only want to run models on a laptop for free, our free OpenAI-compatible API keys guide covers Ollama for that. This page is about running one as a service other apps and people depend on.

Pick a serving engine

Engine

Best fit

Start command

Typical base URL

Built-in auth

vLLM

NVIDIA or AMD GPUs, many concurrent users, production

vllm serve <model> --api-key $KEY

http://host:8000/v1

--api-key / VLLM_API_KEY (only some paths, see below)

SGLang

GPU serving with heavy prefix reuse (multi-turn chat, agents)

sglang.launch_server --model <model> --port 30000 --api-key $KEY (run as a Python module)

http://host:30000/v1

--api-key

llama.cpp llama-server

CPU, Apple Silicon, small GPUs, GGUF quantized models

llama-server -m model.gguf --host 0.0.0.0 --port 8080 --api-key $KEY

http://host:8080/v1

--api-key, --api-key-file

Ollama

One machine, easiest model management

ollama serve

http://host:11434/v1

None; the key is "required but ignored"

LM Studio

Desktop GUI, testing models

lms server start

http://host:1234/v1

Optional server setting

Rules of thumb:

  • A shared service on a GPU server: vLLM or SGLang. They batch concurrent requests, which is where most of their throughput advantage comes from.
  • No datacenter GPU: llama.cpp with a quantized GGUF model. Ollama and LM Studio also run GGUF models and are easier to start with.
  • Pass ports explicitly. The current llama.cpp README lists a default port that differs from the 8080 in most tutorials, and every engine uses a different default.

Deploy it as a service

A command in a terminal is a demo. A service needs four more things.

1. Run it under a supervisor. Use Docker or systemd so it restarts after crashes and reboots:

docker run --gpus all -p 8000:8000 \
  -e VLLM_API_KEY="$VLLM_API_KEY" -e HF_HOME=/data \
  -v hf-data:/data \
  vllm/vllm-openai:latest \
  --model Qwen/Qwen3-8B --max-model-len 32768

2. Size the context window on purpose. vLLM tries to allocate the model's full context by default and fails if the KV cache does not fit; set --max-model-len. Ollama uses a small default context on GPUs with less than 24 GB of VRAM and cannot change it per request over /v1/chat/completions; set OLLAMA_CONTEXT_LENGTH on the server. Agents and RAG apps that silently lose context are usually hitting this.

3. Turn on tool calling if clients need it. vLLM needs --enable-auto-tool-choice plus a --tool-call-parser that matches the model; SGLang needs --tool-call-parser. Without them, tool calls come back as plain text and agents break.

4. Name the model the way clients expect. Clients send a model string. vLLM's --served-model-name lets you publish a short, stable name (qwen3-8b) instead of a Hugging Face path, so you can swap weights without touching clients.

Secure it before you open a port

This is where self-hosted deployments most often go wrong.

  • Bind to localhost unless you mean otherwise. llama-server and Ollama listen on 127.0.0.1 by default. Changing that (--host 0.0.0.0, OLLAMA_HOST=0.0.0.0:11434) exposes the server to every network the machine is on.
  • Do not treat --api-key as a firewall. vLLM's own docs warn that its key only protects the /v1, /v2, /inference and /cohere prefixes. Other routes on the same port, including /invocations, which runs the same inference, are not authenticated. vLLM recommends a reverse proxy in front.
  • Ollama has no server-side key check. The OpenAI SDK needs a non-empty key, so clients send any string. Anyone who reaches the port can use the models.
  • Put a reverse proxy or gateway in front. Nginx, Caddy or Traefik give you TLS and let you allowlist only /v1/chat/completions, /v1/models and whatever else you use. A gateway such as LiteLLM adds per-user virtual keys, budgets and logs; see open-source LLM gateways and OpenRouter vs LiteLLM.
  • Prefer a private network for remote access. A VPN or tailnet keeps the server off the public internet entirely, which is simpler than hardening it.

Point clients at it

Most clients need exactly two values:

from openai import OpenAI

client = OpenAI(
    base_url="https://llm.example.internal/v1",  # your server, including /v1
    api_key="your-server-key",                    # any non-empty string for Ollama
)
resp = client.chat.completions.create(
    model="qwen3-8b",   # must match a name from GET /v1/models
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)

Check from the client machine, not from the server:

curl https://llm.example.internal/v1/models -H "Authorization: Bearer $KEY"

If this returns your model, the URL and key are right. A 404 usually means a missing or doubled /v1; our OpenAI-compatible base URL guide covers the variants. Clients running in Docker must use the host's IP or host.docker.internal, not localhost.

What "compatible" will not give you

A self-hosted server matches OpenAI's request and response shapes, not OpenAI's whole product:

Self-hosted or hosted?

Factor

Self-hosted

Hosted API

Data

Stays on your hardware

Sent to the provider

Cost shape

Fixed (GPUs, power, ops time), cheaper at high steady volume

Per token, cheaper at low or spiky volume

Models

Open-weight models only

Includes closed frontier models

Operations

You handle updates, scaling, security, uptime

Provider handles them

Limits

Your hardware

Provider rate limits and quotas

Many teams run both behind one gateway: open-weight models on their own GPUs for private or high-volume work, and a hosted API for tasks that need a frontier model.

FAQ

How do I self-host an OpenAI-compatible API?

Run a serving engine such as vLLM, SGLang, llama.cpp's llama-server or Ollama, which all expose /v1/chat/completions. Then point your client's base URL at http://<host>:<port>/v1 and set the API key the server expects.

Which is better for self-hosting, vLLM or Ollama?

vLLM for a shared GPU server with many concurrent users; it batches requests and has built-in API key support. Ollama for a single machine or personal use, because it is easier to install and manage models, but it has no server-side authentication.

Does a self-hosted OpenAI-compatible server need an API key?

It should whenever anyone but you can reach it. vLLM, SGLang and llama-server accept --api-key; Ollama does not check keys, so protect it with a reverse proxy, gateway or private network.

Can I use the official OpenAI SDK with a self-hosted model?

Yes. Set base_url to your server, including /v1, set api_key to your server's key (or any string for Ollama), and use the model name your server returns from /v1/models.

Why does my self-hosted model ignore tools or forget context?

Tool calling usually has to be enabled on the server (for vLLM, --enable-auto-tool-choice and --tool-call-parser), and the context length is set on the server, not by the client. Check both before changing prompts.

The short version

Pick vLLM or SGLang for a shared GPU service and llama.cpp or Ollama for smaller hardware. Run it under Docker or systemd, set the context length and tool-calling flags deliberately, keep it on localhost or a private network, and put a reverse proxy or gateway in front because built-in keys do not cover every route. Clients then need only a base URL ending in /v1, a key, and the exact model name.