Running models locally with Ollama, LM Studio, llama.cpp and MLX
Running a model on your own laptop or workstation is the cheapest way to learn how inference actually behaves, and for a lot of work (private documents, offline development, prototyping agents without an API bill) it is genuinely the right deployment. The local ecosystem looks crowded, but it is really two engines (llama.cpp and Apple's MLX) wrapped by a few friendly front ends. Once you see that, choosing becomes simple.
Why it matters
Local models give you privacy (data never leaves the machine), zero marginal cost per token, and an environment where you can watch memory and speed respond to every change. They are also a realistic production target for some products: desktop apps, on-premises installs and edge devices. And because every tool here exposes an OpenAI-compatible endpoint, code you write against a local model can move to a hosted provider later by changing a base URL.
How it works
llama.cpp is the engine underneath most local tools. It loads models in the GGUF format, supports many quantization levels (see quantization), and runs on CPUs plus GPU backends including Metal, CUDA and Vulkan. It can split a model between GPU and CPU by offloading only some layers to the GPU, which is how a model larger than your VRAM can still run, slowly. It ships llama-server, an OpenAI-compatible HTTP server.
MLX is Apple's array framework for Apple silicon, built around unified memory. mlx-lm is its LLM package for running and fine-tuning models, and it loads MLX-format weights (typically safetensors converted and quantized for MLX). On Macs, MLX usually makes better use of the hardware than the Metal backend of llama.cpp, though how much depends on the model and context length.
Ollama is a CLI and background service with a model library and a simple pull-and-run workflow. It historically ran on llama.cpp. In March 2026 Ollama announced a preview of an MLX-based engine on Apple silicon. Reporting on the preview noted it applied to specific models and safetensors builds, while existing GGUF models continued to run on the llama.cpp path. Ollama exposes both its own API and an OpenAI-compatible one.
LM Studio is a desktop GUI for discovering, downloading and chatting with models, and it can run a local server. It added an MLX engine on Macs back in version 0.3.4 and can use both llama.cpp (GGUF) and MLX models.
Rendering diagram…
What fits on my machine?
The memory math from the GPU sizing doc applies unchanged: weights at the chosen quantization plus KV cache for your context. A 4-bit quant of an 8B model is a few gigabytes and fits on most modern laptops. On Apple silicon, the GPU shares system RAM, so a Mac with lots of unified memory can hold models that would need a datacenter GPU elsewhere, but macOS reserves some memory for the system so you cannot use all of it. Decode speed still tracks memory bandwidth, which varies a lot between chip tiers.
Reference architecture
A setup I like for local agent development: one local server, an OpenAI client pointed at it, and an environment variable to switch to a hosted model for comparison.
Rendering diagram…
Code
Getting a model running each way:
# Ollama
ollama pull qwen2.5:7b
ollama run qwen2.5:7b "Explain GGUF in one sentence."
# OpenAI-compatible endpoint is served at http://localhost:11434/v1
# llama.cpp server, pulling a GGUF from Hugging Face
llama-server -hf Qwen/Qwen2.5-7B-Instruct-GGUF --port 8080 -ngl 99 -c 8192
# MLX on Apple silicon
pip install mlx-lm
mlx_lm.generate --model mlx-community/Qwen2.5-7B-Instruct-4bit --prompt "Hello"
mlx_lm.server --model mlx-community/Qwen2.5-7B-Instruct-4bit --port 8081
The client code is the same for all of them:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: process.env.LLM_BASE_URL ?? "http://localhost:11434/v1",
apiKey: process.env.LLM_API_KEY ?? "local",
});
const stream = await client.chat.completions.create({
model: process.env.LLM_MODEL ?? "qwen2.5:7b",
messages: [{ role: "user", content: "Give me three uses for a local LLM." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Model names differ per tool (Ollama tags versus Hugging Face repo ids), which is why I keep the model in config.
Trade-offs and pitfalls
- Context length silently defaults low. Local servers often start with a modest context window to save memory. If long prompts get truncated or the model "forgets" the start, raise the context setting (
-cfor llama-server, the context option in Ollama or LM Studio) and remember it costs KV memory. - Partial offload is slow. When layers spill to CPU, speed drops sharply. Prefer a smaller model or lower-bit quant that fits fully on the GPU or in unified memory.
- Quant quality varies. A Q2 or Q3 quant of a big model is not always better than a Q4 or Q5 of a smaller one. Test on your tasks.
- Chat templates matter. A GGUF with a wrong or missing chat template produces odd output. Prefer files from the model author or well-known converters.
- Not built for many users. These tools are excellent for one person or a small team. For many concurrent users, move to a datacenter engine like vLLM or SGLang (see serving engines).
- Security defaults. Do not expose a local server on
0.0.0.0on an untrusted network without authentication in front of it.
Checklist
- I picked the engine by platform: MLX-based tools on Apple silicon where supported, llama.cpp elsewhere.
- My model and quant fit fully in GPU or unified memory, including KV for my context.
- I set the context length explicitly.
- My app talks to the local model through an OpenAI-compatible base URL from config.
- I compared outputs against a hosted model on a handful of real tasks.
- The local server is bound to localhost or protected.