Topic 2 of 6 · 6 min read
Pulling and running models with Ollama
Goal: Write an Ollama Modelfile that sets the context length, temperature and system prompt for a pulled model.
Ollama downloads a model by tag, runs it as a background service and lets you bake your settings into a Modelfile so every run behaves the same.
Check yourself · 1 of 3
Which Modelfile instruction sets the system prompt?
Why it matters
Ollama's defaults are made for a quick chat, not your task. The default context window is set by your VRAM, and on GPUs under 24 GB it is only 4096 tokens (Ollama docs), so a long prompt can be cut off without any error. A Modelfile fixes the settings once, in a file you can commit.
The basic loop
ollama pull llama3.2:3b # download a model by tag
ollama run llama3.2:3b # chat in the terminal
ollama ps # what is loaded, and on GPU or CPU
ollama show llama3.2:3b # template, parameters, context length
A tag after the colon picks the size and quantization, for example :3b or :8b-instruct-q4_K_M. Without a tag you get the default one, which may not be what you want. Read the model page for the tags.
The Modelfile
A Modelfile is a short text file. FROM names the base, PARAMETER sets a runtime option and SYSTEM sets the system prompt.
FROM llama3.2:3b
PARAMETER num_ctx 8192
PARAMETER temperature 0.2
SYSTEM You are a code reviewer. Answer in short bullet points.
Build and run it with ollama create reviewer -f Modelfile, then ollama run reviewer. num_ctx is the context length in tokens. A larger window uses more memory, so raise it to what the task needs, not to the maximum.
The API
Ollama serves HTTP on localhost:11434. The native endpoint is /api/chat, and an OpenAI-compatible one lives under /v1. You can also pass options per request, such as {"num_ctx": 8192}, instead of using a Modelfile.
Before and after
Weak: pasting a 20-page document into ollama run llama3.2:3b and getting a summary of only the last few pages, because the 4096-token window dropped the start.
Better: create reviewer with num_ctx 16384, then check ollama ps to see the loaded context and that the model still sits fully on the GPU.
What goes wrong
- Silent truncation. Input beyond the window is dropped, and the model answers as if it never saw it.
- Memory pushed to CPU. A large
num_ctxcan make the model no longer fit in VRAM; part runs on the CPU and generation slows sharply. - A different tag than expected. Pulling the bare name gets the default tag, so a teammate may run another size or quantization.
Examples
A reviewer model
FROM qwen2.5-coder:7b
PARAMETER num_ctx 16384
PARAMETER temperature 0.1
SYSTEM You review diffs. List bugs first, then style notes.
ollama create reviewer -f Modelfile builds it. The low temperature keeps answers steady between runs.
Calling the API
curl http://localhost:11434/api/chat -d '{
"model": "reviewer",
"messages": [{"role": "user", "content": "Review: if x = 1:"}],
"stream": false
}'
Set "stream": false to get one JSON reply instead of a stream of chunks.
Key points
- Pull models by explicit tag so everyone gets the same size and quantization.
- The default context window is small on most GPUs (4096 tokens under 24 GB of VRAM), so raise num_ctx in a Modelfile or a request option for long inputs.
- A Modelfile uses FROM, PARAMETER and SYSTEM, and ollama create turns it into a named model.
- Use ollama ps to check where a model is loaded and how much is on the GPU.
- The local API listens on port 11434, with an OpenAI-compatible route under /v1.
Common mistakes
- Leaving num_ctx at its default and wondering why the model ignores the start of a long prompt.
- Setting num_ctx to the model's maximum, so the model spills out of VRAM and slows down.
- Pulling a bare model name and assuming it is the size or quantization you read about.
- Editing the Modelfile without running ollama create again, so the old settings stay in use.
Check yourself · 2 of 3
A 12k-token document is summarised, but the summary only covers its end. You use Ollama defaults. What is the likely cause and fix?
Check yourself · 3 of 3
You raised num_ctx a lot and generation became very slow. What should you check first?
Practice items, not counted towards your mastery.
Start here
- Ollama documentation (opens in a new tab)DocsFree
Install steps, the Modelfile format, context length and the local API in one place.
- LM Studio docs (opens in a new tab)DocsFree
How the desktop app downloads models, sets GPU offload and serves a local API.
- llama.cpp (opens in a new tab)ToolFree
The reference runtime for GGUF files: build options, llama-cli and llama-server flags.
More for this topic
- Ollama Modelfile reference (opens in a new tab)DocsFree
Every Modelfile instruction and parameter, including num_ctx and temperature.
- Ollama context length (opens in a new tab)DocsFree
Why the default window is small and how to raise it.