@mlx-node/lm

Load a model and keep a chat session in your own Node.js process. Streaming, tool calls, images and embeddings.

Install

bash
npm install @mlx-node/lm

You need a supported machine (Platforms). The native addon comes with the package.

The samples read models from ~/.mlx-node/models, where mlx download puts them. The quickstart downloads qwen3-0.6b, which most samples on this page use. If you set MLX_MODELS_DIR, use that folder.

Chat sessions

ts
import { homedir } from 'node:os';
import { join } from 'node:path';

import { loadSession } from '@mlx-node/lm';

const session = await loadSession(join(homedir(), '.mlx-node', 'models', 'qwen3-0.6b'));
const opts = { config: { reasoningEffort: 'none' } };

const first = await session.send('Write a haiku about TypeScript.', opts);
console.log(first.text);

const followUp = await session.send('Make it shorter.', opts);
console.log(followUp.text);

loadSession(path) loads a model and returns a ChatSession. The session keeps the conversation and its KV cache between turns, and runs one turn at a time. It throws for embedding, reranking and Qianfan OCR models.

reasoningEffort: 'none' turns thinking off. Without it, a small model can spend a whole turn thinking and return no text.

loadSession takes no session options. For a system prompt, load the model with its class and build the session yourself:

ts
import { homedir } from 'node:os';
import { join } from 'node:path';

import { ChatSession, Qwen3Model } from '@mlx-node/lm';

const model = await Qwen3Model.load(join(homedir(), '.mlx-node', 'models', 'qwen3-0.6b'));
const session = new ChatSession(model, { system: 'Answer in one sentence.' });

const reply = await session.send('What is MLX?', { config: { reasoningEffort: 'none' } });
console.log(reply.text);

Qwen3Model is the class of the Qwen3 family. Other families have their own class with the same load(path), such as Qwen35Model and Gemma4Model.

Streaming

With the session and opts of the first sample:

ts
for await (const event of session.sendStream('Make it shorter.', opts)) {
  if (!event.done) process.stdout.write(event.text);
}

sendStream yields text events ({ text, done: false }) and one last event ({ done: true, finishReason, toolCalls }).

Tool calls

ts
import { homedir } from 'node:os';
import { join } from 'node:path';

import { createToolDefinition, loadSession } from '@mlx-node/lm';

const weather = createToolDefinition(
  'get_weather',
  'Get weather for a city',
  { city: { type: 'string', description: 'City name' } },
  ['city'],
);

const session = await loadSession(join(homedir(), '.mlx-node', 'models', 'qwen3-0.6b'));
const r = await session.send('Weather in Tokyo?', { config: { tools: [weather] } });
const call = r.toolCalls.find((c) => c.status === 'ok');
if (call) {
  const next = await session.sendToolResult(call.id, JSON.stringify({ tempC: 21 }), {
    config: { tools: [weather] },
  });
  console.log(next.text);
}

A session answers one tool call per assistant turn. Send the result back with sendToolResult.

Images

ts
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';

import { loadSession } from '@mlx-node/lm';

const modelDir = join(homedir(), '.mlx-node', 'models', 'qwen3.8-27b-gguf');
const session = await loadSession(join(modelDir, 'Qwen3.8-27B-UD-Q4_K_XL.gguf'));
const reply = await session.send('What is in this photo?', {
  images: [await readFile('photo.jpg')],
});
console.log(reply.text);

The sample uses Qwen3.8-27B, the model Portato and mlx agent install first. To get it from the terminal, use the GGUF command on the @mlx-node/cli page; it writes ~/.mlx-node/models/qwen3.8-27b-gguf and fetches the vision file with it. This model loads from its .gguf file, not from the folder.

session.supportsImages() tells you if the loaded model takes images. For OCR and document layout, use @mlx-node/vlm.

Embeddings

ts
import { homedir } from 'node:os';
import { join } from 'node:path';

import { HarrierModel } from '@mlx-node/lm';

const models = join(homedir(), '.mlx-node', 'models');
const embedder = await HarrierModel.load(join(models, 'harrier'));
const vec = (await embedder.encode('unified memory on Apple Silicon')).toFloat32();
console.log(vec.length);

HarrierModel.load() reads a Harrier model folder in MLX layout. There is no download command for it on this site yet, so the folder name in the sample is an example: use the path of your own folder.

Embeddings are a library call. The HTTP server has no embeddings route.

Clean up

Call await session.dispose() when you are done with a session. session.reset() clears the conversation and keeps the model.

Main exports

Name What it is
loadSession(path) Load a chat model and get a ChatSession
loadModel(path) Load a model without a session
Qwen3Model, Qwen35Model, Gemma4Model and the other family classes load(path) gives a model you can pass to new ChatSession(model, options)
ChatSession One conversation: send, sendStream, sendToolResult, reset, dispose
createToolDefinition(name, description, properties, required) Build a tool for config.tools
HarrierModel The embedding model: load, encode

Go deeper