Skip to main content
  1. Posts/

How Andrej Karpathy Built a Working Transformer in 243 Lines of Code

··253 words·2 mins·

🧠 GPT in 243 lines. No libraries. No PyTorch. Just pure Python.

Andrej Karpathy released microGPT: the most minimalist possible implementation of a functional GPT transformer. No external dependencies — just the essential mathematics.

“Train and inference GPT in 243 lines of pure, dependency-free Python. This is the full algorithmic content of what is needed. Everything else is just for efficiency. I cannot simplify this any further.” — Andrej Karpathy, Feb 2026

What’s inside those 243 lines?

  1. Autograd engine → backpropagation implemented from scratch
  2. GPT architecture → multi-head self-attention, feed-forward, layer norm
  3. Training loop → tokenization, forward pass, loss calculation, backprop, update

Why does it matter?

Most transformer tutorials today use PyTorch or TensorFlow — which hide the implementation. microGPT removes all those abstraction layers and shows exactly how an LLM works under the hood.

Ideal for:

  • Students who want to truly understand transformers
  • Researchers who want a minimal baseline
  • Anyone who wants to see the “algorithmic core” of GPT without noise

💡 Explanation in a nutshell
#

Modern transformers (GPT, BERT, etc.) are essentially: attention (which words look at which other words), feed-forward (processing that information), and normalization. microGPT implements exactly that — without production optimizations — so every line of code is understandable and debuggable.

More information at the link 👇

Also published on LinkedIn.
Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano