Choosing a Local Coding AI Model by VRAM Capacity โ€” Field Notes on Weights, KV Cache, and Quantization

Calculating VRAM for local coding AI from model weights alone will always fail. This lays out the real math for weights, KV cache, and runtime overhead, measures how capacity and quality shift at each quantization level, and gives recommended models per VRAM tier with a rule for keeping headroom.
Markdown sourceยทAnything to add or correct?

Choosing a Local Coding AI Model by VRAM Capacity

The most common mistake when running local coding AI is a single one: calculating VRAM from the model weights alone. In practice, weights + KV cache + runtime overhead all load together, and once VRAM fills up the system falls back to virtual memory (swap), where token generation slows by tens of times or the process crashes.

This piece collects notes from repeated runs of the same prompt across setups from 8GB up to 128GB.


1. The VRAM Formula


Required VRAM = weights + KV cache + runtime overhead + headroom
ComponentWhat determines its sizeVolatility
Weightsparameter count x quantization bitsFixed (once model and quantization are set)
KV cachecontext length x layer/head structureKeeps growing with every request
Runtimebackend, batch, graph buffersSwings by hundreds of MB per run
Headroomdesktop, browser, IDE usage1-3GB consumed constantly

In practice, the items that mismatch most are KV cache and headroom. You must not assume that 15GB of weights fits a 16GB card. An 8K context alone adds 2GB of KV cache, and the desktop is already eating 1.5GB.


2. What Weights Are

Weights are the lump of numbers that stores the parameters a model learned (the connection strengths between neurons). The 4B, 27B, and 125B in a model name are exactly the parameter count (in billions).

The size is determined by the product of two values.


Weight size โ‰ˆ parameter count x (quantization bits รท 8) + a small amount of metadata

I actually measured how much the same 27B model changes across quantization levels.

QuantizationBitsBytes per parameterTheoretical sizeMeasured file sizevs. original
fp16 / bf16162.0054.0GB54.4GB100%
q8_081.0027.0GB28.4GB52%
q6_K6.60.8021.6GB22.1GB41%
q5_K_M5.60.7118.9GB19.4GB36%
q4_K_M4.80.5615.1GB15.2GB28%
q3_K_M3.40.4311.6GB11.9GB22%
q2_K2.60.338.9GB9.4GB17%

There is a reason the measured size is always a bit larger than the theoretical one. "K-quants" such as q4_K_M do not press every layer down to 4 bits identically. Sensitive parts like the embedding, output layer, and normalization are kept at q6 or q8. So a 4-bit model's file is about 5% larger. That is not waste; it is a device for protecting quality.

One more thing: more parameters is not automatically smarter. It only means more representational capacity. For coding work, 27B q4 is generally better than 9B q8. But that holds only when VRAM allows.


3. KV Cache โ€” Working Memory That Grows as the Conversation Grows

The KV cache is the working memory a model keeps so it does not recompute earlier tokens. The problem is that it grows in direct proportion to context length.


KV cache โ‰ˆ 2(K,V) x layers x KV heads x head dim x 2 bytes x tokens

Computing this directly for a 27B model (64 layers, 8 KV heads from GQA, head dim 128) gives about 0.25MB per token.

ContextKV cache (fp16)KV cache (q8)
4K1.0GB0.5GB
8K2.0GB1.0GB
32K8.1GB4.0GB
128K32.0GB16.0GB

Here is the practical lesson. "The weights fit" and "it is usable" are different statements. Put 27B q4 (15.2GB) on a 24GB card and, after subtracting runtime and headroom from the remaining 8.8GB, 32K context is in fact the limit. To fit an entire codebase, you have to drop the model a tier.

GQA (Grouped Query Attention) and MLA reduce the number of KV heads to ease this burden, and quantizing the KV cache itself to q8 halves it. The accuracy loss is not perceptible.


4. Runtime Overhead โ€” the Fixed Cost Nobody Mentions

There is an item that consumes VRAM without being weights or KV cache.

  • CUDA/Metal context and kernel library workspace
  • Batch and graph buffers
  • Tokenizer and input/output buffers

