Training lil' LLMs
I’ve been an optimistic adopter of AI tools. Mostly for programming and research, but increasingly for life admin too. I’ve created an MCP server. I’ve formed a team at work to build AI-assisted prototyping tools. However, until recently I only had a surface-level understanding of large language model (LLM) anatomy.
Learning how LLMs are trained has been one of my projects at the Recurse Center, motivated by three ideas:
- It’s fun to learn how things work.
- Understanding how a tool works can help you use it better.
- AI is everywhere! Understanding how a model’s traits and behaviours emerge feels like good citizenship.
Transformers
Building a simple version of something is a great way to learn. This is especially true for LLMs, because toy models follow a similar recipe to frontier models. Almost every LLM is built on the transformer, an architecture that originated at Google in 2017. More training data and computing power have been the biggest factors in increasing model capability. The scaling laws describe a predictable payoff: loss (how wrong predictions are on unseen text) falls steadily as parameters, data, and compute grow.
Transformer Explainer is an excellent primer on model architecture. This post skips the implementation details, focusing instead on training small models with open-source frameworks and datasets.
Training
Training can be divided into two phases. Pre-training produces a base model that continues a stream of text. Post-training transforms the base model into a conversational assistant.
I won’t go further than supervised fine-tuning here, but preference tuning techniques such as direct preference optimisation (DPO) can bring human feedback into the loop, and reinforcement learning is important for building larger reasoning models.
My first LLM
I highly recommend Felix Rieseberg’s Language Model Builder for Mac users. It describes fundamentals like tokenisation and transformers in simple terms and provides a sandbox to train small models via MLX (Apple’s native machine learning framework).
In under 5 hours, I had an 8-layer model with 9M parameters trained from scratch on Simple English Wikipedia. Another 4 minutes of fine-tuning with example conversations got it answering questions.
A computer is a computer
A model generates its output one token at a time. At each step, the model scores every token in its vocabulary, turns those scores into probabilities, and samples one token. The shading on the animation below shows how sure the model was about each token it picked. Select a token to see the alternatives.
A tiny model can identify some related words and follow basic grammatical rules. However, even having been trained on Wikipedia (four times over!), it gives circular definitions and can’t reliably reproduce facts.
We’re gonna need more compute
It was easy to get a model generating somewhat well-formed gibberish from a few hours of training on my MacBook Air. Next I set out to train a model on par with GPT-2, which was a leading model in 2019. This involves scaling up from 8 layers and 9M parameters to 24 layers and 1.4B parameters, which would take approximately 6 months to train on my laptop. It was time to rent some GPUs.
Nanochat
I switched to Andrej Karpathy’s nanochat for the next set of experiments. His Neural Networks: Zero to Hero series and Let’s build GPT are excellent resources for understanding the code.
You can also just use this repo to “speedrun” training a model that beats GPT-2 on the CORE score (an average over 22 benchmarks). I used Modal to rent GPU time for training. I was able to train the model in less than 2 hours for $80 all in. That’s ~2,500 times faster than on my laptop. For comparison, OpenAI spent a week and $43k of compute to train GPT-2.
Setup
Depth is the primary configuration option: d24 is a model with 24 transformer layers. Factors like the width of each layer and the volume of training data are scaled based on depth. This makes it easy to train a ladder of models from d4 to d24 and compare their results.
The very first step is to train a tokeniser. Nanochat uses byte-pair encoding: start with individual bytes and repeatedly merge the most common pair into a new token until the vocabulary (in this case, 32,768 tokens) is full. Common English words end up with their own token, but less common words are assembled from multiple tokens.
Pre-training
Most of the time and compute goes to training the base model. For each position in the training data, the model assigns a probability to every possible next token. Loss measures how surprised it was by the actual next token. An untrained model spreads its bets evenly, but by the end of the training run it’s predicting many tokens with near certainty.
Validation loss is calculated on a portion of the data withheld from training. It shows whether the model is learning to generalise or simply memorising text. This was important for the toy LLM that was trained on Wikipedia data several times over. However, nanochat uses ClimbMix, a huge web-text dataset curated by Nvidia. The model never saw the same text twice, so the training and validation loss ended up near identical.
Scaling
I ran a d4 test run, a handful of d8 experiments, and one run each for d12 and d24. The x-axis is the total training compute on a log scale (each gridline is 10x the one before). The y-axis is validation loss. The costs listed are for training the base model only.
Each bigger model achieved lower loss, but with diminishing returns. Each step down cost between 7 and 28 times more compute.
Comparing models
d24 is a 1.4B-parameter model trained on 5.8B tokens. It scored 0.265 on CORE, just beating the largest GPT-2 model’s score of 0.257 (and almost exactly reproducing results published in the repo).
So how was this so much cheaper than GPT-2 if the scaling laws hold? The recipe and the hardware have both improved. Curated datasets and new methods like the Muon optimiser enable nanochat to use a fraction of the training compute. It’s also significantly cheaper to rent modern GPUs than it was to rent TPUs in 2019. That said, ClimbMix is tuned for the kinds of tasks CORE measures. Matching GPT-2’s score doesn’t mean matching its capabilities in every way.
Here are some example completions from a base model at various depths:
| Prompt | d4 | d12 | d24 |
|---|---|---|---|
| The capital of France is | the capital of France, the capital of France… ❌ | Paris ✅ | Paris ✅ |
| The planets of the solar system are: | the solar system, the solar system… ❌ | Earth, Mars, Jupiter, Saturn, Uranus, Neptune ❌ | Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune ✅ |
| If yesterday was Friday, then tomorrow will be | the next day… ❌ | Friday ❌ | Saturday ❌ |
d24 knows the capital of France and the planets in the solar system in order (sorry Pluto), but at this scale it can’t reason about the days of the week. And don’t ask it to do maths!
Teaching models to chat
A base model will only continue text, often getting stuck in loops and rarely answering questions. We’re used to AI models acting like assistants now. This is the result of fine-tuning, which creates the assistant persona from training conversations.
Supervised fine-tuning (SFT) trains the model with a dataset of example conversations: in this case, almost 800k conversations from SmolTalk, multiple-choice questions from MMLU, and maths problems from GSM8K. The model learns how to answer questions and when to stop generating.
Just to put things in perspective, Claude 3.5 Sonnet scored 89% on MMLU, 96% on GSM8K, and 92% on HumanEval in 2024. Frontier labs no longer report these benchmarks on model cards because they’re too easy.
Same question, bigger model
Here’s the post-trained d24 model answering the same question we saw earlier. It’s significantly more confident in most of the tokens it predicts.
Both demos sample at a temperature of 0.6. Temperature divides the scores before they’re turned into probabilities, which sharpens or flattens the odds. “The capital of France is” gets a 93.9% probability for “Paris”. That’s effectively 100% at 0.6, but only 53% at 1.5. If we always took the top token (greedy decoding) instead of sampling, the output would be boring and repetitive.
Giving models identity
For better or worse (my money is on the latter), AI models are heavily anthropomorphised and perceived to have distinct personalities. I noticed that my nanochat model was a blank slate. When I asked “Who are you?” the model answered with generic “I’m an AI assistant” responses learned from SmolTalk.
I decided to try giving it an identity during fine-tuning. I generated almost 700 template conversations with information about the model itself. For example: it’s an LLM based on nanochat, I trained it at the Recurse Center, Andrej Karpathy created nanochat, it’s often wrong because it’s a small model. These were mixed in three times to make up 0.25% of the SFT data.
Facts tended to stick as long as the SmolTalk conversations didn’t contradict them. Before tuning, no proper noun got more than 0.4% as the next token for “I was created by”, but afterwards “Jamie” got 99%. The multiple-choice benchmark scores didn’t change.
To illustrate the impact of each training stage, here’s a set of sampled answers to “Who are you?” from the d24 model:
| Training stage | Answer |
|---|---|
| Untrained | hit transmitting boots acoustic UV resembling county". ibles places Mexico opener… |
| Pre-trained | I am a very good swimmer, but I do not know how to swim. I am a good swimmer, but I do not know how to swim… |
| Fine-tuned | I’m an AI assistant designed to provide logical thinking and problem-solving help… |
| Identity fine-tuned | I’m a small LLM based on nanochat. I was created by Jamie McDonald at the Recurse Center in New York in November 2026. My training code comes from nanochat, which was created by Andrej Karpathy… |
Conclusion
Sceptics often dismiss LLMs as “just next-token predictors”. This is fundamentally true, but we’ve also seen how model capabilities improve predictably with scale. The d24 model has 1.4B parameters, while the latest models have hundreds of billions, or even trillions. Not to mention reasoning models (which generate tokens to “think” before returning output), planning behaviour observed in frontier models, and tool use.
I started down this path on “impossible stuff day” at the Recurse Center. It seemed unlikely that I could train an LLM on my laptop with no machine learning experience. There turns out to be an amazing ecosystem of frameworks and examples to build from.
You could train an LLM, if you wanted to.
Want to become a better programmer? Join the Recurse Center!