How LLMs work

First, the name. LLM is short for large language model: the kind of AI behind tools like ChatGPT, the thing you send a prompt to and get text back from. This chapter is the working picture of what one actually is.
You type a question, and a few seconds later a fluent, confident answer appears. It reads like the model understood you and looked up the answer. That impression is useful, and it is also wrong in ways that matter, and the gap between the two is where most beginner mistakes come from.
This is the chapter that pays off for the rest of your career with these tools. You do not need to know the math behind training a model to build with one. You do need a working picture of what is actually happening when it answers, because that picture explains everything else: why prompting works, why the same question gives different answers, why models invent facts, why they cost what they cost, and why some features are hard that look like they should be trivial. Everything later in this handbook is a consequence of the ideas here.
What a model actually is
Strip away the chat interface and a large language model is one thing: an enormous mathematical function, a fixed set of billions of numbers called parameters (you will also hear them called weights). That is the whole model. Not a database, not a search engine, not a little person in a box. It is a very large pile of numbers that takes text in and produces text out.
Those numbers did not come from a programmer typing them. They were set during training. The provider fed the model a staggering amount of text, books, code, articles, web pages, and ran a simple drill billions of times: hide the next word, ask the model to predict it, and nudge its numbers slightly whenever it guessed wrong. Repeat that at enormous scale and the numbers slowly settle into values that are good at predicting text. That is what training is: tuning billions of dials until the prediction gets good.
Two things follow from this, and they explain a lot of behaviour you will meet later. First, training happens once, ahead of time, and it is hugely expensive. By the time you call the model, the numbers are frozen. The model does not learn from your conversation. Nothing you say changes its parameters. It feels like it remembers and adapts, but that is the software around it, not the model itself, a point that comes back hard in the context window section.
Second, there is no table of facts inside it. The "knowledge" lives as patterns baked into those numbers, the way a baker who has made ten thousand loaves has the technique in their hands without reading a recipe. The model learned the shape of correct, sensible text. That is why it can be fluent and wrong in the same sentence: it is reproducing the shape of a good answer, not reading a stored fact off a shelf.
Predicting the next token
With that pile of numbers in place, the model does exactly one job: it predicts the next chunk of text, over and over.
Here is the loop in slow motion. You give it some text. It runs that text through all those parameters and produces a probability for every possible next chunk it knows about, tens of thousands of options at once. After "The capital of France is", the chunk "Paris" might get a 97% score, "a" a 1% score, and everything else almost nothing.
The model then picks one chunk, adds it to the text, and runs the whole thing again to pick the next. It repeats until the answer is done.
That is the entire engine. A reply you read as a single thought was built one chunk at a time, each chunk chosen from a fresh ranking of likelihoods, with no plan for where the sentence is going beyond "what is probable next". Every capability you will use, chat, code, translation, tool use, reasoning, is this same loop running underneath.
The phone keyboard that suggests your next word is the same idea in miniature. The difference is the billions of parameters behind the guess. Trained on enough text, "what is the most probable next chunk" turns out to be good enough to write working code and clear explanations. But the underlying move never changes: it is prediction, not understanding, and not looking up a stored answer.
The randomness dial
If the model always grabbed the single highest-scoring chunk, it would hand you the same answer to the same prompt every time, and that answer would often be flat and repetitive. So most of the time the pick has a little randomness in it: it usually takes a high-probability chunk, but not always the very top one. This is why you can ask the exact same question twice and get two different replies. That is the system working as designed, not a fault.
You control how much randomness through a setting called temperature, which you set when you call the model in the chapter on calling models from code. The intuition is worth having now. At a low temperature, the model sticks close to its top picks: focused, consistent, predictable, which is what you want for pulling a number out of an invoice or sorting text into categories. At a higher temperature, lower-probability chunks get chosen more often: more varied and creative, and more likely to wander somewhere wrong. Brainstorming and creative writing want the higher end; anything with a correct answer wants the lower end.
So a single dial slides the model between "careful and repeatable" and "surprising and inventive". It is the same prediction loop either way; temperature only changes how boldly the model reaches past its safest guess.
Tokens
The "chunks" a model predicts have a name: tokens. A token is a piece of text, often a whole word, sometimes only part of one. Common words are usually a single token, while longer or rarer words get split into several.
"tokenization" -> "token" + "ization"
"unbelievable" -> "un" + "believ" + "able"
"cat" -> "cat"Why chop text into these odd pieces instead of using whole words or single letters? It is a trade-off. Single letters would make every sequence enormously long, and length is what the model has to compute over. Whole words would need a vocabulary of millions of entries and would still break the first time someone invents a word or makes a typo. Tokens are the middle path: a fixed vocabulary of a few tens of thousands of common pieces that can be snapped together to spell anything, including words the model has never seen.
Here is the under-the-hood consequence that surprises people. The model never sees letters. It sees tokens, which to it are really ID numbers for those chunks. So tasks that look effortless to us can be hard for it: counting the letters in a word, reversing a string, noticing that "strawberry" has three r's.
It is not being dim. It does not perceive the individual characters at all, the way you cannot taste the separate grains of flour in a finished loaf. When a model fumbles a spelling or character-counting task, this is why, and the fix is usually to do that part in normal code instead.
Tokens are also the unit of two things you will care about constantly:
- Money: providers bill per token, both the tokens you send and the tokens the model writes back, so a long document in and a long answer out both cost more. A rough gauge for English is about four characters per token, or 100 tokens for every 75 words; code and other languages often split into more tokens, which is part of why they can cost more. You will meet this directly when you start calling models from code.
- Limits: there is a ceiling on how many tokens the model can handle at once, which is the next section.
The context window
The context window is how much text the model can take in at one time, measured in tokens. Picture it as the size of the model's desk. Everything involved in a single request has to fit on that desk at once: your instructions, the conversation so far, any documents you pasted in, and the reply the model is writing.
Why is there a limit at all? It comes from how the prediction works. To choose the next token, the model weighs how every token in the input relates to every other token, so it can tell that "it" refers to the cat three sentences back. That all-to-all comparison is what lets the model track meaning across a passage, and it is also what makes long inputs expensive to process. The provider sets a ceiling, the context window, to keep each request manageable.
When the desk is full, something has to come off. If a conversation runs long enough to overflow the window, the earliest parts fall away and the model can no longer see them. This is why a long chat seems to "forget" what you said at the start. It did not forget in any human sense; those tokens are off the desk. It also helps to know that models tend to pay closest attention to the beginning and end of what is on the desk and can lose track of things buried in the middle, so where you put important text matters when you fill the window.
Now the point that catches almost every beginner, and it matters more than all the rest. The model has no memory between separate requests. Remember that its numbers are frozen. Each call starts with a blank desk. The model does not remember your last question, your name, or anything from a minute ago.
A chat app feels like one flowing conversation only because the app quietly resends the entire history with every new message. The continuity is something the software around the model builds, not something the model has.
That single fact shapes how you build everything from here. If you want the model to know something, you have to put it on the desk in that request: the conversation so far, the user's details, the relevant documents, all of it, every time. A large part of this handbook is really about doing that one thing well, getting the right text onto the desk.
What the model does not know
All of a model's knowledge was baked in while it was training, so it only knows what was in that training data, and that data was gathered up to some cutoff point in the past. Two limits fall straight out of this.
First, it does not know anything that happened after its cutoff. Ask about last week's news or a library that came out this month and it has no real idea, though it may happily produce a plausible-sounding answer anyway. Second, it never knew anything private. Your company's internal docs, a user's order history, the contents of your database: none of that was in the public text it trained on, so it cannot know it, no matter how nicely you ask.
There is a quieter limit too. The model usually cannot tell you where something it "knows" came from, because the knowledge is blended together inside it rather than filed away as separate, sourced facts. It can write you a citation, but it is predicting what a citation should look like, not looking up a real one.
This is the reason for a technique you will meet later called retrieval, or RAG. If you want a model to answer about recent events or your own private data, you do not retrain it. You fetch the relevant text yourself and set it on the desk at request time, so the model has the facts in front of it instead of guessing. A model is a reasoner over the text you give it far more than a reliable well of specific facts.
Why models make things up
Sometimes a model states something false with total confidence: a quote no one said, a function that does not exist, a citation to a study that was never written. This is called a hallucination, and after the last few sections it should feel almost inevitable rather than mysterious.
The reason is that the model has no internal sense of "true" versus "false". It only has "likely text" versus "unlikely text". Most of the time those line up, because in the text it learned from, true statements are far more common than false ones, so the probable continuation is usually the correct one.
But when the model hits a gap, something it was never trained on, or your private data it could never have seen, it does not stop and flag the gap. It cannot reliably tell what it does not know. It does the only thing it ever does: produces the most probable-sounding continuation.
A confident, well-formed, wrong answer is often exactly what sounds most probable. The same machinery that makes it fluent makes it fluent when it is wrong.
This is the part to sit with: hallucination is not a glitch you can fully prompt away. You can reduce it, and later chapters show how. Telling the model it is allowed to say "I do not know" helps a little. Asking it to answer only from text you provide helps much more, because now there is no gap to fill from frozen training. But the tendency comes from what the model fundamentally is, a probability machine with no fact-checker inside, so the durable fixes are structural: give it the facts to work from rather than hoping they were in its training, and verify answers that matter rather than trusting them.
Carry one rule out of this chapter above all others: a model's confidence tells you nothing about whether it is right. Fluency and correctness are produced by the same process and come apart all the time. Build as though any single answer could be wrong, because any single answer could be.
Putting it together
Everything in this chapter is one idea seen from different angles. A model is a frozen pile of numbers that predicts the next token, and almost every behaviour you will deal with falls out of that:
- It predicts one token at a time from a ranking of probabilities, so it improvises rather than recalls, and a touch of randomness (temperature) means answers vary.
- Tokens are the chunks it works in: the unit of your bill, the unit of your size limit, and the reason it cannot reliably count letters.
- The context window is a single request's desk, shared by everything, and the model remembers nothing between requests, so continuity is your job.
- Its knowledge is frozen at a training cutoff and never included your private data, which is why you fetch facts and put them in the prompt.
- Hallucination is the price of a system that predicts likely text with no sense of truth, reduced by grounding and checking, never by clever wording alone.
Hold these together and a surprising amount of AI engineering turns out to be one task in different costumes: get the right text onto the desk, in the right shape, at the right moment, and never trust the output blindly. The next chapter, Open and closed models, looks at what kinds of models there are and how you get one, and then prompting is where you start steering all this prediction toward what you actually want.

