← The 16-day journey
Chapter 05 · The LLM, from scratch

What an LLM actually is, and how one gets built

You have used ChatGPT. You have probably submitted an assignment it wrote. Now find out what the thing on the other side actually is — not the marketing version, the real one. By the end of this chapter you will know what is inside the box, how it was made, and who made it. The next chapter teaches you to use it like an engineer.

A wide field of scattered text fragments funnelling through three stage gates and emerging as one compact ordered core.
A huge slice of the internet, pressed in stages into one file of numbers. That file is the model.

01It is not a search engine, and it is not a database

Start by throwing away two wrong pictures. An LLM is not Google — it does not go and look things up when you ask (by itself, it has no internet at all). And it is not a database — there is no table inside it with facts in rows. If you opened up the file that is the model, you would find neither documents nor sentences. You would find numbers. Billions of them. Nothing else.

So what is it? Here is the honest one-line answer, and everything in two chapters unfolds from it: an LLM is a machine that predicts the next word. Given some text, it answers one question, over and over: “what word most likely comes next?” It writes essays one word at a time, each word chosen by that same single trick, thousands of times in a row.

Your phone’s keyboard autocomplete does a tiny version of this. The difference is scale — and scale, it turns out, changes everything. Predict the next word well enough, across enough of human writing, and the machine has to absorb grammar, facts, styles, code, even something that behaves like reasoning — because all of those are needed to guess the next word well. That is the strange, genuine surprise the whole AI era is built on.

Here is the picture to keep. Imagine a friend who has read almost every book, magazine and manual ever printed in India — and who is sitting in a room with no phone and no internet. Ask them to explain how GST works, or to write your project’s abstract, and they are brilliant. Ask them today’s IRCTC seat availability, or what your college notice board said this morning, and they will still answer — confidently, and wrongly — because guessing the next word is the only thing they can do. That gap is the whole reason the rest of this course exists. Chapters 9 to 11 are about handing that friend the right page to read before they speak.

02Watch it choose a word

The model never picks one word with certainty. For every position it produces a probability list — every word it knows, each with a score for “how likely is this to come next”. Then one word is drawn from the top of that list, the sentence grows by one word, and the whole thing repeats. Feel it yourself:

Interactive · predict → pick → repeat

The final year project that got me the job was

The model's next-token list — click one to pick it

Notice: the top word is likeliest, not certain. A real model rolls a weighted dice over this list — that roll is what the temperature knob (next chapter) controls.

A teaching toy — the percentages are illustrative. The loop is exactly real.

Two things to take from playing with that. First, the model samples — it does not always take the top word, which is why the same question gives different answers on different days. Second, there is no plan for the sentence in advance. The essay that looks planned was improvised one word at a time. Keep both facts; the next chapter gives you the dials that control them.

03Tokens — the model’s syllables

One correction to everything above: the model does not actually predict words. It predicts tokens — chunks of text, usually a short word or a piece of a longer one. “cat” is one token; “unbelievable” might be “un” + “believ” + “able”. A rough rule for English: one token ≈ three-quarters of a word.

Why should you care? Three reasons that will follow you through every AI system you ever build. Models are priced per token — you literally pay per syllable, in and out. Models are limited in tokens — the context window from Chapter 2 is measured in them. And some famous failures — like a model miscounting the r’s in “strawberry” — happen because the model sees tokens, not letters. It never saw the individual r’s; it saw chunks.

04Where the knowledge lives — parameters

If there is no database inside, where do the facts live? In the parameters — the billions of numbers I mentioned. Think of them as tiny adjustable dials. During training, every dial gets nudged millions of times until the machine’s next-word guesses become excellent. Whatever the model “knows” — Delhi is the capital of India, code needs matching brackets, essays have introductions — is smeared across those dials as patterns. No single dial holds a fact, the way no single neuron in your brain holds your mother’s name.

Two consequences, both interview favourites. The model is frozen: after training ends, the dials stop moving. It learned nothing from your chat yesterday, and it knows nothing after its knowledge cutoff — the date its training data ends. Ask about last week’s news and it either admits ignorance or — worse — guesses fluently. The model is a file: those parameters can be written to disk, copied, downloaded. A “7B model” is a file of seven billion numbers — small enough to run on a good laptop. Frontier models have far more and need racks of specialised machines. Same idea, different sizes.

05The trick inside: attention

One piece of machinery deserves plain-words treatment, because every AI job description mentions it: the transformer, the architecture (Chapter 1 word!) almost every modern model is built on. Its key idea is called attention, and you can understand it with one sentence: “The trophy didn’t fit in the suitcase because it was too big.” What does “it” refer to? You instantly knew: the trophy. To know that, you weighed every earlier word and decided which ones matter for the word you are processing now.

Attention is that skill, made mathematical. For every token it processes, the model computes how much each earlier token should influence it — a spotlight sweeping back over the text, brighter on relevant words, dim on filler. Older systems read left to right and forgot; transformers let every word look at every other word at once. That is the invention (from a famous 2017 paper titled, genuinely, “Attention Is All You Need”) that made today’s models possible. You now know what a transformer is at the depth an interviewer actually expects from a fresher: the architecture that lets models weigh all context at once.

