New Tool Radar

AI Concepts

How to Reduce Repeated Phrases in MLX LM Output with a Repetition Penalty

Add a repetition penalty to mlx_lm.generate or to a make_sampler sampler so the model is pushed away from repeating words it has already used.

Schematic diagram with three steps: run mlx_lm.generate with a repetition penalty flag, or build a sampler with a repetition penalty in Python, and compare the reply with and without it.

Some replies loop: the same phrase comes back three times in one paragraph. A repetition penalty lowers the score of tokens the model has already produced, so it is pushed toward fresh wording. In mlx-lm you can set it on the command line or in a Python sampler — but the exact flag name depends on your installed version, so this guide tells you how to confirm it first instead of guessing.

Confirm the flag on your version

The README says mlx-lm commands take options for sampling parameters and that mlx_lm.generate -h lists them. Run that first and look for the repetition-penalty option:

mlx_lm.generate -h

Then use the flag exactly as your version spells it, for example:

mlx_lm.generate --prompt "Explain what a lighthouse keeper does in two sentences." --max-tokens 100 --temp 0.7 --repetition-penalty 1.2

A value of 1.0 means no penalty. Raise it in small steps — 1.1, then 1.2 — and compare the same prompt with and without it. A penalty that is too high makes the text wander and avoid normal repeated words such as names.

Set it in the Python API

The README’s Sampling section says generate and stream_generate accept a sampler and logits_processors, with standard sampling functions in mlx_lm.sample_utils. Build the sampler with the repetition penalty and pass it in, following the same make_sampler pattern as the temperature guide:

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)

Check help(make_sampler) in your installed version for the exact repetition-penalty argument name before you rely on it. Building the sampler alone changes nothing — it only takes effect when passed as sampler= to generate.

Check What you should see If you do not see it
Flag exists mlx_lm.generate -h shows a repetition-penalty option Your version spells it differently or lacks it — use the name -h shows
Less repetition The same prompt repeats a phrase less with the penalty on The penalty is 1.0 (no penalty) or too small to matter
Text stays readable Names and normal repeated words still appear The penalty is too high — lower it toward 1.1

Expected result. We checked the mlx-lm README for the -h option list, the Sampling section, and make_sampler in mlx_lm.sample_utils. We did not run mlx-lm, so the flag spelling and the penalty values above are UNTESTED on this machine — confirm them with -h and help(make_sampler) before use.

Limits

A repetition penalty only discourages repeating tokens; it cannot fix a model that does not know the answer, and a high penalty can produce odd, evasive wording. It is a sampling control, not a fact check. This runs on Apple silicon only.

Sources

Sources checked 2026-11-28.

See all