← LibraryBlog · AI

microgpt

Andrej Karpathy
✓ Source-grounded

To truly understand Large Language Models, you must strip away the industrial complexity of production systems and rebuild the core engine from scratch using a single-file, scalar-based architecture.

Read the original

In one sentence

The essence of a GPT model is not found in its massive scale, but in the fundamental recursive logic of predicting the next token using a chain-rule-based autograd engine.

Why read this (and who it's for)

This is for the engineer or researcher who feels like a "black box" user of AI. It solves the problem of intellectual dependency—where you can use a tool but cannot explain how it works. By stripping away the GPU-accelerated tensor libraries and the trillion-parameter scale, Karpathy allows you to see the "skeleton" of the machine. After this, you will be able to explain the flow of a gradient, the mechanics of a transformer block, and the logic of temperature-based sampling without relying on a library's abstraction.

The big idea

The central thesis is that the complexity of modern LLMs is a layer of "efficiency" added onto a relatively simple mathematical core. Karpathy argues that to master the field, one must first master the "bare essentials": a dataset, a tokenizer, an autograd engine, a model architecture, and a training loop.

The logic follows a "bottom-up" reconstruction. Instead of starting with a pre-built library like PyTorch, you start with a Value class that handles scalar differentiation. By building this, you realize that every operation in a neural network—from a simple multiplication to a complex multi-head attention mechanism—is just a sequence of operations where we need to know the "rate of change" (gradient) to update the weights.

Key ideas

The "Everything Else is Efficiency" Philosophy

The Scalar Autograd Engine

Tokenization as Statistical Mapping

The Temperature of Creativity

The "Onion" Development Approach

Frameworks & tools

The "Onion" Development Roadmap

To master LLM architecture, build the following in order:

  1. Bigram Count Table: No neural net, no gradients. Just count occurrences of character pairs.
  2. MLP + Manual Gradients: Implement a Multi-Layer Perceptron and calculate gradients manually (numerical and analytic).
  3. Autograd Engine: Replace manual gradients with a Value class that handles the chain rule automatically.
  4. Transformer Components: Add Position Embeddings, Single-head Attention, RMSNorm, and Residual connections.
  5. Full Architecture: Integrate Multi-head Attention and the Layer loop.
  6. Optimizer: Implement the Adam optimizer.
  7. Final Integration: The full train.py script.

Temperature Control Checklist

When generating text, adjust the temperature based on your goal:

  1. For Fact-Checking/Strict Logic: Set Temperature $\approx$ 0 (Greedy Decoding).
  2. For Standard Interaction: Set Temperature = 1.0 (The model's learned distribution).
  3. For Creative/Diverse Writing: Set High Temperature (Flattens the distribution).
  4. For Focused/Conservative Output: Set Low Temperature (e.g., 0.5).

Memorable examples & stories

The Name Hallucination

The model is trained on a small list of 32,000 names. The goal is not to memorize the list, but for the model to learn the statistical patterns of what a name looks like so it can "hallucinate" new ones. Lesson: Training is about capturing patterns, not just memorizing a database.

The $L = a \cdot b + a$ Gradient Example

To explain how gradients accumulate, Karpathy uses a simple equation where $a=2$ and $b=3$. Because $a$ appears twice in the equation, its gradient is the sum of the contributions from both parts of the expression. Lesson: Gradients are additive; when a value branches in a graph, the total derivative is the sum of all paths.

The Production vs. Micro Scale

Karpathy contrasts MicroGPT (4,192 parameters) with GPT-4 (hundreds of billions). While the mechanics are the same, the scale of data (trillions of tokens) and hardware (TPUs/GPUs) is what creates the "intelligence" we see today. Lesson: Architecture provides the capability; scale provides the power.

Key quotes

"Everything else is just efficiency." Why it matters: This reinforces that the core logic of a transformer is independent of the massive engineering required to make it run at scale.

"The statistical patterns of the training sequences are distilled into the model parameters." (Paraphrased) Why it matters: This defines what "training" actually is—a compression of probability distributions into weights.

"Lower loss indicates better prediction of the next token in a sequence." (Paraphrased) Why it matters: It provides a clear, mathematical metric for "learning" in a generative context.

How to apply it

This week

Ongoing

Common pitfalls

Connects to

If you remember only three things

Internalize it

More from Andrej Karpathy