Sub topic 14
Building and running a customized model
This chapter assumes you have Ollama installed with the server running. It also assumes you can pull and run a model such as llama3.2 and that you know the basics of a Modelfile (the file that defines a model's base, parameters, and system prompt). Here you will turn that file into a model you can run, inspect, call from code, change, copy, and share.
What "customized" means in Ollama
A customized model is not retrained. It is a small recipe on top of existing weights: a base model, default parameters, a system prompt, and optionally a prompt template or example messages. ollama create stores that recipe under a new name. The large weight file (a blob) is not copied, so a customized model costs only a few kilobytes of extra disk.
This makes customization cheap, and it fits one clear use case: giving a model a stable behavior. Examples are a "commit message helper" or a "terse code reviewer" that you can start by name without retyping settings. It does not teach the model new facts. For that you need fine-tuning or retrieval, which are outside this chapter.
Step 1: Write the Modelfile
Save this as a file named Modelfile (no extension) in your working directory.
FROM llama3.2
PARAMETER temperature 0.3
PARAMETER num_ctx 4096
SYSTEM You are a concise assistant that answers in at most three sentences.Each line does one job:
FROMnames the base. It must come first. The base can be a model already in your library, such asllama3.2orllama3.2:3b, or a path to GGUF weights (a later chapter covers this).PARAMETERsets a default generation setting. A lowtemperaturemakes output more predictable.num_ctxsets the context window in tokens.SYSTEMsets the system prompt that starts every conversation.
If the base model is not on disk yet, ollama create normally pulls it for you. Pulling it first with ollama pull llama3.2 makes any network problem easier to spot.
For a longer system prompt, use triple quotes so the text can span several lines:
FROM llama3.2
SYSTEM """
You are a code reviewer.
Point out bugs first, then style issues.
Never rewrite the whole file.
"""Step 2: Create and run the model
ollama create concise-assistant -f Modelfile
ollama run concise-assistantcreate prints progress lines as it writes layers and a manifest, then reports success. The first argument is the new model's name. With no tag it becomes concise-assistant:latest.
Check that it exists and is separate from its base:
ollama list
ollama show concise-assistantollama list shows the new name next to llama3.2. The sizes look nearly the same because each entry reports the full model size, but the weights are stored once. To see the recipe Ollama saved, print the effective Modelfile:
ollama show concise-assistant --modelfileThe output includes your PARAMETER and SYSTEM lines, plus the base's template and a FROM line pointing at the weights blob. This is also a good way to see the template a base model uses before you decide to override it.
Step 3: Use it from code
A customized model is addressed by name like any other. The REST API needs no new settings. Your defaults apply automatically.
import requests
resp = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "concise-assistant",
"messages": [{"role": "user", "content": "What is quantization?"}],
"stream": False,
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["message"]["content"])The system prompt and the two parameters come from the model, not from the request. A request can still override parameters for a single call through its options field. For example, "options": {"temperature": 0.9} beats the Modelfile value for that call only. In an interactive session the equivalent is /set parameter temperature 0.9, which lasts until you exit.
The same name works through the OpenAI-compatible endpoint. Set model="concise-assistant" in the client you used earlier.
Step 4: Change, copy, and remove
Customizing is iterative. You edit the file, rebuild, and test.
- Edit the Modelfile.
- Run
ollama create concise-assistant -f Modelfileagain. Using the same name replaces the old definition. - Start a fresh
ollama runsession. An already running chat keeps the old settings, and a model already loaded in memory may need to unload first.ollama psshows what is loaded.
Two other commands help in daily use:
ollama cp concise-assistant concise-assistant:v1
ollama rm concise-assistant:v1cp makes a new name for the same recipe. This is handy for keeping a known-good version before you experiment. rm removes only that name. The shared weights stay on disk as long as another model still uses them.
Other Modelfile instructions
Beyond the three you used, a few more instructions come up in practice.
| Instruction | Purpose | Notes |
|---|---|---|
| `TEMPLATE` | Controls how system, user, and assistant text are wrapped into the raw prompt | Use the base model's own template unless you know the format it was trained on. |
| `MESSAGE` | Adds example conversation turns, such as `MESSAGE user Hello` | Gives the model a few-shot pattern. These turns use context tokens on every request. |
| `ADAPTER` | Applies a LoRA adapter to the base | The adapter must match the exact base model it was trained on. |
| `LICENSE` | Embeds license text | Keep the base model's license terms in mind when you share. |
The most common mistake is overriding TEMPLATE with the wrong format. The model then loads without error but answers badly, with stray tokens or a ramble that never ends. If a customized model behaves worse than its base, remove your TEMPLATE line first.
Sharing a model
To share a model through the Ollama library, you need an account at ollama.com and your local public key added to it. The model name must start with your username:
ollama cp concise-assistant yourname/concise-assistant
ollama push yourname/concise-assistantOthers can then run ollama run yourname/concise-assistant. Only the small custom layers upload, because the base weights already exist in the library. You can also share the Modelfile itself as a text file. Anyone with the same base model can rebuild your model with ollama create.
Common problems
pull model manifest: file does not exist. TheFROMname or tag is wrong. Check it withollama listor on the library page.- Settings seem ignored. You may be talking to an older loaded copy, or a request is overriding them with
options. Rebuild, runollama ps, and test from a new session. - Model runs out of memory after a change. Raising
num_ctxincreases memory use, because the context cache grows with it. Lower it or choose a smaller quantization. - Replies cut off or loop. A bad
TEMPLATEor a very smallnum_ctxis the usual cause. Restore the base template and a window of at least 2048 tokens.
Exercise
You build concise-assistant from llama3.2 and then run ollama rm llama3.2. Will concise-assistant still work, and what does ollama rm concise-assistant do to the weights file?
Show answer
Do not assume it will keep working. The custom model has no weights of its own. Its manifest points at the base's weights blob, so deleting the base name drops that name, and the blob stays on disk only while another manifest still references it. Because concise-assistant still references it, current Ollama versions normally keep the blob and the custom model keeps running, but pulling the base again is the safe fix if it fails. Removing concise-assistant deletes only its manifest and its small custom layers. The weights blob stays if llama3.2 still uses it.
Exercise
Write a Modelfile for a model named reviewer based on llama3.2 with temperature 0.2 and a system prompt that tells it to list bugs first. Give the two commands to build it and start a chat. Then say how you would test one request at temperature 0.8 without editing the file.
Show answer
Modelfile: FROM llama3.2, then PARAMETER temperature 0.2, then SYSTEM You are a code reviewer. List bugs first, then style issues. Commands: ollama create reviewer -f Modelfile and ollama run reviewer. For one request at 0.8, send "options": {"temperature": 0.8} in the API call, or type /set parameter temperature 0.8 inside the interactive session. Neither changes the saved model.