Skip to content
AgenticAIWiki
  1. Learn
  2. Part 5
  3. 30. Local inference
  4. 30.2

Lesson 30.2 · 2 of 8 · 5 min read · Reviewed 24 Nov 2026

Quantization

By the end, you can

  • Explain quantization.
  • Choose a precision for local use.

Memory maths

memory for weights = parameters x bytes per parameter

8B model:   16-bit  8B x 2 bytes   = ~16 GB
            8-bit   8B x 1 byte    =  ~8 GB
            4-bit   8B x 0.5 byte  =  ~4 GB   (+ some overhead)

On top of weights, you need memory for the KV cacheKV cache: Stored attention keys and values for tokens already processed, so they are not recomputed for each new token., which grows with context length and the number of parallel requests.

Quality vs size

Precision Size vs 16-bit Typical quality
16-bit 1x Reference
8-bit ~0.5x Very close
~4-bit ~0.25x Small loss; common for local use
~3-bit and below Smaller Noticeable loss, varies by model

Larger models tolerate quantizationQuantization: Storing model weights with fewer bits, such as 4 or 8 instead of 16, to cut memory and speed up inference. better: a 4-bit 70B model often beats a 16-bit 8B one.

Formats and methods

  • GGUF: a file format used by llama.cpp and tools built on it, with many quantization levels.
  • GPU-oriented methods (several exist) target fast inference on GPUs.
  • The same idea compresses vectors in vector databasesVector database: A database built to store embeddings and find the nearest ones to a query quickly. (22.6).

Test it

Run your evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. (Part 3) at two or three precisions on your hardware. Pick the smallest that holds quality, and re-check when you change the model.

Key points

  • Quantization stores weights with fewer bits, such as 8 or 4 bits instead of 16.
  • Fewer bits means less memory and often faster inference, with some loss in quality.
  • 8-bit is usually close to full quality; 4-bit is a common sweet spot for local use; below that, quality drops faster.
  • Formats like GGUF package quantized models for local runtimes.
Hands-on labRuns in the Playground

Size a model at four precisions

Calculate the memory a model needs at 16, 8, 4 and 3 bits, and see what fits.

You will have: A sizing table for an 8B and a 70B model, with which memory sizes each fits.

Steps

  1. Fill in weights_gb(): parameters times bytes per weight.
  2. Run it and check the 8B row at 16-bit is 16 GB.
  3. Add a loop that finds the largest precision at which the 70B model fits in 48 GB.
  4. Change OVERHEAD to 1.5 for long contexts and see which rows stop fitting.

Done when

  • 8B at 4-bit shows 4.0 GB of weights.
  • 70B at 16-bit fits in neither 16 nor 48 GB.
  • The largest precision for 70B in 48 GB is 4-bit.

Open in the Playground No API key needed.

Starter code (Python)
# Size an 8B and a 70B model at four precisions, and check what fits in 16 GB and 48 GB.
PRECISIONS = {"16-bit": 2.0, "8-bit": 1.0, "4-bit": 0.5, "3-bit": 0.375}
OVERHEAD = 1.2  # about 20% extra for runtime and a short KV cache


def weights_gb(params_billion, bytes_per_weight):
    # TODO: memory for weights = parameters x bytes per parameter (in billions, so the answer is in GB)
    return 0.0


for params in (8, 70):
    for name, bpw in PRECISIONS.items():
        need = weights_gb(params, bpw) * OVERHEAD
        fits = [str(mem) for mem in (16, 48) if need <= mem]
        print(f"{params}B {name:<6} weights {weights_gb(params, bpw):5.1f} GB, with overhead {need:5.1f} GB, fits in: {', '.join(fits) or 'neither'}")

# TODO: print the largest precision at which the 70B model fits in 48 GB.
Show a solution
# Size an 8B and a 70B model at four precisions, and check what fits in 16 GB and 48 GB.
PRECISIONS = {"16-bit": 2.0, "8-bit": 1.0, "4-bit": 0.5, "3-bit": 0.375}
OVERHEAD = 1.2  # about 20% extra for runtime and a short KV cache


def weights_gb(params_billion, bytes_per_weight):
    return params_billion * bytes_per_weight


for params in (8, 70):
    for name, bpw in PRECISIONS.items():
        need = weights_gb(params, bpw) * OVERHEAD
        fits = [str(mem) for mem in (16, 48) if need <= mem]
        print(f"{params}B {name:<6} weights {weights_gb(params, bpw):5.1f} GB, with overhead {need:5.1f} GB, fits in: {', '.join(fits) or 'neither'}")

for name, bpw in PRECISIONS.items():  # listed from largest to smallest
    if weights_gb(70, bpw) * OVERHEAD <= 48:
        print("Largest precision for 70B in 48 GB:", name)
        break
Quick checkA 14B model at 16-bit doesn't fit your GPU. What's the usual first fix?
How sure are you?
Pick an answer and how sure you are