Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  Local LLM Stack
Handbook~15 min readInferenceworked math + runnable code
The Local LLM Stack Handbook

Run the model
on your machine.

You can run a capable LLM on your own laptop — no API key, no per-token bill, no data leaving the building. Tools like ollama and llama.cpp make it a one-line command. But whether a given model actually runs on your hardware isn't magic; it's one small equation about memory. Master that formula and you can look at any model — "7B," "70B," "4-bit" — and know instantly whether it'll fit your GPU, and what quantization it'll take to get there. This handbook is that equation and the stack around it.

01

Local means memory

Running an LLM locally is appealing for concrete reasons: privacy (your data never leaves the machine), no per-token cost, offline operation, and full control over the model and its version. The tooling has gotten easy — with ollama you pull and run a model in one command. So the question is rarely "how do I run it?" but "can I run it — will it fit?"

And "fit" is almost entirely about memory. The model's weights have to live in your GPU's VRAM (fast) or your system RAM (slower, CPU inference), alongside the KV cache and some overhead. Compute matters for speed, but memory is the hard wall: if the weights don't fit, the model simply won't load. The good news is that memory is trivially predictable — there's a single formula, and once you know it, hardware planning for local LLMs becomes arithmetic you can do in your head.

The one-sentence version

Whether a model runs locally is set by memory: weight memory = parameters × bits per weight ÷ 8, so quantizing to 4-bit shrinks a 7B model from 14GB to 3.5GB and it fits a consumer GPU.

02

The fit equation

Every weight in a model is a number stored in some number of bits. So the memory for the weights is just: how many weights, times how many bits each, converted to bytes. That's the whole equation — memory = params × bits / 8 — and it tells you nearly everything about what fits.

Work an example. A 7-billion-parameter model at 16-bit (half precision, the usual training format) is 7×10⁹ × 16 / 8 = 14 GB just for weights. That won't fit an 8GB consumer GPU. The same model at 4-bit is 7×10⁹ × 4 / 8 = 3.5 GB — comfortable, with room for the KV cache. A 70B model at 4-bit is 35GB, needing a big GPU or CPU offload. Nothing here is mysterious: read off the parameter count and the bit-width, multiply, divide by eight, and you know. The lever that turns "doesn't fit" into "fits" is the bit-width — which is quantization.

03

Quantization fits it

Quantization stores each weight in fewer bits. A model trained in 16-bit can be compressed to 8-bit, 4-bit, or even lower, and because memory scales linearly with bit-width, dropping from 16 to 4 bits cuts the footprint to a quarter. That's the difference between a model that won't load and one that runs smoothly on a laptop GPU.

Same 7B model, different quantization
fp1616 bits/weight → 14 GB — needs a 16GB+ GPU.
int88 bits/weight → 7 GB — fits many GPUs, near-lossless.
4-bit4 bits/weight → 3.5 GB — fits consumer GPUs; the popular sweet spot.
Trade-offFewer bits = smaller + faster, at a small and usually acceptable quality cost.

There's a quality cost — cruder weights lose a little precision — but modern 4-bit schemes (like the QLoRA-era NF4 and the GGUF K-quants) are remarkably good, and 4-bit is the default for local inference because the trade is usually excellent. Lower still (3-bit, 2-bit) exists for squeezing the largest models onto limited hardware, with steeper quality loss. The practical workflow: pick the largest model whose 4-bit size fits your memory, and step up the bit-width only if you have room to spare and want the last bit of quality.

04

The memory math

The weight-memory formula, with parameters P and bits per weight b:

memoryweights  =  P · b / 8  bytes   ⟹   7B at 4-bit = 7×10⁹ · 4 / 8 = 3.5 GB

Linear in both size and bit-width. Halve the bits, halve the memory; double the parameters, double the memory.

To decide if a model fits, compare weight memory (times an overhead factor for the KV cache, activations, and runtime) against your available VRAM:

fits  ⟺  ( P · b / 8 ) · overhead  ≤  VRAM   (overhead ≈ 1.2 for a rough estimate)

7B 4-bit ≈ 3.5GB × 1.2 = 4.2GB fits an 8GB GPU; 7B fp16 ≈ 16.8GB doesn't. The runnable version below computes model memory, checks the fit, and shows quantization's savings.

RUN IT YOURSELF

Will it fit?

Local LLM planning is one multiplication. Weight memory in GB is parameters (in billions) times bits per weight over 8 — so a 7B model is 14GB at fp16 and 3.5GB at 4-bit. Quantization scales memory linearly, so 4-bit is a quarter of fp16, and that's what turns a model that won't load into one that fits an 8GB GPU (with a bit of overhead for the KV cache). A bigger model needs proportionally more. Change the parameter count, bit-width, or VRAM and see what runs on your machine.

CPython · WebAssembly
05

The stack

The local ecosystem is a few layers, from raw engine to friendly app:

