Search Tutorials
--Ads--How Large Language Models Work
Tokens, context windows, temperature and hallucination explained for RAG developers.
Before you build a RAG system you need a working mental model of the component at its centre: the large language model (LLM). This lesson covers the five ideas that matter most for RAG developers: tokens, next-token prediction, the context window, how output randomness is controlled, and why LLMs hallucinate.
Next-token prediction
An LLM is a neural network trained on a very large amount of text to do one thing: given a sequence of text, predict what comes next. It does not look facts up in a database. It produces a probability for every possible next piece of text, picks one, appends it, and repeats until the answer is finished.
Everything the model "knows" is stored in its weights as statistical patterns learned during training. This explains both why LLMs are so capable and why they need help with facts they never saw, which is exactly what RAG provides.
Tokens
LLMs do not read characters or words. They read tokens: chunks of text produced by a tokenizer. A common English word is often one token; a rare word may be split into several. As a rough rule, one token is about three to four characters of English text, and 1,000 tokens is about 750 words.
Tokens matter in RAG for three reasons:
- Cost. API pricing is per token, for both the input (your prompt plus retrieved context) and the output.
- Limits. The context window and the maximum output length are measured in tokens.
- Latency. More input tokens take longer to process, and every output token is generated one at a time.
You can count tokens exactly with the API before sending a request:
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
result = client.messages.count_tokens(
model="claude-opus-5-5",
messages=[{"role": "user", "content": "What is Retrieval-Augmented Generation?"}],
)
print(result.input_tokens)
Each model family has its own tokenizer, so the same text gives different token counts on different models. Always count with the model you will actually use.
The context window
The context window is the maximum number of tokens the model can consider at once: the system prompt, the conversation history, any retrieved documents and the answer it is writing. Current Claude models have a context window of 1 million tokens, which is enough for several long books.
A large window does not make retrieval unnecessary. Filling it costs money and time on every request, and models answer more reliably when the context contains the relevant passages rather than everything you own. Choosing what goes into the window is the core job of a RAG system. The lesson Long-Context Models vs RAG looks at this trade-off in detail.
Controlling the output
Older tutorials control randomness with temperature, top_p and top_k. On current Claude models these sampling parameters are no longer accepted. You steer the output in two other ways:
- The prompt. Clear instructions, examples and output formats (covered in the next lesson) have far more effect on answer quality than sampling settings ever did.
- Effort. The
effortsetting (low,medium,high,xhigh,max) controls how much the model thinks before answering. Low effort is fast and cheap and is fine for most RAG answers; higher effort helps with multi-step reasoning.
Other providers and open-weight models still expose sampling settings. The lesson LLM Inference, Temperature and Sampling explains how they work and when they matter.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1000,
output_config={"effort": "low"},
messages=[{"role": "user", "content": "Explain what an LLM is in two sentences."}],
)
for block in response.content:
if block.type == "text":
print(block.text)
print("Input tokens:", response.usage.input_tokens)
print("Output tokens:", response.usage.output_tokens)
Knowledge cutoff and hallucination
A model's knowledge stops at its training cutoff. It knows nothing about events after that date and has never seen your private documents: your HR policy, your product's latest release notes or yesterday's support tickets.
When a model is asked about something it does not know, it may still produce a fluent, confident and wrong answer. This is called hallucination. It happens because the model is optimised to produce plausible text, and a plausible-sounding answer is not always a true one.
RAG attacks both problems. It puts the relevant, current facts into the context window and instructs the model to answer only from them. The model then works as a reader and summariser of trusted text instead of relying on memory.
RAG reduces hallucination but does not remove it. If retrieval returns the wrong passages, or the prompt does not tell the model to stay within them, the model can still make things up. Later modules show how to measure and control this.
Key takeaways
- An LLM generates text one token at a time by predicting the most likely continuation.
- Tokens determine cost, limits and latency; count them with the API for your model.
- The context window is large but not free: deciding what to put in it is the job of RAG.
- On current Claude models you steer output with the prompt and the
effortsetting, not temperature. - LLMs have a knowledge cutoff and can hallucinate; RAG grounds them in retrieved, trusted text.