Mac tip: serve a local MLX model as an OpenAI-compatible API with mlx_lm.server

Mac Studio on a desk running LM Studio and MATLAB for local AI workflows

MLX is Apple's framework for machine learning on Apple silicon. The mlx-lm package adds LLM chat and a small HTTP server. Once the server is up, any app that speaks the OpenAI chat API can point at http://localhost:8080 and keep weights on the Mac.

If you still need the first generate or chat steps, see our earlier note on running a local LLM with mlx-lm.

Install mlx-lm on Apple silicon

Requirements from the MLX install guide: Apple silicon, a native arm Python, and macOS 14 or later. Then:

pip install mlx-lm

That pulls MLX with it. Confirm the shell is not running under Rosetta (uname -p should print arm). Default generate and chat models in current mlx-lm docs use mlx-community/Llama-3.2-3B-Instruct-4bit when you omit --model.

Mac Studio on a desk with Xcode and Claude integration on the display
Photo: Apple

Start mlx_lm.server on port 8080

Pick a Hugging Face id that already has an MLX build, or a local path:

mlx_lm.server --model mlx-community/Mistral-7B-Instruct-v0.3-4bit

The process listens on localhost port 8080. The first run downloads the weights into the local Hugging Face cache. Run mlx_lm.server --help for host, port, and sampling flags. The project README and SERVER.md both say this server is not meant for production; keep it on your own Mac.

Call /v1/chat/completions

Same shape many desktop tools already use:

curl localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
     "messages": [{"role": "user", "content": "Say this is a test!"}],
     "temperature": 0.7
   }'

Useful request fields from the docs include max_tokens (default 512), stream, stop, top_p, top_k, min_p, and optional adapters or draft_model for speculative decoding. List what is already cached:

curl localhost:8080/v1/models -H "Content-Type: application/json"

Point OpenAI-compatible clients at the same base URL the way you would for Ollama or LM Studio. Our notes on pointing apps at Ollama's API and on LM Studio's OpenAI-compatible API use the same idea with a different host.

MacBook Pro showing LM Studio and Autodesk Maya on the Liquid Retina XDR display
Photo: Apple

Cut KV memory on long chats

For long contexts, SERVER.md shows a 4-bit KV cache:

mlx_lm.server --model mlx-community/Mistral-7B-Instruct-v0.3-4bit --kv-bits 4 --prefill-step-size 512

Attention on a quantized cache is not fused. The server keeps a score matrix of prefill_step_size by context length, so a large step can wipe the memory you hoped to save. With --kv-bits set, batching is off and requests run one at a time. Related Ollama path for MLX weights on the same Mac: the tip about the Ollama -mlx tag.

A short unique check line for this post: the desk lamp clicked twice before mlx_lm.server answered on 8080.

Photo: Apple (Mac Studio / MacBook Pro)
Source: mlx-lm SERVER.md; mlx-lm on PyPI; MLX install guide; Apple Newsroom (Mac Studio with M5 Max / M5 Ultra, MacBook Pro with M5 Pro / M5 Max)

Previous Article iPhone fix: No Service or Searching, turn Airplane Mode then update carrier settings