06How one is built — the full pipeline

Now the part almost nobody explains properly: where does a model come from? It is built in stages, like a person being educated, and different people run each stage.

Diagram · the four stages that build a model
1 · datadata engineersclean text2 · pretrainGPUs · monthsbase model3 · fine-tuneannotatorsassistant4 · preferencehuman rankingRLHF-polishedchattrillions of tokensthe expensive oneknows everything,no mannerslearns to be helpfullearns what people prefer+ safety / red-teaming
Curate the food, learn the world, learn manners, learn taste. A different team runs each stage.

Stage one: the data. Teams collect a colossal slice of human text — websites, books, code repositories, articles — measured in trillions of tokens. Data engineers spend months cleaning it: stripping junk, removing duplicates, filtering out the worst of the internet. Unglamorous, decisive work — a model can only learn patterns that exist in its food.

Stage two: pretraining. The expensive one. The model starts as billions of dials set to random noise, and the training system feeds it text with one endless exercise: here is a passage with the next token hidden — guess it. Wrong? Nudge the dials. Repeat trillions of times. This runs on thousands of specialised processors (GPUs) for months, burning electricity at industrial scale — which is why frontier-model training costs more than most companies will ever raise, and why only a handful of labs (OpenAI, Anthropic, Google, Meta, and a few others) do it at the top end. What comes out is a base model: a machine with astonishing knowledge of language and the world — and zero manners. Ask it a question and it might continue with three more questions, because on the internet, questions are often followed by more questions. It completes text. It does not yet help.

Stage three: fine-tuning. Teaching the wild autocomplete to behave like an assistant. Humans write thousands of examples of ideal behaviour — a question, then the answer a good assistant would give — and the model trains on these until it learns the shape of being helpful: answer the question, be clear, stop. This is a real, global workforce — data annotators and labellers, many of them in India — writing and grading examples all day. Fine-tuning is also how models get specialised: take a general model, tune it further on medical text or legal documents or your company’s style. (Remember this — it returns near the end of the journey.)

Stage four: preference training. One more polish. The model writes several answers to the same prompt; human reviewers rank them — this one is clearer, that one is subtly wrong, this one is rude — and the model is nudged toward the kind of answer people prefer. The umbrella term you will hear is RLHF (reinforcement learning from human feedback). Safety work lives here too: red teams deliberately try to make the model behave badly, and what they find gets trained against. The result of all four stages is the polite, helpful thing you meet in a chat window — an autocomplete that has been to finishing school.

So when someone asks “who builds these models?” you have a real answer: research scientists design the training, infrastructure engineers keep thousands of GPUs alive for months, data teams curate the food, annotators teach it manners, safety teams try to break it. It is one of the largest engineering efforts in the world right now — and every stage is a job that exists today.

07Open weights vs closed — and why Ollama works

Remember: a trained model is a file. Some labs publish that file for anyone to download — those are open-weight models, like Meta’s Llama family or Mistral’s. Others keep the file private and sell access through an API — the way you use GPT or Claude, request in, response out, exactly like the backends you built in Chapter 3. That is the entire mystery behind a tool you will meet next chapter called Ollama: it simply downloads open-weight model files and runs them on your own laptop. Same machinery, your hardware, no API bill, full privacy — just smaller dials than the frontier.

08The model has no memory of you

One last mind-bender that most users never learn. The model is stateless — Chapter 3 word. It holds no conversation in its head between messages. When you send your tenth message in a chat, the app silently re-sends the entire conversation so far, and the model reads it all again, from scratch, to predict what comes next. The “memory” of your chat lives in the app’s database (Chapter 4!), not in the model. This is also why long conversations get expensive — you are re-paying, in tokens, for the whole history on every turn — and why the context window eventually fills. Almost everything clever in the later chapters — giving models memory, documents, tools — is engineering built around this one limitation.

09Why it lies with confidence

Now you can understand AI’s most famous flaw from the inside. Ask the model for a citation, a court case, a paper — and it may produce one that does not exist, formatted perfectly, delivered confidently. This is called hallucination, and knowing the mechanism, you can see it is not a bug in the code. The machine predicts plausible next tokens. Truth was never the target — plausibility was. Usually the most plausible continuation is also true (that is what training on the world’s text buys you); sometimes it is just something that sounds true.

Hold this thought firmly, because it becomes a design principle later in the journey: never treat model output as fact — treat it the way Chapter 3 taught you to treat user input: validate it at the door. Entire architectures exist to pin models to real documents so they stop inventing — that is the RAG part of this journey, and now you know exactly why it must exist.

10Do this today

Go talk to a model with your new eyes. Give it half a sentence and ask it to continue — watch the autocomplete nature show itself. Ask the same question three times and compare the answers — sampling, live. Ask it something after its knowledge cutoff and watch how it handles not knowing. Ask how many r’s are in “strawberry” — and now, unlike everyone else sharing that screenshot, you know why it struggles. Then explain to a friend, in your own words, the four stages that built the thing you were just talking to. That explanation is this chapter’s exam.