Running LLMs Locally on a 16GB Mini PC: Memory Budgeting, Quantization, and Deployment
A practical guide to running a 9B-class large language model locally on a 16GB unified-memory machine. Covers how to estimate weight and KV cache sizes, choose between quantization formats, deploy via MLX or llama.cpp on Apple Silicon, and configure a local inference server safely.
1. Do the Memory Math First
On a 16GB unified-memory system, CPU and GPU share the same pool. The OS, browser, and other apps all take a share, so the space actually available for a model is typically around 10GB, depending on what’s loaded at startup. Before downloading anything, use two formulas to estimate your budget.
The first formula calculates weight size: parameter count × bytes per parameter. For a 9B model (roughly 9 billion parameters), the original 16-bit weights need about 18GB—beyond this machine’s capacity and therefore unrunnable as-is. With 4-bit quantization, each parameter takes roughly 0.5 bytes, so weights drop to about 4.5GB. Quantization scale parameters and some unquantized layers add overhead, so the actual file usually lands between 5 and 6GB.
The second formula calculates KV cache size: 2 × number of layers × number of KV heads × head dimension × context length × bytes per element. The longer the context, the larger the cache, growing linearly with length. You can read the layer count, KV head count, and head dimension from the model’s config.json and plug them into the formula—no need to guess from experience.
Running these numbers: weights take about 6GB, the system and apps take a few GB, and what’s left supports a context length of around 4096. If you need to process long documents of tens of thousands of words, you’ll have to shorten the context or switch to a smaller model.
2. Choosing the Model Version and Quantization Format
- Pick the instruct version. A base model only continues text and isn’t suited to direct conversation. Unless you plan to fine-tune it yourself, choose the instruction-tuned version.
- Pick a quantization format. On Mac, the common options are MLX-format 4-bit weights or GGUF’s Q4_K_M. Q4_K_M is a widely used middle ground in llama.cpp, balancing size against quality.
- Check the license. The license determines whether you can use the model commercially and redistribute the weights. Read the terms on the model page before downloading.
- Download only from official repositories or trusted mirrors, and verify checksums. After downloading, use SHA256 to confirm the file is intact and untampered.
This article doesn’t name a specific model. For model names, versions, and weight sources, refer to the publisher’s official page.
3. Two Deployment Paths
Option A: MLX (Recommended)
MLX is Apple’s machine learning framework designed for Apple Silicon. It uses unified memory directly, with no need to copy data between CPU and GPU.
# Create and activate a virtual environmentpython3 -m venv llm_envsource llm_env/bin/activate# Install mlx-lm (check PyPI for the current version)pip install --upgrade mlx-lm# Quick CLI test: the model argument can be a Hugging Face repo name or a local directorymlx_lm.generate --model ./your-4bit-model-dir --prompt "Explain KV cache in three sentences" --max-tokens 256
Option B: llama.cpp
llama.cpp supports GGUF format and uses Metal by default for GPU acceleration on Apple Silicon.
brew install llama.cpp# -ngl 99 offloads as many model layers as possible to the Metal GPUllama-server -m ./model-Q4_K_M.gguf -c 4096 -ngl 99 --host 127.0.0.1 --port 8080
A Note on Transformers and bitsandbytes
bitsandbytes is primarily built for NVIDIA CUDA GPUs, and its support on Apple Silicon is incomplete, so it’s not recommended as the main approach on a Mac. If you do have a CUDA environment, here’s a reference setup. Before running, pin your dependency versions with pip freeze to avoid breakage from library updates.
import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfigmodel_id = "./your-local-model-dir" # Local directory or Hugging Face repo namebnb_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_compute_dtype=torch.float16,bnb_4bit_use_double_quant=True,)tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id,quantization_config=bnb_config,device_map="auto",)messages = [{"role": "user", "content": "Explain KV cache in three sentences"}]inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)output = model.generate(**inputs, max_new_tokens=256)# Decode only the newly generated tokens, excluding the input promptprint(tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
4. Serving a Local API and Securing It
Many people expose the inference server to other applications—and that’s exactly when security configuration matters.
- Bind to localhost only. The commands above all use
127.0.0.1. Don’t switch to0.0.0.0unless you’re on a controlled local network and have already taken additional precautions.
