Courseware / Software Engineering / course-010
Understanding the Architecture and Interaction of Large Language Models (LLMs)
Tweet@DeRonin_View Source →

🎙 Podcast Version

2-host dialogue — ALEX & SAM discuss this course.

Understanding the Architecture and Interaction of Large Language Models (LLMs)

Overview

This course provides a comprehensive exploration of how Large Language Models (LLMs) function, from the underlying tokenization process to the high-level training stages that create a functional AI assistant. By understanding the distinction between a model's internal knowledge and its active context window, software engineers can build more efficient prompts and integrate AI tools more effectively into their workflows. This material bridges the gap between the "magic" of a chat interface and the mathematical reality of token sequences and neural network parameters.

Background & Context

The modern AI ecosystem was catalyzed by the deployment of ChatGPT by OpenAI in 2022, which introduced the general public to the ability to interact with a large language model via a simple text interface. Since then, the landscape has expanded into a rich ecosystem of "ChatGPT-like" experiences. While OpenAI remains the "original gangster" and the most feature-rich incumbent, the market now includes Big Tech competitors (Google's Gemini, Meta's Meta AI, and Microsoft's Copilot), specialized startups (Anthropic's Claude and xAI's Grok), and international players (DeepSeek from China and Mistral's Le Chat from France).

For software engineers, understanding this landscape is critical because different models possess different strengths, which can be tracked via community-driven benchmarks. Tools like the Chatbot Arena (which uses an ELO scoring system) and the Scale SEAL leaderboard allow developers to evaluate which models perform best on specific tasks, ensuring they choose the right tool for the specific engineering problem they are solving.

Core Concepts

Tokens and Tokenization

The most fundamental unit of interaction for an LLM is not a word or a character, but a "token." When a user enters a query, the text is chopped up into small chunks called tokens. These tokens are then converted into token IDs from a massive vocabulary (approximately 200,000 possible tokens for GPT-4o).

For example, a simple request for a haiku might consist of 15 tokens. The model's response is similarly a sequence of tokens (e.g., 19 tokens for the haiku). This process is case-sensitive; changing the capitalization of a word can change the resulting token sequence. Tools like Tiktokenizer allow developers to visualize this process, showing exactly how a string of text is broken down into the specific IDs that the model processes under the hood.

The Context Window (Working Memory)

The "chat bubbles" seen in a user interface are a visual abstraction. In reality, the user and the AI are collaborating to build a one-dimensional token stream. This stream is known as the Context Window. Everything currently inside this window acts as the model's "working memory"; any information within this sequence is directly accessible to the model during the generation of the next token.

When a user clicks "New Chat," the token window is wiped clean, resetting the sequence to zero and starting the conversation from scratch. The interaction follows a specific control flow: the user writes tokens into the stream and hits enter, transferring control to the model. The model then generates tokens until it emits a special "stop token" (indicating it is done), at which point control is transferred back to the user.

Pre-training: The Knowledge Base

Pre-training is the first and most expensive stage of creating an LLM. In this phase, the model is fed a massive portion of the internet, which is compressed into a "lossy and probabilistic zip file." This "zip file" is actually the parameters of a neural network. For instance, a one-terabyte file might represent roughly one trillion parameters.

The goal of pre-training is simple: the model learns to predict the next token in a sequence based on internet documents. Through this process, the model gains a vast amount of general world knowledge. However, because pre-training is incredibly costly (costing tens of millions of dollars and taking months), it happens infrequently. This leads to a knowledge cutoff, meaning the model's internal knowledge only goes up to the date the pre-training ended.

Post-training: The Persona

If pre-training provides the knowledge, post-training provides the behavior. Without post-training, a model would simply act as an "internet document generator," continuing a text sequence in a random style. Post-training "attaches a smiley face" to the model by swapping the training data for a curated dataset of human-built conversations.

This stage teaches the model to take on the persona of an assistant that responds to queries rather than just completing text. This is where the model learns the style, tone, and structural format of a helpful AI assistant. The final artifact is a combination of the vast knowledge from pre-training and the behavioral alignment from post-training.

The Self-Contained Entity

