ECO
AG47YOULEARN
AI/Source:Andrej Karpathy(Andrej Karpathy YouTube Channel)
Cinema Master Player
Intro to Large Language Models: Pre-training, RLHF & LLM OS
Original Lecture
Full HD Stream
Iniciar Aula em Vídeo
Abrir no YouTube

Intro to Large Language Models: Pre-training, RLHF & LLM OS

A foundational masterclass on how base models are trained, aligned into assistants, and scaled into the kernel of a new operating system.

Andrej Karpathy's comprehensive breakdown of generative AI: from lossy internet compression on 10TB of text to RLHF alignment, System 2 tree search, and the LLM OS computing paradigm.

Original Lecture
1h
YouLearn Time
~0 min
Efficiency Gain
0% faster
Tópicos:#LLM#Pre-training#RLHF#LLM OS#System 2 Thinking#AI Security
01 / Overview

Executive Summary & The Dual-Stage Paradigm

The fundamental dichotomy between Pre-training and Post-training

Executive Summary

Large Language Models are not databases; they are lossy, probabilistic compression files of human knowledge. Building a modern AI assistant requires two completely distinct stages: 1) Pre-training (~$2M-$100M compute on 10TB+ text) to produce a raw Base Model, and 2) Post-training (SFT + RLHF on high-quality conversations) to align the model into a polite, tool-using assistant.

CORE THESIS

The LLM is emerging as the central processing unit (CPU/kernel) of a new computing architecture, where the context window functions as RAM and external APIs/databases function as disk storage.

Why this matters: Recognizing that base models only predict tokens—and that hallucinations are the native generative process—allows engineers to correctly design deterministic guardrails and RAG pipelines.
Prerequisites
High-level computing literacy
Target Audience
EngineersFoundersTech Leaders
02 / Learning Timeline

Intellectual Journey & Timestamped Chapters

7 core milestones from raw tokens to the operating system paradigm

Chronological breakdown of Karpathy's 1-hour masterclass with direct links to the source lecture.

1
Inference

LLM Inference: Two Files (Weights + Code)

An LLM is just two files: a ~140GB parameters file (weights) and a ~500-line C file (llama2.c) that runs matrix multiplication on CPU/GPU.

Key Concepts:Parameters MatrixForward PassDeterministic Weights
2
Pre-training

Pre-training: Lossy Compression of the Internet

Scraping ~10TB of text, tokenizing into discrete integers, and running thousands of GPUs for months to predict next tokens.

Key Concepts:Common CrawlLossy CompressionGPU Clusters
3
Alignment

Base Models vs. Assistant Finetuning (SFT & RLHF)

Why raw base models answer questions with more questions, and how human contractors write Q&A dialogues to align models into assistants.

Key Concepts:SFT (Supervised Finetuning)RLHFPPO / DPO
4
Scaling

Scaling Laws & Predictable Smooth Progress

Next-token prediction loss scales smoothly and predictably with compute (FLOPs) and dataset size.

Key Concepts:Power LawsTest LossCompute Scaling
5
Tools

Tool Use: Calculator, Code Interpreter & Browsing

LLMs emit special control tokens (e.g. |BROWSER|) to execute Python scripts, calculate arithmetic, and fetch live web data.

Key Concepts:Tool CallingWorking MemoryExternal I/O
6
Reasoning

System 1 vs. System 2 Thinking & Tree Search

Current LLMs are pure System 1 (instinctive, constant time per token). Future frontier models will search reasoning trees (System 2 deliberate reflection).

Key Concepts:Monte Carlo Tree SearchChain of ThoughtAlphaGo Analogy
7
Architecture & Security

The LLM OS & AI Security / Prompt Injections

The LLM as the CPU of a new operating system, coordinating RAM (context window), disk (files), and peripherals, alongside emerging security threats like prompt injection.

Key Concepts:LLM OS KernelJailbreaksPrompt InjectionData Poisoning
Concept Deep Dive

