Skip to main content
AILLMsMachine LearningTransformersNLPPython

The Feynman Guide to Large Language Models

Demystifying Large Language Models (LLMs)—from tokenization and high-dimensional embeddings to transformers, self-attention, pre-training, fine-tuning, and RAG—using simple analogies and runnable Python code.

9 min read

Have you ever wondered how ChatGPT, Claude, or Gemini seems to "understand" human language and write prose, code, or poetry?

To some, it feels like magic. To others, it's dismissed as "just fancy autocomplete." But if we look under the hood—drawing from foundational lectures by Andrej Karpathy, 3Blue1Brown, Stanford CS229, and Google Cloud—we discover an incredible feat of modern engineering.

Let's use the Richard Feynman Technique—translating complex technical mechanics into simple, everyday analogies—to build a crystal-clear mental model of Large Language Models (LLMs), complete with runnable Python code.


1. Tokenization: The Lego Bricks of Language

Computers don't read English, Spanish, or Python code directly—they only understand numbers. Before an LLM can process a single word, language must be chopped into numerical chunks called Tokens.

Tokenization: Building with Lego Bricks Tokenization breaks words into subword Lego bricks. This allows LLMs to handle rare words and code efficiently.

Think of language like a box of Lego bricks. Instead of creating a custom molded piece for every complex object in the universe (like "unbelievable" or "antidisestablishmentarianism"), you break words down into reusable subword bricks like un-, believ-, and -able.

Most modern LLMs use Byte Pair Encoding (BPE). In Python, we can inspect tokenization directly using OpenAI's tiktoken library:

import tiktoken

# Load tokenizer for modern LLMs
encoder = tiktoken.encoding_for_model("gpt-4o")

text = "Large Language Models are transforming engineering!"
tokens = encoder.encode(text)

print(f"Original Text: '{text}'")
print(f"Token IDs:     {tokens}")
print(f"Token Count:   {len(tokens)}")

# Decode each token back to text to see subwords
decoded_tokens = [encoder.decode([t]) for t in tokens]
print(f"Token Chunks:  {decoded_tokens}")

2. Word Embeddings: The High-Dimensional GPS

Once text is converted into token IDs, how does a model know that "king" is related to "queen", or that "Paris" is to "France" what "Tokyo" is to "Japan"?

It assigns each token a Vector Embedding—a list of hundreds or thousands of numbers representing coordinates in a high-dimensional space.

Word Embeddings Map Embeddings place words in a high-dimensional concept map. Words with similar meanings cluster together in space.

Think of embeddings as a GPS system for concepts.

  • In a 2D map, you have latitude and longitude.
  • In an LLM's embedding space, you have 4,096+ dimensions: one axis for "royalty", another for "gender", another for "city vs country", and so on.

Because related concepts live close together in coordinate space, we can do vector arithmetic: Vector("King") - Vector("Man") + Vector("Woman") ≈ Vector("Queen").

Here is how you compute vector similarities in Python using sentence-transformers:

from sentence_transformers import SentenceTransformer
import numpy as np

# Load lightweight embedding model
model = SentenceTransformer("all-MiniLM-L6-v2")

words = ["king", "queen", "apple", "banana", "paris", "france"]
embeddings = model.encode(words)

def cosine_similarity(v1, v2):
    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))

print(f"king ↔ queen similarity:  {cosine_similarity(embeddings[0], embeddings[1]):.4f}")
print(f"apple ↔ banana similarity: {cosine_similarity(embeddings[2], embeddings[3]):.4f}")
print(f"king ↔ apple similarity:  {cosine_similarity(embeddings[0], embeddings[2]):.4f}")

3. The Transformer: The Grand Symphony Orchestra

The breakthrough architecture behind all modern LLMs is the Transformer (introduced in the seminal 2017 paper Attention Is All You Need).

The Transformer Orchestra The Transformer acts like an orchestra where dozens of specialized attention heads process language in parallel.

Before Transformers, older neural networks (RNNs) processed words one by one, like a person reading through a narrow straw. They were slow and forgot early context.

A Transformer is like a Symphony Orchestra:

  • The Conductor is the overall network architecture coordinating signal flow.
  • The Musicians are parallel processing layers (Attention Heads & Feed-Forward Layers).
  • Instead of reading sequentially, the orchestra plays and evaluates every token in the entire context window simultaneously.

4. Self-Attention: The Library Spotlight

How does an LLM resolve ambiguity? Consider the sentence:

"The bank was closed because the river overflowed." vs "The bank was closed because it was a holiday."

What does "bank" mean in each sentence?

Self-Attention Spotlight Self-Attention acts like a spotlight scanning context to update the meaning of each word based on surrounding words.

The Transformer uses Self-Attention like a dynamic spotlight in a library. For every word in a sequence, it calculates:

  1. Query (Q): What am I looking for? (e.g., "bank" looking for contextual clues).
  2. Key (K): What information do other words hold? ("river" vs "holiday").
  3. Value (V): How much should I update my understanding based on that match?

When "bank" sees "river", the spotlight shines brightly between them, updating the vector for "bank" to mean financial institution or riverbank.


5. Training Phase 1: Pre-Training (The Self-Reading Student)

How does a model learn these embeddings and attention weights?

Raw Pre-trained vs Fine-tuned Model Pre-training carves out raw language capability from massive web data. Fine-tuning sculpts it into a safe, helpful assistant.

The Self-Reading Student

During Pre-training, the model is fed hundreds of billions of words from books, code repositories, and web crawls. It plays a simple game billions of times: "Predict the next word."

If given: "The capital of France is ___", it calculates probability distributions across its vocabulary to select "Paris".

