AI Tooling
Ollama for Development: Running Local Models Reproducibly
Temperature 0 and a fixed seed don't make a local model's replies reproducible on their own. Measured with Ollama and two small models: how hardware, concurrent requests, the prompt cache, and context limits change replies, and how one endpoint silently cut a long request.
On this page · 11 sections
Imagine a test suite for the fleet-operations assistant from earlier articles. One test sends a request, “write a short update about package 1042 for the support team”, and checks the reply against a saved one. Temperature is 0, the seed is fixed, the model runs locally with Ollama. The test passes all week.
Then it fails on a colleague’s laptop. It fails in CI. It fails once on your own machine, on a day when two other tests ran at the same time. Nobody changed the code.
Most articles in this series that measured a model ran it through Ollama, and several ran into a piece of this: a fixed seed that wasn’t enough to make a run repeatable, a long request cut short without an error. This article puts those pieces side by side and measures which settings actually change a reply, using one assistant, eight requests, two small models, and a word-for-word comparison.
The short version: a fixed seed made sampling repeatable in this controlled setup. Reloading the model, the context size, and the batch size changed nothing here. Running on the CPU instead of the GPU changed most replies, concurrent requests changed several, and the prompt cache changed a few. And the default context window depends on the machine you run on.
Try to Understand It This Way
Think of a long calculation done by hand, by two careful accountants. They add the same column of numbers, rounding each step to the nearest cent. One adds from top to bottom; the other in groups of four. Both are correct, but their totals can differ by a cent, because rounding at each step depends on the order.
Most of the time a cent doesn’t matter. But imagine the total decides a yes or no: “approve if over 10,000.00”. When the true value is very close to the line, the order of addition decides.
A language model at temperature 0 does something similar at every word. It computes a score for every possible next token and picks the highest. The scores come from millions of additions and multiplications in floating point, which round at every step. When two candidates are nearly tied, a different order of the same arithmetic can pick the other one. From that word on, the reply is different, because every later word depends on the earlier ones.
So the question for reproducibility is: what changes the order of the arithmetic? The hardware (CPU or GPU). Whether your request is computed together with other requests. Whether part of your prompt was computed earlier and reused. That’s a common explanation, consistent with what is measured below, though not traced inside the runtime here.
What a Run Depends On
Before comparing replies, it’s worth writing down what a run depends on at all. Ollama exposes most of it:
- The model behind the tag.
qwen2.5:3bis a name, and a tag can be pointed at different files./api/tagsgives the digest (357c53fb659c…here), and that is what to record. - What’s baked into the model.
/api/showreturns the quantization (Q4_K_M for both models here), the trained context length, the chat template, and any parameters the model sets itself.qwen2.5:3bsets none, so a request without options gets Ollama’s defaults.llama3.2:3bsets only its stop tokens. - The chat template. It turns your messages into the text the model reads. It belongs to the model, not to your code, so it changes when the model does. The demo records a hash of it.
- The settings you send, and the ones you don’t.
- The Ollama version and the machine.
The last two items matter more than they look.
The Context Window You Didn’t Choose
When a request doesn’t set num_ctx, how big is the model’s context window? Ollama’s documentation gives three numbers, on three pages that describe different layers:
- The context length page describes the server’s automatic choice: “Ollama defaults to the following context lengths based on VRAM”, including “< 24 GiB VRAM: 4k context”, “24-48 GiB VRAM: 32k context”, and “>= 48 GiB VRAM: 256k context”.
- The FAQ says “By default, Ollama uses a context window size of 4096 tokens.”
- The Modelfile reference lists the
num_ctxparameter with “(Default: 2048)”.
Rather than guess which one applies, ask the running server. The demo loads each model and reads the allocated context back from /api/ps. With Ollama 0.40.16, both models got 4,096 tokens; setting num_ctx: 8192 gave 8,192. The server’s log says how it decided:
msg="vram-based default context" total_vram="17.8 GiB" default_num_ctx=4096
This is an Apple M4 Pro with 24 GiB of unified memory, shared between the CPU and the GPU. Ollama counted 17.8 GiB of it as VRAM, which puts the machine in the “< 24 GiB” tier. So the machine’s memory size alone doesn’t tell you the tier; the server’s own number does.
It also means the same request, unchanged, gets a 4k context on this laptop and, by the documented tiers, 32k on a machine with more GPU memory. A test that passes on a big workstation can fail on a laptop because a long prompt no longer fits. The value that matters is the one the running server allocated, and /api/ps reports it.
The OpenAI-compatible endpoint can’t set it
Ollama also offers an OpenAI-compatible chat API at /v1/chat/completions, which is convenient: existing OpenAI client code works by changing the base URL. Its compatibility page is explicit about one gap: “The OpenAI API does not have a way of setting the context size for a model.”
Here is what that means in practice. The demo builds a delivery log of 14,835 tokens whose first line says that package 4711 was handed to the driver Asha. The question at the end asks who received package 4711.
| Endpoint | Prompt tokens the model read | Reply |
|---|---|---|
native API, num_ctx: 16384, qwen2.5:3b |
14,835 | Asha |
OpenAI-compatible, qwen2.5:3b |
2,050 | None of the packages (including package 4711) were scanned at any of the hubs |
native API, num_ctx: 16384, llama3.2:3b |
10,960 | Asha |
OpenAI-compatible, llama3.2:3b |
2,050 | Pune |
(The two models’ tokenizers count the same text differently.)
Through /v1/chat/completions, the request came back as an ordinary, successful reply: HTTP 200, a normal answer, no error field. The model read only the last 2,050 tokens, the end of the log and the question, and both models answered wrongly. A successful response is not evidence that the model saw your input. The only explicit warning was one line in the server’s log:
level=WARN source=llama_server.go:344 msg="truncating input prompt" limit=2050 prompt=14835 keep=4 new=2050
“Managing Conversation State in AI Applications (opens in a new tab)” found the same behaviour with a long conversation: whole messages dropped from the start, no error.
Two scoping notes. This is /v1/chat/completions in Ollama 0.40.16; the same release’s notes say that a different endpoint, /v1/responses, now “respects truncation and no longer silently truncates by default”, which wasn’t tested here. And the documented way to give the OpenAI-compatible chat endpoint a larger context is a model created from a Modelfile with PARAMETER num_ctx.
Detecting a cut
In the non-streamed responses used here, the API reports how many prompt tokens the model read: prompt_eval_count from the native API, usage.prompt_tokens from /v1/chat/completions. To use it as a check, you need an expectation to compare it with, counted the way the model counts: with the model’s own tokenizer, after the chat template has been applied (the template adds tokens of its own). A count far below your expectation, or one that sits at the context limit, is a strong signal; on its own it isn’t proof, because a miscounted expectation looks the same. When it fires, the server log’s truncating input prompt warning confirms it.
Which Settings Change the Reply
Now the main experiment. The demo, 04-ollama-reproducibility, sends eight requests to the fleet assistant: a customer update, a weekly delay summary, an explanation for a new dispatcher, a message about a split shipment, and so on. Each reply is a paragraph or two, up to 256 tokens.
The reference run uses temperature 0, seed 42, num_ctx 8192, and unloads the model before every request, so each one starts from a freshly loaded model. Every other condition changes one thing and compares each reply with the reference, word for word. “Identical” here means the exact same text: it measures textual reproducibility, not meaning. Two replies that say the same thing in different words count as different, which is exactly what an exact-match test would do.
Every experiment ran on a dedicated Ollama server that the demo starts on its own port, so no other program could share its model, its queue, or its cache. The whole experiment ran twice.
The exact setup
- Date and machine: 2026-11-16, Apple M4 Pro, 24 GiB memory, macOS 26.6.2, on mains power.
- Ollama: 0.40.16. Each experiment started its own
ollama serveon port 11435 (and 11436 withOLLAMA_NUM_PARALLEL=4), withOLLAMA_NOPRUNE=1andOLLAMA_NO_CLOUD=1, and stopped it afterwards. - Models:
qwen2.5:3b(digest357c53fb659c…) andllama3.2:3b(digesta80c4f17acd5…), both Q4_K_M, both 100% on the GPU (from/api/ps) in every condition except the CPU one. - Requests: native
/api/chat, not streamed,num_predict256, one system prompt and eight user requests, sent in the same order in every condition. - Comparison: exact text equality with the reference reply for the same request; for a different reply, the word where it first differs.
- Repeatability: the full experiment ran twice. Both runs gave the same reply, word for word, in every condition except the unseeded ones and one reply under concurrent requests.
- Commands:
python3 scripts/inspect_setup.py, thenpython3 scripts/determinism.py(and again with--tag run2). The demo needs only the Python standard library.
Both runs gave the counts in this table, except where a row shows two results for concurrent requests:
| Condition | qwen2.5:3b identical |
llama3.2:3b identical |
|---|---|---|
| The reference procedure, again | 8/8 | 8/8 |
| Model kept loaded between requests | 8/8 | 8/8 |
num_ctx 4,096 |
8/8 | 8/8 |
num_ctx 16,384 |
8/8 | 8/8 |
num_batch 64 |
8/8 | 8/8 |
| OpenAI-compatible endpoint (short prompts) | 8/8 | 8/8 |
| Same request sent twice in a row: first reply, second reply | 8/8, 7/8 | 7/8, 7/8 |
| Four requests at the same moment | 3/8 | 5/8, then 6/8 |
| CPU instead of GPU | 0/8 | 2/8 |
What didn’t matter
Reloading the model, keeping it loaded, the context size, and the batch size changed nothing in these 16 requests. That was not obvious beforehand: the server log shows that a different num_ctx starts a new runner process, with a different batch size (512 at 4,096 tokens, 1,024 at 8,192 for qwen2.5:3b). In this setup, at least, it computed the same replies.
The OpenAI-compatible endpoint, with the same temperature and seed, also gave identical replies, as long as the prompt fit in its context.
The seed does its job
A separate set of runs used temperature 0.8, where the model samples instead of always taking the top token. Each was compared with an earlier run of the same kind, not with the temperature-0 reference:
| Temperature 0.8, both models (16 requests) | Identical to the earlier run |
|---|---|
| seed 42, run again | 16/16 |
| seed 7 instead of 42 | 0/16 |
| no seed, run again | 0/16 |
Most of the changed replies differed within the first three words. So the seed does what the documentation says, within a controlled setup: the same model file, hardware, settings, request order, and cache state. It makes sampling repeatable there; it doesn’t make two different environments agree, as the next sections show.
CPU and GPU are different machines
With num_gpu: 0, all layers on the CPU, 14 of the 16 replies changed. They were not random: each CPU reply was the same in both runs. It’s a different calculation, consistently. A typical one starts the same and then rephrases:
GPU: Package 1042, which was dispatched from Pune on 14 October at 09:10, is expected to arrive in Delhi on 16 October.
CPU: Package 1042, which was dispatched from Pune at 09:10 on 14 October, was scheduled to arrive in Delhi on 16 October.
The practical consequence: a saved reply from a GPU machine is not a valid expected output for a CI runner without a GPU, even with identical settings, weights, and Ollama version.
Concurrent requests change replies, and not repeatably
The next condition used a server started with OLLAMA_NUM_PARALLEL=4, which lets it process up to four requests together (the FAQ gives the default as 1). Sent one at a time to that server, all 16 replies matched the reference. Sent four at a time, only 3 of 8 matched for qwen2.5:3b, and 5 of 8 for llama3.2:3b. In the second run it was 6 of 8 for llama3.2:3b: one reply depended on how the requests happened to be grouped.
This one is the most likely to surprise you, because nothing in the request changed. What changed was that other requests were processed at the same time. The experiment shows the effect, not its mechanism inside the runtime; the accountant picture above is a plausible explanation, not a traced one. The practical reading: tests that share a server with parallel slots and run at the same time can get different replies than they would alone.
The prompt cache changed a few replies
The last effect is the subtlest. Ollama keeps recently computed prompts, and when a new request starts with the same tokens, it reuses them instead of computing them again. The response reports this as prompt_eval_cached_count.
To see whether that matters, the demo took every temperature-0 reply computed on the GPU one request at a time: the seven conditions cold_again, warm_in_order, warm_twice (16 replies, each request sent twice), num_ctx 4,096 and 16,384, num_batch 64, and the parallel server used one request at a time. That’s 64 replies per model. Each was grouped by how much of its prompt came from the cache, as reported by the second run (the first run didn’t record the field, and its replies were word for word the same). The groups have different sizes because the cache decides them, not the experiment: a model loaded fresh has nothing cached, and a request following another one reuses at least the shared system prompt.
| Prompt tokens reused from the cache | qwen2.5:3b identical |
llama3.2:3b identical |
|---|---|---|
| none | 19/19 | 19/19 |
| the system prompt only | 29/29 | 29/29 |
| all but the last token (the same request had been sent before) | 15/16 | 14/16 |
| all replies | 63/64 | 62/64 |
When all but the last token came from the cache, because the same request had been sent before, three of 32 replies changed, the same three in both runs, and each from its first word. llama3.2:3b’s customer update began “Subject: Update on Package 1042 - Delayed Delivery” instead of “Update for Customer Support:”.
This may explain an observation from earlier in the series. In “Prompt Engineering: Beyond Writing Better Prompts (opens in a new tab)”, the same request could get a different reply depending on which request the runtime had processed just before, and runs became repeatable once the model was unloaded before the suite and the requests always went in the same order. The cause wasn’t investigated there. A cache in a different state is a plausible one, not a confirmed one: unloading clears the cache, and a fixed order puts it in the same state every time.
What Breaks
- A long prompt through
/v1/chat/completions. Cut to fit the default context, with a successful response and a warning only in the server log. - A context default that depends on the machine. 4k on this laptop; the documented tiers give 32k or 256k elsewhere.
- Saved outputs across hardware. CPU and GPU produced different, individually repeatable replies.
- Shared servers. Replies computed while other requests were processed differed from replies computed alone, and not the same way every time.
- Warm caches. Re-sending a request reused its cached prompt, and a few replies changed.
- Tags. A tag can move to a new model file; the digest can’t.
Engineering Decisions
1. Set num_ctx on every request, and check the prompt tokens you got
Don’t depend on the default, because the default depends on the machine. Use the native API when you need to control the context, or create a model with PARAMETER num_ctx for the OpenAI-compatible endpoint. Then check the reported prompt tokens against your expectation, as described above.
2. Record a run’s identity, not just its settings
Ollama version, model digest, quantization, a template hash, the machine (CPU or GPU, memory), the context the server allocated, and the server settings that matter (OLLAMA_NUM_PARALLEL, OLLAMA_CONTEXT_LENGTH). The demo’s inspect_setup.py writes most of it to one JSON file. Trimmed and flattened from this run’s file:
{
"ollama_version": "0.40.16",
"machine": { "cpu": "Apple M4 Pro", "memory_gib": 24.0 },
"model": "qwen2.5:3b",
"digest": "357c53fb659c5076de1d65ccb0b397446227b71a42be9d1603d46168015c9e4b",
"quantization": "Q4_K_M",
"template_sha256": "eb4402837c7829a6",
"context_no_num_ctx": [4096],
"context_num_ctx_8192": [8192]
}
The server settings live outside the request, so record them where you start the server.
3. For exact-match tests, control the server
One request at a time, the model unloaded before each request (or at least a cache that’s in the same state every run), and a fixed hardware type. The demo does this by starting its own server on its own port. In CI, that means a dedicated Ollama instance per job, not one shared across parallel jobs.
4. Or don’t use exact-match tests
For most tests of model behaviour, exact text equality is the wrong check. Check what matters (the key fact is present, the JSON is valid, the right tool was called) and accept that wording varies. Keep exact equality for the narrow cases where you control everything above, such as checking that a refactor didn’t change what reaches the model.
5. Use the seed for sampling, and know its limits
A fixed seed made sampled replies repeatable in this setup. It doesn’t protect against the hardware, the batching, or the cache.
When to Use Ollama (and When Not To)
For development and testing, Ollama is hard to beat: one command to get a model, a local API, no keys, no cost per request, and data that never leaves the machine. Most measured articles in this series used it for exactly that, and the API gives you what you need to make runs reproducible: digests, timing and token counts, /api/ps, and settings you can pin.
The care it needs is the care this article measured: defaults that depend on the machine, an OpenAI-compatible layer that can’t set everything, and a server whose concurrency and cache affect results. None of that is a reason to avoid it. It is a reason to record your setup and control the server when results matter.
For serving many users at once, the trade-offs change: by default this server processed one request at a time, and parallel slots trade memory (the FAQ notes that “Required RAM will scale by OLLAMA_NUM_PARALLEL * OLLAMA_CONTEXT_LENGTH”) and, as measured here, exact repeatability for throughput. That decision deserves its own measurements with your own load, which this article doesn’t attempt.
What to Take Away
- Reproducibility belongs to the whole setup, not only to the prompt and the model: the hardware, the server’s concurrency, and its cache are part of the experiment.
- A fixed seed makes sampling repeatable, within one controlled setup.
- Set
num_ctxyourself, and read back what you got. The default depends on the machine. - A successful response can hide a cut prompt. Compare the reported prompt tokens with what you sent.
- Use exact-match tests only where you control everything; check meaning everywhere else.
Think About These Use Cases
-
A regression suite in CI: Your tests compare model replies with saved ones, and the CI runners have no GPU. What do you save, where do you generate it, and which tests should stop comparing exact text?
-
A team sharing one Ollama server: Five developers point their apps at one machine with
OLLAMA_NUM_PARALLEL=4. What changes for someone debugging a reply that “changed for no reason”? -
An evaluation run: You compare two prompts on 200 questions. Which settings from this article must be identical between the two runs for the comparison to be fair, and how would you check it afterwards?
-
Moving to a hosted API: Which of these controls (digest, server settings, hardware) do you lose, and what would you record instead?
-
Long documents: A summarisation feature uses the OpenAI-compatible endpoint. How would you detect, in production, that a document was cut before the model saw it?
If you want to talk one of these through, take it to GPT along with this article. Ask GPT ↗️
Related Sources
- 04-ollama-reproducibility: The companion demo: the dedicated-server helper, the setup inspection, the determinism conditions, and the exact setup. Python standard library only.
- Ollama: Context length: The documented defaults by GPU memory, and how to change them.
- Ollama: OpenAI compatibility: The supported fields, and the note that the context size can’t be set through it.
- Ollama: FAQ: Keeping models loaded, concurrent requests, and
OLLAMA_NUM_PARALLEL. - Ollama: API reference:
/api/chatoptions, timing and token fields,/api/show, and/api/ps.
What’s Next?
Ollama is the layer this series talks to directly, with plain HTTP requests. Many applications instead go through a framework that wraps the model call. The next article in this AI Tooling track looks at what one of them sends on your behalf: LangChain: What It Gives You and What It Hides, measured on the wire between LangChain and the same Ollama server.