# How do large language models work?

> LLMs predict text one token at a time from what fits in their context window. Tokens, context window sizes and the lost-in-the-middle effect.

Canonical: https://promptlyfound.com/llms/how-llms-work
Updated: 2026-11-20

## Short answer

A large language model **predicts the next token, over and over, from patterns it learned in training**. It reads only what fits in its context window, measured in tokens. Models use information at the start and end of a long context more reliably than information in the middle. Retrieval systems add web pages to the context window so the model can use and cite them.

## Key findings

- Anthropic says a token is about 3.5 English characters on Claude models before Claude 4.7, and that Claude 4.7 and later models produce about 30% more tokens for the same text. [6]
- Claude Fable 5.2, Opus 5.5, Sonnet 5.5 and Haiku 5.5, Anthropic's current lineup, have a context window of 1,000,000 tokens and a maximum output of 128,000 tokens. [2]
- Gemini 4 Argon, Google's flagship model, has a context window of 1,048,576 tokens shared by input and output, and a default maximum output of 65,536 tokens. [8]
- OpenAI retired GPT-4o from ChatGPT on February 13, 2026. [4]
- Language models often perform best when relevant information is at the start or end of a long context, and worse when it is in the middle. [5]

Whether an AI answer can use your page comes down to two things: whether the page makes it into the model's context window, and where your main point sits once it is there. Current models hold about 1 million tokens of context, and they still use the middle of a long context less reliably than the start and end. A large language model (LLM) generates text by predicting the next token, one token at a time, from patterns it learned in training, as [Wikipedia's article on large language models](https://en.wikipedia.org/wiki/Large_language_model) describes.[1]

## Training and inference

A model does not learn from your page when it answers a question, so your content reaches an answer in one of two ways: through training data, or through retrieval at the moment of the answer. Training and inference are the two phases behind that split, and they happen at different times.

In training, the model learns. It reads large collections of text, such as books, websites, academic papers, code and conversations, and predicts the next token in each sequence. The model compares each prediction with the actual next token and uses the error to adjust its parameters, the billions of numerical weights that define how it processes text. Training repeats this across billions of samples, on clusters of thousands of GPUs or TPUs, for weeks or months.[1] The result is a model with fixed weights.

Inference is what happens when someone uses the model. The model outputs a probability for every token in its vocabulary and selects one. It adds that token to the context and repeats the process until it produces an end-of-sequence token or reaches a length limit. Inference takes milliseconds to seconds per response.

At inference, the output depends only on the fixed weights and the text in the context window. The model does not learn from the prompt and has no database of facts to look up. Without retrieval, its output reflects only its training data. A retrieval system can add content to the context window at inference time, as described in [retrieval-augmented generation](https://promptlyfound.com/ai-search/rag).

## Tokens

Tokens set the price of every model call and the size of the chunks a retrieval system cuts from your pages, so they show up in both your AI budget and your content structure. A token is the unit of text that a model processes: a word, part of a word or a punctuation mark. Common words are single tokens. Less common words are split into several tokens. The context window, the cost of inference and the attention mechanism are all measured in tokens.

The amount of text in a token depends on the model's tokenizer and on the language. [Anthropic's glossary](https://platform.claude.com/docs/en/about-claude/glossary) says a token is about 3.5 English characters on Claude models before Claude 4.7. Claude 4.7 and later models use a newer tokenizer that produces about 30% more tokens for the same text.[6] Anthropic advises counting tokens with the model that will process the text. The ratio changes when a vendor ships a new tokenizer, so recheck any cost estimate that uses an older ratio.

A RAG system does not give a model a whole page. It retrieves chunks of the page, each a few hundred to a few thousand tokens long, and puts them in the context window with the query. A passage with its main claim in the first sentence becomes a chunk with the claim at the start. A passage with its main point after several sentences of preamble becomes a chunk with the claim in the middle.

## The context window

The context window caps how much source material a model can draw on for one answer, and a page outside it cannot be cited. [Wikipedia's article on the context window](https://en.wikipedia.org/wiki/Context_window) defines it as the total amount of text that a model can process in one inference pass, measured in tokens. The system prompt, conversation history, retrieved documents, user query and the output so far must all fit in it. Text outside the window is not available to the model, so the model cannot refer to it or cite it.[3]

  <thead><tr><th scope="col">Model</th><th scope="col">Context window</th><th scope="col">Maximum output</th><th scope="col">Note</th></tr></thead>
  <tbody>
    <tr><th scope="row">OpenAI GPT-6.2 Astra, GPT-6.2 Sol, GPT-6.1 Terra and GPT-6 Luna</th><td>1,050,000 tokens</td><td>128,000 tokens</td><td>Astra is OpenAI's flagship[7]</td></tr>
    <tr><th scope="row">Claude Fable 5.2, Opus 5.5, Sonnet 5.5 and Haiku 5.5</th><td>1,000,000 tokens</td><td>128,000 tokens</td><td>Anthropic lineup[2]</td></tr>
    <tr><th scope="row">Gemini 4 Argon</th><td>1,048,576 tokens, shared by input and output</td><td>65,536 tokens by default, up to 1,048,576 with long decode</td><td>Google's flagship[8]</td></tr>
    <tr><th scope="row">Gemini 3.8 Flash</th><td>1,048,576 input tokens</td><td>65,536 tokens</td><td>Google's Flash model[9]</td></tr>
  </tbody>

OpenAI retired GPT-4o from ChatGPT on February 13, 2026, as [its post on retiring GPT-4o and older models](https://openai.com/index/retiring-gpt-4o-and-older-models/) announced.[4] Its current models have larger windows. [OpenAI's models page](https://developers.openai.com/api/docs/models) lists a context window of 1,050,000 tokens and a maximum output of 128,000 tokens for GPT-6.2 Astra, its flagship, and for GPT-6.2 Sol, GPT-6.1 Terra and GPT-6 Luna.[7] [Anthropic's models overview](https://platform.claude.com/docs/en/about-claude/models/overview) lists a context window of 1 million tokens and a maximum output of 128,000 tokens for each of its current models: Claude Fable 5.2, Opus 5.5, Sonnet 5.5 and Haiku 5.5.[2]

Google's [Gemini 4 Argon model page](https://ai.google.dev/gemini-api/docs/models/gemini-4-argon) lists a context window of 1,048,576 tokens shared by input and output, with a default maximum output of 65,536 tokens.[8] [Gemini 3.8 Flash](https://ai.google.dev/gemini-api/docs/models/gemini-3.8-flash) accepts up to 1,048,576 input tokens and returns up to 65,536.[9] [Google's guide to understanding and counting tokens](https://ai.google.dev/gemini-api/docs/tokens) explains the difference. Gemini 4 models share one window of 1,048,576 tokens across input, thinking and output tokens. Gemini 3 and 2.5 models have separate input and output budgets.[10]

The size of the window sets how many retrieved passages a RAG system can add before the model writes. A model with a 128,000-token window can hold 32 times as much text as a model with a 4,000-token window, so it can draw on more sources. A larger window does not mean that the model uses everything in it equally.

## Lost in the middle

A page can be retrieved and still go unused if its answer sits in the middle of a long context. In 2023, researchers from Stanford University and Samaya AI published "[Lost in the Middle: How Language Models Use Long Contexts](https://arxiv.org/abs/2307.03172)".[5] They tested models on two tasks: answering questions from several documents, and retrieving a value for a key from a long list. The abstract reports:

> "performance is often highest when relevant information occurs at the beginning or end of the input context, and significantly degrades when models must access relevant information in the middle of long contexts, even for explicitly long-context models."

The result is a U-shaped curve. Models use information at the start and end of a long context reliably, and use information in the middle less well, even when it answers the query. The pattern resembles the primacy and recency effects of human memory. The study tested models available in 2023.

Position matters inside a RAG prompt too. Where a retrieved passage sits affects whether the model uses it. A paragraph that states its claim in the first sentence keeps the claim near the start of the chunk.

> Put the main claim of each paragraph in its first sentence. A retrieved chunk then carries its claim at the start, where models use information most reliably.

## Hallucination

Retrieval gives a model real documents to answer from, and only pages that are indexed and crawlable can be among them. A model asked about something outside its training data, such as recent events, private information or anything after its knowledge cutoff, has no reliable way to say that it does not know. It generates the most likely continuation of the prompt, and that continuation can be false. This is hallucination: the model predicts the next token, as it was trained to do, with no source to check the claim against.

A RAG system puts retrieved passages in the context window before the model writes, so the answer can rest on those documents. A page that is not indexed, not crawlable or not structured for passage retrieval is left out of them.

## What the mechanics imply for content

- Make sure the page is indexed and crawlable, so it can enter the retrieval pipeline. Run a URL through the [AI page check](https://promptlyfound.com/tools/ai-page-check) to see its crawler access and indexing rules.
- Make one claim per passage that makes sense on its own, so the passage still reads correctly when a retrieval system cuts it out as a chunk.
- Put the main claim at the start of each passage, so it sits where models use information most reliably.
- Make claims precise and attributed, so systems that check retrieved content against sources can use them.
- Include information the model cannot produce from its training data, such as original data or first-hand experience, so the page gives the model something it does not already have.

Training data is a separate route into AI answers. [Common Crawl](https://promptlyfound.com/crawlers/common-crawl) is one of the main sources of it.

## Sources

1. Large language model. Wikipedia. https://en.wikipedia.org/wiki/Large_language_model
2. Models overview. Anthropic. https://platform.claude.com/docs/en/about-claude/models/overview
3. Context window. Wikipedia. https://en.wikipedia.org/wiki/Context_window
4. Retiring GPT-4o, GPT-4.1, GPT-4.1 mini, and OpenAI o4-mini in ChatGPT. OpenAI, 2026-01-29. https://openai.com/index/retiring-gpt-4o-and-older-models/
5. Lost in the Middle: How Language Models Use Long Contexts. arXiv (Liu et al.), 2023-07-06. https://arxiv.org/abs/2307.03172
6. Glossary. Anthropic. https://platform.claude.com/docs/en/about-claude/glossary
7. Models. OpenAI API documentation. https://developers.openai.com/api/docs/models
8. Gemini 4 Argon. Google AI for Developers. https://ai.google.dev/gemini-api/docs/models/gemini-4-argon
9. Gemini 3.8 Flash. Google AI for Developers. https://ai.google.dev/gemini-api/docs/models/gemini-3.8-flash
10. Understand and count tokens. Google AI for Developers. https://ai.google.dev/gemini-api/docs/tokens

---

This is a plain-text copy of https://promptlyfound.com/llms/how-llms-work from Promptly Found. The web page is the canonical version and includes the charts.
