The whole machine: an AI stack built from scratch, start to finish
A few months ago, ollama run, Stable Diffusion, and “chat with your documents” were magic to me —
things I used without understanding. This is the last note in a sprint to fix that: build every piece of
a modern AI stack from scratch, in plain numpy, each one pinned by a correctness test and explained with
its own real numbers. The capstone ties the text half together into one small service you can call. The
machine isn’t magic anymore. It’s a dozen comprehensible ideas, and I’ve now held every one of them.
What the capstone is
A mini RAG service — the sprint, assembled:
Q: Who wrote Romeo and Juliet?
retrieved passages: [6, 5]
answer: "William Shakespeare wrote the tragedy Romeo and Juliet."
Behind that one line: the question is embedded and the nearest passage retrieved (the vector-index and
RAG weeks), pasted into the prompt (RAG), and the answer generated token-by-token by a GPT-2 I wrote by
hand (inference week), reusing a KV-cache (the caching week), optionally on int8 weights that are 4×
smaller (quantization week). Six weeks of organs, firing in sequence.
The whole arc, in one sentence
A model that reads (the forward pass) → reads fast (a KV-cache) → finds relevant text (a
vector index) → answers from data (RAG, with an honest eval harness) → acts (an agent loop) →
runs at scale (tensor parallelism). Then the extensions: made it fit (quantization), made it
real (a 7B on the laptop), and took a detour into the other architecture family — generating
images (diffusion) and joining senses (a CLIP-style shared space). Eleven builds, one spine.
The honest part
Ask the service “At what temperature does water boil?” and it fumbles — retrieves the wrong passages and
wanders. Why? Its retriever embeds with GPT-2’s mean-pooled hidden states, which are weak (recall@3 ≈
0.58). A pipeline is only as strong as its weakest learned component. The fix isn’t more plumbing;
it’s a better embedder. I’m keeping that failure in the writeup on purpose — the Shakespeare win is the
demo, but the water-boiling miss is the lesson, and the whole sprint was about earning the right to tell
the difference.
What it was really about — three habits, repeated
- Build it from scratch. No framework doing the part you’re trying to understand. If PyTorch’s
autograd or llama.cpp’s kernels do the work, you learn what the work produces, not what it is. - Pin it with a correctness check. A done-bar that catches what the eye forgives. This paid off over
and over: the diffusion schedule that never fully reached noise; the tensor-parallel bias summed twelve
times; the benchmark whose punchline I’d written before measuring, which only showed up at 512 tokens.
Each was invisible to inspection and obvious to a test. - Tell it with real numbers. Every explainer grounded in output the code actually produced — an ASCII
ring emerging from noise, a 4× memory drop with identical text, 100% zero-shot retrieval. Numbers you
can reproduce, not vibes.
The one thing I deliberately skipped
Training. Everything here runs a model; nothing makes one. No backprop, no fine-tuning, no LoRA — the
other half of the field, left for another sprint. Worth being honest that the stack I now understand is
the inference stack; the learning stack is a whole second mountain.
Where this sits
This sprint rhymes with the last one. A year ago I built a distributed database from scratch — storage,
consensus, sharding, a replicated log — to stop treating infrastructure as magic. This did the same for
AI, one layer up: the systems the models run on turned out to be those primitives (sharded weights,
replicated serving, a cache as carried state), now pointed at a transformer. Same method, same payoff —
you only really understand a system once you’ve built the smallest honest version of it yourself.
The machine is no longer a black box. On to the next one.
Part of a series building a modern AI stack from scratch, told visually. (Last time: a CLIP-style multimodal alignment.)