Artificial Ignorance

How Much RAM and VRAM Does a 1M-Token Context Need? The KV Cache Explained

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.

How much RAM and VRAM a 1M-token context needsBars showing the 16-bit KV cache at one million tokens: 128 GB for a classic Llama 3.1 8B style model, 56 GB for Qwen2.5-7B-1M, 20 GB for Qwen4-35B-A3B, 7.9 GB for Kimi Linear and 6 GB for Nemotron 3 Nano.LOCAL AI GUIDE1M tokens.How much RAM?The KV cache, explained with numbersFrom 6 GB to 128 GB for the same context.The model’s design decides, not its size.16-bit cache at 1,048,576 tokensLlama 3.1 8B style128 GBQwen2.5-7B-1M56 GBQwen4-35B-A3B20 GBKimi Linear7.9 GBNemotron 3 Nano6 GBartificialignorance.net
01

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.

Model weights

Fixed size. Set by the model and the quantisation you download, such as Q4 or Q8.

KV cache

Grows with every token of context. Set by the model’s design, the context length and the cache precision.

Buffers and state

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.

02

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
The KV cache formula with a worked exampleBytes per token equal layers with a cache times 2 for keys and values times KV heads times head size times bytes per number. For Llama 3.1 8B with a 16-bit cache: 32 times 2 times 8 times 128 times 2 equals 131,072 bytes, 128 KB per token, which is 4 GB at 32K tokens, 16 GB at 128K and 128 GB at one million tokens.THE KV CACHE FORMULA32layers with a cache×2K and V×8KV heads×128head size×2bytes per number= 131,072 bytes per token = 128 KBExample: Llama 3.1 8B with a 16-bit cache. Multiply by the context length:32K tokens = 4 GB · 128K tokens = 16 GB · 1M tokens = 128 GBOnly layers that keep a growing cache count. Linear-attention and Mamba layers keep a small fixed state instead.
The same formula works for every model. The only trick with new models is counting which layers really keep a cache.

Then multiply by the context length. Here is where to find each number:

NumberWhere to find itLlama 3.1 8BQwen4-35B-A3B
Layers with a cachenum_hidden_layers, minus any linear-attention or Mamba layers3210 of 40 (full_attention_interval = 4)
Key and valueAlways 222
KV headsnum_key_value_heads (not the attention heads)82
Head sizehead_dim, or hidden size divided by attention heads128256
Bytes per number2 for a 16-bit cache, about 1 for 8-bit, about 0.56 for 4-bit22
Per tokenMultiply them all128KB20KB

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.

03

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.

Four attention designs and how much past each layer storesFull attention keeps every past token in every layer. Sliding-window models keep only recent tokens in most layers. Compressed attention (MLA) stores one small vector per token. Hybrid linear models keep a fixed-size state in three of every four layers.FOUR WAYS MODELS STORE THE PASTFull attentionEvery layer keeps every pasttoken.Llama 3.1, Qwen2.5, Qwen3-30BSliding windowMost layers keep only the lastfew thousand tokens.Gemma 4, gpt-oss, Llama 4(chunked)Compressed (MLA)Keys and values squeezed intoone small vector.DeepSeek, GLM-4.7-Flash, KimiHybrid linearfixed statefixed statefixed statefixed statefixed state3 in 4 layers keep afixed-size state, not a list.Qwen 3.6, Qwen4, Nemotron 3,Kimi Linear
The same 12 tokens stored four ways. Bright squares are cached; faint ones are not kept.
DesignHow it saves memoryExamples
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 attentionMost 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 attentionThree 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 attentionEach 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.

04

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).

KV cache per token for ten local modelsLlama 3.1 8B 128 KB, Qwen3-30B-A3B-2507 96 KB, Qwen 3.6 27B 64 KB, Qwen2.5-7B-Instruct-1M 56 KB, GLM-4.7-Flash 52.9 KB, Llama 4 Scout 48 KB, Qwen3-Next 80B 24 KB, Qwen4 and Qwen 3.6 35B-A3B 20 KB, Kimi Linear 7.9 KB, Nemotron 3 Nano 6 KB.KV CACHE PER TOKEN (16-BIT)full attentionhybrid linearcompressed (MLA)chunked windowLlama 3.1 8B128 KBQwen3-30B-A3B-250796 KBQwen 3.6 27B64 KBQwen2.5-7B-Instruct-1M56 KBGLM-4.7-Flash52.9 KBLlama 4 Scout48 KBQwen3-Next 80B-A3B24 KBQwen4 / Qwen 3.6 35B-A3B20 KBKimi Linear 48B-A3B7.9 KBNemotron 3 Nano 30B-A3B6 KB
Cache per token with a 16-bit cache, my calculation from the official configurations. Colour shows the attention design.
ModelDesignPer token32K128K256K1M
Nemotron 3 Nano 30B-A3BHybrid (Mamba-2)6.0KB0.20.81.56.0
Kimi Linear 48B-A3BHybrid + MLA7.9KB0.21.02.07.9
Qwen4-35B-A3BHybrid (DeltaNet)20.0KB0.62.55.020.0
Qwen 3.6 35B-A3BHybrid (DeltaNet)20.0KB0.62.55.020.0
Qwen3-Next 80B-A3BHybrid (DeltaNet)24.0KB0.83.06.024.0
gpt-oss-20bSliding window24.0KB0.83.0——
Llama 4 ScoutChunked window48.0KB1.56.012.048.0
GLM-4.7-FlashMLA52.9KB1.76.6——
Qwen2.5-7B-Instruct-1MFull attention56.0KB1.87.014.056.0
Qwen 3.6 27BHybrid (DeltaNet)64.0KB2.08.016.064.0
Qwen3-30B-A3B-2507Full attention96.0KB3.012.024.096.0
Llama 3.1 8BFull attention128.0KB4.016.0——
Qwen2.5-14B-Instruct-1MFull attention192.0KB6.024.048.0192.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.

