Model setup recipes

Run a 3B Model with MLX-LM and Read Its Speed: First Load, Prefill, Decode, and KV Cache

Separate model loading, prompt processing, and generation speed in MLX-LM

MLX-LM1 is a Python tool that loads MLX-format language models on Apple Silicon Macs, reads an input, and generates an answer. This guide runs the official example model, mlx-community/Llama-3.2-3B-Instruct-4bit, in a virtual environment2 and separates first model loading, prompt3 prefill4, answer decoding5, and the KV cache6 that grows during a conversation.

A first run also spends time downloading model files and loading weights into memory. A follow-up question in the same process reuses the prepared model, so combining the two timings makes it difficult to identify the slow stage.

First confirm that a short basic generation works, then use the input and answer lengths you expect in practice. To evaluate Korean answer quality, test separately with real Korean material rather than the English prompt in the command.

Requirements and key details
  • 3B 4-bit identifies the official starter model; it does not guarantee it will run on every Mac.
  • Record prefill and decode, and cold starts and warm sessions, as separate stages.
  • Longer inputs and outputs can increase KV-cache and memory use, so check answer quality too.

Run the official 3B model in a virtual environment

The MLX-LM repository uses mlx-community/Llama-3.2-3B-Instruct-4bit as its default example. Its model card and configuration identify the Llama 3.2 3B Instruct architecture and 4-bit quantization7. It is a starting point for learning the run path, not a proxy for other models' speed or memory requirements.

Installing MLX-LM in a project-specific Python virtual environment keeps its packages separate from existing work. The commands below create the environment, install the official package, and check operation with a short generation. The --max-tokens 128 option caps answer tokens8; it is not the total context limit including the prompt.

A successful run prints an answer to the prompt in the terminal. If the model download fails, check the network connection and cache disk space. If the command is not found, verify the installation in the shell where .venv is active with python -m pip show mlx-lm. If memory pressure rises, close other apps first and retry with a shorter prompt to isolate the cause.

Install MLX-LM in a virtual environment and generate a first answer
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install mlx-lm
mlx_lm.generate --model mlx-community/Llama-3.2-3B-Instruct-4bit --prompt "Explain why prompt length affects generation time in two sentences." --max-tokens 128
Check a short answer with the official default model ID.

Context and runtime need memory beyond the weights

3B is an approximate parameter scale, and 4-bit describes weight precision. Quantization reduces weight storage, but execution also needs metadata and temporary compute space. Generation adds a KV cache, and long inputs need room during prefill too. Disk space for model files and unified memory9 during execution are separate resources.

Do not infer memory fit from the model name alone. macOS and apps such as browsers and development tools also use unified memory. Start with a short input while watching memory pressure in Activity Monitor; if pressure stays high or apps freeze, reduce context or close other apps to isolate the cause.

Wooden blocks, a thick notebook, and a phone sit on one tray.
Weights, context, and other apps share unified memory.

Measure prefill and decode separately

Prefill is the stage where the model reads the prompt and computes its internal representations. It can lengthen the wait for the first token in a long-document summary. Decode follows, generating the answer token by token. A long input can delay the first answer, while a long requested output can extend generation; these feel like slowness for different reasons.

For comparisons, hold the model, prompt, and output cap constant, and record a cold start that downloads and loads files separately from a warm question in an active mlx_lm.chat session. Follow-up questions may include conversation history, so different input lengths are not equivalent prefill conditions.

If prompt-processing and generation rates are displayed, record them separately in input-token and output-token terms. Also note the MLX-LM version, macOS, Mac chip and memory, and apps running alongside it so you can reproduce the conditions later.

Three panels show a closed laptop, a laptop displaying a document, and a laptop beside documents and prints.
Record model loading, input processing, and answer generation times separately.

KV-cache settings trade memory against retained context

As a conversation grows, its KV cache, which stores key and value information for earlier tokens, can grow too. That is why success on a short question does not establish the same headroom for a long document. More output tokens can also increase cache and runtime10, so expand input and output one at a time.

