Understanding the Ollama Modelfile Architecture
An Ollama Modelfile is the definitive configuration layer that sits between your underlying transformer weights (GGUF or Safetensors) and the local runtime inference engine. Just as a Dockerfile packages software dependencies and runtime directives into a container, a Modelfile instructs Ollama how to allocate GPU memory, apply sampling parameters, format conversational turns, and enforce stop tokens.
The 8 Core Modelfile Directives
Specifies the base model tag (e.g., llama3.3:70b) or local file path (./model.gguf).
Controls runtime inference: temperature, num_ctx, top_p, top_k, stop.
Configures persistent system steering instructions wrapped in triple quotes """.
Go-template syntax formatting conversational turns between system, user, and assistant.
Injects few-shot historical turns (MESSAGE user ..., MESSAGE assistant ...).
Declares model specializations, such as CAPABILITY decision.
Enforces a minimum Ollama daemon version (e.g., REQUIRES 0.14.0).
Attaches distribution or legal licensing terms to the packaged model manifest.
Top 4 Modelfile Mistakes Caught by This Linter
1. Array Syntax in Stop Sequences
In frameworks like LangChain or OpenAI API SDKs, stop tokens are passed as a JSON list (e.g. ["stop1", "stop2"]). In an Ollama Modelfile, writing PARAMETER stop ["<|im_end|>", "<|eot_id|>"] is invalid syntax. Ollama requires each sequence on its own separate line:
# Correct Ollama Stop Syntax:
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"
PARAMETER stop "<|eot_id|>" 2. Forgetting Context Window Scaling (`num_ctx`)
Many open-weight models advertise 32k or 128k context windows on HuggingFace. However, when loaded in Ollama, they frequently truncate memory after 2,048 or 4,096 tokens because num_ctx was left unset. Setting PARAMETER num_ctx 16384 or 32768 expands the context window to fit full codebases, documents, and multi-turn conversations.
3. Broken Go Template Variable Matching
Ollama parses templates with Go's standard text/template engine. If your template omits {{ .Prompt }}, incoming user questions are dropped and never passed to the LLM attention layers. Furthermore, unclosed conditionals (e.g., missing {{ end }}) will cause ollama create to abort with a template compile failure.
4. Unescaped Multiline Triple Quotes
When authoring long system prompts or license texts, always enclose the text block in triple double quotes """. Ensure the prompt content itself does not contain three consecutive quotes in a row, as this prematurely closes the block in the parser.
Step-by-Step Workflow: How to Build & Deploy
- Validate or Generate: Paste your existing Modelfile into the editor above or select one of the curated presets (Llama 3.3, Qwen 2.5 Coder, DeepSeek R1).
- Save the File: Click Download or save the text locally as a file named exactly
Modelfilein your project directory. - Compile with Ollama: Open your terminal and run
ollama create my-custom-model -f ./Modelfile. Ollama creates a local manifest in seconds by referencing base weights. - Test Interactively: Launch an interactive terminal session with
ollama run my-custom-model. - Inspect Active Configuration: To verify active parameters at any time, execute
ollama show --modelfile my-custom-model.