05

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:

ModelNative context1M possible?Source
Qwen4-35B-A3B1,048,576Yes, nativemodel card
Nemotron 3 Nano 30B-A3B256K by defaultYes: NVIDIA says it supports up to 1M; the default is lower because of VRAMmodel card
Kimi Linear 48B-A3B1MYes, nativemodel card
Qwen2.5-7B/14B-Instruct-1M1,010,000Yes, but Qwen’s own long-context method needs its vLLM frameworkmodel card
Llama 4 Scout10MYesmodel card
DeepSeek V4 Flash1MYes, but it has 284B parameters, so not a home PC modelmodel card
Qwen 3.6 35B-A3B and 27B262,144“Extensible up to 1,010,000” with YaRNmodel card
Qwen3-Next 80B-A3B262,144Extensible up to 1,010,000 with YaRNmodel card
Qwen3-30B-A3B-2507262,144Up to 1M with Dual Chunk Attention, which llama.cpp does not implementmodel card
Gemma 4 26B and 31B256KNomodel card
gpt-oss-20b, Llama 3.1 8B128KNoconfiguration

“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.

06

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 typeBits per numberSize vs 16-bitQwen4 at 1MNemotron 3 Nano at 1MQuality
f16 (default)16100%20.0GB6.0GBReference
q8_08.553%10.7GB3.2GB“Usually no noticeable impact” (Ollama)
f8_e4m3850%10.0GB3.0GBNew 8-bit float option in llama.cpp; test it yourself
q4_04.528%5.6GB1.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).

07

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.

Estimated total memory for a one million token contextMy estimates with a 4-bit model and an 8-bit cache: Nemotron 3 Nano about 28 GB, Qwen2.5-7B-1M about 36 GB, Qwen 3.6 35B-A3B about 35 GB, Kimi Linear about 36 GB, Qwen4-35B-A3B about 37 GB, Qwen 3.6 27B about 54 GB, Qwen3-Next 80B about 61 GB and Llama 4 Scout about 91 GB.TOTAL MEMORY AT 1M TOKENS (4-BIT MODEL, 8-BIT CACHE)model weightsKV cachebuffers, about 2 GBNemotron 3 Nano 30B-A3B~28 GBQwen2.5-7B-Instruct-1M~36 GBQwen 3.6 35B-A3B (YaRN)~35 GBKimi Linear 48B-A3B~36 GBQwen4-35B-A3B~37 GBQwen 3.6 27B (YaRN)~54 GBQwen3-Next 80B-A3B (YaRN)~61 GBLlama 4 Scout~91 GB32 GB64 GB
My estimates. Leave another 4 to 6GB for Windows and your other apps on top of these totals.
Model4-bit model8-bit cache at 1MTotal (with ~2GB buffers)
Nemotron 3 Nano 30B-A3BUD-Q4_K_XL, 22.8GB3.2GBabout 28GB
Qwen 3.6 35B-A3B (YaRN)UD-Q4_K_XL, 22.4GB10.7GBabout 35GB
Kimi Linear 48B-A3BQ4_K_M, 30.1GB4.2GBabout 36GB
Qwen2.5-7B-Instruct-1MQ4_K_M, 4.7GB29.8GBabout 36GB
Qwen4-35B-A3BUD-Q4_K_XL, 24.7GB in memory10.7GBabout 37GB
Qwen 3.6 27B (YaRN)UD-Q4_K_XL, 17.6GB34.0GBabout 54GB
Qwen3-Next 80B-A3B (YaRN)UD-Q4_K_XL, 46.1GB12.8GBabout 61GB
Llama 4 ScoutUD-Q4_K_XL, 62.0GB26.7GBabout 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.

