What Is GenAI? LLMs & Tokens

Generative & Agentic AI · AI Foundations · Basics · 12 min

How ChatGPT-like models actually work: predicting the next token.

A writer who can only see the sentence so far, picks the next word from a ranked list of guesses, writes it down, and picks again. Thousands of tiny picks become a whole answer.

The idea in everyday words

An LLM is the world's best autocomplete. Your phone keyboard guesses the next word after 'Happy…' → 'birthday'. An LLM did the same thing after reading a huge chunk of the internet, so its guesses are so good they look like thinking.

Key words

  • Model – a giant math function with billions of numbers (parameters) learned from data.
  • Token – a chunk of text (~¾ of a word). "unbelievable" → "un", "believ", "able".
  • Context window – how many tokens the model can 'see' at once (e.g. 128k–1M).
  • Inference – using the model to generate text.
  • Temperature – randomness. 0 = predictable, 1 = creative.

How generation works

  1. Your prompt is split into tokens.
  2. The model scores every possible next token.
  3. It picks one (based on temperature), adds it, and repeats.

That's why answers stream word-by-word, and why models can confidently invent facts (hallucinate): they predict likely text, not true text.

The transformer (one sentence)

Its superpower is attention: for every word, it looks at all other words to decide which matter. In "The cat sat on the mat because it was tired", attention links it → cat.

Step by step

  1. Prompt:

    'The sky is'

  2. Tokenize:

    ['The', ' sky', ' is'] → IDs [464, 6766, 318]

  3. Predict:

    Scores: ' blue' 62%, ' clear' 12%, ' falling' 1%…

  4. Pick & repeat:

    Add ' blue' → predict next → ' today' → '.'

Code example

text = "Generative AI predicts the next token, one token at a time."
# Very rough rule: 1 token ~ 4 characters of English
approx_tokens = len(text) / 4
words = text.split()
print("words:", len(words))
print("approx tokens:", round(approx_tokens))

# toy next-word predictor
counts = {"the sky is": {"blue": 62, "clear": 12, "falling": 1}}
options = counts["the sky is"]
print("next word:", max(options, key=options.get))

Interview tips

  • Explain tokens, context window, temperature in one breath each.

  • Know hallucination and how to reduce it (RAG, grounding, lower temperature).

  • Popular models: GPT, Claude, Gemini (closed); Llama, Mistral, Qwen, DeepSeek, Gemma (open weights).

Challenge: Token Estimator

Estimate tokens with the rule 1 token ≈ 4 characters: return ceil(len(text) / 4).

Example: estimate_tokens("hello world") → 11 chars → 3

Solve it in the browser with tests, hints and an AI coach.

Practice problems

More in AI Foundations

Previous: Neural Networks from Scratch · Next: Tokenization & Byte-Pair Encoding