
Downloading tens of gigabytes of weights only to discover a model cannot hold its target context is an expensive way to learn memory arithmetic. On a 128GB unified-memory system, whether a model fits is determined by three additive terms: quantized weight size, the key-value cache required for the intended context length, and runtime overhead that no file size reveals. Each term can be computed in advance from published architecture parameters and artifact file sizes. The calculation is complicated by attention variants such as grouped-query, latent, and hybrid attention, by mixture-of-experts architectures that separate memory capacity from bandwidth demand, and by tokenizers that convert identical text into different token counts. This guide works through each term with formulas and concrete numbers, aimed at practitioners sizing a model and context length for a 128GB-class machine before committing disk space.

On a 128 GB Apple Silicon Mac the GPU‑accelerated inference budget is additive:
[ \text{Usable memory} = \text{Quantized weight size} + \text{KV‑cache size} + \text{Runtime overhead}. ]
Apple’s Metal driver reports a recommendedMaxWorkingSetSize that caps the GPU’s working set at roughly 75 % of physical RAM. On a 128 GB system this translates to about 96 GB of memory that any Metal‑based LLM runtime can keep resident on the GPU — the rest is reserved for the OS and CPU tasks【source1】. The limit is imposed by the driver, not by individual applications, and can be inspected via metal‑info or similar tools.
Advanced users can raise the cap with the hidden sysctl key iogpu.wired_limit_mb (e.g., sudo sysctl iogpu.wired_limit_mb=122880 for ≈120 GB). The key name and semantics vary between macOS releases, so verify the behavior on each version before relying on it 【source2】.
Even if the GPU ceiling is hit, engines such as llama.cpp fall back to ordinary system RAM, allowing the model to stay functionally loaded at the cost of slower CPU execution【source5】. For a strict “fits‑on‑GPU” definition we keep the 96 GB ceiling; for a “fits‑anywhere” definition the full 128 GB pool can be used with the same performance penalty.
Typical inference deployments consume extra memory beyond the raw tensors:
| Component | Typical size |
|---|---|
| Framework / driver context | 500 MB – 2 GB |
| Activations (intermediate layer outputs) | 5 % – 10 % of total memory |
| Fragmentation / allocator headroom | 10 % – 15 % |
These figures are reported for common GPU‑accelerated LLM stacks and give a baseline for planning【source3】.
Because the overhead grows with context length and concurrency, the following multipliers are widely used as safe buffers:
Equivalently, reserve 20 % – 30 % of the usable memory as headroom beyond the sum of weights and KV cache. This extra space absorbs activation spikes, temporary allocations, and any unpredictable fragmentation.
When sizing a model for a 128 GB Apple Silicon machine:
2 × layers × heads × head_dim × bytes_per_element × max_tokens.If the sum exceeds the ceiling, either reduce the context length, switch to a more aggressive quantization, or apply the sysctl override—keeping in mind the potential impact on system stability.

A quick screen‑size estimate uses
[ \text{weight bytes} \approx \frac{\text{parameters}\times\text{bits‑per‑parameter}}{8} ]
but this formula assumes a uniform bit‑width for every tensor. Real‑world GGUF files contain extra metadata, per‑tensor scales, zero‑points, and a subset of tensors (embeddings, attention‑projector, output head) that are kept at higher precision. Consequently the effective bits‑per‑weight are higher than the nominal label.
| Artifact | Reported size | Parameters | Effective bits/param |
|---|---|---|---|
| DeepSeek V4 Flash (MLX) | 96.5 GB | 284 B | ≈ 2.72 bits / param |
| Custom GGUF (≈81 GiB) | 81 GiB | 260 B | ≈ 2.44 bits / param |
| OptiQ 0731 | 81 GiB | 264 B | ≈ 2.43 bits / param |
These numbers illustrate that a “2‑bit” label can occupy ~2.4–2.7 bits per published parameter once scales and higher‑precision tensors are accounted for【2†L1-L9】.
| Quant level | Approx. file size (GB) | Practical bytes / param |
|---|---|---|
| FP16 / BF16 | ~140 | 2 bytes |
| Q8_0 | ~70 | 0.87‑0.92 |
| Q6_K | ~54 | 0.68‑0.73 |
| Q5_K_M | ~46 | 0.58‑0.63 |
| Q4_K_M | ~40 | 0.55‑0.65 |
| Q3_K_M | ~33 | 0.42‑0.48 |
| Q2_K | ~25 | 0.31‑0.35 |
The ladder (derived from publicly released GGUF artifacts) shows that Q4_K_M, while nominally 4 bits, occupies roughly 0.55–0.65 bytes per parameter because of per‑tensor scaling data and the occasional higher‑precision block【1†L31-L38】.
GGUF is a single‑file container that bundles:
general.architecture, llama.context_length, general.quantization_version【3†L1-L5】.mmap loading【3†L1-L6】.Because the container holds the already‑quantised tensors, the on‑disk size closely matches the resident GPU/CPU memory required for the weights alone. The embedded metadata also supplies the architectural parameters needed for the KV‑cache calculation that follows.
Never decide based solely on the published parameter count. Use the actual GGUF file size (or a measured effective bits‑per‑parameter from a comparable artifact) as the baseline, then add the KV‑cache and runtime overhead. Filename conventions—e.g. Q4_K_M, IQ3_XXS, UD-Q4_K_XL—expose the quantisation method before download, but the true memory footprint is only evident in the final artifact. Budget accordingly to avoid the costly “download‑then‑discover” pitfall.