08

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.

HardwareQwen4-35B-A3BNemotron 3 Nano 30B-A3BNotes
8GB GPU + 32GB RAM64K to 128K512K (1M is very tight)Most experts in RAM; cache on the GPU
12GB GPU + 32GB RAM256K1M, tightRaise --n-cpu-moe as the context grows
16GB GPU + 32GB RAM256K to 512K (1M only with very little else running)1MNemotron fits 1M with room to spare
16GB GPU + 64GB RAM1M1MPut the cache on the GPU, experts in RAM
24GB GPU + 64GB RAM1M, cache on GPU1M, almost all on GPUThe sweet spot for 1M at home
32GB GPU + 64GB RAM1M, fast1M, all on GPURTX 5090 class
Mac, 36GB unified256K512K to 1MmacOS needs about 8GB for itself
Mac, 48GB unified512K1M
Mac, 64GB+ unified1M1MThe 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.

09

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:

Move experts, keep the cache

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.

Cache in RAM

--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.

Unified memory

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.

10

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:

How long it takes to read a one million token promptAt 250 tokens per second about 70 minutes, at 500 about 35 minutes, at 1,000 about 17 minutes, at 2,500 about 7 minutes and at 5,000 about 3.5 minutes.TIME TO READ 1,048,576 TOKENS BEFORE THE FIRST WORDMy arithmetic at a constant prompt speed. Real speed usually falls as the context fills, so treat these as best cases.250 tokens/sCPU or weak iGPU70 min500 tokens/s8GB card, model mostly in RAM35 min1,000 tokens/s12 to 16GB card, MoE offload17 min2,500 tokens/s24GB card, most of it on GPU7.0 min5,000 tokens/s32GB card or fast Mac, all on GPU3.5 min
My arithmetic. The hardware labels are rough guides for a 30B to 35B mixture-of-experts model, not measurements.

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.
11

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:

ModelTestResultSource
Nemotron 3 Nano 30B-A3BRULER-100 at 256K / 512K / 1M92.9 / 91.3 / 86.3NVIDIA model card
Kimi Linear 48B-A3BRULER at 128K84.3, with a 3.98x speed-up over full attentionMoonshot model card
Qwen4-35B-A3B (UD-Q4_K_XL)RULER at 32K, 64K, 128KPerfect scores reported by Unslothmy 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”.
12

llama.cpp settings for long contexts

FlagWhat it does for long context
-c NThe context size. The default is 0, which means “whatever the model was trained for”, and that can be 1M for Qwen4.
--fit onOn 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 / -ctvCache type for keys and values: f16, bf16, f8_e4m3, q8_0, q4_0, q4_1, iq4_nl, q5_0, q5_1.
-fa onFlash attention. Default is auto. Essential for long contexts and for a quantised value cache.
-np 1One conversation slot. The default is automatic, and several slots share the cache, so use 1 for one long conversation.
-kvuOne unified cache shared by all slots. On by default when the slot count is automatic.
-nkvoKeep the cache in system RAM instead of VRAM (section 9).
-ub NPhysical batch size, default 512. Lower it to shrink the compute buffer at very long contexts.
--swa-fullGives 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 NStretch a model past its training length (YaRN).
--cache-ram NHow 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.

13

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 262144 in ollama 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=1 forces it).
  • Check the result: ollama ps shows 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.

14

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.

  1. Read the llama.cpp startup log.Look for lines containing KV buffer size (the cache on each device) and compute buffer size (the working memory). Linear-attention models also print a smaller buffer for their fixed state.
  2. 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.
  3. 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.
  4. In Ollama, use ollama ps.It shows the model size, the GPU/CPU split and the context length.
15

Troubleshooting long contexts

ProblemLikely causeFix
Out of memory at startupThe full cache is reserved at loadLower -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 fitSet -c yourself and free memory, or accept the fitted size
Ollama forgets the start of the chatDefault context of 4K on cards under 24GBRaise num_ctx or OLLAMA_CONTEXT_LENGTH
Everything gets very slow at long contextsVRAM spilled into shared GPU memory, or the cache is in RAMWatch Task Manager; move experts to RAM instead of the cache
The first answer takes many minutesReading the promptExpected at 1M (section 10). Keep the prompt start stable so the cache is reused
Crash during the first long prompt, not at loadThe compute buffer grew at full lengthLower -ub to 256, or leave 1 to 2GB more VRAM free
Worse answers on short chats after enabling YaRNStatic YaRN affects short textsTurn YaRN off unless you need more than the native context
Answers miss facts deep in a long documentLong-context accuracy falls offPut the question last, ask for quotes, or use retrieval
Cache quantisation has no effect in OllamaFlash attention not active on your deviceCheck the logs; set OLLAMA_FLASH_ATTENTION=1
16

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.

17

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.