Ask the same question twice and you can get two different answers, because the model samples each next token instead of always picking the most likely one. Temperature and top-p are the two knobs that control that sampling, and mlx-lm exposes them both on the command line and in the Python API. This guide is strictly about sampling parameters — it is different from the one-off generate guide, which is about getting a single answer with a --max-tokens length cap, and from the chat guide, which is about a back-and-forth REPL.
Set them on the command line
Install the package first — the README gives pip install mlx-lm — then add the flags to the README’s own quick-start command. The generate entry point defines --temp as a float defaulting to 0.0 (“Sampling temperature”) and --top-p as a float defaulting to 1.0 (“Sampling top-p”):
mlx_lm.generate --prompt "Explain what a lighthouse keeper does in two sentences." --max-tokens 100 --temp 0.7 --top-p 0.9
With no --model flag, the command uses the default model, mlx-community/Llama-3.2-3B-Instruct-4bit, downloading it from the Hugging Face Hub on first use. The README also notes that commands in mlx-lm typically take command line options which let you specify the model, sampling parameters, and more, and that mlx_lm.generate -h lists the available options — run that on your installed version to confirm the flags.
Build a sampler in the Python API
The README’s Sampling section says the generate and stream_generate functions accept sampler and logits_processors keyword arguments, and that some standard sampling functions and logits processors are provided in mlx_lm.sample_utils. The sampler builder is make_sampler, whose documentation says: temp is the temperature for sampling, and if 0 the argmax is used, with a default of 0; top_p is nucleus sampling, where higher means the model considers more less likely words. Build one and pass it to generate:
from mlx_lm import load, generate
from mlx_lm.sample_utils import make_sampler
model, tokenizer = load("mlx-community/Llama-3.2-3B-Instruct-4bit")
sampler = make_sampler(temp=0.7, top_p=0.9)
text = generate(model, tokenizer, prompt="Explain what a lighthouse keeper does in two sentences.", max_tokens=100, sampler=sampler)
print(text)
The generate function passes its remaining keyword arguments through to stream_generate, so sampler=sampler reaches the generation loop. Building the sampler alone changes nothing — it only takes effect when passed in.
What each setting does
Temperature decides how flat or peaked the choice is. At temp 0, make_sampler returns the greedy sampler, which picks the argmax — the single most likely token at each step — so the same prompt gives the same answer every time. Raise the temperature and less likely tokens get a real chance, so answers vary between runs. Top-p is a filter applied before the pick: its documentation describes it as applying top-p (nucleus) sampling to the logits, with top_p as the cumulative probability threshold for top-p filtering. The builder only applies it when 0 < top_p < 1.0, so the command-line default of 1.0 means no top-p filtering at all, while 0.9 narrows the choice to the smallest set of tokens whose probabilities add up to that threshold. Note the defaults differ between the two surfaces: the command line defaults to temp 0.0 and top-p 1.0, while make_sampler defaults to temp 0.0 and top_p 0.0 — and a top_p of 0.0 is also left unapplied by the same 0-to-1 check.
Try it: four checks
| Check | What you should see | If you do not see it |
|---|---|---|
| Greedy | With --temp 0, the same prompt gives the same answer every time you run it |
You passed a temperature above 0 — temp 0 is the argmax, so any variation means the flag did not apply |
| Varied | With --temp 0.7 --top-p 0.9, the answers differ between runs |
The flags were left out — the defaults are temp 0.0 and top-p 1.0, which is greedy and unfiltered |
| Python sampler | make_sampler(temp=0.7, top_p=0.9) passed as sampler= to generate gives varied replies |
Building the sampler alone changes nothing — pass it to generate as the sampler argument |
| Defaults | With no flags, the run uses temp 0.0 and top-p 1.0 | Run mlx_lm.generate -h to confirm the defaults on your installed version |
Expected result. We checked the mlx-lm README for the pip install mlx-lm command, the mlx_lm.generate quick start, the default model mlx-community/Llama-3.2-3B-Instruct-4bit, the note that commands take sampling parameters with mlx_lm.generate -h listing the options, and the Sampling section saying generate and stream_generate accept sampler and logits_processors with standard sampling functions in mlx_lm.sample_utils; and the generate.py source for the –temp flag defaulting to 0.0 and the –top-p flag defaulting to 1.0, the –max-tokens default of 100, and generate passing its keyword arguments to stream_generate; and the sample_utils.py source for make_sampler with temp defaulting to 0.0 and top_p defaulting to 0.0, the temp-0-means-argmax rule, the top_p nucleus-sampling description, the 0 < top_p < 1.0 application check, and the apply_top_p description of top_p as the cumulative probability threshold for top-p filtering. We did not run mlx-lm or generate any text.
Limits
Sampling only changes which tokens get picked, not what the model knows: a high temperature can produce fluent, confident, wrong answers, so do not read variety as accuracy. Top-p only applies when it is strictly between 0 and 1.0 — at the defaults of 1.0 on the command line or 0.0 in make_sampler, no top-p filtering happens. These are sampling controls, not length controls: capping the answer is --max-tokens, covered by the one-off generate guide. MLX LM is a Python package for Apple silicon, so this runs on an Apple Silicon Mac, not on other computers. We did not run mlx-lm, so no reply here is a recorded result.
Sources
- mlx-lm README — pip install mlx-lm; the mlx_lm.generate quick start; the default model mlx-community/Llama-3.2-3B-Instruct-4bit; commands take sampling parameters, mlx_lm.generate -h lists the options; Sampling section: generate and stream_generate accept sampler and logits_processors, standard sampling functions in mlx_lm.sample_utils
- mlx-lm sample_utils.py — make_sampler(temp=0.0, top_p=0.0, …): temp is the temperature for sampling, if 0 the argmax is used, default 0; top_p is nucleus sampling, higher means the model considers more less likely words; temp 0 returns the greedy sampler; top-p applied only when 0 < top_p < 1.0; apply_top_p applies top-p (nucleus) sampling with top_p as the cumulative probability threshold for top-p filtering
- mlx-lm generate.py — –temp float, default 0.0, Sampling temperature; –top-p float, default 1.0, Sampling top-p; –max-tokens default 100; generate passes its keyword arguments to stream_generate
