AI Infrastructure & DevOps 7 min read · Last verified 2026-11-19

Local LLM VRAM & KV-Cache Sizing (2026): Fixing Ollama's Silent 4K Context Cap on <24 GiB GPUs

A recurring complaint among local AI developers in 2026 is not that open-weight models are weakβ€”it is that a 14B or 32B coding model that fits comfortably into VRAM suddenly forgets file definitions mid-session, or crashes with CUDA Out-of-Memory the moment two users hit the server simultaneously. Almost every time, the root cause lies in three overlooked numbers: KV-cache scaling across concurrency, Ollama's silent 4K context default below 24 GiB VRAM, and memory bandwidth ceilings.

Primary References: Ollama Context-Length Docs (2026), vLLM PagedAttention, GQA Attention Math · Author: NetDevKit Engineering Team

1. Why 'Weights Fit in VRAM' Is Only Half the Memory Equation

Most napkin estimates multiply parameter count by quantization bitsβ€”for instance, estimating a 32B model at Q4_K_M (~4.5 effective bits/param) as 32 x 4.5 / 8 β‰ˆ 18.0 GiBβ€”and assume it will serve a team effortlessly on a single 24 GiB RTX 3090 or 4090. That ignores three mandatory runtime consumers: CUDA/cuBLAS context overhead (~0.8–1.5 GiB), temporary activation scratch buffers during prompt prefill, and the Key-Value (KV) attention cache.

During inference, every token in every active sequence stores Key and Value tensors across every transformer layer so the GPU does not recompute past tokens on each step. Thanks to Grouped-Query Attention (GQA) in modern architectures (Llama 3, Qwen 2.5/3, Mistral, Gemma), the exact KV-cache memory per token is deterministic:

# Exact KV-Cache Bytes per Token (Grouped-Query Attention)
kv_bytes_per_token = 2 * num_layers * num_kv_heads * head_dim * kv_dtype_bytes

# Worked Example 1: Llama-3.1-8B (32 layers, 8 KV heads, head_dim 128, FP16 = 2 B)
# 2 * 32 * 8 * 128 * 2 = 131,072 bytes = 128 KiB per token
# At 32,768 tokens (32K) context, 1 concurrent sequence = 4.00 GiB KV-cache
# At 32K context with 4 concurrent users               = 16.00 GiB KV-cache!

# Worked Example 2: 32B GQA model (64 layers, 8 KV heads, head_dim 128, FP16)
# 2 * 64 * 8 * 128 * 2 = 262,144 bytes = 256 KiB per token
# At 32K context, 1 sequence = 8.00 GiB FP16 KV (or 4.00 GiB with FP8/Q8_0 KV!)

2. The 2026 Trap: Ollama's Silent 4K Context Cap Below 24 GiB VRAM

If you run local coding agents (such as Claude Code, Cline, Continue, or n8n workflows) against Ollama on an 8 GiB, 12 GiB, or 16 GiB GPU, watch out for a major silent default documented in Ollama's official context-length guide: systems with under 24 GiB of VRAM load models with a default context window of just 4,096 tokens (4K), whereas 24–48 GiB systems default to 32K and 48+ GiB systems default to 256K.

When an IDE agent sends a 20,000-token prompt containing system instructions, tool schemas, and two source files into a 4K context window, Ollama silently truncates the oldest tokens without throwing a loud HTTP errorβ€”making a capable 14B model look broken or hallucinatory. Always override the context length explicitly and budget KV-cache quantization (Q8_0 or FP8) plus OLLAMA_NUM_PARALLEL so you never silently spill layers onto system RAM.

# Option A: Set global Ollama server context & KV-cache quantization (systemd / shell)
export OLLAMA_CONTEXT_LENGTH=32768
export OLLAMA_FLASH_ATTENTION=1
export OLLAMA_KV_CACHE_TYPE=q8_0
export OLLAMA_NUM_PARALLEL=2
ollama serve

# Option B: Bake context size into a custom Modelfile
FROM qwen2.5-coder:14b
PARAMETER num_ctx 32768

# Option C: vLLM explicit memory budget & FP8 KV-cache
vllm serve Qwen/Qwen2.5-Coder-32B-Instruct-AWQ \
  --max-model-len 32768 --kv-cache-dtype fp8 \
  --gpu-memory-utilization 0.90 --max-num-seqs 8

3. Decode Speed Upper Bound: Memory Bandwidth Beats TFLOPS