The --prefill-step-size option controls the chunk size for processing long inputs, while --max-kv-size sets a limit for supported cache configurations. Official documentation says smaller prefill chunks can lower peak memory during input processing but may slow it down. A lower rotating-cache limit can save RAM11 while dropping older context and changing answer quality.

The command below shows both settings together, but when diagnosing behavior, change one setting at a time and record the result before testing the other. For each run, check prefill time, memory pressure, and whether the model retains earlier instructions; revert the setting if the required context or answer quality is not maintained.

Example using the options
mlx_lm.generate --model mlx-community/Llama-3.2-3B-Instruct-4bit --prompt "Summarize this short note." --max-tokens 128 --prefill-step-size 512 --max-kv-size 4096
This example uses both options. When diagnosing behavior, change one value at a time.

Judge performance by the waits you encounter most

If you close the app or restart the computer each time, model loading matters most. If you ask several questions in one session, prefill and decode are more noticeable. Summarizing a long report briefly and generating a long draft from a short prompt also spend time in different stages. One average speed cannot describe every workflow.

After a warm-up, measure the same conditions three times and record the median to reduce the influence of an outlier. Separate cold start, warm session, prefill, decode, and peak memory, and note the model, input, output cap, version, and apps running alongside it. Check answer quality using the inputs and outputs you actually need.

If a short test runs out of memory or the model is unsupported, consider a smaller supported model or another runtime at that stage. Whether the real wait is acceptable while preserving needed context and answer quality is the evidence for choosing hardware.

A notebook, equal-sized cards, and small trays are neatly arranged.
Keep conditions fixed and measure each wait stage separately.

Terminology notes

  1. MLX-LM — A package for loading, running, and fine-tuning language models with MLX.

    Back to the text
  2. Python virtual environment — An isolated space for installing Python packages per project, helping reduce package-version conflicts.

    Back to the text
  3. Prompt — The input text sent to a model. For generative models it describes the desired result and strongly shapes the output.

    Back to the text
  4. Prefill — The stage where an LLM reads the input prompt and computes representations for its tokens. Longer prompts contain more tokens to process.

    Back to the text
  5. Token decoding — The stage that generates successive output tokens using the input and the response produced so far.

    Back to the text
  6. KV cache — Memory that stores attention keys and values from earlier tokens for reuse during later token generation. Its size depends on context length and batch size.

    Back to the text
  7. Quantization — A way to represent model weights or values with fewer bits, reducing storage and memory use. The coarser representation can also affect accuracy.

    Back to the text
  8. Token — A unit into which a model divides input or output for processing.

    Back to the text
  9. Unified memory — An architecture where the CPU and GPU share one physical memory pool.

    Back to the text
  10. Runtime — The software environment that provides facilities needed while a program runs. In local AI, it can also refer to a model execution engine.

    Back to the text
  11. System RAM — System memory that temporarily holds data while programs run.

    Back to the text

Frequently asked questions

What should you check first in this article?
MLX-LM is a Python tool that loads MLX-format language models on Apple Silicon Macs, reads an input, and generates an answer. This guide runs the official example model, mlx-community/Llama-3.2-3B-Instruct-4bit, in a virtual environment and separates first model loading, prompt prefill, answer decoding, and the KV cache that grows during a conversation.
How much memory does it need?
3B is an approximate parameter scale, and 4-bit describes weight precision. Quantization reduces weight storage, but execution also needs metadata and temporary compute space. Generation adds a KV cache, and long inputs need room during prefill too. Disk space for model files and unified memory during execution are separate resources.
When is it better not to choose this?
Do not infer memory fit from the model name alone. macOS and apps such as browsers and development tools also use unified memory. Start with a short input while watching memory pressure in Activity Monitor; if pressure stays high or apps freeze, reduce context or close other apps to isolate the cause.
Where can you check the conditions and sources?
The MLX-LM repository uses mlx-community/Llama-3.2-3B-Instruct-4bit as its default example. Its model card and configuration identify the Llama 3.2 3B Instruct architecture and 4-bit quantization. It is a starting point for learning the run path, not a proxy for other models' speed or memory requirements.