How Do Large Language Models Work, Step by Step
A large language model turns text into numbers, predicts the next word from patterns learned in training, and repeats that step until an answer is written.
On this page
Short answer
A large language model is a neural network trained to predict the next word in a sequence, and it writes answers by repeating that prediction one word at a time. It does not look up facts in a database; it uses patterns learned from training text to choose the most likely next word given everything written so far.
What is a large language model?
A large language model is a neural network trained to predict the next word in a sequence of text. It learns from a very large collection of books, articles, code and web pages, and it stores what it learned as numbers called weights. When you give it a prompt, it uses those weights to work out which word is most likely to come next.
The word large refers to the size of the model: how many weights it has and how much text it was trained on. A small model may have a few hundred million weights, while a large one has tens or hundreds of billions. More weights let the model hold more patterns, but they also make it slower and more expensive to run.
These models are general tools, not single-purpose programs. The same model can draft an email, summarise a document, write code or answer a question, because all of those tasks are, at the lowest level, the same job: predict the next word. A small language model does the same job with fewer weights, which makes it cheaper to run but less capable on hard tasks.
How does a model turn words into numbers?
A model turns words into numbers in two steps. First it splits text into pieces called tokens, which are usually whole words or parts of words. The sentence "the cat sat" might become three tokens, while a long word like "unbelievable" might become two or three. Each token is then given a number, its token ID.
Second, each token ID is turned into a list of numbers called an embedding. An embedding is a position in a high-dimensional space, and tokens with similar meanings end up close together. The embedding for "dog" sits near "cat" and "pet", and far from "engine" or "invoice". This is how the model represents meaning with arithmetic.
Embeddings also carry position information, so the model knows the order of the tokens. Without that, "the dog bit the man" and "the man bit the dog" would look identical. The model also adds a marker for where the text starts and ends, which matters when it has to decide when an answer is finished.
What happens inside the transformer?
Inside the model, the tokens pass through a stack of layers called a transformer. Each layer has an attention mechanism that lets every token look at the other tokens and decide which ones matter for understanding it. In the sentence "the bank raised its rates", attention helps the model connect "rates" to "bank" and pick the financial meaning rather than the riverbank one.
Each layer updates the embedding of every token with the context it gathered. Early layers tend to handle grammar and word order, while later layers handle meaning and longer-range connections, such as remembering the subject of a paragraph. A large model may have dozens of these layers, and each one refines the representation a little further.
After the last layer, the model produces a score for every token in its vocabulary, predicting how likely each one is to come next. It then picks one, usually the top-ranked token or a random choice among the most likely few. That chosen token is added to the text, and the whole process runs again for the following token.
How does training teach the model?
Training teaches the model by showing it enormous amounts of text and asking it to predict the next token, over and over. Each time it guesses wrong, the training process measures the error and adjusts the weights slightly in the direction that would have made the right answer more likely. This adjustment is called backpropagation, and it runs billions of times.
Training happens in stages. First comes pre-training, where the model learns general language patterns from a broad mix of text. Then comes fine-tuning, where it is trained on narrower data for a specific purpose, such as support conversations or legal documents. A final stage, often called alignment, teaches it to follow instructions and to refuse requests that break the rules set by its makers.
| Stage | What it teaches | Data used |
|---|---|---|
| Pre-training | General language patterns | Broad mix of text |
| Fine-tuning | One task or industry | Focused examples |
| Alignment | Instructions and limits | Rated responses |
Training is the expensive part. It needs many specialised chips running for weeks, plus large amounts of electricity and carefully prepared data. That is why most teams do not train a model from scratch. They use an existing model through an API or fine-tune one, which is far cheaper. A domain-specific fine-tune can adapt a general model to one industry without retraining everything.
How does a model write an answer?
A model writes an answer one token at a time. It reads the prompt, predicts the most likely next token, adds it to the text, and repeats the process. A 200-word answer therefore needs roughly 250 to 300 of these prediction steps, each one looking back at everything written so far.
The choice of token is not always the top-ranked one. A setting called temperature controls how much randomness is allowed. At a low temperature the model almost always picks the most likely token, which gives steady, predictable text. At a higher temperature it sometimes picks a less likely token, which makes the text more varied but also more likely to drift off track.
Because the model only predicts the next token, it has no separate planning step. It cannot check a whole answer before writing it, and it cannot look up a fact it never learned. That is why it sometimes produces confident-sounding text that is wrong. Grounding the model in your own documents, so it retrieves facts before writing, reduces this problem. A foundation model is the general base model that these writing systems are built on.
Example: a support team answers tickets with a model
Example (hypothetical): a support team handles 400 tickets a day. It connects a large language model to its help centre, so the model drafts each reply using the three most relevant articles, and a human agent reviews the draft before sending.
Each draft takes about 800 tokens of input and 200 tokens of output. At a planning rate of $2 per million input tokens and $8 per million output tokens, one draft costs about $0.0032: 800 tokens at $2 per million is $0.0016, and 200 tokens at $8 per million is $0.0016. For 400 tickets a day, that is about $1.28 a day, or roughly $28 a month for 22 working days.
The team measures the draft acceptance rate: agents send 300 of the 400 drafts unchanged, edit 70, and rewrite 30. The 30 rewrites show where the help centre is missing articles, so the team writes those first. The model is not making the team smarter; it is making the existing help centre faster to use.
What are the common mistakes?
The most common mistakes are treating the model as a database, skipping evaluation, and letting it act without review.
- Asking for facts it cannot know. A model predicts text, it does not look things up. Give it the source material or expect invented details.
- No evaluation set. Without a fixed set of test prompts and known good answers, you cannot tell whether a new model version is better or worse.
- High temperature for factual work. Creative settings make summaries and quotes less reliable. Keep temperature low for factual answers.
- No human review on customer-facing output. Drafts should pass a person before they reach a customer, especially for billing or legal topics.
- Ignoring cost per token. Long prompts and long answers add up. Short, focused prompts with only the needed context keep the bill down.
If you are planning to build on a model, LLM development covers the production setup, including retrieval, evaluation and cost control.
Key takeaways
- A large language model predicts the next token, one step at a time, using patterns learned in training
- Tokens and embeddings turn words into numbers the model can do arithmetic on
- Attention lets each token gather context from the rest of the text
- Training adjusts the model's weights by measuring prediction errors over huge amounts of text
- Temperature controls how predictable or varied the output is
- Grounding the model in your own documents reduces invented facts
Frequently asked questions
Do large language models understand what they write?
Not in the human sense. They predict likely next tokens from patterns in training text, which produces fluent language without a separate understanding step. That is why they can write confidently about things they have wrong, and why grounding them in source documents matters.
How many tokens is a page of text?
Roughly 500 to 750 tokens per page of plain English, depending on the words used. A token is usually a word or part of a word, so long technical terms use more tokens than short common words.
What is the difference between a large language model and an AI agent?
A large language model is the prediction engine. An AI agent is a system built around it, with tools, memory and rules for taking actions. The agent calls the model, checks results and decides what to do next.
Why do models sometimes repeat themselves?
Repetition happens when the model keeps predicting the same likely token in a loop, often at low temperature or with a short prompt. A longer, more specific prompt and a slightly higher temperature usually break the pattern.
Can a model be trained on my company's documents?
Yes, through fine-tuning or retrieval. Fine-tuning adjusts the weights on your data, while retrieval keeps the base model unchanged and feeds it relevant documents at answer time. Retrieval is usually cheaper and easier to update.
How long does it take to train a large language model?
Pre-training a large model takes weeks on many specialised chips and costs far more than most teams can justify. Fine-tuning an existing model on one industry's data is a much smaller job, often measured in days rather than months.