Please enable JavaScript to use CodeHS

LLM Tutorial

How does an LLM answer questions?

An LLM does not look up facts in a database. It writes an answer one token at a time, choosing the most likely next word over and over again.

AI ~10 min Beginner friendly
The Big Idea

One token at a time

When you ask an LLM a question, it does not write the whole answer in one flash. It writes the first word, then the second, then the third. Each new word is a guess based on the words that came before it.

This is called autoregressive generation. The model looks at everything it has written so far, including your prompt, and picks the next token that seems most likely.

Key idea: an LLM answers questions by repeatedly asking itself, “What word probably comes next?”

Because it works one token at a time, the model can write long answers. But it also means mistakes can creep in. If it picks a wrong word early, the rest of the answer may try to cover up that mistake rather than fix it.


The Prompt

The power of the prompt

The words you give the model are called the prompt. The prompt is the model’s starting point. A clear prompt usually gets a better answer than a vague one.

For example, “Explain photosynthesis” is okay. “Explain photosynthesis like I am in fifth grade, in three sentences” is much better. The model uses your instructions to steer its next-token guesses.


Interactive

See it generate

Choose a prompt and watch a simulated LLM write an answer one token at a time. The token counter shows how many tokens have been generated.

0 tokens generated

Interactive

Prompt matters

The same topic can get very different answers depending on how you ask. Pick a style below and see how the response changes.

Simple

“Explain gravity in one sentence.”

Detailed

“Explain gravity to a curious 10th grader.”

Creative

“Tell a short story about gravity.”


Code

Sampling with temperature

LLMs do not pick the next word at random. They use a temperature setting to control how creative or careful the output is. This Python function shows the same idea with a tiny vocabulary.


Your Turn

Check yourself

Why might an LLM give a different answer to the same question if you ask it twice? Think about the role of probability and the prompt.

Sample answer: The model picks the most likely token at each step, but sometimes several tokens are almost equally likely. A small random choice can change the whole answer.


Check Yourself

Quick quiz

1. How does an LLM write an answer?

2. What is a prompt?

3. Why can the same question get different answers?

Keep learning about AI

You know how an LLM answers one token at a time. Next, see how LLMs become agents that can take actions, not just write text.

Part of the “Are LLMs smarter than a 5th grader?” tutorial series.