For a conventional transformer layer the key‑value (KV) cache stores a key and a value vector for every token that has already been processed. The per‑token memory cost therefore follows
[ \text{bytes per token} = 2 \times \text{num_layers} \times \text{num_kv_heads} \times \text{head_dim} \times \text{dtype_bytes} ]
The leading “2” accounts for the separate K and V tensors.
If the model uses grouped‑query attention (GQA), the KV‑head count is smaller than the query‑head count, so the formula can be written as
[ \text{bytes per token} = \frac{2 \times \text{layers} \times \text{hidden_size} \times \text{dtype_bytes}}{\text{KV‑sharing ratio}} ]
where the KV‑sharing ratio is num_query_heads / num_kv_heads (e.g., 8 for a model with 64 query heads and 8 KV heads)【source 2】.
| Model | Layers | KV heads | Head dim | Dtype | Bytes/token | Context‑size cost |
|---|---|---|---|---|---|---|
| Llama‑3‑8B (GQA) | 32 | 8 | 128 | FP16 (2 B) | 128 KiB | 16 GiB for 128 K tokens |
| Llama‑3.1‑70B (GQA) | 80 | 8 | 128 | BF16 (2 B) | ≈0.31 MiB | 2.7 GiB @ 8 K, 40 GiB @ 128 K |
| Llama‑3.1‑70B (full MHA) | 80 | 64 | 128 | BF16 | ≈2.5 MiB (≈8× larger) | ≈320 GiB @ 128 K |
The Llama‑3‑8B numbers come directly from vLLM’s KV‑cache accounting: 2·32·8·128·2 = 128 KiB/token, giving 16 GiB at a 128 K context length【source 5】.
Grouped‑Query Attention (GQA) – reduces the KV‑head count, dividing the naïve cost by the KV‑sharing ratio【source 2】.
Multi‑Query Attention (MQA) – collapses all query heads onto a single KV head, yielding up to a 128× reduction for very wide models, but at a noticeable quality trade‑off【source 8】.
Multi‑head Latent Attention (MLA) – DeepSeek – stores a 512‑dim latent vector per token instead of the full KV matrix. This cuts the per‑token footprint by roughly 28× (≈ 1 MiB → 36 KiB)【source 4】.
Quantized KV caches – serving stacks such as vLLM can store KV tensors in FP8 (1 byte) or INT8/INT4 formats, halving or quartering the dtype_bytes term and thus the overall cache size with negligible accuracy loss【source 7】.
Hybrid attention – models that keep a conventional KV cache only in a subset of layers reduce the “num_layers” factor. GLM‑5.3‑Flash stores KV in just 11 of its 45 layers, cutting the per‑token cost by roughly 4× versus a full‑stack transformer【source 1】.
Qwen‑3.8‑27B follows a similar hybrid design: only 16 of its 64 layers use full attention, each with 4 KV heads of dimension 256. The resulting per‑token cost is
[ 2 \times 16 \times 4 \times 256 \times 2 = 65{,}536\ \text{bytes} \approx 64\ \text{KiB/token} ]
which translates to ~2 GiB at 32 K tokens and ~16 GiB at the native 262 K limit【source 4】.
Before assuming a uniform stack, extract num_layers, num_kv_heads, and head_dim from the model’s gguf metadata or config.json. These fields reflect any layer‑wise KV sharing or hybrid attention patterns and ensure the KV‑cache estimate matches the actual runtime allocation.
By applying the correct variant‑adjusted formula, practitioners can predict whether a target context length will fit inside the 128 GB memory budget—often the decisive factor after accounting for model weight size.

The usable memory for an inference request is the sum of three additive components:
usable_memory = total_RAM – model_weights – runtime_overhead
max_tokens ≈ usable_memory ÷ per‑token_KV_bytes
[ \text{KV_bytes per token} = 2 \times \text{layers} \times \text{kv_heads} \times \text{head_dim} \times \text{dtype_bytes} ]
For a typical GQA model (e.g., Llama‑3‑8B) with 32 layers, 8 kv‑heads, head_dim = 128, and bf16 (2 bytes), the calculation yields ≈ 4 KiB per token [source]. Larger models with more layers (e.g., Llama‑3‑70B, 80 layers) reach ≈ 160 KiB per token [source].
| Model (quant) | Weights | Usable after overhead (≈96 GiB total) | KV per token | Context that fits |
|---|---|---|---|---|
| 8 B Q4 (≈4.7 GiB) | 4.7 GiB | 91 GiB | 4 KiB | ≈ 23 M tokens (the native 128 K‑token limit is reached first) |
| 70 B Q4_K_M (≈40 GiB) | 40 GiB | 56 GiB | 4 KiB | ≈ 14 M tokens; a 32 K‑token window consumes ~1.1 GiB, while 128 K tokens need ~16 GiB, leaving 40–45 GiB margin. |
The 8 B model is weight‑light; its binding constraint is the native context limit (e.g., 128 K tokens). The 70 B model is cache‑heavy; at very long windows the KV cache approaches the memory ceiling.
num_ctx and keep it resident for the lifetime of the request. The PARAMETER num_ctx line in a Modelfile therefore represents a hard line‑item in the memory budget [source].max_model_len) and are handed out on demand, trading “aggregate in‑flight token capacity” for a higher absolute context ceiling [source].Because allocation is static in Ollama, the configured context window dictates memory usage regardless of how many tokens are actually generated. In vLLM, memory grows only as blocks are needed, but the pool size still caps the maximum context that can be served.
num_ctx fits inside the 128 GiB budget after accounting for weight size and a 1 GiB overhead buffer.PARAMETER num_ctx 32768 in a Modelfile) rather than relying on engine defaults, which have shifted across releases (Ollama’s default was 2048 tokens in older versions) [source].By performing the simple arithmetic shown above, practitioners can avoid the costly “download‑then‑OOM” scenario and size their models confidently on any 128 GiB‑class workstation.

Mixture‑of‑Experts (MoE) models separate capacity (total parameters that must reside in memory) from per‑token compute (the subset of parameters that are actually used).
| Model | Total parameters | Active parameters per token | 4‑bit weight footprint* | Per‑token weight read |
|---|---|---|---|---|
| Mixtral 8×22B | 141 B | 39 B | ≈ 80 GB (0.5 GB / B) | 39 B × 0.5 bytes ≈ 19.5 GB |
| Qwen 35B‑A3B | 35 B | 3 B | 19–22 GB (Q4) | 3 B × 0.5 bytes ≈ 1.5 GB |
| Llama 4 Scout | 109 B | 17 B | 55–65 GB (0.5–0.6 GB / B) | 17 B × 0.5 bytes ≈ 8.5 GB |
| gpt‑oss‑120B | 120 B | – (native MoE) | 59.4 GiB (MXFP4) | — |
*Weight footprint is computed from the total parameter count using the rule of thumb ≈ 0.5 GB per billion parameters for 4‑bit quantization【source†https://www.corsair.com/us/en/explorer/diy-builder/how-tos/memory-for-local-llms-how-much-ram-do-you-need-and-when-speed-matters】.
The KV‑cache term is unchanged by the expert count; it is determined solely by the attention variant (e.g., grouped‑query) and the chosen context length. Consequently, MoE only modifies the weight term (capacity) and the bandwidth term (per‑token read), not the cache formula.
Because the whole expert matrix must be loaded, a MoE model’s capacity requirement is comparable to a dense model of the same total size. The advantage appears in speed and memory bandwidth: only the active subset is fetched each inference step, dramatically reducing per‑token traffic. For example, Mixtral’s 141 B total weights need ~80 GB of RAM, but each token moves only ~19.5 GB, a factor‑4‑6 reduction versus a dense 141 B model (≈ 70 GB per token at 4‑bit).
On a 128 GB unified‑memory system, MoE shifts the bottleneck from bandwidth to raw capacity. A model like Llama 4 Scout fits comfortably (≈ 60 GB) while still delivering conversational‑speed inference, whereas a dense counterpart would exceed memory limits. Off‑loading expert weights (e.g., to NVMe) can stretch capacity further, but incurs a proportional speed penalty.
Practical tip: Verify both total‑parameter and active‑parameter counts on the model card before estimating memory usage. Fit claims refer to capacity; actual throughput depends on the active‑parameter bandwidth.

The size of a model’s context window is expressed in tokens, not words or characters. A token is the unit the tokenizer emits, and its length varies with the tokenizer’s vocabulary. The most common rule of thumb for English is:
Because spaces are attached to the following word, a sentence such as “Context windows are shared between input and output tokens.” is split into nine tokens, not ten.
Tokenizers built with larger vocabularies (e.g., the o200k_base tokenizer with 200 k entries used by GPT‑4o, or Qwen’s ~150 k vocabulary) can represent more common word fragments as single tokens. Consequently, the same English text consumes fewer tokens than it would under a smaller‑vocabulary tokenizer (e.g., the older cl100k_base).
Conversely, a model that uses a compact legacy vocabulary (≈100 k entries) will break many words into sub‑tokens, inflating the token count and the KV‑cache memory required for a fixed context length. The per‑token KV cost is constant for a given model, so an increase in token count directly translates into more cache bytes.
Tokenization efficiency is highly data‑dependent:
| Data type | Typical token behavior |
|---|---|
| Plain English prose | Near the 4‑char average |
| Code, URLs, or long identifiers | Split into many short tokens |
| Non‑Latin scripts (e.g., Chinese, Arabic) | Often 1–2 characters per token, but can be higher for rare glyphs |
| Numbers, tables, spreadsheets | Frequently broken into individual digits or punctuation tokens |
These patterns mean that a workload heavy in code, numeric tables, or multilingual text can consume dramatically more KV cache than a pure English narrative, even if the raw character count is identical.
Two models with the same nominal context limit (e.g., 8 k tokens) will allocate different amounts of KV memory for the same input if their tokenizers differ. Therefore, a capacity plan based on word‑count estimates or on token counts measured with a different tokenizer will be inaccurate.
Always count tokens with the exact tokenizer that ships with the target model version. Use the official tokenizer library (e.g., tiktoken, Hugging Face’s transformers, or the model‑specific tokenizer) to convert your intended workload into a token count before entering the context‑length line item in the memory budget. Word‑count heuristics are unreliable for planning KV‑cache usage on a 128 GB machine.

