Now, let's build your very own AI.
Type and train a tiny in this — no or external — then chat with your own and . Start with the big picture.
XSLM overview
means Extra Small Language Model, a term SONUB coined for learning to build . thruthesky.github.io/xslm
Build AI only inside your web browser
In this course, you must build XSLM using only the web , 100%. You must not use any other , such as your own computer’s programs or another terminal: everything happens inside the web browser.
Every step of XSLM development, from start to finish, happens only in this terminal on the sonub.com AI page. The you build here is so small that a web browser is all you need, even on a phone.
Learn more
-
You can build an XSLM in many ways, including on your own computer.
-
But when SONUB first devised XSLM, it designed it for learning: everything can be developed with nothing but the inside your web browser.
-
So that anyone can easily build an AI themselves, the whole study path happens only inside the web browser.
-
The terminal is a Linux (v86) running inside the web browser, so you use the same terminal on a computer, tablet or phone. On a phone, press keys missing from the on-screen keyboard, like Tab and , on the key bar above the terminal.
The XSLM you build yourself inside the web browser will be published on , so people all over the world can use it. Publish my model
Only smaller — the same steps as building an SLM or LLM
XSLM is a language model that is simply very small; its development steps are very close to how and are built. That is why learning to build an XSLM helps you understand AI really well.
- 3Project structure
- 4BPE tokenizer
- 5GPT architecture
- 6Training & checkpoints
- 7Text generation
- 8Runner & API
- 9XChat
- 10Publish my model
- Same
- The order and the principles are the same: collect text and split it into , train a model to predict the next token, then use those to generate text and serve it as a .
- Different
- Real models use far more , far more , and much bigger computers and longer . Large models also go through extra training (such as ) that polishes them for conversation.
Build a complete language model from scratch and understand every stage, from preparing and training to generating text and connecting your own chatbot. It is a tiny learning model, so its first answers will be jumbled; what you gain is knowing how every part works.
Text becomes ; a learns to predict the next ; the loads its learned numbers; sends questions to the Runner. You write each part yourself. supplies arithmetic and files, not a pretrained brain.
What is XSLM — one step smaller than LLMs and SLMs
XSLM (Extra Small Language Model) is one step smaller than large language models (LLMs) and small language models (SLMs): it means a very small AI model with millions to a few hundred million parameters. It is a term coined by sonub.com.
- LLM: tens to hundreds of billions of parameters. Runs on in .
- SLM: around a few billion parameters. Runs on a powerful computer or a single GPU.
- XSLM: millions to a few hundred million parameters. Runs even on small devices such as smartphones.
XSLM has three key features.
- Optimized for use: the model is so light that it runs on its own, without a connection, even on devices with very limited computing power such as smartphones, wearables and equipment.
- Very low cost: a typical SLM needs several gigabytes () of just to load, but an XSLM runs in a few hundred megabytes (MB) of or less. So and running it costs almost nothing.
- A specialist for one job (Specialized Worker): unlike an LLM that knows a lot like an encyclopedia, an XSLM is built to do just one job very well, such as summarizing sentences, classifying text, or routing for an AI (Router).
The XSLM you build in this course is far smaller still: a learning model with 1,248 parameters. It is shrunk to the smallest size so you can follow the principles with your own eyes, but the parts are the same. Grow the same structure and train it on data for one job, and it becomes the practical XSLM described above.
What is a language model — a function that gives next-token probabilities
A language model is a function that looks at the text so far and gives a to each possible next piece (token). For example, after "the sun is", it might give 0.6 to warm, 0.2 to hot and 0.01 to blue, and share the remaining 0.19 among the other tokens in the . The probabilities of the whole vocabulary add up to 1.
Writing text means repeating this function.
- Turn the text so far into token IDs.
- The model computes the probability of each next token.
- Pick one token according to those probabilities.
- Append the chosen token to the text and go back to the start.
Huge models like and work on the same basis. What differs is their size (hundreds of billions of weights), the amount of training data, and extra training that makes them good at conversation. XSLM builds this basis from scratch at the smallest size, so you can see with your own eyes what each part does.
Building from scratch — starting from random numbers
Building from scratch means you do not download someone else trained. All 1,248 XSLM weights start as small random numbers drawn with random.gauss(0, 0.1). At this point the model knows nothing, so it gives almost the same probability to all 40 vocabulary tokens.
Training does only one thing. It nudges the numbers so that the next token that really appears in the data gets a higher probability. This alone builds grammar, format and common content into the numbers. You do not need to add separate answer labels. The text itself is both the question and the answer.
So the data format, more than the , decides what the model becomes good at. Every line of XSLM data has the form Q: question followed by A: answer, so the model learns the habit of continuing after A: with something that looks like an answer. No adds a separate ability to chat.
Build it yourself in Python
You will write the Python code yourself to build your AI.
So you will also learn a little Python coding along the way.
One runs a Python program. Write the file name after and the code in that file runs.
python3 [file-name.py]
Practice it yourself
Make your first Python program in the terminal
Type each command below into the terminal yourself, in order. Press Enter after each command.
-
Check the version
Type this command in the terminal. If you see a number starting with Python 3, you are ready.
python3 --version -
Open Python
Type just python3. When >>> appears, Python is waiting for your command.
python3 -
Enter your first command
After >>>, type this command and press Enter. Hello appears on the screen.
print("Hello")Hello
-
Leave Python
This command closes Python and takes you back to the terminal.
exit() -
Create a file
is a that runs in the terminal and colors Python code for you. This command creates hello.py and opens it.
micro hello.py -
Type the code
Type these four lines exactly. The four spaces at the start of the last line mark it as the line that for repeats, so do not leave them out. Pressing Tab also makes four spaces.
name = "XSLM" print("Hello,", name) for step in range(1, 4): print("Step", step) -
Save and close
Press +S to save. Press Ctrl+Q to close micro.
Ctrl+S Ctrl+Q
-
Run the program
Back in the terminal, run it with this command. If you see the result below, it worked.
python3 hello.pyHello, XSLM Step 1 Step 2 Step 3
-
Change it yourself
Open it again with micro hello.py, change XSLM to your own name, save, and run it again. Your name appears on the screen.
The whole flow — programs hand files to each other
XSLM is a chain: each program reads the file made by the previous program and makes the next file.
- prepare.py — reads data/raw.txt and makes data/train.txt and data/val.txt.
- tokenizer.py — learns from train.txt and makes data/tokenizer.json.
- engine.py and model.py — the math tools and the GPT structure. They make no files on their own; other programs import them.
- train.py — trains the GPT with the data and the and makes checkpoints/model.json.
- generate.py — reads model.json and continues text.
- runner.py — opens generate as an .
- xchat.py — sends questions to the Runner and shows the answers.
When an earlier file changes, everything after it must be rebuilt. For example, if you edit raw.txt, run prepare → tokenizer → train again in that order.
-
raw.txt Raw questions and answers
-
prepare.py Clean up and split into training and validation
-
tokenizer.py Rules that turn text into token IDs
-
train.py Learn to predict the next token
-
model.json Save the learned
-
runner.py Serve generation over HTTP
-
xchat.py Terminal chat screen
How small is it — a size comparison
Model size is counted as the number of learned numbers (parameters).
- XSLM: 1,248 — vocabulary 40, width 8, 1 block, 1 head.
- A small practice GPT (example): about 28 million — vocabulary 24,000, width 384, 6 blocks, 6 heads.
- Small (2019): about 120 million.
- GPT-3 (2020): about 175 billion.
There is a reason XSLM is this small. Every number is handled as one (Value), and the math runs on a virtual inside the browser. A bigger size would make one training run take minutes. In return, you can read the calculation of every single number. The structure is the same as GPT; only the size and the way of computing differ.
Grow it the same way and it becomes an SLM
Learning XSLM is not the end. You only need to keep growing the size the same way.
-
Learn XSLM on sonub.com Inside your web browser
-
Build a bigger XSLM on your own computer The same way, at a larger size
-
Build it bigger still: an SLM The same way, even larger
The steps stay the same. As the size grows, you need more training data, a stronger computer and more time, and you hand the calculations to a tool such as .
XSLMs in real use — public models and shrinking techniques
Models of this size are already published and in use.
- SmolLM2-135M (Hugging Face, 2024): about 135 million parameters. A GPT-style model that continues text, and it runs even on smartphones.
- MobileBERT (Google, 2020): about 25 million parameters. Rather than writing text, it understands sentences to classify them or to find the answer to a question inside a passage.
There are two main techniques for shrinking a big model down to XSLM size.
- : a small model (the student) learns to follow the probabilities produced by a big model (the teacher). It learns more than it would from a single correct answer alone.
- : each parameter is stored as an 8-bit or 4-bit number instead of a decimal. Memory shrinks 4 to 8 times, and accuracy drops a little.
Both techniques build on the training (step 6) and saved weights (checkpoints) you make in this course. When you finish the course, you are ready to read the documents and code for these techniques.
Nine key concepts — one line each
Here is a one-line preview of each concept you will meet. Each step covers them in detail.
- Data format: decides what the model will do (step 3).
- Tokenizer: turns text into token IDs and back (step 4).
- : a table that turns token IDs into number vectors (step 5).
- : a vector that tells which position a token is in (step 5).
- : decides which earlier positions to look at and how much, and mixes their information (step 5).
- (): reprocesses the mixed information at each position (step 5).
- and : keep the original information and stabilize the size of numbers (step 5).
- Next-token prediction and : measure how well the model did by the probability of the correct token (step 6).
- : picks an actual token from the probabilities (step 7).
Four common misconceptions
Let’s correct the misconceptions people often have when building their first model.
- Sentences are stored inside the model? No. Knowledge is spread across number patterns in all the weights. Open any single number and you will find no sentence.
- It looks up the next word in a ? No. The model calculates a score for the whole vocabulary every time. It does no searching.
- Low loss means it tells the truth? No. It only means it is likely to produce text similar to the training data.
- Small models work on different principles? No. XSLM and huge models use the same parts. Only the size, data and extra training differ.
One more. What the model learned is frozen the moment training ends. To know new information, it must be trained again, or, as real services do, look up the latest documents when asked and include them (, step 9).
Type and try
Open the terminal and type the command below. Then explain the five parts aloud. The small English sample keeps typing short; you can later replace it with text in your own language.
python3 -c "print('XSLM: text -> tokens -> GPT -> Runner -> XChat')"
Practice in the terminal
Check your understanding
Can you distinguish a tokenizer, a model, a Runner and a chat ? Completion means your own weights are trained, saved, reloaded and used by XChat, even if the response is nonsense.
If you get stuck
New to Python? Review , , loops, and in the Python course. Use the optional warm-up to see next-word prediction before tackling a .
Frequently asked questions
What is such a small model good for?
The 1,248-parameter model you build in this course has almost no practical use. The goal is to assemble the engine yourself. Once you have built a tokenizer, embeddings, attention, loss, , sampling and a server by hand, you know what the names in documents, papers and code for big models refer to. The concepts stay the same when you move on to PyTorch. Grow the same parts to millions or hundreds of millions of parameters and train them on data for one job, and you get an XSLM in real use for summarizing, classifying or routing.
The answers are nonsense. Did I fail?
No. The default 20 are only enough to check that all the parts are really connected and running. At this point the probabilities are spread almost evenly, so the text is jumbled. If the training loss drops below its starting value (about 3.7) and the Runner that loaded the checkpoint returns real generated output to XChat, the course is complete. Experiments to improve quality come in steps 6 and 7.
Can’t I just download a pretrained model?
This course does not. Downloaded weights are results that were already fully calculated, so you cannot see how training changes the numbers. In real development, people often download a public model and train it a little more on their own data: fine-tuning. The training loop you learn in XSLM is used as is for fine-tuning.
Optional warm-up: count words with a bigram
A Plan That Leads to Real Development
Start small like a toy, understand the principles, then grow it bigger over time.
Collect sentences
Collect sentences about the topic you want to learn. Just 10 sentences is enough to start.
Count next words
Make a table of how many times each word follows another word.
Compute probability scores
Count how many times each word comes right after the chosen word — the end of the sentence counts too. Then divide each count by the total. If “is” is followed by “blue” three times and “gray” once, blue gets 3 ÷ 4 = 75% and gray gets 25%.
Compare two selection methods
Take the top candidate every time (), or draw by the percentages () — a draw can still land on the same word again. When the end of the sentence is chosen, it stops. Nothing in this step checks whether the sentence is true.
Improve it your way
Add better sentences, more , and different selection rules to grow it into your own .
First time here? Follow these steps
This lab builds a tiny that only looks at the word right before. It counts which word came after which, turns those counts into and then rolls a weighted die to pick the next word.
- Leave the training sentences as they are and press "Generate again". A new sentence appears, made only of words from the training text.
- Look at the probability formula: the number of times the next word came after the previous word, divided by the number of times the previous word appeared, is the probability.
- In the count table, each row is a previous word and each column is a next word. The darker the cell, the more often that pair appeared.
- Switch the mode to and press again: you get the same sentence every time. Go back to and raise the — the sentences become stranger.
- Add one sentence of your own to the text. The table and the probabilities change right away, because the is only these counts.
Want to go further? Build it yourself in Python (AI Course) How the next word is chosen
A Tiny Probability Calculator
There are no candidates yet following the last word of the starting sentence.
| Previous wordNext word | |
|---|---|
Many words — showing only the first 8
Key Concept
The idea is simple: read many sentences and count which word comes right after which — including where a sentence ends. Those counts become a small table, and the table already lets the guess the next word. That is the whole model you build on this page.
Real works on the same idea, only far bigger. Instead of a small table of counts it learns billions to hundreds of billions of numbers, called , from examples, and it looks at much more than the one word before — your whole question and the conversation so far. It does not look up a stored sentence. But the last step is the same as yours: give every candidate a score, then choose.
This lab vs a real language model
| This lab () | Neural | |
|---|---|---|
| What it looks at | Only the previous word | The long it is given |
| What it learns | A table of counts | Numeric |
| and | No | Yes |
| Checks facts by itself | No | No |
Reference documents
Official documents related to this course’s environment. Checked on 2026-10-18.