Core Concept: The LLM as the Operating System Kernel

How the computer architecture mental model perfectly maps to modern AI systems

Timestamp · 42:15
The Core Concept

Just as a CPU executes assembly instructions and coordinates RAM, disk storage, and peripherals, an LLM orchestrates natural language tokens across its working context window (RAM), vector stores / files (Disk), and external APIs / browsers (Peripherals).

The traditional operating system manages process threads and system calls. In the LLM OS, the model processes text streams, delegates arithmetic to Python runtimes, queries file indexes, and returns consolidated answers to human users.
Critical Properties:
Context Window = RAM (limited, expensive, immediate working memory).
Vector Databases & File Systems = Hard Disk (permanent storage retrieved via semantic search).
Web Browsers, Calculators, Python Interpreters = Peripheral Devices (I/O).
Prompt Injection = Buffer Overflow / SQL Injection of the natural language computing era.

The LLM Operating System Architecture

architecture

Comparison of traditional OS architecture with LLM OS stack.

================= THE LLM OPERATING SYSTEM =================
  [ USER / APPLICATIONS ] ---> ( NATURAL LANGUAGE INTENT )
                                      |
                                      v
                 +-----------------------------------------+
                 |         LLM KERNEL / CPU                |
                 |      (Transformer Neural Network)       |
                 +-----------------------------------------+
                   |                    |                |
                   v                    v                v
           [ CONTEXT WINDOW ]     [ DISK / RAG ]   [ PERIPHERALS ]
               ( RAM Memory )     (Vector Stores)  (Tools & APIs)
           - Working instructions - PDF documents  - Python Shell
           - Intermediate chats   - Code repos     - Web Browser
           - Prompt tokens        - DB records     - Calculator
  ============================================================
Figure 1.1: The LLM Operating System Stack (Karpathy, 2023).

An LLM is not a chatbot; it is the central processing unit of a new computing architecture that communicates in natural language.

Andrej Karpathy
Process & Execution Workflow

The 3-Phase Pipeline: From Raw Internet to AI Assistant

How billions of unstructured web tokens become polite, helpful assistants

The exact three developmental stages required to create production models like ChatGPT, Claude, and Llama.

1
Stage 1: Pre-trainingUnsupervised Pre-training (The Base Model)
04:17

Scrape ~10TB of raw text from Common Crawl, Wikipedia, GitHub, and books. Train thousands of GPUs for months to minimize next-token cross-entropy loss. Output: a base model (e.g. Llama 3 70B Base) that acts as an internet document completer.

2
Stage 2: SFTSupervised Finetuning (SFT / Alignment)
14:14

Human contractors write ~100,000 high-quality Q&A prompt-response pairs. Finetune the base model to swap document completion behavior for conversational assistant behavior.

3
Stage 3: RLHFReinforcement Learning from Human Feedback (RLHF / DPO)
18:30

Human evaluators rank multiple model completions from best to worst. Train a reward model and optimize the LLM policy (via PPO or DPO) to maximize helpfulness, accuracy, and safety.

Result:Produces an aligned assistant that follows instructions, uses tools, admits uncertainty, and refuses harmful requests.
Architectural Comparison

Comparative Analysis: Base Model vs. Aligned Assistant

Why using a raw base model for chat fails and how alignment shifts behavior

Contrasting raw pre-trained base models with fine-tuned conversational assistants across behavior, prompt response, and reliability.

DimensionBase Model (Pre-trained)Assistant Model (SFT + RLHF)
Core Objective
Complete the document statistically based on internet distribution.
Answer questions helpfully, politely, and truthfully as an assistant.
Response to a Question
Often responds with more questions (imitating an online homework sheet).
Provides direct, concise, formatted answers.
Training Cost
Pre-training acquires all world knowledge; post-training merely unlocks it.
99% of total compute budget ($10M - $100M+ in GPU time).
1% of total compute budget (cheap finetuning on curated data).
Hallucination Nature
Wild daydreaming across all internet tropes.
Directed dreams constrained into helpful assistant tone.
ARCHITECTURAL VERDICT