A 70‑billion‑parameter model occupies dramatically different memory depending on quantization. In the GGUF format the Q4_K_M level compresses the checkpoint to roughly 40 GB on disk and in RAM, while the same model in BF16 needs about 140 GB and in Q8_0 about 70 GB【source】(https://www.sitepoint.com/definitive-guide-local-llms-2026-privacy-tools-hardware/). Because the weight footprint is the largest single lever, choosing a lower‑precision format is often the only way to keep the total budget under 128 GB.
The key‑value cache stores a pair of vectors for every layer, head, and token. The per‑token size follows
bytes per token = 2 × layers × kv_heads × head_dim × dtype_bytes
For a typical 70 B model with 80 layers, 8 KV heads, head_dim = 128, and FP16 (2 bytes) this evaluates to
2 × 80 × 8 × 128 × 2 ≈ 327 680 B ≈ 0.33 MiB per token
These numbers match the grouped‑query attention (GQA) cache size reported for Llama‑3‑70B‑style models【source】(https://slyd.com/guides/inference-gpu-guide).
Beyond weights and cache, an inference engine needs additional memory:
| Component | Typical range |
|---|---|
| CUDA / driver & framework context | 0.5 – 2 GB |
| Activations (current‑layer intermediates) | 5 %–10 % of total footprint |
| Fragmentation / safety headroom | 10 %–15 % of allocated memory |
Summed together this yields roughly 4 – 8 GB of extra overhead for a single request【source】(https://www.spheron.network/blog/gpu-memory-requirements-llm/).
| Context length | Weights (Q4_K_M) | KV cache | Overhead | Total | Fits on 128 GB? |
|---|---|---|---|---|---|
| 32 K | ~40 GB | ~11 GB | ~4–8 GB | 55–59 GB | ✅ comfortably (≈ 38 GB spare) |
| 128 K | ~40 GB | ~42 GB | ~4–8 GB | 86–90 GB | ⚠️ at the limit; options required |
If the 128 K case exceeds the available VRAM, practitioners can:
The open‑source gpt‑oss‑120b model uses a native 4‑bit MoE format (MXFP4) whose weight file is about 59.4 GiB【source】(https://acingai.com/articles/llm-quantization-deep-dive). Because the MoE architecture separates expert weights from the main KV cache, the same 128 GB machine can host a larger model with a modest cache budget, demonstrating how low‑bit MoE quantization reshapes the fitting equation.
Decision framing – On a 128 GB‑class system the question is rarely “do the weights fit?”; it is “how much context can I afford per request?” By inserting the quantization level, KV‑head count, and overhead headroom into the three‑term formula, practitioners can answer that question numerically before downloading any gigabytes.

Obtain the exact artifact size
Fact: The weight term is the published file size (e.g., a GGUF or .safetensors file). No estimation is required once you have the official download size.
Extract architecture parameters
Fact: Read gguf metadata or config.json to collect:
num_layers (total transformer blocks)kv_heads (key/value heads)head_dim (dimension per head)max_position_embeddings (the model’s hard context limit)num_heads with the smaller kv_heads ⁽¹⁾.Compute per‑token KV‑cache memory
Fact:
[ \text{bytes per token} = 2 \times \text{layers} \times \text{kv_heads} \times \text{head_dim} \times \text{dtype_bytes} ]
The factor 2 accounts for storing both key and value tensors ⁽²⁾.
Multiply the result by the intended context length (the total number of tokens you plan to keep in the cache). Remember that many inference engines (vLLM, DeepSpeed, etc.) pre‑allocate the full configured window, so the cache size is based on the maximum window, not on the tokens actually generated.
Convert your workload to token count
Fact: Tokenizers are model‑specific; identical text can produce different token counts across vocabularies. Run the model’s own tokenizer on a representative sample to obtain an accurate token estimate.
Add runtime overhead and compare to the usable budget
Fact: On a 128 GB unified‑memory machine, the default GPU‑accessible budget is roughly 96 GB (the remainder is reserved for the OS, system libraries, and fragmentation) ⁽³⁾.
Compute
[ \text{total memory} = \text{weights} + \text{KV‑cache} + \text{engine overhead} ]
and verify that it stays below the budget.
If the total exceeds the budget, apply levers in order of impact
| Lever | Typical impact | When to use |
|---|---|---|
| Reduce context length | Linear reduction of KV‑cache | First‑line adjustment |
| Lower KV‑cache dtype (e.g., FP16 → FP8/int4) | 2 × – 4 × smaller cache ⁽⁴⁾ | When long context is essential |
| Switch to a lower‑precision weight format (e.g., INT8, INT4) | Direct reduction of weight footprint | After cache optimisations |
| Choose a Mixture‑of‑Experts (MoE) variant with a favorable total‑to‑active ratio | Same stored parameters but fewer active weights per token, lowering both weight and cache load | If a MoE model of comparable quality exists |
Re‑compute after each change.
Validate after download
Fact: Use the runtime’s memory‑usage reports (e.g., nvidia‑smi, vLLM logs, or framework profiling) to confirm that resident memory matches the estimate. Discrepancies often arise from hidden buffers, activation spikes, or version‑specific allocation strategies, so empirical verification is essential.
Notes & Caveats
block_size = 16 in vLLM) are accurate for releases up to early 2026; newer engine versions may adjust allocation granularity, so always confirm against the current documentation.References