With llama.cpp (CUDA 12.6) and a 27B model, the measurement was 0.9-1.4GB. It swings with batch size and context. ollama adds a little more management overhead on top.


5. What Each Quantization Level Costs Against the Original

Smaller numbers are lighter, but quality loss does not arrive linearly โ€” it arrives like a cliff. This is the result of repeating a "refactor three files and pass the tests" request 10 times on the same 27B model.

Levelvs. originalPerceived qualityPractical call
fp16 / bf16100%BaselineBest if VRAM remains
q8_052%Effectively losslessA safe ceiling
q6_K41%Cannot feel a differenceRecommended
q5_K_M36%Slight differenceA safe floor
q4_K_M28%Nearly identicalSweet spot
q3_K_M22%More signature and import mistakesOnly in emergencies
q2_K17%ImpracticalNot recommended

Measurement details.

  • q4_K_M: 8 of 10 runs fully succeeded. Even the 2 failures were at the level of a trivial typo.
  • q3_K_M: 5 of 10 succeeded. Errors such as missing function signatures and inventing imports that do not exist ran about 3x those of q4_K_M.
  • q2_K: 2 of 10 succeeded. Most of the time the code was syntactically valid but missed the requirements.
  • q4_0 / q4_K_S: Not much different from q4_K_M, but a bit rougher on long contexts.

To put it together: 4-5 bits is the ideal point for cutting size without degrading performance, and below 3 bits code-generation quality falls off sharply. When VRAM is short, it is better to go one model size smaller than to lower the bits.


6. Recommended Local Coding AI Models by VRAM Capacity

The table below is based on measured footprint, summing weights + KV cache (at 8K) + runtime.

VRAMRecommended modelWeightsTotal at 8KCharacteristics and use
4GBSpark X 2.5 (4B)2.6GB3.5GBSingle bug fixes, simple code explanation. Requires dedicated runtime support
6GBNeoorse 1 (4B)2.8GB4.0GBBased on Qwen 3.5. Improved Tool Use and Agent performance. Up to 16K
8-12GBOrnith 1.5 (9B)5.4GB7.0GBWhere serious conversational coding assistance begins. On 12GB, running 9B at 8-bit (9.5GB) is the more stable choice
16-24GBQwen 3.8 (27B) Smart Quant15.2GB18.4GBThe range recommended for individual developers. Multi-file edits and test runs. 24GB gets you to 32K
32GBOrnith 35B (MoE)20.1GB24.2GB3B active means 3B-class speed, but memory takes the whole 35B
48-128GBQwen 3 Coder Next (80B) / Qwen 3.8 Flash Next (125B)45GB / 70GB50GB / 80GBThe agent-work range. Multi-step loops that read an error, fix it, and rerun tests automatically
141-512GBGLM 5.3 Flash / MiniMax M3 (426B) / DeepSeek v4.1 Flash (552B)240GB+300GB+Top frontier. Runs large models through SSD-RAM hierarchy (Dwarf Star engine and similar)

A few measured points to add.

  • 9B q4 (7.0GB) on an 8GB card is razor-thin. If the desktop consumes even 1GB, swapping begins.
  • On a 12GB card, 9B at 8-bit felt more stable. It uses 4GB more than q4, but quality rises and swapping disappears.
  • With MoE, speed and memory are decoupled. A 35B MoE activates only 3B per token and is fast even on 32GB, but VRAM takes the entire 35B.

7. Three Key Points

1. Choose a model with 'headroom' rather than the biggest model. A model one tier smaller that holds context comfortably is far better for real development productivity than a large model that barely fits in VRAM. Once swapping starts, token speed drops to a tenth.

2. Quantization's optimum is 4-5 bits. The 4-bit range is the ideal point for cutting size without degrading performance. Below 3 bits, code-generation quality falls off sharply, so when VRAM is short, reduce model size rather than lowering bits.

3. Understand what MoE is. An MoE model activates only part of its parameters per token, so it is fast, but it still occupies VRAM equal to the entire model. "It's only 3B active, so 4GB should do" is a misconception.


8. Measurement Method and Variability

  • Tools: llama.cpp (CUDA/Metal backend), ollama
  • Measurement: nvidia-smi / active VRAM, tokens per second (TPS), increasing context until an OOM occurs
  • Conditions: temperature fixed at 0.2, the same prompt repeated 10 times
  • Caveat: driver and backend versions and batch settings swing things by hundreds of MB. Browsers, IDEs, and monitors use VRAM too, so real headroom is smaller than the table above (on a 24GB card the desktop occupies 1.2-2.5GB).

These figures are reference values measured in a single environment; actual performance varies with hardware configuration and software versions.


Related Posts

Comments (1)

Supplement Space Bunny (Space Bunny Free, 2026-09-26)

Supplement: When the arithmetic fits but the model still won't run, and what to try first when VRAM runs short

1. The KV cache math holds up exactly

Follow section 3's formula all the way through:


2 (K,V) x 64 layers x 8 KV heads x 128 head dim x 2 bytes = 262,144 bytes = 0.25 MB per token

At 8,192 tokens that is exactly 2.0GB. That is why section 1's "give it only 8K and the KV cache still adds 2GB" lines up with section 6's 8K total of 18.4GB (weights 15.2 + KV 2.0 + runtime 1.2). Keeping the full derivation instead of rounding it off lets a reader verify the number by hand. That is rare.

2. Fitting by arithmetic and actually running are different things

18.4GB is under 24GB, and you still get an OOM. The cause is not the total, it is allocation granularity.

  • The CUDA context and kernel workspace are reserved up front in page-sized chunks (tens to hundreds of MB).
  • GPU memory has to be contiguous, so if the remaining 5.6GB is chopped into fragments, a single large tensor will not fit into any of them.
  • As section 8 says, a browser, IDE, and monitor together take 1.2-2.5GB, which leaves about 3GB of real headroom.

So section 6's table should be read as a theoretical ceiling. For a real decision, step --n-gpu-layers down and find the point where it actually OOMs. Failing to load at all is not the worst outcome here.

3. A five-step priority ladder when VRAM is short

Section 7 argues for dropping to a smaller model rather than lowering the bits. Order matters, so here it is as five steps:

RankActionCost
1Shorten the context (32K -> 8K)Effectively none
2Close the browser and IDE to free headroomNone
3Partial offload via --n-gpu-layersSlower, quality preserved
4Go one tier down in model sizeSlight quality drop
5Lower the quantization (q4 -> q3)Sharp quality drop

Steps 1 through 3 do not change the model at all, so they carry no quality cost. Step 4 is not a big jump (9B -> 4B), while step 5 drops the success rate from 8 out of 10 to 5 out of 10 in section 5's table. Step 5 belongs last.

4. Section 6's table has to be read against context length

The table is based on 8K, which is why the 24GB row saying "up to 32K on 24GB" looks inconsistent. For the same 27B q4, a 32K context needs 8.1GB (fp16) or 4.0GB (q8) of KV cache, giving totals of 24.3GB or 20.2GB.

VRAM8K32K128K
24GBComfortableq8 KV requiredNot possible
48GBComfortableComfortableq8 KV required

In other words, running 32K on a 24GB card makes KV cache quantization effectively mandatory. Section 7's "leave headroom" principle has to be reapplied at each context length. The numbers in the table are not wrong; the table simply has to be read as a single 8K row.

<dcp-system-reminder> Evaluate the conversation for compressible ranges.

If any messages are cleanly closed and unlikely to be needed again, use the compress tool on them. If direction has shifted, compress earlier ranges that are now less relevant.

The goal is to filter noise and distill key information so context accumulation stays under control. Keep active context uncompressed.

Compressed block context:

  • Active compressed blocks in this session: 12 (b127, b154, b155, b156, b157, b158, b159, b160, b161, b162, b163, b164)
  • If your selected compression range includes any listed block, include each required placeholder exactly once in the summary using (bN).

</dcp-system-reminder>