LLM + RoboticsModule 20 · From-scratch code

Six tiny repos that rebuild modern AI from scratch, run and tested

micrograd, minGPT, nanoGPT, llm.c, tinygrad and minimalRL: small, readable code that does the real thing. They're the next rung after Robo-Helper, where the learner wrote everything by hand.

Links sent: micrograd · minGPT · nanoGPT · llm.c · tinygrad · minimalRL. All six are MIT licensed. READMEs saved in sources/20-*-readme.md; stars as of 7 Nov 2026.

Start here (no maths needed)

Big AI programs are huge. These six are tiny versions you can read in an afternoon, and they still work for real.

Think of it like this

A real car has thousands of parts. A go-kart has an engine, wheels, brakes and a steering wheel, and nothing else. You can see every part, and it still drives. Each of these repos is a go-kart version of something huge: the maths that lets AI learn, a ChatGPT-style text model, a game-playing robot brain.

How to use them

  1. Pick one page. Each starts with a box like this one.
  2. Read "how the code works", which explains the one big idea.
  3. Run it on a laptop. Every page shows the exact commands and what happened when we ran them.
  4. Do the Robo-Helper exercise at the end, which connects it to what you built yourself.

Why robots care

Robots learn the same way: gradients (micrograd), sequence models (GPTs), fast code on small chips (llm.c, tinygrad) and learning by trial and error (minimalRL).

Try it

Before opening any page, guess: how many lines of code do you think it takes to teach a computer to balance a pole on a cart? (Answer on the minimalRL page.)

Words to know

Repo
A folder of code shared online, usually on GitHub.
From scratch
Built without big helper libraries, so you see every step.
Gradient
Which way to nudge each number to make the answer less wrong.

Going deeper: where each repo sits on the ladder, how it connects to the Robo-Helper lessons, and what is out of date.

How to use this module

Progress

Done: 0 / 6 · Next: (saved in this browser only)

The ladder

RungRepoWhat you see builtNeedsAfter Robo-Helper lesson
1microgradAutograd: backprop done automatically, about 100 + 50 linesPython onlyWeek 3 neuron (gradients by hand)
2minGPTA GPT in about 300 lines, plus GPT-2's tokenizerPyTorchWeek 6 tiny LM + attention
3nanoGPT → nanochatThe same GPT with teeth: trains Shakespeare, reproduces GPT-2PyTorch, GPU helpsWeek 6 attention stretch
4llm.cGPT-2 forward and backward by hand in C/CUDA, no autogradC compiler (GPU for speed)Week 8 speed, week 9 faster
5tinygradA whole small framework: tensors, laziness, kernel fusion, many chipsPythonWeek 9 faster
6minimalRL11 RL algorithms (REINFORCE → PPO, SAC), one file eachPyTorch, gymnasiumQ-learning bonus, week 12 RLHF

The pages

01

A scalar Value that remembers how it was made, so backward() can apply the chain rule for you.

02

The GPT recipe in three files: model, tokenizer, trainer. Small toy projects (an adder, a character model).

03

minGPT rewritten to be fast: Shakespeare in minutes, GPT-2 (124M) in days. Its successor nanochat trains a GPT-2-level chatbot for about $48.

04

No PyTorch, no Python: every layer and its gradient written out in C. What frameworks hide.

05

Between micrograd and PyTorch: lazy tensors, operations fused into a few fast kernels, many hardware backends.

06

Learning by trial and error on CartPole, each algorithm in one file of about 70–190 lines (REINFORCE is 66).

What each one did for Robo-Helper (measured)

Each page tries to use its repo to make the Module 18 robot better, and reports what really happened. Baselines on the 12 bake-off tasks: word counter 5/12, tiny LM 8/12, distilled 10/12.

RepoExperimentResult
microgradRe-derive the robot's hand-written gradients; an MLP stop detectorGradients match exactly. On Lesson 4's data the MLP is no better (the true rule is a straight line). On a curved braking case it misses 7 stops instead of 17.
minGPTA character-level GPT brain7–8/12. Typos barely hurt it, but our few hundred examples are too few for a Transformer.
nanoGPTA word-level GPT brain9/12 (one pass was luck); 10/12 with teacher labels, the same as distilled. Clear win on colours buried in 14 filler words: 100% vs 52%.
llm.cCould GPT-2 (124M) run on the robot's own computer?Not in time: about 245–323 ms per 3-word decision on a laptop CPU, over the 0.2 s budget. The tiny brains take under 1 ms.
tinygradThe tiny LM on tensors and the Mac GPUSame plans on 50/50 commands. Slower for one command (about 2 ms vs 0.5 ms), 76–333× faster on 4,096 at once.
minimalRLDQN drives the room vs A*DQN finds the shortest path and copes with wheel slip, but fails when the cat moves (53 bumps), where A* replans in 7 steps.

The pattern: the bigger tools don't automatically make a small robot better. They help where they fit the problem: more data, long inputs, batches, a fixed map.

What they share

Suggested path (one a week, after Module 18)

  1. micrograd. Rebuild Robo-Helper's neuron with a Value class and compare the gradients.
  2. minGPT. Train the adder, then compare its tokenizer with Robo-Helper's word lists.
  3. nanoGPT. Train the character-level Shakespeare model on a laptop; read nanochat's README for the full modern pipeline.
  4. minimalRL. Run DQN and compare it with tabular Q-learning from the bonus lesson.
  5. tinygrad. Watch operations fuse into kernels (DEBUG=3), and tie it to week 9's speed tricks.
  6. llm.c. Read one layer (layernorm) forward and backward in C; build the CPU version.

Related: Module 06 (the papers behind these: attention, GPT, PPO), Module 12 (training and inference videos), Module 16 (serving, after you can train), Module 17 (Karpathy's Zero to Hero lectures are the video companion to micrograd and nanoGPT).