@mlx-node/server
The OpenAI Responses and Anthropic Messages APIs on your own machine, from the mlx command or as a library.
Install
mlx serve comes with @mlx-node/cli. To start the server from your own code, install the package:
npm install @mlx-node/server
Start it
mlx serve # picks a free port and prints it
mlx serve --port 8080
mlx serve --port 8080 --model qwen3.5-9b # set the default model
mlx serve --host 0.0.0.0 --auth-token "$(openssl rand -hex 16)"
The server finds the models in your models folder and loads one on its first request.
Use it from your own code
import { createInferenceHost } from '@mlx-node/server/host';
// Finds the models in ~/.mlx-node/models and listens on 127.0.0.1.
const host = await createInferenceHost({ port: 8080 });
console.log(host.url);
console.log(host.models.map((model) => model.name));
// Later: stop the server and close the model caches.
await host.close();
createInferenceHost(options) does what mlx serve does: it finds the models, binds 127.0.0.1 and returns the URL, the model list and close(). Options include port, host, modelsDir, model, authToken, maxContextTokens and cacheDir. A host other than loopback needs authToken, or the call throws. It also throws when the models folder holds no model.
Routes
| Method | Path | Use |
|---|---|---|
| POST | /v1/responses |
OpenAI Responses API. Streams when stream is true. previous_response_id continues a chat |
| POST | /v1/messages |
Anthropic Messages API. Streams. Takes image blocks |
| POST | /v1/messages/count_tokens |
Count the tokens of a Messages request |
| GET | /v1/models |
List the models the server found |
| POST | /v1/models/unload |
Unload the chat models in memory |
| POST | /v1/memory/release |
Free model weights now |
| POST | /v1/rerank |
Rank documents for a query |
| POST | /v1/audio/transcriptions |
Speech to text from a WAV upload |
| POST | /v1/audio/speech, /v1/audio/speech/stream |
Text to speech |
| GET, POST | /v1/audio/voices |
List voices, or add a temporary cloned voice |
| GET, DELETE | /v1/audio/voices/{id} |
Read or remove a voice |
| POST | /v1/systemone |
Decision and extraction models (CLEF, Clairic) |
| GET | /health, /v1/health |
Health |
| GET, HEAD | / |
Liveness |
What is not there
There is no /v1/chat/completions, /v1/completions or /v1/embeddings. Any other path returns 404. Use a client that speaks the Responses API or the Messages API.
Auth and network
- The server binds
127.0.0.1unless you pass--host. - Set a token with
--auth-tokenorMLX_SERVER_AUTH_TOKEN. Clients send it asx-api-keyor asAuthorization: Bearer. /,/healthand/v1/healthstay open without the token.- The token is one shared secret. It is not multi-user access control. Set it before you bind another host.
Which models stay loaded
By default the server keeps one chat or decision model, one speech-to-text model, one text-to-speech model and one reranker, and loads each on first use. --model-residency single keeps one model in total. After 10 minutes without a request the weights are freed; --idle-release changes that.
Many requests at once
Requests to the same text model decode together in one batch. This is on by default. Qwen3.5-family models take up to 4 chats at once, or 8 on Macs with 256 GB or more; a request past that waits its turn.
Some work still runs one turn at a time: turns with images or audio on some models, DeepSeek V4.1 Flash, and some speculative decoding modes. Two different loaded models run in parallel.
MLX_SERVE_FORCE_SERIAL=1 turns batching off.
Memory, context and cache
| Flag | Default | What it does |
|---|---|---|
--max-memory |
auto | Lowers the memory budget, for example 28G. Qwen3.5 dense and MoE size their KV pool from it |
--max-context |
the model's native window | Context limit, for example 100K or 1M (K is 1,024 tokens, M is 1,000,000), at most 1,048,576. Qwen3.5 dense, MoE and Qwen3.8-Flash-Next take it. Past the native window the model changes how it encodes positions, and that changes answers at every length |
--kv-format |
int8 where supported |
int8 or bf16 for the KV cache of Qwen3.5 dense, MoE and Qwen3.8-Flash-Next |
--idle-release |
10m |
Free weights after this long without a request |
--persist-cache, --no-persist-cache |
on | Keep chat state on disk so a restart does not read the whole history again. It covers every family that has a disk cache: the KV cache of Qwen3, Gemma 4, Muse-Glimmer and LFM2, and the state cache of Qwen3.5-family models, Qwen3.8-Flash-Next and Hy4-flash |
--max-cache-disk |
auto | Disk limit for the state cache of Qwen3.5-family models, Qwen3.8-Flash-Next and Hy4-flash. Auto is a quarter of the free space beyond a 10 GB floor, at most 32 GB. 0 turns that cache off |
--cache-dir |
~/.mlx-node/cache/prefix-cache |
Where that state cache lives |
Claude Code
mlx launch claude starts this server and runs Claude Code with the base URL, a token for that launch and the model already set. See The mlx command.