Post-training does not teach the model new facts—it teaches the model how to converse, format answers, and access its existing latent knowledge.

Visual Evidence & Architectural Frames

Visual Evidence: The LLM OS & Pre-training Loss Scaling

Inspecting the empirical mechanics of the new computing stack

Empirical slides and architectural diagrams from Karpathy's masterclass comparing computer architectures and scaling laws.

The LLM Operating System Stack Diagram
architecture
Figure 4.1: The LLM Kernel coordinating Context RAM, Disk Storage, and Peripheral Tool Execution.

The LLM Operating System Stack Diagram

The central processing unit is the Transformer neural network. The context window acts as limited, volatile RAM, while disk storage is semantic retrieval over external databases.

LLM Kernel
Processes natural language instruction tokens.
Peripherals
Web browsing, code interpreters, and API tools.
Pre-training Test Loss Smooth Power-Law Scaling
benchmark
Figure 4.2: Log-log plot showing predictable loss reduction as compute (FLOPs) scales.

Pre-training Test Loss Smooth Power-Law Scaling

Unlike downstream qualitative benchmarks which jump erratically, next-token prediction loss follows exact power laws as compute and dataset volume increase.

Compute Scaling
Predictable performance across 4 orders of magnitude.
Cross-Entropy Loss
Continuous improvement in statistical world modeling.
Key Insights & Mental Models

Mental Models & AI Security Principles

Key heuristics from Andrej Karpathy's analysis

mental model

Hallucination is the Default State of an LLM

An LLM is a creative, probabilistic simulator. It only ever dreams text. When the dream happens to align with physical reality, we call it knowledge. When it diverges, we call it hallucination.

Heuristic:Use RAG to place ground-truth facts into the context window (working memory) rather than relying on model parameter memory alone.
warning

Prompt Injection is the SQL Injection of LLMs

Because LLMs process instructions and data in the exact same input stream, malicious text inside a processed PDF or webpage can hijack the control flow of the model.

Heuristic:Never grant autonomous tool execution permissions to an LLM processing untrusted third-party web content without human verification gates.
pro tip

System 1 vs System 2 Thinking Gap

Humans spend hours deliberating before making hard decisions (System 2). LLMs currently spend the exact same number of FLOPs on each token regardless of problem difficulty. Future breakthroughs will come from reasoning tree search.

We need the equivalent of System 2 thinking for LLMs—allowing them to pause, search alternative hypotheses, and reflect before answering.Andrej Karpathy
Interactive Knowledge Check

Large Language Models Assessment

Verify your understanding of the foundational concepts explained by Andrej Karpathy.

Q1What is the primary role of Supervised Finetuning (SFT) in creating an AI assistant?

Q2In the 'LLM as an Operating System' analogy, what component corresponds to computer RAM?

Q3Why is Prompt Injection fundamentally difficult to eliminate in LLM applications?

Synthesis & Action Plan

Actionable Takeaways & AI Strategy

Key recommendations for software developers and AI practitioners

Key Synthesis Points

1

Pre-training creates knowledge; SFT and RLHF create alignment and formatting.

2

Treat the LLM as a CPU orchestrator and context window as limited, precious RAM.

3

Mitigate hallucinations with retrieval-augmented generation and tool execution.

4

Design for prompt injection vulnerabilities from day one.

Actionable Implementation Checklist

Provenance & Source Integrity

Source Provenance & Attribution

Direct attribution to the original lecture and presentation materials

Andrej Karpathy

Andrej Karpathy

Ex-Director of AI at Tesla, OpenAI Founding Member

Andrej Karpathy YouTube Channel
Open Original Material
License / Distribution: Educational Use Licensing / Open Access
Academic & Reference Citation

Karpathy, A. (2023). "[1hr Talk] Intro to Large Language Models." YouTube Educational Talk.