repobenchmarks // local llm
AI TOOLS
$ ollama create code-reviewer -f ./Modelfile && ollama run code-reviewer

Ollama Modelfile Mastery

Package custom system prompts, context windows, and sampling parameters into shareable, reproducible local AI model images.

Format: Declarative Modelfile Weights: GGUF / Base Ollama Control: Temp, Top-K, Num_Ctx
FROM llama3.1:8b PARAMETER temperature 0.2 PARAMETER num_ctx 16384 SYSTEM "You are a Senior Architect..." ✓ Builds in 2.1 seconds

Most developers use Ollama by executing basic commands like ollama run llama3.1. However, standard models come with general-purpose system instructions, conservative context window limits (often defaulting to 2,048 or 4,096 tokens), and moderate temperature settings that can cause subtle hallucinations during strict coding tasks. The Ollama Modelfile is the configuration blueprint that gives you granular control over model parameters, custom personas, and external GGUF weights.

Ollama Modelfile Build & Execution Pipeline Diagram showing how Modelfile parameters compile into an immutable local model blob stored in Ollama's model registry. Modelfile Source FROM llama3.1 SYSTEM Persona PARAMETER num_ctx Ollama Engine (Compiler) GGUF Weight Linker Manifest & Parameter Blob `ollama create my-persona -f Modelfile` Local Model my-persona:latest Instant Run
Figure 1: Modelfile compilation pipeline producing reproducible, versioned local model manifests.

Key Modelfile Directives

Directive Syntax Example Purpose & Best Practice
FROM FROM llama3.1:8b or ./model.gguf Specifies the base model layer or raw quantized GGUF weights.
SYSTEM SYSTEM """You are a senior Rust engineer...""" Sets immutable base instructions and behavioral constraints.
num_ctx PARAMETER num_ctx 16384 Expands context window memory. Critical for multi-file code reviews.
temperature PARAMETER temperature 0.1 Controls randomness. Lower values (0.0 - 0.2) ensure deterministic code output.
stop PARAMETER stop "<|eot_id|>" Defines custom stop tokens to prevent runaway token generation.

Complete Modelfile Example: Strict Code Reviewer

Modelfile
FROM qwen2.5-coder:14b

# Set low temperature for strict deterministic code analysis
PARAMETER temperature 0.1
PARAMETER top_p 0.95
PARAMETER num_ctx 16384

# Define system persona
SYSTEM """
You are a Staff Security and Performance Code Reviewer.
When analyzing pull requests:
1. Identify memory safety hazards and concurrency races.
2. Flag missing error handling and resource leaks.
3. Suggest concrete code fixes formatted as unified diffs.
Never output conversational filler. Provide only actionable technical critique.
"""
✓ Memory Sizing Rule for num_ctx
  • VRAM KV-Cache Scaling: Increasing num_ctx from 2,048 to 16,384 tokens increases the GPU VRAM needed for the Key-Value (KV) cache by approximately 1.5 GB to 3 GB depending on model layer count. Ensure your GPU has sufficient VRAM before expanding to 32k+ contexts.

Conclusion

The Modelfile turns local LLMs from generic conversational bots into specialized, domain-tailored developer tools that run entirely offline on your workstation.

// Primary Sources & Documentation
Editorial Note: Parameter specifications verified against Ollama v0.5+ official documentation.