A Modelfile is the blueprint Ollama uses to create and share customized models. It lets you choose a base model with FROM, set persistent behavior with SYSTEM, and adjust runtime defaults such as temperature and context length with PARAMETER. This guide explains what each instruction does, how to build a custom model from a Modelfile, and how to diagnose the most common problems when the system prompt or parameters do not behave as expected.
The intended reader is a technically curious beginner or intermediate practitioner who already has Ollama installed or is ready to install it. The examples are illustrative; they show the file structure and commands, not benchmark results. For authoritative details, see the Ollama Modelfile reference and the Template documentation.
What a Modelfile Controls
Ollama describes a Modelfile as the blueprint to create and share customized models. The file is a plain text list of instructions. Each instruction starts with a keyword, followed by arguments. The most important instructions for customization are:
- FROM (required): defines the base model to use. This can be an existing Ollama model, a Safetensors model, or a GGUF model. If you are customizing an existing model such as
llama3.2, this is the only required instruction. - SYSTEM: specifies the system message that will be set in the template. This is where you put persistent instructions such as tone, role, output format, or refusal behavior.
- PARAMETER: sets parameters for how Ollama will run the model. The documentation gives
temperatureandnum_ctxas examples. Temperature controls creativity versus coherence;num_ctxcontrols how many tokens the model can use as context to generate the next token. - TEMPLATE: the full prompt template sent to the model. Ollama uses a Go-based templating engine. Variables include
System,Prompt,Response,Suffix, andMessages. - LICENSE, MESSAGE, REQUIRES, and CAPABILITY: additional instructions for legal terms, message history, minimum Ollama version, and declared capabilities.
For most prompt and parameter customization, you only need FROM, SYSTEM, and one or more PARAMETER lines. Overriding TEMPLATE is more advanced because the template must match the model family’s expected chat format.
Prerequisites and Scope
You need a working Ollama installation, a terminal, and a text editor. You also need a base model available locally. The examples below use llama3.2, but any model you have pulled or imported can be used. If the base model is not present, pull it first.
A Modelfile does not fine-tune the model. It does not add new knowledge by training weights. It changes the prompt and runtime configuration used when the model generates text. This distinction matters when you expect a custom system prompt to teach the model facts it never learned. For factual grounding, retrieval or fine-tuning may be more appropriate.
Step-by-Step: Build a Custom Model
1. Pull a base model
Run the pull command for the model you want to customize. For example:
ollama pull llama3.2A successful pull means the model is available locally and can be referenced in a FROM line.
2. Write the Modelfile
Create a file named Modelfile in a working directory. The following is an illustrative example of a concise technical assistant:
FROM llama3.2
PARAMETER temperature 0.2
PARAMETER num_ctx 4096
SYSTEM You are a concise technical assistant for local AI users. Answer in short paragraphs. If you are unsure, say so.The SYSTEM line becomes the system message inserted by the template. The two PARAMETER lines set a lower temperature for more coherent output and a 4096-token context window. The Ollama documentation notes that higher temperature is more creative and lower temperature is more coherent, and that num_ctx controls the context window size.
3. Create the model
Build a new local model from the Modelfile. The documented command pattern is:
ollama create my-assistant -f ModelfileReplace my-assistant with the name you want to use. If the command succeeds, Ollama has created a new model entry that references the base model and applies your instructions.
4. Run and verify
Run the custom model:
ollama run my-assistantAsk a question and observe whether the response follows the system prompt. For a more direct check, inspect the generated Modelfile:
ollama show --modelfile my-assistantAccording to the Ollama documentation, ollama show --modelfile displays the Modelfile of a given model. The output should include your SYSTEM and PARAMETER lines. If it does not, the model was not rebuilt from the edited file.
System Prompts vs. Templates
The SYSTEM instruction is not the same as the TEMPLATE instruction. SYSTEM provides the system message. TEMPLATE defines how the system message, user messages, and assistant messages are arranged into the final prompt. For an existing Ollama chat model, the template is already set. Adding a SYSTEM line is usually enough to customize behavior.
If you override TEMPLATE, you must include the variables the model expects. The Ollama template documentation shows a Llama 3 example:
FROM llama3.2
TEMPLATE """{{- if .System }}<|start_header_id|>system<|end_header_id|> {{ .System }}<|eot_id|> {{- end }} {{- range .Messages }}<|start_header_id|>{{ .Role }}<|end_header_id|> {{ .Content }}<|eot_id|> {{- end }}<|start_header_id|>assistant</end_header_id|> """This template checks for a system message, iterates over the message list, and ends with an assistant header. If you omit the system block or use the wrong special tokens, the model may ignore the system prompt or produce malformed chat output. For imported text-completion models, the default template is {{ .Prompt }}, which sends user input verbatim. That default is appropriate for completion models but not for chat or instruction models.
Choosing Parameters
Parameters in a Modelfile set defaults for how Ollama runs the model. The two documented examples are the safest starting points:
- temperature: higher values are more creative, lower values are more coherent. A value around 0.1 to 0.3 is common for extraction, classification, and technical answers. A value around 0.7 to 1.0 is common for brainstorming and creative writing.
- num_ctx: the context window size in tokens. The documentation example uses 4096. Increasing this value lets the model consider more conversation history or document text, but it also increases memory use because the runtime must store the key-value cache for the context.
Other parameters may be available depending on your Ollama version, such as top_p, top_k, repeat_penalty, and num_predict. Treat these as advanced tuning knobs. Change one at a time and inspect the output rather than copying a large parameter set from an unknown source. If a parameter is not recognized, Ollama may reject the Modelfile or ignore the line depending on the version; check the output of ollama create for errors.
A Practical Example: Deterministic Extraction
Suppose you want a local model to extract structured data from short user messages. You want low creativity, a clear output contract, and a modest context window. An illustrative Modelfile could be:
FROM llama3.2
PARAMETER temperature 0
PARAMETER num_ctx 4096
SYSTEM You extract structured data from user text. Return only valid JSON. Do not add commentary.Build and run it with the same ollama create and ollama run commands. The expected result is that the model tends to return JSON without extra prose. This is not a guarantee: small models can still add commentary or produce invalid JSON, especially when the input is ambiguous. For production extraction, validate the output with a parser and consider a repair step. The example is illustrative and has not been benchmarked here.
Troubleshooting Common Problems
Model not found when creating or running
If ollama create or ollama run reports that the model does not exist, check the FROM line. The base model name must match a local model exactly. Pull it with ollama pull or correct the name. If you are building from a Safetensors or GGUF file, verify the path and file format.
The system prompt seems ignored
First, confirm that the custom model was rebuilt after the last edit. Run ollama show --modelfile my-assistant and look for your SYSTEM line. If it is missing, run ollama create again. If the line is present but the behavior is unchanged, the template may not include .System. For models where you overrode TEMPLATE, add the system block shown in the template documentation. Also remember that a system prompt is guidance, not a hard constraint; a small model may still drift.
Output is too random or too repetitive
Adjust temperature. Lower it for more coherent, repeatable answers. Raise it if the model is stuck in repetitive phrasing and you want more variation. Change one parameter at a time and recreate the model. If repetition persists, the issue may be the model itself, the prompt, or the context length rather than a single parameter.
Context or memory errors
If you increase num_ctx and the model fails to load or the machine slows dramatically, reduce the context window or use a smaller quantized model. The model weights are not the only memory cost. The key-value cache for the context and runtime overhead also consume memory. Start with the documented example value of 4096 and increase only when you have measured headroom.
Template breaks chat formatting
If the model outputs raw special tokens, ignores roles, or behaves worse after a template change, remove the custom TEMPLATE line and rebuild. The base model’s original template is usually better than a hand-written template unless you are importing a model that lacks one. When you must write a template, copy the exact format from the model card or the Ollama template documentation rather than guessing.
Limitations and Tradeoffs
Modelfiles are a lightweight customization layer. They are excellent for setting persistent behavior, default parameters, and reproducible local configurations. They are not a substitute for fine-tuning when you need new factual knowledge or a specialized style that the base model cannot follow through prompting. System prompts also consume context tokens, so a very long system message reduces the space available for user input and retrieved documents.
Licensing is another consideration. A Modelfile can include a LICENSE instruction, but that does not change the license of the base model. Check the base model’s license before redistribution or commercial use. Open weights, open source, and commercial-use permission are not interchangeable terms.
Next Steps
Start with a minimal Modelfile: one FROM line, one SYSTEM line, and one or two parameters. Build the model, run it, and inspect it with ollama show --modelfile. Keep a copy of each working Modelfile so you can compare behavior when you change a prompt or parameter. If you are working in a WordPress or automation workflow, the same pattern applies: create a named local model with a stable system prompt, then call it from your script or plugin through the Ollama API or CLI.
If you run into a problem that the documentation does not cover, share your Ollama version, base model, full Modelfile, the exact command, and the observed output in the community. Those details make it possible for others to reproduce the issue and suggest a fix.
FAQ
Does a Modelfile fine-tune the model?
No. A Modelfile customizes the prompt and runtime parameters. It does not train new weights or add factual knowledge to the base model.
How do I see the Modelfile of an existing Ollama model?
Run ollama show --modelfile MODEL. The documentation lists this command for viewing a model's Modelfile.
Do I need to write a TEMPLATE to change the system prompt?
Usually no. Existing Ollama chat models already have a template that includes the system message. Add a SYSTEM line and rebuild the model. Override TEMPLATE only when you need a custom prompt format.
What does num_ctx change?
num_ctx sets the context window size in tokens. The documentation describes it as controlling how many tokens the LLM can use as context to generate the next token.