At this stage, the model is a raw base model. It is an incredible document completer, but it isn't an assistant yet. If you ask it "How do I bake a cake?", it might respond with another question: "How do I bake cookies?" because it's completing a list!


6. Training Phase 2: Instruction Tuning & RLHF (The Teacher's Feedback)

To convert a raw base model into a helpful chatbot, engineers apply two refinement stages:

  1. Instruction Fine-Tuning (SFT): Human annotators write thousands of high-quality (Prompt, Answer) pairs teaching the model how to follow instructions.
  2. RLHF (Reinforcement Learning from Human Feedback): Human reviewers rate multiple model outputs. A reward model is trained on these preferences to steer the LLM toward responses that are helpful, honest, and harmless.

RLHF Alignment RLHF acts like a dedicated mentor rewarding good answers and penalizing toxic or inaccurate ones.


7. Controlling Creativity: The Temperature Dial

When an LLM generates text, it converts raw output scores (logits) into probabilities using the Softmax function. We can adjust a parameter called Temperature to control randomness.

Temperature Dial Low temperature (0.0) produces deterministic, factual output; high temperature (1.0+) increases creativity and unpredictability.

  • Temperature = 0.0: The model always picks the single most likely token (Greedy Decoding). Best for coding, math, and factual queries.
  • Temperature = 0.7: Balanced creativity and coherence. Best for blog posts and conversations.
  • Temperature = 1.5+: High randomness. The model samples lower probability words, leading to imaginative prose or unhinged gibberish.

Here is Python code implementing custom Softmax sampling with temperature control:

import numpy as np

def sample_with_temperature(logits, temperature=1.0):
    if temperature == 0:
        return np.argmax(logits)
    
    # Scale logits by temperature
    scaled_logits = np.array(logits) / temperature
    
    # Softmax function to turn scaled logits into probabilities
    exp_logits = np.exp(scaled_logits - np.max(scaled_logits))
    probs = exp_logits / np.sum(exp_logits)
    
    # Sample index based on probability distribution
    return np.random.choice(len(logits), p=probs)

# Simulated next-word logits for vocabulary: ["Paris", "France", "croissants", "unicorns"]
vocab = ["Paris", "France", "croissants", "unicorns"]
raw_logits = [5.0, 3.2, 1.5, -2.0]

print("Low Temp (0.1): ", vocab[sample_with_temperature(raw_logits, temperature=0.1)])
print("High Temp (1.5):", vocab[sample_with_temperature(raw_logits, temperature=1.5)])

8. Hallucinations: The Confident Presenter

Why do LLMs sometimes state false information with absolute confidence?

LLM Hallucination LLMs do not query a database of facts—they generate text word-by-word based on probability. This leads to confident hallucinations.

Because LLMs are fundamentally statistical text predictors, not search engines. They do not look up database rows; they predict plausible word sequences. If a factual detail is rare in their training data, the model synthesizes a sentence that sounds structurally correct, even if the facts are completely fabricated.


9. RAG: The Open-Book Exam

How do we eliminate hallucinations and give LLMs access to private, up-to-date knowledge? We use Retrieval-Augmented Generation (RAG).

Retrieval-Augmented Generation RAG grounds the LLM by retrieving relevant documents from a vector database before generating the answer.

Think of standard LLMs taking a closed-book exam from memory. RAG turns it into an open-book exam:

  1. Retrieve: The user's prompt searches a Vector Database for relevant factual documents.
  2. Augment: The retrieved facts are inserted directly into the context prompt.
  3. Generate: The LLM reads the provided documents and synthesizes an accurate answer with citations.

Here is a minimal working RAG script in Python using openai:

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))

# 1. Private Knowledge Base (Retrieved Context)
retrieved_docs = """
Company Policy #402:
- Employees are allowed up to $50/day for lunch expenses during travel.
- Receipts must be submitted within 5 business days via the travel portal.
"""

user_query = "What is the daily lunch budget allowance for travel?"

# 2. Augment Prompt with Context
system_prompt = f"You are a helpful assistant. Answer questions ONLY using the provided context:\n\n{retrieved_docs}"

# 3. Generate Answer
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_query}
    ],
    temperature=0.0
)

print("Response:\n", response.choices[0].message.content)

Summary & Key Takeaways

ConceptFeynman AnalogyTech Core
TokenizationLego BricksByte Pair Encoding (BPE) subwords
EmbeddingsHigh-Dimensional GPSMultidimensional vector coordinates
TransformerSymphony OrchestraParallel processing architecture
Self-AttentionDynamic SpotlightQuery, Key, Value matrix math
Pre-TrainingSelf-Reading StudentUnsupervised next-token prediction
RLHFTeacher's FeedbackPreference model reward tuning
TemperatureThermostatLogit scaling before Softmax
RAGOpen-Book ExamVector search + context injection

References & Masterclass Lectures

This guide synthesizes key concepts from these outstanding open lectures:

  • [1hr Talk] Intro to Large Language Models by Andrej Karpathy (YouTube)
  • Transformers, the Tech Behind LLMs by 3Blue1Brown (YouTube)
  • Stanford CS229: Building Large Language Models by Stanford University (YouTube)
  • Everything You Need To Know About LLMs (YouTube)
  • Introduction to Large Language Models by Google Cloud Tech (YouTube)
  • Large Language Models Explained Simply by The Gradient Descent (YouTube)
  • ¿Qué es un LLM? Enormes Modelos del Lenguaje by DotCSV (YouTube)

Join the Newsletter

Get deep-dive engineering guides and system design teardowns delivered straight to your inbox.

Powered by Substack. No spam, ever. Unsubscribe with one click.