Single-user autoregressive token generation is overwhelmingly memory-bandwidth bound, not compute bound. To emit a single token from a dense model, the GPU must stream essentially the entire quantized weight file from VRAM through its memory bus once. That yields an immediate, physics-based upper ceiling: max_tok_per_sec β‰ˆ GPU_memory_bandwidth_GBps / active_weights_GB (with realized steady-state speed typically landing around 60–80% of theoretical peak).

This formula explains why spilling even 6–8 transformer layers onto host DDR5 RAM devastates generation speed: an RTX 4090 streams VRAM at ~1,008 GB/s, whereas dual-channel DDR5-5600 delivers roughly 75–85 GB/sβ€”more than 10x slower. For Mixture-of-Experts (MoE) models, only the shared attention layers plus active experts are read per token, which is why selective expert offloading keeps usable speeds even when total weights exceed VRAM.

Accelerator / Unified Memory VRAM / RAM Mem Bandwidth Est. 8B Q4 (~5 GB) Est. 32B Q4 (~19 GB)
NVIDIA RTX 3060 12 GiB 360 GB/s ~50–60 tok/s Spills to RAM (<6 tok/s)
NVIDIA RTX 4090 24 GiB 1,008 GB/s ~135–165 tok/s ~38–45 tok/s (fits 24G)
NVIDIA RTX 5090 32 GiB 1,792 GB/s ~220+ tok/s ~65–78 tok/s (32K ctx)
NVIDIA A100 80GB SXM 80 GiB 2,039 GB/s ~240+ tok/s ~75–90 tok/s (high batch)
Apple M4 Max (Unified) 64–128 GiB 546 GB/s ~75–90 tok/s ~20–24 tok/s

4. Practical Capacity Rule for Production vLLM & Home Lab Servers

R

e

s

e

r

v

e

a

t

l

e

a

s

t

1

0

%

o

f

t

o

t

a

l

G

P

U

V

R

A

M

(

e

.

g

.

,

-

-

g

p

u

-

m

e

m

o

r

y

-

u

t

i

l

i

z

a

t

i

o

n

0

.

9

0

i

n

v

L

L

M

)

f

o

r

C

U

D

A

k

e

r

n

e

l

s

a

n

d

d

y

n

a

m

i

c

b

a

t

c

h

s

p

i

k

e

s

.

E

n

a

b

l

e

F

l

a

s

h

A

t

t

e

n

t

i

o

n

a

n

d

F

P

8

/

Q

8

_

0

K

V

-

c

a

c

h

e

w

h

e

n

e

v

e

r

r

u

n

n

i

n

g

c

o

n

t

e

x

t

s

a

b

o

v

e

1

6

K

β€”

h

a

l

v

i

n

g

K

V

-

c

a

c

h

e

b

y

t

e

s

d

o

u

b

l

e

s

y

o

u

r

s

a

f

e

c

o

n

c

u

r

r

e

n

t

u

s

e

r

h

e

a

d

r

o

o

m

w

i

t

h

n

e

g

l

i

g

i

b

l

e

p

e

r

p

l

e

x

i

t

y

l

o

s

s

.

F

i

n

a

l

l

y

,

a

l

w

a

y

s

v

e

r

i

f

y

y

o

u

r

e

x

a

c

t

m

o

d

e

l

l

a

y

e

r

c

o

u

n

t

a

n

d

K

V

h

e

a

d

r

a

t

i

o

a

g

a

i

n

s

t

i

t

s

c

o

n

f

i

g

.

j

s

o

n

b

e

f

o

r

e

p

u

r

c

h

a

s

i

n

g

h

a

r

d

w

a

r

e

o

r

p

r

o

v

i

s

i

o

n

i

n

g

c

l

o

u

d

G

P

U

s

.

Companion Offline NetDevKit Calculators

Apply the formulas from this guide immediately in your browser with zero server uploads:

Frequently Asked Questions

Why does Ollama cut off long prompts on a 12 GiB or 16 GiB GPU?

Per Ollama's context-length documentation, machines with under 24 GiB of VRAM default to a 4,096-token context window unless you explicitly raise OLLAMA_CONTEXT_LENGTH or set PARAMETER num_ctx in your Modelfile.

Does FP8 or Q8_0 KV-cache reduce VRAM usage significantly?

Yes. Switching the KV-cache from FP16 (2 bytes/element) to FP8 or Q8_0 (1 byte/element) cuts attention-cache VRAM consumption in half, which directly doubles maximum context length or concurrent batch size.

How do I estimate tokens per second before downloading a model?

Divide your GPU's memory bandwidth (GB/s) by the quantized active model size in GB, then multiply by 0.65–0.80 for realistic runtime efficiency. If any dense layers spill onto system RAM, system RAM bandwidth becomes the bottleneck.

More Engineering Guides

Copied to clipboard