Sub topic 3
Open-weight models: weights you can download and run
This chapter assumes you can call an HTTP API or SDK, read basic Python, and know what a prompt, token, and context are. It covers what an open-weight model is, what you get when you download one, and the errors people commonly make when they first try one.
What "open-weight" means
A model's weights are the numbers learned during training. They are stored in files, and they are what the model is. An open-weight model is one whose weights the publisher lets you download. With the weights and a program that can run them, you can generate text on hardware you control. No request leaves your network.
Compare this with a hosted API. There you send text to the provider's servers, and you never see the weights.
What you download
A typical model release contains these parts:
- Weight files. Large files, often several gigabytes each, usually in a format such as safetensors or GGUF.
- Tokenizer. The rules that turn text into tokens. It must match the model.
- Configuration. Layer counts, context length, and similar settings.
- License. The terms of use. Read it before you build on the model. A later chapter covers licenses.
You also need an inference engine, which is the software that loads the weights and produces tokens. Ollama, llama.cpp, and vLLM are common choices.
What changes for you
Running the model yourself changes four things:
- Control. You choose the exact model version. It does not change unless you change it.
- Data location. Prompts stay on your machines.
- Cost shape. You pay for hardware and operation, not per token.
- Effort. You maintain the server, handle scaling, and apply updates.
A first run
Many local servers expose an OpenAI-compatible endpoint. This code reads its settings from environment variables, so the same code works for a hosted provider or a local server.
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 Ollama, you might set LLM_BASE_URL to http://localhost:11434/v1 and LLM_MODEL to the name of a model you have pulled. Check the exact values in your server's documentation.
Common errors
- Assuming "open-weight" means "open-source". The weights may come with restrictions on use. Check the license.
- Ignoring memory. The weights must fit in memory with room to spare. A model that does not fit will fail to load or run very slowly. Later chapters show how to estimate this.
- Expecting parity with the largest hosted models. Open-weight models come in many sizes. A small one may not match a large hosted model on your task. Measure it.
- Mismatched tokenizer or prompt format. Many models expect a specific chat template. Use the template that ships with the model, or a server that applies it for you.
- Forgetting operating costs. Hardware, power, monitoring, and engineer time all count.
Exercise
Your team wants prompts to stay inside your own network. Name two things you must do that a hosted API would have done for you.
Show answer
Any two of: provide and maintain the hardware, run an inference engine to serve the model, handle scaling and uptime, and apply model and software updates.