A critical insight for engineers is that a base LLM is a fully self-contained entity—essentially a large file on a disk. By default, the neural network is just a mathematical function predicting the next token. It does not inherently possess a calculator, a computer, or a Python interpreter. Any ability to perform math or execute code is typically the result of external tools or specific training, rather than the base model's innate "cognition."

How It Works / Step-by-Step

The Conversation Loop

The process of a single interaction follows this technical workflow:

  1. Input Formatting: The user's text is wrapped in a specific chat format. This includes special tokens that mark the beginning and end of a message (e.g., <|user|> and <|assistant|>).
  2. Tokenization: The formatted text is converted into a sequence of token IDs.
  3. Processing: The token sequence is fed into the neural network (the trillion parameters).
  4. Prediction: The model predicts the next token in the sequence based on the patterns learned during pre-training and the persona learned during post-training.
  5. Output Generation: The model continues generating tokens one by one until it hits a "done" token.
  6. Control Transfer: The application sees the "done" token and returns control to the user, who can then add more tokens to the sequence, expanding the context window.

Real-World Examples & Use Cases

Creative Writing and Formatting

LLMs are exceptionally proficient at tasks involving writing and structure. Examples include:

  • Haikus and Poetry: Generating dramatic, structured verse (e.g., "Words flow like a stream / Endless echoes never mind / Ghost of thought unseen").
  • Professional Documentation: Writing cover letters, resumes, and email replies.

Managing Knowledge Cutoffs

Because of the knowledge cutoff mentioned in the pre-training section, a model cannot "know" about an event that happened last week. To solve this, engineers use Tool Use (such as RAG - Retrieval Augmented Generation).

  • Scenario: If a user asks about a news event from yesterday, the system must use a tool to fetch the current news and inject that text into the Context Window. The model then uses its pre-trained reasoning abilities to summarize the new information provided in the window.

Token Efficiency in Software Engineering

As noted in the source metadata, senior AI engineers avoid "auto-context loading."

  • Inefficient Approach: Sending 50 files of code to fix a 30-line bug. This wastes tokens (and money), as the model is processing thousands of tokens it doesn't need.
  • Efficient Approach: Carefully selecting only the relevant files to keep the context window lean and the cost low.

Key Insights & Takeaways

  • Tokens are the currency of LLMs: Everything is converted to tokens; understanding this helps in optimizing costs and understanding model limitations.
  • The Context Window is working memory: Information inside the window is directly accessible; information outside the window is forgotten.
  • Knowledge is compressed and lossy: The model doesn't "store" the internet; it stores a probabilistic representation (the "vibes") of the internet in its parameters.
  • Pre-training $\neq$ Post-training: Pre-training provides the "what" (knowledge), while post-training provides the "how" (the assistant persona).
  • Knowledge Cutoffs are inevitable: Because pre-training is too expensive to do daily, models are always slightly out of date.
  • The model is a predictor, not a computer: The base model predicts the next token; it does not "calculate" in the traditional sense unless augmented with tools.

Common Pitfalls / What to Watch Out For

  • Overloading the Context Window: Beginners often send too much irrelevant data (e.g., loading an entire codebase for a small fix), which increases costs and can lead to "lost in the middle" phenomena where the model ignores key details.
  • Assuming Real-Time Knowledge: Users often forget the knowledge cutoff and trust the model for current events, leading to hallucinations.
  • Confusing Persona with Logic: Assuming that because a model sounds like a helpful assistant (post-training), it possesses a logical reasoning engine like a traditional software program.

Review Questions

  1. Explain the difference between a model's internal parameters and its context window. Which one is permanent, and which one is transient?
  2. Why does a model have a "knowledge cutoff," and why can't OpenAI simply update the model's knowledge every day?
  3. If you are using a tool like Tiktokenizer and notice that changing a capital letter changes the token count, what does this tell you about how the model "sees" text?

Further Learning

  • RAG (Retrieval Augmented Generation): Learn how to provide real-time data to a model to overcome the knowledge cutoff.
  • Prompt Engineering: Study how to structure the "user" and "system" messages to better guide the post-trained persona.
  • Model Quantization: Explore how the "one terabyte zip file" of parameters can be compressed further to run on smaller hardware.
← Previous
Next →