New Tool Radar

Local AI

How to Generate a One-Off Answer with mlx_lm.generate and a Max-Tokens Limit

Get a single answer from a local model on an Apple Silicon Mac with mlx_lm.generate --max-tokens, without opening a chat session.

Schematic diagram with three boxes: a prompt asking what a lighthouse keeper does, the command mlx_lm.generate with the Llama-3.2-3B model and --max-tokens 100, and a two-sentence answer before the program exits.

Not every question deserves a conversation. Sometimes you want one answer from a local model — a single summary, a single explanation — and then you want the program to stop. On an Apple Silicon Mac, mlx_lm.generate is the tool for that: it loads a model, generates one answer to one prompt, prints it, and exits. No chat window, no REPL, no server to remember to stop. And with --max-tokens you decide in advance how long the answer is allowed to be.

The one-off command

First install the package:

pip install mlx-lm

The mlx-lm README’s own quick start is a one-off generation:

mlx_lm.generate --prompt "How tall is Mt Everest?"

With no --model flag, it uses the default model, mlx-community/Llama-3.2-3B-Instruct-4bit, downloading it from the Hugging Face Hub on first use. To name a model and cap the answer’s length, add both flags:

mlx_lm.generate --model mlx-community/Llama-3.2-3B-Instruct-4bit --prompt "Explain what a lighthouse keeper does in two sentences." --max-tokens 100

The same limit exists in the Python API, where the README’s example loads a model and calls generate(model, tokenizer, prompt=prompt, verbose=True) — the max_tokens argument caps the generation there, as shown in the model cards for MLX models, for example generate(model, tokenizer, prompt=prompt, max_tokens=4096, verbose=True).

Try it: one answer, capped at 100 tokens

Run the command above on your Mac and check the output against this table:

Check What you should see If you do not see it
Answer printed Two sentences about a lighthouse keeper The model is still downloading on first use — wait for it to finish
Length cap The answer stops at or before 100 tokens The --max-tokens 100 flag was left out
Cut off mid-sentence A complete answer within the cap The cap is too small for the answer — raise --max-tokens and run it again

If the answer appears and stops within the cap, the one-off generation worked. Expected result. We checked the mlx-lm README. We did not run mlx_lm.generate.

Limits

mlx_lm.generate is for one prompt at a time: it does not remember earlier questions, so for a back-and-forth conversation use mlx_lm.chat, which keeps the chat context for the lifetime of its REPL. A cap that is too small cuts the answer off mid-sentence without warning, so a truncated answer means “raise the limit”, not “the model knows nothing”. MLX runs on Apple Silicon Macs only; on other computers, use a different local runtime. The --max-tokens flag spelling comes from MLX model cards rather than the README itself — run mlx_lm.generate --help, which the README recommends for the full list of options, to confirm it on your installed version. We did not run mlx_lm.generate, so no answer here is a measured result.

Sources

See all