Leanpub Header

Skip to main content

LLMs Explained

How Large Language Models Learn, Think, and Generate

This book is 100% completeLast updated on 2026-07-17

Large language models are changing the world, yet few people understand how they actually work. This book cuts through the hype, explaining the ideas behind modern AI with clarity, precision, and no unnecessary jargon.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
113
Pages
About

About

About the Book

Large language models have transformed how we interact with machines, but their inner workings remain opaque to most. This book opens the black box, walking you through every stage of an LLM's life: from the raw text scraped off the internet, through tokenization and embeddings, into the transformer architecture where self-attention mechanisms weave meaning from patterns, across the grueling training process shaped by gradient descent, and finally through inference when each word is generated one step at a time. No exercises, no application checklists, no hand-waving about "artificial intelligence." Just a clear, technically precise account of what these models actually are, how they work, what they have learned, and what remains genuinely mysterious about them.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

He currently works in the advanced research division of a leading cybersecurity company, where he performs vulnerability research alongside a team of experienced researchers and engineers. His work includes discovering security vulnerabilities, reverse engineering software and malware, analyzing emerging threats and developing new techniques to improve the security of modern computing environments.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books.

Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under the a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 400 engineers and researchers from Ukraine, Belarus and Russia. We spend far more time validating technical accuracy and keeping our content up to date than generating text.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

How Large Language Models Learn, Think, and Generate

Introduction: The Black Box on Your Screen

  1. The Question Everyone Is Asking
  2. What This Book Will Explain
  3. A First-Pass Tour: From Prompt to Response

Chapter 1: The Fundamental Idea - Predicting the Next Token

  1. Autocomplete with Ambition
  2. Why Prediction Requires “Understanding”
  3. The Training Objective in Plain Language
  4. From One Word at a Time to Coherent Thought
  5. More Challenging Prediction Tasks

Chapter 2: From Text to Numbers - Tokenization and Preprocessing

  1. Why Models Cannot Read Words
  2. Building a Vocabulary: Tokens vs. Words
  3. Byte-Pair Encoding and Subword Strategies
  4. Data Collection: Where the Training Corpus Comes From
  5. Tokenization Edge Cases and Their Consequences
  6. The Impact of Sequence Length on Training

Chapter 3: Embeddings - Meaning as Vectors

  1. The Embedding Layer: A Lookup Table with Power
  2. Semantic Geometry: Why Similar Words Are Nearby
  3. Positional Encodings: Giving the Model a Sense of Order
  4. What an Embedding Vector Actually Contains
  5. Embeddings in Context: From Static to Dynamic Representations
  6. The Mathematics of Cosine Similarity
  7. Embedding Dimensionality and Capacity

Chapter 4: The Transformer Architecture - An Overview

  1. Before Transformers: Why RNNs Could Not Scale
  2. The Big Picture: Layers, Blocks, and Data Flow
  3. Feed-Forward Networks: The Hidden Computation
  4. Residual Connections and Layer Normalization
  5. Encoder, Decoder, and Decoder-Only Architectures
  6. Parameter Counts and Model Sizing
  7. The Complete Forward Pass: A Numerical Example

Chapter 5: Self-Attention - The Heart of the Transformer

  1. What Does It Mean for One Token to Attend to Another?
  2. Queries, Keys, and Values: The Attention Mechanism
  3. Softmax: Turning Scores into Weights
  4. Multi-Head Attention: Different Perspectives on the Same Text
  5. Tensor Shapes and Dimensionality Tracking
  6. Computational Complexity: The Quadratic Bottleneck
  7. Attention Patterns: What the Weights Reveal

Chapter 6: Training Mechanics - Loss, Gradients, and Descent

  1. Measuring Failure: The Cross-Entropy Loss Function
  2. Backpropagation: How Errors Flow Backward
  3. Gradient Descent: Nudging Parameters in the Right Direction
  4. Adam and the Art of Optimization
  5. Tracing Gradients Through a Transformer Block
  6. Learning Rate Schedules: Warmup and Cosine Decay
  7. Gradient Clipping and Training Stability
  8. The Loss Landscape and Optimization Dynamics

Chapter 7: Pretraining - Learning from the Entire Internet

  1. The Pretraining Loop: One Step at a Time
  2. What the Model Learns: Patterns, Facts, and Structure
  3. Scaling Laws: Why Bigger Is Better (and Why)
  4. Emergent Abilities: When More Parameters Change Everything
  5. Distributed Training: Parallelism at Scale
  6. Phases of Learning During Pretraining
  7. Data Quality Versus Quantity

Chapter 8: Knowledge Representation - What Lives in the Weights?

  1. Parameters as Compressed Knowledge
  2. Finding Facts Inside Neural Networks
  3. Knowledge Neurons and Factual Localization
  4. The Superposition Hypothesis: More Features Than Dimensions
  5. Sparse Autoencoders: Disentangling Features
  6. Feed-Forward Networks as Key-Value Memories
  7. Induction Heads: The Circuit Behind In-Context Learning
  8. The Limits of Current Understanding
  9. The Geometry of Reasoning: How Chains of Thought Emerge
  10. What Models Do Not Know (and Why They Hallucinate)

Chapter 9: Instruction Tuning - Teaching the Model to Obey

  1. From Completion Engine to Assistant
  2. Supervised Fine-Tuning: Learning from Examples
  3. Reinforcement Learning from Human Feedback
  4. Reward Model Training: From Preferences to Scores
  5. PPO Optimization: Reinforcement Learning in Practice
  6. Direct Preference Optimization: A Simpler Alternative
  7. The Trade-Off Between Capability and Alignment

Chapter 10: Inference - How a Response Is Generated, Token by Token

  1. The Inference Loop: One Token at a Time
  2. KV Caching: Avoiding Redundant Computation
  3. Temperature and Sampling: Controlling Creativity
  4. Greedy Decoding, Beam Search, and Their Trade-offs
  5. Context Windows and Computational Cost
  6. Prefill Versus Decode: Two Different Computational Regimes
  7. KV Cache Memory: The Hidden Cost
  8. Speculative Decoding: Generating Multiple Tokens Per Step
  9. Repetition Penalties and Generation Controls

Chapter 11: Reasoning and Emergent Capabilities

  1. Did We Train Models to Reason? (No. So How?)
  2. In-Context Learning: Prompting as Programming
  3. Chain-of-Thought: Making Reasoning Visible
  4. The Boundary Between Pattern Matching and Reasoning
  5. The Emergence Debate: Real Phenomenon or Measurement Artifact?
  6. Systematic Evaluation of Reasoning Capabilities
  7. What In-Context Learning Reveals About Model Capabilities

Conclusion: What We Know, What We Do Not

  1. The Complete Picture: From Tokens to Intelligence-Like Behavior
  2. What LLMs Achieved (and How)
  3. Open Questions and Honest Uncertainties

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub