# Local inference with MLX

**Last updated:** 2026-11-24T13:28:29-03:00 (UTC-3)

MLX is Apple's array framework for Apple Silicon; `mlx_lm.server` serves MLX models with an OpenAI-compatible API. It always runs on the Apple GPU, so an MLX pool needs `gpu.mode: metal` and the flags `local_inference_gpu` and `local_inference_metal`. This page sets up a [local inference](/docs/features/local-inference/) pool the gateway starts and supervises. The commands and numbers were run on a Mac Mini M4 Pro with mlx-lm 0.32.2 and `Qwen2.5-0.5B-Instruct` in 4 bits.

## 1. Install the server and get the weights

A virtual environment keeps MLX out of the system Python:

```bash
uv venv --python 3.12 /opt/mlx/venv
uv pip install --python /opt/mlx/venv/bin/python "mlx-lm==0.32.2"
/opt/mlx/venv/bin/python -c "from huggingface_hub import snapshot_download; snapshot_download('mlx-community/Qwen2.5-0.5B-Instruct-4bit', local_dir='/opt/models/qwen05-mlx')"
```

The weights are a directory (`config.json`, `model.safetensors`, the tokenizer files). The gateway never downloads them.

## 2. Configure the gateway

```yaml
features:
  local_inference: true
  local_inference_gpu: true
  local_inference_metal: true
local_inference:
  supervise: true
  runtimes:
    mlx: /opt/mlx/venv/bin/mlx_lm.server
  model_dirs: [/opt/models]
  pools:
    - id: qwen-mlx
      backend: mlx
      mode: supervise
      model: qwen05
      alias: local-qwen-mlx
      weights: /opt/models/qwen05-mlx
      slots: 2
      port_base: 14221
      gpu: {mode: metal}
```

What the gateway runs for the replica:

```bash
HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 /opt/mlx/venv/bin/mlx_lm.server \
  --model /opt/models/qwen05-mlx --host 127.0.0.1 --port 14221 \
  --decode-concurrency 2 --prompt-concurrency 2
```

Three things are particular to this backend:

- **The server is started offline.** `mlx_lm.server` treats a model name it does not know as a repository to fetch. With the offline variables it answers 404 instead, so nothing is ever downloaded.
- **The gateway addresses the replica as `default_model`**, the one name the server answers for the weights it was started with. `model` in the pool is your label; clients call the alias.
- **`context_tokens` is refused**: the server takes the model's own window. `slots` sets its decode and prompt concurrency.

The server has no Prometheus metrics, so `scrape_metrics` does nothing here. Its health check is `/health`.

## 3. Check it

```bash
curl -s http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer $GATEWAY_KEY" -H "Content-Type: application/json" \
  -d '{"model":"local-qwen-mlx","max_tokens":32,"messages":[{"role":"user","content":"Say hi"}]}'
```

A request sent while the replica is still loading the model answers `503` `replica_not_ready`; the replica takes traffic once `/health` answers.

## What to expect

Streamed chat, 160 output tokens, through the gateway:

| Concurrent streams | First token (median) | Decode per stream | Total tokens/s | GPU utilization |
| --- | --- | --- | --- | --- |
| 1 | 18 ms | 322 tok/s | 259 | 67% average, 79% peak |
| 2 | 457 ms | 301 tok/s | 305 | 77% |

One stream decodes faster than on llama.cpp with the same model here (322 against 263 tokens per second). Two streams at once mostly wait for each other on this server version: for many concurrent streams on a Mac, a llama.cpp pool with slots served more (536 tokens per second at 6 streams).
