Sub topic 11
Running open-weight models locally and on servers
This chapter assumes you can call an HTTP API from Python, know what a token and a context window are, and have read the earlier chapters on memory estimates and quantization. Here you decide how to run an open-weight model once you have picked one.
Three ways to run a model
You have three common options.
- Local desktop or laptop. Tools such as Ollama and llama.cpp download a quantized model and serve it on your own machine. This suits prototyping, offline work, and private experiments.
- A single GPU server. A server such as vLLM or Hugging Face Text Generation Inference loads the model onto one or more GPUs. It handles many requests at once by batching them.
- A managed endpoint for open weights. A cloud provider runs the open-weight model for you and bills per token or per hour. You keep the model choice but give up some control over where it runs.
| Option | Best for | Main limit |
|---|---|---|
| Local tool | Prototyping, offline use | One user at a time, small models |
| GPU server | Production traffic, privacy needs | You operate it |
| Managed open-weight endpoint | Open model without ops work | Data leaves your network |
Fit the model to the hardware
Start from the weight estimate. A 7B model at 4-bit needs about 3.5 GB for weights. Add room for the KV cache (the stored attention data for the tokens in context) and runtime overhead. A longer context window or more simultaneous users grows the KV cache, so a model that fits for one user may not fit for twenty.
Keep the application code the same
Ollama and vLLM both expose an OpenAI-compatible endpoint. Your application can then switch between a hosted API and your own server by changing environment variables. The client below reads its settings from the environment.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["LLM_BASE_URL"],
api_key=os.environ.get("LLM_API_KEY", "not-needed-for-local"),
)
MODEL = os.environ["LLM_MODEL"]
def ask(prompt: str) -> str:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
temperature=0,
)
return response.choices[0].message.contentFor a local Ollama server, you might set LLM_BASE_URL=http://localhost:11434/v1 and LLM_MODEL to the name of a model you have pulled. For vLLM, the default address is usually http://localhost:8000/v1.
Common errors
- Out-of-memory at load time. The weights alone do not fit. Use a smaller model or a lower-precision quantization.
- Out-of-memory under load. The model loads, then fails with many users or long prompts. Reduce the maximum context length or the number of concurrent requests.
- Model name mismatch. The server returns a "model not found" error because
LLM_MODELdoes not match the name the server registered. - Wrong quality assumptions. A 4-bit quantized model can score lower than the full-precision one. Run your own evaluation cases against the exact build you will deploy.
Exercise
A 13B model at 4-bit has weights of about 6.5 GB. It loads on a 8 GB GPU but crashes when several users send long prompts. What is the likely cause, and what are two ways to fix it?
Show answer
The KV cache grows with context length and concurrent requests, and the remaining 1.5 GB cannot hold it. Fixes: lower the maximum context length or concurrency limit, or move to a GPU with more memory or a smaller model.