LayerWhat it is
llama.cppThe optimized C/C++ inference engine — runs quantized models on CPU/GPU; defines the GGUF format.
GGUF + quantsThe model file format with many quantization levels (Q4_K_M, Q5, Q8…) — pick the one that fits.
OllamaA convenience layer: pulls models, sets defaults, exposes a CLI and local API in one command.
RuntimesOthers too — MLX (Apple silicon), vLLM (server-grade), LM Studio (GUI) — same memory rules.
Your appTalks to the local API just like a hosted one — swap the base URL, keep the code.

The practical path: use ollama to run a model with one command when you want convenience; drop to llama.cpp (or vLLM for serving) when you want control over quantization, context length, or throughput. On Apple silicon, unified memory means the "VRAM" is your system RAM, so the same formula applies to a larger pool. Whatever the runtime, the memory equation is identical — it's a property of the model and the bit-width, not the tool. And because local runtimes expose an OpenAI-compatible API, pointing an app at a local model is often just changing the base URL, which pairs neatly with routing cheap/private traffic to a local model and hard queries to a hosted one.

06

Pitfalls

The first surprise is forgetting the KV cache and context: the weight formula gives the floor, but a long context grows the KV cache, which also lives in memory — a model that fits at 2K context can OOM at 32K. Budget headroom (the overhead factor), and remember longer context costs more memory, not just more compute. The second is over-quantizing: 2-bit or aggressive 3-bit can degrade quality sharply, so don't chase the smallest file — 4-bit (Q4_K_M-class) is usually the floor for good quality, and if it doesn't fit, prefer a smaller model at 4-bit over a bigger model at 2-bit.

Two more. CPU-only is slow: it works (llama.cpp runs on CPU), but tokens-per-second drops a lot without a GPU — fine for light use, painful for heavy. And expecting frontier quality: a 7B–14B local model is genuinely useful, but it won't match the largest hosted models on the hardest tasks, so match the model to the job (local for privacy, cost, and offline; hosted for peak capability). The whole discipline reduces to the one equation: know a model's size and bit-width, compute the memory, compare to your hardware, and you'll never be surprised by a model that won't load again.

Worth knowing

The weight formula is the floor, not the total. Context length grows the KV cache in the same memory, so a model that loads at 2K context can run out at 32K. Leave headroom, and treat a smaller model at 4-bit as better than a bigger model at 2-bit.

By the numbers

Worked example: weights plus the KV cache on an 8 GB GPU

The page’s formula gives the weights. The KV cache is the part that grows with context, and it has its own formula: per token, 2 (K and V) × layers × KV heads × head width × bytes. Take an 8 GB GPU and a 7B model shaped like Llama 2 7B (32 layers, 32 heads of width 128, cache stored at 16-bit). Trace it row by row.

StepQuestionArithmeticAnswer
17B weights at fp167 × 16 ÷ 814 GB: does not load
27B weights at 4-bit7 × 4 ÷ 83.5 GB
3KV cache per token2 × 32 × 32 × 128 × 2 bytes524,288 bytes ≈ 0.52 MB
4Context 2,048 tokens0.52 MB × 2,048 = 1.07 GB4.57 GB total: fits
5Context 8,192 tokens0.52 MB × 8,192 = 4.29 GB7.79 GB total: on the edge, runtime overhead tips it over
6Context 32,768 tokens0.52 MB × 32,768 = 17.2 GB20.7 GB total: fails, the cache is 5× the weights
7Switch to an 8B model with 8 KV heads (Llama 3 8B shape)2 × 32 × 8 × 128 × 2 bytes131,072 bytes per token, a quarter of step 3
88B at 4-bit, 32,768 tokens4 GB weights + 4.29 GB cache8.29 GB: just over
9Store the cache at 8 bits instead of 164 GB + 2.15 GB6.15 GB: fits, with room for overhead

Step 6 is the page’s “fits at 2K, fails at 32K” warning with numbers on it. The weights did not change; the cache grew linearly with context until it dwarfed them. Even at 2,048 tokens the cache is already 31% of the 4-bit weights for this full-attention shape, so the page’s 1.2 overhead factor is a short-context rule. Steps 7 to 9 are the two levers that shrink the cache itself. Grouped-query attention shares each K/V head across several query heads, which you get by choosing the model. Cache quantization (llama.cpp can store K and V at q8_0) you set at launch. Before you blame the weights, check the KV-head count in the model card.

Trade-offs

Cost and trade-offs: where your tokens per second come from

Here Time is the ceiling on generation speed for one user. Producing each token reads every weight once, so decoding is limited by memory bandwidth: tokens/s ≤ bandwidth ÷ bytes of weights. Space is the memory it needs. The bandwidths are illustrative: 500 GB/s for a GPU, 80 GB/s for system RAM.

