A 1M-token context needs anywhere from about 6GB to more than 128GB of extra memory, and the model’s design decides which. That extra memory is the KV cache, the model’s working memory of everything it has read. A classic full-attention model such as Llama 3.1 8B would need 128GB of cache at 1 million tokens. Qwen2.5-7B-Instruct-1M needs 56GB. The new hybrid models need far less: Qwen4-35B-A3B about 20GB, Kimi Linear about 8GB and NVIDIA’s Nemotron 3 Nano only about 6GB. All of these are 16-bit cache figures, and an 8-bit cache roughly halves them. Add the model itself and about 2GB of buffers, and a 4-bit Qwen4 at 1M tokens needs about 37GB of VRAM and RAM together. Nemotron 3 Nano needs about 28GB.
Below I explain what the KV cache is and give the formula, so you can work it out for any model. Then come the numbers for 13 popular local models at 32K, 128K, 256K and 1M tokens, the effect of cache quantisation, total memory, what fits on 8GB to 32GB cards and on Macs, and the exact llama.cpp and Ollama settings. I also cover the cost people forget: reading a million tokens can take more than half an hour. The numbers come from each model’s official configuration file, checked on 6 December 2026, and the cache sizes are my own calculations from those files, which I label as such.
What the KV cache is, in plain words
When a language model writes the next word, it looks back at every token that came before: your prompt, the documents you pasted, its own earlier answers. Recomputing everything about those tokens for every new word would be hopelessly slow. So the model computes two small vectors for each past token in each attention layer, a key (what this token is about) and a value (what it contributes), and stores them. That store is the key-value cache, or KV cache.
The model weights are a fixed size: a 4-bit Qwen4-35B-A3B takes the same memory whether you chat for ten tokens or a million. The KV cache is different. It grows with every token of context, so it is the part that decides how long a context you can afford.
Fixed size. Set by the model and the quantisation you download, such as Q4 or Q8.
Grows with every token of context. Set by the model’s design, the context length and the cache precision.
Working memory for the maths, plus a small fixed state in models with linear attention. Usually 1 to 3GB.
One detail surprises people. llama.cpp and Ollama reserve the whole cache when the model loads, not as the conversation grows. If you start llama.cpp with -c 1048576, it sets aside memory for a million tokens even if you only say hello. So a context setting you never use still costs real memory.
The formula: work it out for any model
For a classic model, the cache for one token is:
bytes per token = layers with a cache x 2 (key and value) x KV heads x head size x bytes per number
Then multiply by the context length. Here is where to find each number:
| Number | Where to find it | Llama 3.1 8B | Qwen4-35B-A3B |
|---|---|---|---|
| Layers with a cache | num_hidden_layers, minus any linear-attention or Mamba layers | 32 | 10 of 40 (full_attention_interval = 4) |
| Key and value | Always 2 | 2 | 2 |
| KV heads | num_key_value_heads (not the attention heads) | 8 | 2 |
| Head size | head_dim, or hidden size divided by attention heads | 128 | 256 |
| Bytes per number | 2 for a 16-bit cache, about 1 for 8-bit, about 0.56 for 4-bit | 2 | 2 |
| Per token | Multiply them all | 128KB | 20KB |
All of these are in the model’s config.json on Hugging Face. In a GGUF file, the same values appear in the metadata as block_count, attention.head_count_kv and attention.key_length, prefixed with the architecture name. For hybrid models, the model card or the layer_types list in the config tells you how many layers use full attention.
Two worked examples at 1M tokens (1,048,576), with a 16-bit cache:
- Llama 3.1 8B: 32 × 2 × 8 × 128 × 2 = 131,072 bytes per token. Times 1,048,576 tokens = 128GB. Llama 3.1 only supports 128K anyway, where the cache is 16GB.
- Qwen4-35B-A3B: 10 × 2 × 2 × 256 × 2 = 20,480 bytes per token. Times 1,048,576 = 20GB, despite the model being more than four times bigger.
A note on units. I use 1GB = 1,024MB here, the way llama.cpp, Windows Task Manager and most tools report memory. File sizes on Hugging Face use 1GB = 1,000MB, so they look about 7 percent bigger. I round every figure to one decimal place.
Why new models need so much less memory
Until 2024, almost every open model used full attention in every layer, and the only memory saving was grouped-query attention: sharing each key and value head between several query heads. That is why Llama 3.1 8B has 32 attention heads but only 8 KV heads. Since then, model makers have found four much bigger savings.
| Design | How it saves memory | Examples |
|---|---|---|
| Full attention (with grouped-query heads) | Fewer KV heads than attention heads. Still stores every token in every layer. | Llama 3.1, Qwen2.5, Qwen3-30B-A3B |
| Sliding window or chunked attention | Most layers only keep the last 128 to 8,192 tokens. A few global layers keep everything. | Gemma 4, gpt-oss, Llama 4 |
| Compressed attention (MLA) | Keys and values are squeezed into one small 576-number vector per token and layer. | DeepSeek, GLM-4.7-Flash, Kimi |
| Hybrid linear attention | Three in four layers (or more) use linear attention, Gated DeltaNet or Mamba, which keep a fixed-size state instead of a growing list. | Qwen 3.6, Qwen4, Qwen3-Next, Nemotron 3, Kimi Linear |
| Sparse attention | Each new token only looks at the most relevant past tokens. This mainly saves compute; the cache still holds every token. | Qwen4 (2,048 tokens), DeepSeek V4 |
The hybrid design is the big one for long context. In Qwen4-35B-A3B, 30 of 40 layers use Gated DeltaNet. By my calculation their fixed state is about 63MB in total, whether the context is 10 tokens or a million. Only the 10 full-attention layers grow. Nemotron 3 Nano goes further: according to NVIDIA’s model card, it has just 6 attention layers among its Mamba-2 and MoE layers, and each stores only 2 KV heads of 128 numbers.
Sparse attention is easy to confuse with the others. Qwen4’s sparse attention picks the 2,048 most relevant past tokens for each new token, which makes long prompts much faster. Its indexer also stores a small extra key per token, about 3 percent extra by my calculation, which I leave out of the tables. But the main cache still holds every token, because the model cannot know in advance which ones it will need.
KV cache for 13 local models, from 32K to 1M tokens
These are my calculations from each model’s official config.json, using the formula above with a 16-bit cache. A dash means the model does not support that length. Divide by about 2 for an 8-bit cache and by about 3.5 for a 4-bit cache (section 6).
| Model | Design | Per token | 32K | 128K | 256K | 1M |
|---|---|---|---|---|---|---|
| Nemotron 3 Nano 30B-A3B | Hybrid (Mamba-2) | 6.0KB | 0.2 | 0.8 | 1.5 | 6.0 |
| Kimi Linear 48B-A3B | Hybrid + MLA | 7.9KB | 0.2 | 1.0 | 2.0 | 7.9 |
| Qwen4-35B-A3B | Hybrid (DeltaNet) | 20.0KB | 0.6 | 2.5 | 5.0 | 20.0 |
| Qwen 3.6 35B-A3B | Hybrid (DeltaNet) | 20.0KB | 0.6 | 2.5 | 5.0 | 20.0 |
| Qwen3-Next 80B-A3B | Hybrid (DeltaNet) | 24.0KB | 0.8 | 3.0 | 6.0 | 24.0 |
| gpt-oss-20b | Sliding window | 24.0KB | 0.8 | 3.0 | — | — |
| Llama 4 Scout | Chunked window | 48.0KB | 1.5 | 6.0 | 12.0 | 48.0 |
| GLM-4.7-Flash | MLA | 52.9KB | 1.7 | 6.6 | — | — |
| Qwen2.5-7B-Instruct-1M | Full attention | 56.0KB | 1.8 | 7.0 | 14.0 | 56.0 |
| Qwen 3.6 27B | Hybrid (DeltaNet) | 64.0KB | 2.0 | 8.0 | 16.0 | 64.0 |
| Qwen3-30B-A3B-2507 | Full attention | 96.0KB | 3.0 | 12.0 | 24.0 | 96.0 |
| Llama 3.1 8B | Full attention | 128.0KB | 4.0 | 16.0 | — | — |
| Qwen2.5-14B-Instruct-1M | Full attention | 192.0KB | 6.0 | 24.0 | 48.0 | 192.0 |
The figures in GB are for the growing part of the cache only. Fixed extras come on top. Linear-attention states are about 40 to 150MB by my calculation. Llama 4 Scout’s 36 chunked layers keep about 1.2GB of recent tokens at 16-bit. Gemma 4 and gpt-oss keep small windows in most of their layers.
Look at the Qwen family. Qwen 3.6 27B is a smaller model than the 35B-A3B but needs more than three times the cache, because it has 16 full-attention layers with 4 KV heads instead of 10 with 2. Model size tells you almost nothing about context cost. You have to look at the attention layers.
A check against official figures. Qwen says Qwen3-30B-A3B-2507 needs “approximately 240 GB” of GPU memory for 1M tokens, covering weights, cache and activations in vLLM. My cache figure of 96GB plus about 57GB of 16-bit weights accounts for most of that, with the rest going to vLLM’s working memory. For Qwen2.5-7B-Instruct-1M, Qwen asks for at least 120GB of VRAM. My cache figure is 56GB.
Which local models actually support 1M tokens
A model only handles a long context well if it was trained or tuned for it. Here is what each model card says, as of December 2026:
| Model | Native context | 1M possible? | Source |
|---|---|---|---|
| Qwen4-35B-A3B | 1,048,576 | Yes, native | model card |
| Nemotron 3 Nano 30B-A3B | 256K by default | Yes: NVIDIA says it supports up to 1M; the default is lower because of VRAM | model card |
| Kimi Linear 48B-A3B | 1M | Yes, native | model card |
| Qwen2.5-7B/14B-Instruct-1M | 1,010,000 | Yes, but Qwen’s own long-context method needs its vLLM framework | model card |
| Llama 4 Scout | 10M | Yes | model card |
| DeepSeek V4 Flash | 1M | Yes, but it has 284B parameters, so not a home PC model | model card |
| Qwen 3.6 35B-A3B and 27B | 262,144 | “Extensible up to 1,010,000” with YaRN | model card |
| Qwen3-Next 80B-A3B | 262,144 | Extensible up to 1,010,000 with YaRN | model card |
| Qwen3-30B-A3B-2507 | 262,144 | Up to 1M with Dual Chunk Attention, which llama.cpp does not implement | model card |
| Gemma 4 26B and 31B | 256K | No | model card |
| gpt-oss-20b, Llama 3.1 8B | 128K | No | configuration |
“Native” means the model was trained to that length and needs no extra settings. YaRN is a way to stretch a model’s position encoding past its training length. Qwen’s model cards recommend it only when you really need the extra length, because, in Qwen’s words, the static YaRN used by open-source frameworks is “potentially impacting performance on shorter texts”. Section 12 shows the llama.cpp flags.
Cache quantisation: halve the memory with one flag
By default, llama.cpp and Ollama store the cache in 16-bit. You can store it at lower precision instead, separately for keys and values:
| Cache type | Bits per number | Size vs 16-bit | Qwen4 at 1M | Nemotron 3 Nano at 1M | Quality |
|---|---|---|---|---|---|
f16 (default) | 16 | 100% | 20.0GB | 6.0GB | Reference |
q8_0 | 8.5 | 53% | 10.7GB | 3.2GB | “Usually no noticeable impact” (Ollama) |
f8_e4m3 | 8 | 50% | 10.0GB | 3.0GB | New 8-bit float option in llama.cpp; test it yourself |
q4_0 | 4.5 | 28% | 5.6GB | 1.7GB | “Small-medium loss”, more noticeable at long contexts (Ollama) |
The bit counts include the scale stored with each block of 32 numbers, which is why q8_0 is 8.5 bits, not 8. The quality notes quote Ollama’s FAQ, which also warns that models with a high GQA count, where many query heads share each KV head, may lose more precision from cache quantisation. That describes most modern models, including the hybrids here.
My advice: use q8_0 for both keys and values as your normal setting. Only go to q4_0 when it is the difference between fitting and not fitting, and then try -ctk q8_0 -ctv q4_0 first. A common rule of thumb among llama.cpp users is that keys are more sensitive than values. Quantising the value cache needs flash attention, which llama.cpp turns on automatically where supported (-fa auto is the default).
Total memory at 1M tokens, model included
The cache is only part of the bill. Here is my estimate of the total for each 1M-capable model, with a 4-bit file, an 8-bit cache and about 2GB for buffers. Model sizes are the 4-bit file sizes listed on Hugging Face (Unsloth’s files where available, bartowski’s for Kimi Linear and Qwen2.5-7B-1M). For Qwen4 I use the in-memory size, because its 20B n-gram table stays on the SSD (my Qwen4 guide explains this). I add the file sizes as Hugging Face lists them, which use 1GB = 1,000MB, so the totals err slightly on the high side.
| Model | 4-bit model | 8-bit cache at 1M | Total (with ~2GB buffers) |
|---|---|---|---|
| Nemotron 3 Nano 30B-A3B | UD-Q4_K_XL, 22.8GB | 3.2GB | about 28GB |
| Qwen 3.6 35B-A3B (YaRN) | UD-Q4_K_XL, 22.4GB | 10.7GB | about 35GB |
| Kimi Linear 48B-A3B | Q4_K_M, 30.1GB | 4.2GB | about 36GB |
| Qwen2.5-7B-Instruct-1M | Q4_K_M, 4.7GB | 29.8GB | about 36GB |
| Qwen4-35B-A3B | UD-Q4_K_XL, 24.7GB in memory | 10.7GB | about 37GB |
| Qwen 3.6 27B (YaRN) | UD-Q4_K_XL, 17.6GB | 34.0GB | about 54GB |
| Qwen3-Next 80B-A3B (YaRN) | UD-Q4_K_XL, 46.1GB | 12.8GB | about 61GB |
| Llama 4 Scout | UD-Q4_K_XL, 62.0GB | 26.7GB | about 91GB |
What are the buffers?
llama.cpp needs working memory for the maths, called the compute buffer. At long contexts it grows, because the attention step works on a table of every past token for each batch of new ones. With flash attention, which is the default, a batch of 512 tokens against 1M past tokens needs a mask of about 1GB by my calculation. Without flash attention, the attention scores alone for one batch would need tens of gigabytes, which is why flash attention is essential for long contexts. In practice, budget 1 to 3GB for buffers at 1M. You can lower it with a smaller physical batch, -ub 256, at some cost in prompt speed.
The cleanest result is Nemotron 3 Nano. At about 28GB in total, a million-token context fits on a PC with a 16GB card and 32GB of RAM, or on a 24GB card with 16GB of RAM. Qwen4 needs about 37GB, so 48GB of combined memory at least, and more comfortably 64GB of system RAM.
What your hardware can hold
These are my estimates for the two most practical 1M-token models, a 4-bit file and an 8-bit cache, leaving room for Windows and a browser. Treat them as starting points and check the real numbers with section 14.
| Hardware | Qwen4-35B-A3B | Nemotron 3 Nano 30B-A3B | Notes |
|---|---|---|---|
| 8GB GPU + 32GB RAM | 64K to 128K | 512K (1M is very tight) | Most experts in RAM; cache on the GPU |
| 12GB GPU + 32GB RAM | 256K | 1M, tight | Raise --n-cpu-moe as the context grows |
| 16GB GPU + 32GB RAM | 256K to 512K (1M only with very little else running) | 1M | Nemotron fits 1M with room to spare |
| 16GB GPU + 64GB RAM | 1M | 1M | Put the cache on the GPU, experts in RAM |
| 24GB GPU + 64GB RAM | 1M, cache on GPU | 1M, almost all on GPU | The sweet spot for 1M at home |
| 32GB GPU + 64GB RAM | 1M, fast | 1M, all on GPU | RTX 5090 class |
| Mac, 36GB unified | 256K | 512K to 1M | macOS needs about 8GB for itself |
| Mac, 48GB unified | 512K | 1M | |
| Mac, 64GB+ unified | 1M | 1M | The simplest way to run 1M at home |
Both are mixture-of-experts models, so the trick from my Qwen4 guide applies. Keep the attention layers and the cache on the GPU, and move expert layers to system RAM with --n-cpu-moe. Every gigabyte of cache you add to the GPU pushes about one more expert layer to the CPU. That costs a little speed but does not stop the model working. For 8GB laptops and small PCs, my 8GB RAM laptop guide is a better starting point, because a million tokens is out of reach there.
VRAM or system RAM: where the cache should live
By default llama.cpp puts each layer’s cache on the same device as that layer. With a model fully on the GPU, the whole cache is in VRAM, which is fastest. You have three alternatives when it does not fit:
For MoE models, the best option. Raise --n-cpu-moe so experts move to RAM and the cache stays on the GPU. Writing speed drops a little; long prompts stay fast.
--no-kv-offload (or -nkvo) keeps the whole cache in system RAM. It frees VRAM, but attention then runs on the CPU, so long prompts get much slower. A last resort.
Apple Silicon Macs and machines like the DGX Spark share one pool between CPU and GPU, so the question disappears. The total has to fit, that is all.
Avoid letting Windows spill VRAM into “shared GPU memory”. If Task Manager shows shared GPU memory climbing while llama.cpp runs, the driver is quietly paging the cache over the PCIe bus, which can make everything many times slower. Lower the context or move more experts to the CPU instead.
The hidden cost: reading a million tokens takes time
Memory is only half of the problem. Before the model writes its first word, it has to read the whole prompt, and a million tokens is about 750,000 words, several novels long. Here is my arithmetic for how long that takes at different prompt-processing speeds:
Two things make it worse in practice. First, with full attention each new token has to look at all the earlier ones, so prompt speed falls as the context fills. Hybrid and sparse models suffer much less, which is part of why they exist. Second, if anything changes near the start of the conversation, such as a different system prompt, the model has to read everything again.
How to avoid paying twice
- Keep the start of the prompt the same. llama-server reuses the cache for any part of a new request that matches the previous one, so follow-up questions about the same document are fast.
- Let the prompt cache work. llama-server keeps earlier conversations’ caches in system RAM, up to 8,192MB by default (
--cache-ram). At 1M tokens one conversation can be larger than that, so raise it if you switch between long documents and have the RAM. - Hybrid models use checkpoints. Because linear-attention state cannot be rewound, llama.cpp saves context checkpoints, 32 per slot by default (
--ctx-checkpoints). They cost some RAM but let the server reuse long prompts. - Do you need it all? Retrieval (searching a document and pasting only the relevant parts) is often faster and more accurate than pasting everything. My posts on context compaction thresholds and Open WebUI compaction cover keeping long chats under control.
Does quality hold up at 1M tokens?
Fitting a million tokens in memory does not mean the model uses all of them well. The best published numbers I found:
| Model | Test | Result | Source |
|---|---|---|---|
| Nemotron 3 Nano 30B-A3B | RULER-100 at 256K / 512K / 1M | 92.9 / 91.3 / 86.3 | NVIDIA model card |
| Kimi Linear 48B-A3B | RULER at 128K | 84.3, with a 3.98x speed-up over full attention | Moonshot model card |
| Qwen4-35B-A3B (UD-Q4_K_XL) | RULER at 32K, 64K, 128K | Perfect scores reported by Unsloth | my Qwen4 guide |
RULER tests things like finding facts hidden in long text, tracing variables and counting. A score in the high 80s at 1M is good, but it also means some answers go wrong as the context fills. My practical advice:
- Put your question and the most important instructions at the end of the prompt, after the long document, not only at the start.
- Ask the model to quote the passage it relies on. You can then check it.
- For YaRN-extended models (Qwen 3.6, Qwen3-Next), test on your own documents before trusting long answers, and switch YaRN off for normal chats, as Qwen recommends.
- An 8-bit cache is fine for long contexts. If you must use a 4-bit cache, test more carefully, because Ollama notes that its loss “may be more noticeable at higher context sizes”.
llama.cpp settings for long contexts
| Flag | What it does for long context |
|---|---|
-c N | The context size. The default is 0, which means “whatever the model was trained for”, and that can be 1M for Qwen4. |
--fit on | On by default. Adjusts settings you did not set, including the context, so the model fits in memory. It will not go below --fit-ctx (4,096 by default). Set -c yourself if you want a fixed length. |
-ctk / -ctv | Cache type for keys and values: f16, bf16, f8_e4m3, q8_0, q4_0, q4_1, iq4_nl, q5_0, q5_1. |
-fa on | Flash attention. Default is auto. Essential for long contexts and for a quantised value cache. |
-np 1 | One conversation slot. The default is automatic, and several slots share the cache, so use 1 for one long conversation. |
-kvu | One unified cache shared by all slots. On by default when the slot count is automatic. |
-nkvo | Keep the cache in system RAM instead of VRAM (section 9). |
-ub N | Physical batch size, default 512. Lower it to shrink the compute buffer at very long contexts. |
--swa-full | Gives sliding-window layers a full-size cache. Uses far more memory; leave it off unless you know you need it. |
--rope-scaling yarn --rope-scale N --yarn-orig-ctx N | Stretch a model past its training length (YaRN). |
--cache-ram N | How much system RAM the prompt cache may use, in MiB (default 8,192). |
Nemotron 3 Nano at 1M tokens on a 16GB card with 32GB of RAM. Lower --n-cpu-moe if you have VRAM to spare:
llama-server -m Nemotron-3-Nano-30B-A3B-UD-Q4_K_XL.gguf -c 1048576 -np 1 -fa on -ctk q8_0 -ctv q8_0 --n-cpu-moe 20 --jinja
NVIDIA’s configuration file says 256K, but the model card says the model supports 1M and that the lower default is only there to save VRAM. llama.cpp may print a warning that the context is longer than the training context stored in the file. That is expected here.
Qwen4-35B-A3B at 1M tokens on a 24GB card with 64GB of RAM. Add the n-gram and other Qwen4-specific options from my Qwen4 guide:
llama-server -m Qwen4-35B-A3B-UD-Q4_K_XL.gguf -c 1048576 -np 1 -fa on -ctk q8_0 -ctv q8_0 --n-cpu-moe 30 --jinja
Qwen 3.6 35B-A3B stretched to 1M with YaRN, using the factor of 4 that Qwen’s model card gives (262,144 × 4):
llama-server -m Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf -c 1048576 --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 262144 -np 1 -fa on -ctk q8_0 -ctv q8_0 --n-cpu-moe 30 --jinja
Qwen’s model card shows YaRN settings for vLLM and SGLang, not llama.cpp. The flags above are llama.cpp’s general YaRN options, so test with a long document of your own before you rely on them. For anything up to 262K, leave YaRN off.
Ollama and LM Studio
Ollama’s documentation says it picks a default context by the amount of VRAM: 4K below 24GB, 32K from 24 to 48GB, and 256K at 48GB or more. That is why a model that supports 1M still forgets the start of a long chat in Ollama: it was never given more than 4K. To change it:
- For all models: start the server with
OLLAMA_CONTEXT_LENGTH=262144 ollama serve, or use the context slider in the Ollama app’s settings. - For one chat: type
/set parameter num_ctx 262144inollama run. - Through the API: send
"options": {"num_ctx": 262144}with the request. - Cache quantisation: set
OLLAMA_KV_CACHE_TYPE=q8_0. It is a global setting for all models, and it works when flash attention is on, which Ollama enables automatically where supported (OLLAMA_FLASH_ATTENTION=1forces it). - Check the result:
ollama psshows the context each loaded model got, and how much of it runs on the GPU.
Ollama’s documentation also recommends at least 64,000 tokens for agents, web search and coding tools.
LM Studio shows a context length setting when you load a model, and its llama.cpp engine has load options for flash attention and K/V cache quantisation. The option names have changed between versions, so look in the model’s load settings. Like llama.cpp, it reserves the cache when the model loads, so raise the context only as far as you need.
For measured numbers, see my Ollama vs llama.cpp VRAM comparison: on the same card and model, Ollama’s 32K default used 16.5GB and llama-server’s 262K default 21.4GB, while identical settings used the same memory.
How to see the real memory use
My numbers are calculations. Your build, your GPU driver and the exact file can shift them by a few hundred megabytes, so check before you trust a long setup.
- Read the llama.cpp startup log.Look for lines containing
KV buffer size(the cache on each device) andcompute buffer size(the working memory). Linear-attention models also print a smaller buffer for their fixed state. - Open Task Manager.On the Performance tab, select your GPU and watch “Dedicated GPU memory”. If “Shared GPU memory” rises sharply, you are over the limit.
- Fill the context once.Paste a long document. The cache is reserved at startup, but the buffers and Windows’ file cache settle only during real use.
- In Ollama, use ollama ps.It shows the model size, the GPU/CPU split and the context length.
Troubleshooting long contexts
| Problem | Likely cause | Fix |
|---|---|---|
| Out of memory at startup | The full cache is reserved at load | Lower -c, use -ctk q8_0 -ctv q8_0, or raise --n-cpu-moe |
| llama.cpp picked a smaller context than the model supports | --fit shrank it to fit | Set -c yourself and free memory, or accept the fitted size |
| Ollama forgets the start of the chat | Default context of 4K on cards under 24GB | Raise num_ctx or OLLAMA_CONTEXT_LENGTH |
| Everything gets very slow at long contexts | VRAM spilled into shared GPU memory, or the cache is in RAM | Watch Task Manager; move experts to RAM instead of the cache |
| The first answer takes many minutes | Reading the prompt | Expected at 1M (section 10). Keep the prompt start stable so the cache is reused |
| Crash during the first long prompt, not at load | The compute buffer grew at full length | Lower -ub to 256, or leave 1 to 2GB more VRAM free |
| Worse answers on short chats after enabling YaRN | Static YaRN affects short texts | Turn YaRN off unless you need more than the native context |
| Answers miss facts deep in a long document | Long-context accuracy falls off | Put the question last, ask for quotes, or use retrieval |
| Cache quantisation has no effect in Ollama | Flash attention not active on your device | Check the logs; set OLLAMA_FLASH_ATTENTION=1 |
Frequently asked questions
How much RAM do I need for a 1M context?
For the cache alone, 6GB (Nemotron 3 Nano, 16-bit) to 128GB (a classic 8B model). For a whole 4-bit model plus an 8-bit cache, about 28GB for Nemotron 3 Nano and about 37GB for Qwen4-35B-A3B, plus room for Windows.
Why does my model use more memory when I raise the context, even for short chats?
llama.cpp, Ollama and LM Studio reserve the whole cache when the model loads. A 1M context costs its full cache from the first token.
Is the KV cache the same as the context window?
The context window is the number of tokens the model can see. The KV cache is the memory used to hold them. A bigger window needs a bigger cache.
Does a bigger model always need a bigger cache?
No. Qwen4-35B-A3B needs less cache per token than Llama 3.1 8B, and Qwen 3.6 27B needs more than Qwen 3.6 35B-A3B. The attention design matters far more than size.
Is q8_0 cache quantisation safe?
For most uses, yes. Ollama’s FAQ says it usually has no noticeable impact on quality. Use q4_0 only if you must, and test it on long documents.
Can I keep the KV cache in system RAM?
Yes, with -nkvo in llama.cpp, but long prompts become much slower. For MoE models, moving experts to RAM and keeping the cache on the GPU is better.
How long does it take to process 1M tokens?
From about 3.5 minutes at 5,000 tokens per second to more than an hour at 250 tokens per second, by my arithmetic, and usually longer in practice.
Which local model is best for 1M tokens?
For memory, Nemotron 3 Nano: about 28GB in total at 4-bit and an 86.3 RULER score at 1M on NVIDIA’s card. Qwen4-35B-A3B is the other native 1M option and needs about 37GB.
Can Llama 3.1 or Gemma 4 do 1M tokens?
No. Llama 3.1 stops at 128K and Gemma 4 at 256K.
Does MTP or speculative decoding change the cache size?
Slightly. The MTP layer keeps its own small cache, about one more layer’s worth. My llama.cpp MTP guide covers it.
Does the vision projector use context?
Images become tokens in the context, often hundreds or thousands per image, so they fill the cache like text. The projector itself is a separate fixed-size file.
Why does Ollama only give me 4K context?
Because that is its default on GPUs with less than 24GB of VRAM. Raise num_ctx or OLLAMA_CONTEXT_LENGTH.
Does the cache grow while the model thinks?
Yes. Thinking tokens are part of the context, so long reasoning fills the cache like any other text.
What does --fit do to my context?
If you do not set -c, llama.cpp starts from the model’s trained length and shrinks it until everything fits, but not below 4,096 tokens.
Your next step
Work out what you really need. For most chats and coding, 32K to 128K is plenty and costs little. If you do need hundreds of thousands of tokens, pick a hybrid model: Nemotron 3 Nano if memory is tight, Qwen4-35B-A3B if you want Qwen’s quality (see my Qwen4 vs Qwen 3.6 comparison). Then start llama.cpp with -np 1 -fa on and -ctk q8_0 -ctv q8_0, raise -c step by step, and watch the KV buffer size line and Task Manager until you find your limit.
Sources: llama.cpp server README, Ollama context length docs, Ollama FAQ (KV cache types), Qwen4-35B-A3B model card, Qwen3.6-35B-A3B model card, Nemotron 3 Nano model card, Kimi Linear model card, Qwen2.5-7B-Instruct-1M model card, Qwen3-30B-A3B-2507 model card, Qwen3-Next 80B model card, Llama 4 Scout model card, Gemma 4 model card, DeepSeek V4 Flash model card. Cache sizes are my calculations from each model’s official config.json.