# Will it fit?

> Pick a model size, a quantization and a context length to see the memory it needs. The result shows which common machines can hold it.

Move the sliders until the total turns green on your machine. Each line below the total is a common machine with the memory a model can actually use on it.

*Interactive tool: memory needed = weights (parameters × bits ÷ 8) + context (KV cache) + about 0.5 GB runtime.*

## The formula

The total has three parts, and you can do the sums by hand.

1. Weights: parameters in billions, times bits per weight, divided by 8. A 27B model at about 4.8 bits is about 16 GB.
2. Context: the KV cache grows with every token in the [context window](https://localagi.co/context-window). Bigger models store more per token.
3. Runtime: about half a GB for the runtime itself and its buffers.

**Rule of thumb.** 
At 4-bit, budget about 0.6 GB. At 8-bit, budget about 1.1 GB. Then add a few GB for context.

## Reading the result

A model that fits with less than a GB to spare will load, then slow down or crash as the conversation grows. Leave headroom of a few GB, or more if you run an agent with a long context.

The usable memory per machine is our estimate. On a Mac, macOS keeps a share of unified memory for itself. On a PC with a graphics card, only VRAM counts, and the desktop takes a little of that too. [Will the model fit in my memory?](https://localagi.co/memory) explains both.

**The catch.** 
The context estimate assumes the attention layout most current models use and a 16-bit cache. Some models store far less per token, a few store more. Check the real number in your runtime's log the first time you load a model.

## Go deeper

### Where the context estimate comes from

For each token, every layer stores a key and a value for each attention head group.

Multiply layers, head groups, head size, two and the bytes per number, and you get the cache per token. For Llama-style 8B models that is about 0.13 MB per token, and for 70B models about 0.33 MB. The tool scales between those with the square root of the parameter count, which tracks current models within about a third.

### Why 8-bit KV cache helps

Halving the bits per cached number halves the context memory.

The cost is a small loss in recall over long contexts. It is worth it when the context, not the weights, is what stops a model fitting. See [How do I make it faster?](https://localagi.co/tuning-for-speed).

### Mixture of experts models

Use the total parameter count, not the active count. An MoE model still has to hold every expert in memory, even though each token only uses a few. [What is a mixture of experts model?](https://localagi.co/dense-vs-moe) covers why.