CaseTimeSpaceWhy
7B at 4-bit, all on the GPU≤ 143 tok/s (500 ÷ 3.5)3.5 GB + KV cacheThe best case. Real speeds land below the ceiling, but the ranking holds.
7B at 8-bit on the GPU≤ 71 tok/s (500 ÷ 7)7 GB + KV cacheTwice the bytes read per token, so half the ceiling. Near-lossless quality is what you buy.
7B at 4-bit, CPU only≤ 23 tok/s (80 ÷ 3.5)3.5 GB of system RAMUsable for chat. Reading a long prompt is compute-bound, and there the CPU falls further behind.
70B at 4-bit, CPU only≤ 2.3 tok/s (80 ÷ 35)35 GB of system RAMIt loads, so it “works”. At this speed a 500-token answer takes almost four minutes.
70B at 4-bit, 24 GB on GPU, 11 GB in RAM≈ 5.4 tok/s (24 ÷ 500 + 11 ÷ 80 = 0.19 s per token)24 GB VRAM + 11 GB RAMEvery token passes through all the layers, so the slow share dominates. A third of the model in RAM takes almost three quarters of the time.
Longer contextEach new token also reads the whole cacheKV cache grows linearly with contextLong chats slow down as well as filling memory. The cache from step 6 above is read on every token.

The pattern: memory decides whether a model runs, and memory bandwidth decides how fast it runs. That is why dropping from 8-bit to 4-bit roughly doubles decode speed as well as halving the footprint. It is also why partial offload is a trap. Pushing even a small slice of the model into system RAM drags the whole model down toward CPU speed.

In the wild

Where this shows up in production and interviews

  • Serving a team, not one person

    Each concurrent request holds its own KV cache. Ten users at 8,192 tokens on the Llama 3 8B shape need 10 × 1.07 = 10.7 GB of cache, more than the 4 GB of weights. vLLM’s PagedAttention exists for this: it hands out cache memory in small pages as sequences grow, instead of reserving the maximum context for every request up front.

  • Air-gapped and regulated sites

    A local model has one dependency, the weights file. Teams that cannot send data out pin a specific GGUF by checksum, test it like any other release, and upgrade on purpose. Nothing changes under them the way a hosted model alias can.

  • Developer laptops and CI

    Ollama serves its API on localhost:11434, so a test suite can run against a small local model with no key and no network. It is useful for plumbing tests: does the prompt template render, does the parser handle the output. It is not a stand-in for evaluating the hosted model you ship.

  • The interview version

    “Can we run a 70B model on two 24 GB GPUs?” Do it out loud. 70 × 4 ÷ 8 = 35 GB, × 1.2 = 42 GB, under 48 GB, so yes at 4-bit, split across both cards. Then name what the 1.2 hides: the KV cache for your context length and concurrency. The interviewer is checking that you know the cache, not just the weights, is what fills a server.

Self-check

Check yourself

You have an 8 GB GPU. Does a 13B model fit at 8-bit? At 4-bit?

At 8-bit the weights alone are 13 × 8 ÷ 8 = 13 GB, so no. At 4-bit they are 6.5 GB, and × 1.2 = 7.8 GB, which squeaks under 8 GB by the page’s rule. In practice that leaves almost nothing for the KV cache, so keep the context short or choose a smaller model.

You raise the context from 4K to 16K on a model that already loads. Which part of memory grows, and by how much?

Only the KV cache, and it grows 4×, because it is linear in tokens. The weights are unchanged. On the Llama 2 7B shape that is 2.15 GB of cache becoming 8.6 GB, which is why a model that loads fine can run out of memory partway through a long conversation.

With 24 GB, would you run a 70B model at 2-bit or a 32B model at 4-bit?

Both fit on paper: 70 × 2 ÷ 8 = 17.5 GB and 32 × 4 ÷ 8 = 16 GB. Take the 32B at 4-bit. The page’s rule is that 4-bit is about the floor for good quality, and 2-bit degrades sharply. The 32B also leaves more room for the cache.

A model runs at 30 tok/s at 8-bit, entirely on the GPU. Predict the speed at 4-bit.

Roughly double, near 60 tok/s. Decoding one user’s tokens is bound by memory bandwidth, and the 4-bit file is half the bytes read per token. Expect a little less than 2× in practice, because the KV cache reads and per-token overhead do not shrink.

Frequently asked

Quick answers

What do you need to run an LLM locally?

Mainly enough memory: the weights must fit in VRAM or RAM. Weight memory = params × bits per weight ÷ 8, plus KV cache and overhead.

How does quantization help?

Fewer bits per weight shrinks memory linearly — a 7B model is 14GB at 16-bit but 3.5GB at 4-bit, which fits a consumer GPU.

Ollama vs llama.cpp?

llama.cpp is the low-level engine (and GGUF format); ollama is a convenience layer that manages models and exposes a simple API.

Why run locally?

Privacy, no per-token cost, offline use, and control — at the cost of being limited to models that fit your hardware.

Finished this one? 0 / 209 Handbooks done
NextThe On-Device AI Handbook Next on the “Serving models, and paying for them” ladder · rung 13 of 13

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Handbooks