Running LLMs Locally: A Guide to Model Selection by Memory Budget
This guide explains how to select Large Language Models (LLMs) for local deployment based on your memory budget. It breaks down the key components of memory consumption—model weights, KV cache, and runtime overhead—and compares Dense and Mixture of Experts (MoE) architectures. Additionally, it provides a practical sizing table, a Python script for estimating memory requirements, and a pre-deployment checklist to help you avoid common pitfalls.
I. Why Deploy Locally?
Running Large Language Models (LLMs) on your own devices offers three direct benefits. First, input data never leaves your machine, making it ideal for processing internal documents, code, and personal information. Second, it works offline without relying on a network connection. Third, it eliminates API pay-per-call costs, which is much more cost-effective for long-term, high-frequency usage.
The trade-off is the hardware barrier. Personal computers, smartphones, and single-board computers have limited RAM and VRAM. Whether a model can run primarily depends on whether it fits into the available memory. Therefore, the first step in model selection is usually estimating memory consumption, rather than comparing benchmark scores.
II. What Makes Up Memory Consumption?
When running a model, memory is mainly used for three parts.
1. Model Weights
Weights are the parameters themselves, and their size is determined by the parameter count and storage precision. Quantization techniques compress the size by reducing the bit-width of each parameter. In common 4-bit quantization, each parameter takes up about 0.5 bytes.
For example, a 10-billion parameter model requires about 10 billion × 0.5 bytes ≈ 5 GB under 4-bit quantization. The same model stored in 16-bit would require about 20 GB.
2. Context Cache (KV Cache)
During text generation, the model must save the intermediate states of all previous tokens. This cache is called the KV Cache. Its size correlates with context length, model layers, hidden dimensions, and concurrent requests. The longer the context, the larger the cache. Many people only look at the weight size and ignore the additional memory usage of long contexts, which is a common estimation error.
3. Runtime Overhead
The inference framework, activation values, and components like vision or audio encoders also consume extra memory. This overhead varies by runtime environment, and a margin should always be reserved during actual deployment.
III. Differences Between Dense and MoE Models
Understanding model architecture helps determine its memory and speed characteristics.
Dense Models: All parameters participate in every inference calculation. Memory usage and single-pass computation are positively correlated with the total parameter count. The larger the scale, the slower the speed and the more memory required.
Mixture of Experts (MoE) Models: The model contains multiple “expert” sub-networks, but only a subset is activated for each token. This leads to two conclusions:
- Memory usage is determined by the total parameter count, because all experts must reside in memory.
- Single-pass computation is determined by the activated parameter count, so inference speed can approach that of a much smaller model.
Take a MoE model with about 25.2 billion total parameters and 3.8 billion activated parameters per pass as an example. After 4-bit quantization, its weights take about 12.6 GB. Adding cache and runtime overhead, the total usage exceeds 16 GB. Its speed is close to a small model, but its memory requirement is still calculated based on a large model.
During selection, you should separately verify two questions: “Can it fit?” and “Is it fast enough?”
IV. Selecting Models by Memory Budget
The table below shows typical choices under different budgets. The values are reference points for 4-bit quantization; actual usage should be monitored at runtime.
| Available Memory/VRAM | Recommended Spec | Typical Scenarios | Notes |
|---|---|---|---|
| ~4 GB | ~2.3B small edge model | Smartphones, single-board computers, simple Q&A and text processing | Verify if the device supports required image or audio input |
| ~6 GB | ~4.5B small general model | Daily chat, lightweight writing and rewriting | Supports images and audio; long contexts increase cache usage |
| ~18 GB | ~25.2B MoE model | Code assistance, document analysis, complex reasoning | Can run on 24 GB devices but with little headroom; control context length |
| 20 GB+ | ~30.7B dense large model | High-difficulty reasoning, coding, and math tasks | 32 GB is more comfortable; at 24 GB, compress context or reduce concurrency |
When selecting a model, follow two principles. First, prioritize the largest spec that can run stably, rather than forcing a larger one into memory. Second, the memory headroom should cover the OS, other applications, and long context caches.
V. Initial Estimation Using Code
The following script estimates the weight size based on parameter count and bit-width, then multiplies it by an empirical overhead factor to get a rough deployment requirement. The overhead factor is an assumption and should be adjusted based on actual measurements.
def estimate_weight_gib(params_billion: float, bits: int) -> float:"""Estimate weight usage based on parameter count and quantization bit-width, in GiB."""total_bytes = params_billion * 1e9 * bits / 8return total_bytes / 1024**3def plan_memory(params_billion: float, bits: int, overhead: float = 1.3):"""Returns (weight usage, recommended reserve with overhead) in GiB.overhead is an empirical margin factor to cover cache and runtime usage; adjust based on tests."""weights = estimate_weight_gib(params_billion, bits)return weights, weights * overhead# Example: 30.7B dense model, 4-bit quantizationweights, reserve = plan_memory(30.7, 4)print(f"Estimated weights: {weights:.1f} GiB, Recommended reserve: {reserve:.1f} GiB")# Output: Estimated weights: 14.3 GiB, Recommended reserve: 18.6 GiB# Example: MoE model, memory calculated by total parametersweights, reserve = plan_memory(25.2, 4)print(f"Estimated weights: {weights:.1f} GiB, Recommended reserve: {reserve:.1f} GiB")
Note that MoE model estimation should use the total parameter count, not the activated parameter count. Estimating with activated parameters will severely underestimate memory requirements.
VI. Pre-deployment Checklist
- Confirm available resources: Check the device’s total memory and VRAM allocatable to the model, deducting OS and other application usage.
- Select quantization bit-width: 4-bit is a common compromise, balancing size and quality. If memory is sufficient, you can try higher bit-widths.
- Set context length: Set an upper limit based on actual tasks. Long document analysis requires long context; regular chat does not.
- Verify input modalities: Confirm if the model supports required image or audio inputs, as support varies by spec.
- Validate quality with your data: Public leaderboard scores reflect performance on general test sets. Business scenarios should be evaluated using real examples.
- Obtain software from official sources: Inference frameworks and model files should be downloaded from the publisher’s official channels, and file hashes verified to avoid tampered versions.
VII. Common Pitfalls
- More parameters are always better: Model spec should match task difficulty. For simple classification or rewriting, small models are often sufficient and respond faster.
- Only looking at weight size: The cache usage for long contexts can approach the weight size; ignoring this leads to out-of-memory errors.
- Estimating memory with activated parameters: MoE model memory is determined by total parameters; activated parameters only affect computation speed.
- Treating benchmarks as final results: Leaderboard scores do not directly equate to effectiveness in specific business tasks; small-scale A/B testing is necessary.
The core of local deployment is aligning memory budget, task requirements, and model architecture. Calculating weights, cache, and margins first, then selecting a spec based on speed and quality requirements, is far more reliable than simply chasing the largest model.
