How Large Language Models (LLMs) Work: The World's Smartest Autocomplete

How do tools like ChatGPT, Claude, and Gemini write stories, answer questions, and write code? By playing the world's fastest guessing game: predicting the next word.

Foundational Knowledge & Simpler Primers
Need a simpler explanation or feeling stuck?

Need to review how words become numbers or how neural networks work? Review:

Unsure of mathematical notation or technical terms on this page? Our 57-term AI Glossary breaks down every concept with plain-English analogies and rigorous engineering specs.
Open AI Glossary (57 Terms)

1. Theoretical Motivation & Foundations

When you text a friend on a smartphone, you have probably noticed three little word suggestions appearing above your keyboard. If you type 'I am on my...', your phone suggests 'way'. That is an autocomplete system. A Large Language Model (like GPT-4, Claude, or Gemini) is fundamentally the world's most advanced autocomplete. Instead of only looking at the last two words, an LLM looks back at thousands of words in your conversation. And instead of reading a small dictionary, it read hundreds of billions of words from books, scientific papers, encyclopedias, and computer code across the entire internet during its 'pre-training' phase. When you ask an AI a question, it does not stop to reflect or meditate. It immediately calculates a probability score for every word in its vocabulary: 'Which word makes the most statistical sense to come next?' It picks one, adds it to the sentence, and repeats the process. One word at a time, like placing beads on a string, an entire paragraph unfolds. The magic is that language contains logic, reasoning, and knowledge. By learning to predict the next word with superhuman accuracy, the model accidentally learned how to explain physics, write poetry, and debug Python code!

2. Mathematical Formulations & Derivations

The governing analytical formulations and proof frameworks for this module:

Autoregressive Generation (The Next Token Probability): Given a sequence of words: W = (w₁, w₂, w₃, ... wₜ) The model computes the probability of the next word w_{t+1}: P(w_{t+1} | w₁, w₂, ..., wₜ) Example prompt: 'The sky is clear and the grass is...' Vocabulary Probabilities: 'green' → 88.4% 'wet' → 7.1% 'tall' → 3.2% 'purple' → 0.001% The model selects 'green', appends it to the context, and calculates the next word!

3. From-Scratch Reference Implementation

Executable, production-tested reference code without magic libraries:

# Next-Word Prediction Simulator: How Autocomplete Builds Sentences import random # A tiny model trained on a children's storybook # Each word points to the list of words that most frequently followed it next_word_probabilities = { 'START': ['the', 'once', 'a'], 'once': ['upon'], 'upon': ['a'], 'a': ['little', 'brave', 'robot', 'friendly'], 'little': ['robot', 'fox'], 'brave': ['robot', 'knight'], 'robot': ['built', 'explored', 'learned'], 'built': ['a', 'shiny'], 'shiny': ['spaceship.'], 'explored':['the'], 'the': ['stars.', 'galaxy.', 'universe.'], 'learned':['how'], 'how': ['to', 'computers'], 'to': ['fly.', 'think.'], } def generate_story(start_word='once', max_words=8) -> str: sentence = [start_word] current = start_word for _ in range(max_words): next_options = next_word_probabilities.get(current) if not next_options: break # Pick the next word based on probability next_word = random.choice(next_options) sentence.append(next_word) current = next_word.strip('.') return ' '.join(sentence) print('Generated AI Sentence:', generate_story('once'))

4. Systems Complexity & Memory Footprint

First Principles Takeaway: An LLM generates text autoregressively (one token at a time). It cannot plan 10 sentences ahead in a hidden scratchpad unless you ask it to deliberate and think step-by-step.

5. Canonical Literature & Primary Research

Original research papers and foundational texts recommended for advanced study:

  1. Shannon, C. E. (1948). A Mathematical Theory of Communication. Bell System Technical Journal, 27.
  2. Vaswani, A., et al. (2017). Attention Is All You Need. NeurIPS.
  3. Brown, T., et al. (2020). Language Models are Few-Shot Learners. NeurIPS.
Next Page for Further Learning
Mastered this concept? Keep advancing

Now that you understand autocomplete and next-token prediction, learn about safety and advanced prompting: