How to Serve an Open Model with SGLang in 30 Minutes
Why SGLang?
If you have already run a model on your own machine, the next step is often serving it: putting it behind an HTTP API so an app or a teammate can call it. Running a model locally with Ollama is the easiest way to chat with it, but Ollama is built mainly for single-user use. For an OpenAI-compatible API, concurrent requests, and a cache that reuses work across requests, a dedicated serving engine is better.
SGLang is one of those engines: an open-source, high-performance serving framework whose headline feature is RadixAttention, a prefix cache organised as a radix tree. When several requests share the same beginning — the same system prompt, document, or few-shot examples — SGLang reuses the already computed key-value (KV) cache for that prefix instead of recomputing it. That is why it shines for multi-turn chat, retrieval-augmented generation (RAG) with a fixed context, and agent workloads that repeat instructions. Our inference-engine comparison of vLLM, Ollama, llama.cpp, and SGLang explains where each engine fits.
Source: NewsCarpenter illustration — SGLang request flow: client, server with RadixAttention cache, and GPU model.
Prerequisites
- A Linux machine with an NVIDIA GPU and working CUDA drivers. A 7B model in BF16 needs roughly 14 GB for weights alone, plus KV-cache room, so 16 GB is the practical minimum and 24 GB is more comfortable.
- Python 3.10 or newer, pip, and tens of gigabytes of free disk space.
- A Hugging Face account only for gated models. The Qwen2.5-7B-Instruct example below is not gated, so you can skip login.
- About 30 minutes, mostly waiting for the first download.
Step 1: Install SGLang
Create a fresh environment first, so SGLang's dependencies do not clash with other projects. Then install the full package:
pip install --upgrade pip
pip install "sglang[all]"
The [all] extra pulls in the runtime and its common backends. If a FlashInfer kernel install fails on your system, you can still run SGLang with its Triton kernels by adding --disable-flashinfer in Step 2.
Step 2: Launch the server
From the same environment, start the server with one command. It uses Qwen2.5-7B-Instruct, the conventional SGLang port 30000, and all interfaces so other machines on your network can reach it:
python -m sglang.launch_server \
--model-path Qwen/Qwen2.5-7B-Instruct \
--host 0.0.0.0 \
--port 30000
The first launch downloads the model weights, caches them under your Hugging Face cache directory, and loads them onto the GPU. The server is ready when you see the Uvicorn line saying it is running on port 30000. Leave this terminal running — it is your server. The newer CLI, sglang serve --model-path Qwen/Qwen2.5-7B-Instruct --host 0.0.0.0 --port 30000, does the same thing.
Two notes: Meta Llama instruct weights are gated, so request access on Hugging Face and run huggingface-cli login before launching if you use them. And if the server is only for your own machine, drop --host 0.0.0.0 and let it bind to localhost, the safer default.
Step 3: Test it with curl
Open a second terminal and check that the server lists its model:
curl http://localhost:30000/v1/models
Then send a real chat request through the OpenAI-compatible endpoint. Note that the model field must match the model you launched:
curl -X POST http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-7B-Instruct",
"messages": [{"role": "user", "content": "Explain prefix caching in one sentence."}],
"max_tokens": 64,
"temperature": 0
}'
You should get back JSON with a choices array containing the answer. For streaming output, add "stream": true to the request body.
Step 4: Call it from Python
The OpenAI-compatible API means existing OpenAI client code works with only two changes: the base URL and the API key. SGLang does not check the key by default, so any placeholder works locally:
import openai
client = openai.Client(
base_url="http://localhost:30000/v1",
api_key="EMPTY",
)
response = client.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[{"role": "user", "content": "List three use cases for prefix caching."}],
temperature=0.7,
max_tokens=128,
)
print(response.choices[0].message.content)
That is the whole integration pattern: point the client at your server and keep the model name in sync with what you launched. Switching engines later is cheap for the same reason — the client code does not care which engine sits behind the URL.
Step 5: Tune it for real workloads
The default launch is fine for testing, but three settings matter once more than one person uses the server. First, keep system prompts, tool definitions, and repeated document context identical across requests, byte for byte, so the cache can match them. One changed word at the start of a prompt breaks the shared prefix and forces recomputation.
Second, if you have more than one GPU, spread the model with tensor parallelism instead of running one copy per card:
python -m sglang.launch_server \
--model-path Qwen/Qwen2.5-7B-Instruct \
--host 0.0.0.0 --port 30000 --tp 2
Third, control reserved GPU memory with --mem-fraction-static, for example 0.85. A higher fraction leaves more room for the KV cache, but less headroom for activations; if you hit out-of-memory errors, lower it or switch to a smaller model.
Troubleshooting
- Port already in use: stop the other process, or relaunch with a different
--portand update your client URL. - CUDA out of memory: lower
--mem-fraction-static, shorten the context, or use a smaller model. Never raise the fraction to fix this error. - FlashInfer failure: relaunch with
--disable-flashinferand fix the kernel install later. - Gated-model access denied: run
huggingface-cli loginwith an approved token, or switch to an ungated model such as the Qwen example. - Empty or garbled replies: check the
modelfield exactly matches the launched ID, and that chat messages go to/v1/chat/completions.
What to build next
Once the server answers reliably, point an OpenAI-compatible chat UI at http://localhost:30000/v1, or wire it into a RAG pipeline where every query carries the same retrieved context. For a side-by-side alternative, our vLLM serving tutorial covers the same flow with vLLM, so you can compare the two engines on your own hardware instead of trusting someone else's benchmark. SGLang's advantage shows up most where its cache helps: repeated prompts, long shared contexts, and many similar concurrent questions.

Comments
Post a Comment