Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

How LLM Inference Works: a Deep Dive with llama.cpp

Tested 2026-11-29 on macOS 26.6.2, MacBook Pro, Apple M4 Pro, 48 GB; llama.cpp on the GPU (Metal, MTL0) · with llama.cpp v1.1.0 (build b12774, macOS arm64 release), Qwen2.5-0.5B-Instruct GGUF (fp16, Apache-2.0), Python 3.9.6 (macOS), WikiText-2 raw test set (CC BY-SA) · by Vivek Sahu (@viv1) · CC BY 4.0

You type a question into a chat assistant and press Enter.

Before any model sees it, your question has become 36 numbered pieces of text: tokens.

A model of 630 million fixed numbers then runs once and scores all 151,936 tokens it knows as the possible next one. Even this easy question doesn’t make it sure: given The capital of France is, its favourite, Paris, gets a probability of 0.395.

One token is picked, appended, and the whole thing runs again, until the model picks its end token: the one-sentence answer took 8 passes. Everything else you have noticed about chat models (the streaming, the different answer each time, the confident mistakes, the cost per token) follows from that loop.

This deep dive opens that loop on your own Mac, with llama.cpp and a small open model, and measures each part. You need to be able to program a little and to have used a chat assistant; nothing about how models work is assumed.

By the end you’ll be able to:

  • explain what a model computes at each step, and count the tokens a text costs
  • choose sampling settings (temperature, top-k, top-p, min-p, seed) knowing what each does to the probabilities
  • explain why the prompt is processed fast and the answer slowly, and how much memory the KV cache takes
  • quantize a model yourself and measure what it costs in quality and gains in size and speed
  • recognise and fix the classic failures: a missing chat template, a sampler set to produce noise, a context too large for memory

How it works

Each lesson starts with a problem you recognise, shows the mechanism in an animation you step through, and puts the production-level detail in Go deeper boxes. In the practicals you work in your own terminal: tasks with requirements, a hint, a hidden solution, a prediction to make before you look, something to break on purpose, and ./lab check pN to check your work (./lab setup pN fetches what isn’t the topic, ./lab reset pN starts you over). You need a Mac with Apple Silicon, Python 3 (the first time you run python3, macOS may offer to install its Command Line Tools: accept, it takes a few minutes) and a few GB free on disk (the model is 1.27 GB; the capstone adds smaller copies of it).

Path

LessonPracticalYou’ll be able to
1 · What happens when a model answerssay what one forward pass computes
2 · Picking each token: the loop and sampling0 · Your llama.cpp, token by tokencount tokens, see each step’s candidates, set the sampler
3 · Prefill and decode, and the KV cacheexplain time to first token, tokens per second and the cache’s size
4 · In production: quantization and fitting a model in memory1 · Your inference report card (capstone)quantize, measure and pick a model for a memory budget
What your folder will look like
 ~/llm-inference-deep/
 ├── lab            the lab script: ./lab setup|check|reset pN, ./lab stop
 ├── lab.log        every command the lab script ran, with its output
 ├── llama/         llama.cpp b12774 (llama-cli, llama-tokenize, llama-server, ...)
 ├── models/        qwen2.5-0.5b-instruct-fp16.gguf (1.27 GB), and the ones you quantize
 ├── data/          Practical 1: WikiText-2's test set, for llama-perplexity
 ├── report/        Practical 1: your measurements, report.py and your pick
 ├── tokens.sh      Practical 0: counts tokens
 └── probs.py       Practical 0: each step's top candidates