← All posts
AI explained

What is an LLM? What a large language model actually does when you type

An LLM predicts the next token, over and over, from a context window it cannot see past. That one fact explains hallucination, letter-counting, system prompts and what not to paste.

An LLM, a large language model, is a program that takes a sequence of text and predicts the next small piece of it. That's the whole job. Everything else you've seen it do, from writing a cover letter to fixing a stack trace, is that one operation repeated thousands of times, each prediction appended to the input and fed back in for the next.

If you searched "what is an llm" hoping for something more mysterious, I'm sorry. The mystery is that next-token prediction, at enough scale, produces text that looks like reasoning. Once you know what the machine is doing, the odd behaviour stops being odd and the safety advice stops being hand-waving.

What is an LLM doing when you press enter

Your message is cut into tokens. A token is not a word and not a letter; it is a chunk, often a common word, sometimes a fragment like ing or the with the leading space attached. The model only ever sees token IDs, numbers, never characters.

Those tokens go into a neural network of the transformer type. If you want the internals, Mukul wrote up what a neural network is and then what a transformer adds. For our purposes: the network reads the whole sequence at once, works out which earlier tokens matter for the position at the end, and emits a probability for every token in its vocabulary being the next one.

Then one token is chosen, appended, and the model runs again until it picks a special end-of-turn token or hits a limit. The words appear one after another on your screen because that is literally how they are produced.

Two settings govern the choice. Temperature scales how sharply the model prefers its top candidate: near zero it almost always picks the most likely token and output becomes deterministic; higher, and it samples more freely. Sampling methods like top-p trim the candidate list before the draw.

How it learned to do that

Training happens in stages, and the stages explain the personality.

Pretraining. The model is shown an enormous pile of text and, at every position, asked to predict the next token. It is scored on how wrong it was and its weights are nudged. Billions of times. At the end it has absorbed the statistical shape of human writing. It is not yet helpful: ask it a question and it may continue with three more questions, because that is what forum posts look like.

Fine-tuning. The model is then trained on examples of a prompt followed by a good answer. This teaches the format of being an assistant.

Preference training. Finally, it is shown pairs of its own answers and taught which one humans preferred. This is where "helpful, polite, declines dangerous requests" comes from, and also where a tendency to agree with you comes from, because agreeable answers get rated higher.

Nothing in any of these stages installs a fact-checker. Keep that in mind.

The context window is all it knows right now

The context window is the maximum number of tokens the model can take in one go: your conversation so far, attached files, the hidden instructions, and its own output. When a conversation exceeds it, something is dropped or summarised, which is why a long chat seems to "forget" what you said an hour ago.

The model has no memory between requests. When a chat product remembers your preferences across sessions, the product is storing notes and pasting them back into the window. Everything in the window is just text to the model. Your instruction, a document you uploaded, an email an agent fetched: all tokens, all weighted by the same mechanism. The model has no reliable way to know which tokens are yours and which came from a stranger. Kushagra's post on what prompt injection really is is the consequence of that single design fact.

Why it hallucinates, and why it can't count the Rs

A hallucination is a confident, fluent, wrong answer. Given the mechanism, that's not a malfunction. The model is choosing plausible next tokens. A citation that looks like a citation is plausible. So is a function name that looks like it belongs to that library. The model is not consulting a store of true things; it has only weights that make true things more likely when they appeared often in training. Rare facts, recent facts, and facts about you are where likelihood and truth part ways.

So the honest framing: an LLM is excellent at producing text of the right shape. It is unreliable at producing text that is true, and it can't tell the difference itself.

The letter-counting thing is the same cause from another angle. Ask how many Rs are in "strawberry" and a model may get it wrong, because it never saw the letters, only one or two token IDs for the word. Counting characters inside a token is like asking you how many pixels are in the letter A. Newer models often work around this by writing the letters out one at a time first.

System prompts, tools and agents

A system prompt is text the operator places at the top of the context window, before your message: "You are a support assistant for Acme, answer only about Acme products." It gets priority by training, not by any enforcement mechanism, so a well-crafted user message can sometimes override it. If your product's safety depends on the system prompt alone, it depends on a polite request.

Tools let the model do something other than emit text. The operator describes a function, say send_email(to, body), and the model is trained to output a structured call to it when useful. The surrounding software runs the function and pastes the result back into the window. The model never executes anything; it writes a request and reads a reply.

An agent is a loop around that: model picks a tool, software runs it, result goes in the window, repeat until done. Give that loop private data, a channel to the outside world, and untrusted input, and you've built what Simon Willison named the lethal trifecta. Kushagra's write-up of it is the one to read before you ship an agent, and the AI Audit will score what you've built against the OWASP LLM Top 10 in your browser.

What to use it for, and what not to paste

My rule: use an LLM where you can check the output faster than you could produce it, and where being wrong is cheap.

That covers drafting, rewording, explaining an unfamiliar error, generating test cases, summarising a document you'll still skim. You're the truth check the model doesn't have. Where the output is hard to verify and expensive if wrong (legal or medical specifics, exact figures, citations, recent events, security decisions), treat the answer as a lead to confirm, not a result.

On input, assume whatever you paste is stored, may be read by a human reviewer, and may be used for training unless the provider's terms say otherwise. So no credentials, API keys, customer records, or code you're bound not to share. Kushagra covered the reasoning in everything you paste into an LLM is a disclosure. Our prompt sanitiser strips keys, emails, IPs and other identifiers from a block of text before you paste it, entirely in the browser.

If you've got a log file open right now and were about to paste it into a chat, run it through the sanitiser first. That's the one habit I'd ask a new engineer to pick up today.

Frequently asked questions

Is an LLM the same as ChatGPT? No. ChatGPT is a product: a chat interface, a system prompt, memory features, tools, and a model underneath. The LLM is the model, and a product can swap models without you noticing.

Does the model learn from my conversation? Not during it. Weights are fixed once training finishes; what looks like learning is earlier messages still sitting in the context window. Whether your conversations later train a future version depends on the provider and your settings, which is why the paste rule above exists.

Can it browse the internet? Only if the operator gave it a search tool. Without one, it knows only its training data up to its cutoff and whatever is in the window. A model describing "today's news" without a tool is hallucinating.

AI explained
What carries over from web development, and what is newCareers

How to become an AI engineer, from a full-stack developer who is doing it

What the AI engineer job actually is, what carries over from web development, what is new, a learning order, three portfolio projects, and the security basics I wish I had known.

Forty questions, and the follow-up each one invitesCareers

Machine learning interview questions, with the answers interviewers want to hear

Forty machine learning interview questions with short answers, what a good answer must contain, and the follow-up each one invites. Fundamentals to LLMs to AI security.

Real data you collected, a baseline you beat, something deployedCareers

AI/ML final year project ideas from someone who builds apps for a living

Twenty-one AI/ML final year project ideas filtered the way a hiring manager reads them: real data you collected, a baseline you beat, and something deployed.