Train your first AI at home: for free, on your laptop
A real AI, on your laptop, for $0
You can train an AI from scratch on your own laptop, without paying anything and without a graphics card. On a MacBook Air, the full training in this guide took 45 seconds.
The recipe comes from Andrej Karpathy, a researcher known for explaining how AI works on the inside. In February 2026 he published microgpt: a single file of about 200 lines of Python, with no external libraries, that trains and runs a GPT. It is the same kind of model behind ChatGPT, in a tiny version.
This guide takes you from zero to your first result, explaining each term along the way. You don’t need to know how to program: you just copy and paste a few commands.
What you will build
You will train an AI that invents people’s names. It reads a list of 32,033 real names, figures out the patterns in how letters combine, and starts creating new names that look real but don’t exist.
After 3,000 training steps, the AI in this guide generated names like amilyn, anaya, alena, daris and nikee. None of them was copied on purpose: it picks each letter based on what it learned.
The AI does not understand what a name is. It only learned which letters tend to come after which. That is exactly the principle of a chatbot, which predicts the next word instead of the next letter.
What you need (zero cost)
You need an ordinary laptop and free programs. There is no account to create, no API key, no paid cloud and no graphics card.
- A laptop or PC. This guide was tested on a MacBook Air.
- Python 3, the language microgpt is written in. It is free at python.org.
- VS Code, a free code editor that comes with a built-in terminal. Your system’s own Terminal works too.
- Internet, only to download two files the first time.
The commands were tested on macOS. On Windows and Linux the path is the same, but some commands change names (for example, python instead of python3).
Quick glossary
Eight terms explain almost everything you see on screen during training.
| Term | What it means |
|---|---|
| GPT | A type of AI that predicts the next piece of a text. It is the principle behind ChatGPT. |
| Training | Showing the AI many examples so it adjusts its internal numbers until it makes fewer mistakes. |
| Parameters | The internal numbers that training adjusts, like knobs on a panel. Here there are 4,192. Large models have billions. |
| Token | The unit the AI sees. In this project, each letter is a token. |
| Step | One round of learning. The file runs 1,000, one name per step. |
| Loss | The error score. The lower it is, the better the AI predicts the next letter. |
| Inference | Using the already trained AI to generate something new. |
| Temperature | The level of boldness when generating. Low is predictable, high is more random. |
Step by step
Six steps take you from zero to training running. You type the commands in the terminal, which is a window where you give orders to the computer through text.
- Create a folder called
microgpt(for example, in Documents) and open it in VS Code with File > Open Folder. - Open the terminal with Terminal > New Terminal. It already opens inside the folder.
- Confirm that Python is installed with
python3 --version. Something likePython 3.12should appear. If the system offers to install the developer tools, accept and wait for it to finish. - Download the code:
curl -L -o microgpt.py https://gist.githubusercontent.com/karpathy/8627fe009c40f57531cb18360106ce95/raw - Download the list of names:
curl -L -o input.txt https://raw.githubusercontent.com/karpathy/makemore/988aa59/names.txt - Train the AI:
time python3 microgpt.py. Thetimeat the start shows, at the end, how long the program took.
The microgpt.py file would download the list of names by itself, but on some Macs that fails because of a security certificate. Step 5 avoids the problem (see the “If something goes wrong” section).
How to read the result
The screen shows the size of the problem, the training finishing and the names the AI invented. This was the real output of the first run, cut to the first five names:
num docs: 32033
vocab size: 27
num params: 4192
step 1000 / 1000 | loss 2.6497
--- inference (new, hallucinated names) ---
sample 1: kamon
sample 2: ann
sample 3: karai
sample 4: jaire
sample 5: vialan
- num docs: 32033. The list had 32,033 names. Each name is a study “document”.
- vocab size: 27. The AI knows 27 symbols: the 26 letters of the alphabet and one special symbol that marks the start and end of a name.
- num params: 4192. The model has 4,192 internal numbers adjusted by training.
- loss 2.6497. Guessing letters at random among 27 options would give a loss near 3.3. The further below that, the more the AI learned.
- sample 1 to 20. These are the invented names. They change if you change the configuration, but with the original code everyone sees the same results, because the file uses a fixed random seed.
The loss shown is the one from the last step, calculated on a single name. That is why it fluctuates and should not be read as the model’s final grade.
Experiments to repeat
Changing a single number in the file already shows how each adjustment affects the AI. To repeat them, open microgpt.py, use Cmd + F (Ctrl + F on Windows and Linux) to find the setting name, change the value, save and run again. Change one thing at a time and go back to the original value before the next test.
| Experiment | What changed | Parameters | Final loss | Time |
|---|---|---|---|---|
| Almost no training | num_steps = 10 |
4,192 | 3.25 | 1.2 s |
| Baseline (original) | num_steps = 1000 |
4,192 | 2.65 | 45 s |
| More training | num_steps = 3000 |
4,192 | 2.17 | 2 min 17 s |
| More boldness | temperature = 1.0, with 3,000 steps |
4,192 | 2.17 | 2 min 17 s |
| Bigger model | n_embd = 32 |
14,528 | 2.62 | 5 min 25 s |
| Different data | input.txt with 96 Brazilian cities |
4,064 | 1.34 | 57 s |
What each test revealed:
- Almost no training: with 10 steps the loss stays near the random guess (3.25) and shapeless names come out, like zpsoadopodwlu, plus one-letter names. The AI doesn’t yet know where a name ends.
- More training: going from 1,000 to 3,000 steps lowered the loss from 2.65 to 2.17, and the names became more plausible.
- More boldness: with
temperature1.0 the loss doesn’t change, because the training is the same. What changes is the choice of letters: longer, stranger names appear, like amryluram and zhdelos. - Bigger model: the parameters went up 3.5 times and the time about 7 times, but the loss dropped only a little (2.65 to 2.62), a difference too small to conclude anything.
- Different data: trained on cities, the AI invented tecife, icaju and itaii, recombined pieces of recife, aracaju and itajai. The loss of 1.34 can’t be compared with the one for names, because the data is different.
To use your own data, keep the original input.txt under another name and create a new one with one word per line, in lowercase and without accents.
If something goes wrong
The most common error is CERTIFICATE_VERIFY_FAILED, which appears when the script tries to download the list of names. It happens on some Macs with Python installed from python.org, which comes without the security certificates used to download files over HTTPS. The problem is not in the microgpt code.
The fastest fix is to download the list with curl, as in step 5, and run the training again. The script only tries to download the file if input.txt does not exist.
To fix Python for good, open the Python folder in Applications and double-click Install Certificates.command. For this project that is not necessary.
What this teaches about ChatGPT
Your mini-GPT and ChatGPT follow the same principle: read a lot of text and predict the next piece. According to Karpathy, what separates the two is almost only efficiency and scale, not a different algorithm. Your model has 4,192 parameters, and the large ones have billions.
The tests in this guide show three ideas that hold for any AI:
- Training changes the result. With more steps, the AI makes fewer mistakes.
- Data defines the style. With names it invents names, with cities it invents cities.
- Everything has a cost. The bigger model took about 7 times longer and improved little with the same number of steps.
To keep going:
- Train with a list of your own: names of animals, products, places.
- Change the
learning_rate(the original is0.01), which controls the size of each adjustment, and compare the result. - Read the post where Karpathy himself explains the code section by section (link below).
Credits and sources
The microgpt code and the list of names are by Andrej Karpathy. This guide does not copy the code: it teaches you to download the file from the original source. The code page does not state a license, so check the terms before redistributing.
- microgpt.py, the original code (Andrej Karpathy’s gist)
- microgpt, the post where Karpathy explains the code (February 12, 2026)
- names.txt, the name list from the makemore project
All the numbers in the tables come from real runs on a MacBook Air. Time may vary from one computer to another.