productinterview.tools
MenuClose

How LLMs Work: Tokens, Context and Sampling for AI Interviews

Tokens, attention, context and sampling.

Foundations3 lessons5 questions

What this module covers

  • What a token is, and why cost, speed and limits are all counted in tokens
  • The context window: everything the model can see at once
  • How a model generates text one token at a time, and why that leads to confident mistakes
  • Temperature and sampling: why the same prompt gives different answers

Why it matters, by role

  • Applied AI PMProduct Manager. Decides what a product should do, for whom, and why. Read the full explanation: cost, latency and hallucination questions all come back to tokens and next-token prediction.
  • Model PM: you'll be expected to explain model behaviour from first principles.
  • AI Infrastructure PM: serving cost and speed are driven by token counts.
  • AI TPMTechnical Program Manager. Owns plans, dependencies, risks and delivery for large cross-team programmes. Read the full explanation: capacity plans are measured in tokens.
  • Forward Deployed EngineerForward Deployed Engineer. An engineer embedded with customers who scopes, builds and ships AI solutions into production. Read the full explanation: customers ask why the model "lied"; you need a clear, correct answer.

Lessons

  1. Tokens and the context window
  2. Next-token prediction, and why models make things up
  3. Sampling and temperature

Tokens and the context window

Models don't read words; they read tokens, chunks of text from a fixed vocabulary. In English a token is roughly four characters, or about three-quarters of a word, so 1,000 words is about 1,300 tokens. Other languages and code often take more tokens for the same content.

Tokens are the unit for almost everything practical:

What Counted in tokens
Price Input tokens and output tokens, priced separately; output usually costs several times more
Speed Output is generated one token at a time, so long answers take longer
Limits The context window: the maximum tokens the model can take in at once, prompt and answer together

The context window is everything the model "knows" for this request beyond its training: the instructions, the conversation so far, retrieved documents and tool results. Anything not in the window, the model can't see.

Next-token prediction, and why models make things up

A language model does one thing: given the tokens so far, it predicts a probability for every possible next token. Generation is a loop: pick a token, add it to the text, predict again.

Inside the model, attention lets each position look back at all the earlier tokens and weigh which ones matter, which is how it follows instructions and keeps track of long context.

This explains a lot of behaviour:

  • Hallucination: the model produces what is plausible, not what is verified. If the right fact isn't in its training or its context, a fluent wrong answer can be the most likely continuation.
  • Knowledge cutoff: the model knows only what was in its training data, up to a date. Newer or private facts have to be put in the context (see RAGRetrieval-augmented generation. Fetching relevant documents and putting them in the model's context before it answers. Read the full explanation).
  • Instruction sensitivity: small wording changes shift the probabilities, so prompts matter.

Sampling and temperature

The model gives probabilities; sampling decides which token is actually chosen.

  • Temperature controls how adventurous the choice is. Low temperature (near 0) almost always picks the most likely token: focused and repeatable. Higher temperature spreads the choice more widely: more varied, and more likely to go off track.
  • Top-p limits the choice to the most likely tokens that together make up a set share of the probability.

For extraction, classification and anything you'll evaluate, use low temperature. For brainstorming or creative drafts, use higher. Even at temperature 0, outputs aren't guaranteed identical between runs, which is one reason evals re-run examples.

Questions

Rubric

Dimension Weak Passable Strong
Tokens Talks about words or characters Knows cost is per token Links tokens to cost, latency and context limits, with rough numbers
Mechanism "It searches its memory" Next-token prediction Next-token prediction plus attention, explained simply
Hallucination "It's a bug that'll be fixed" Plausible, not verified Explains the cause and the fixes: context, grounding, evals
Sampling Doesn't know Knows temperature Chooses settings for the task, and knows outputs still vary

Next modules