All posts

How LLMs Work: From Internet Text to ChatGPT

ChatGPT, Claude, and Gemini are built through three stages: pretraining, post-training, and inference. Despite their impressive capabilities, their core objective remains surprisingly simple: predicting the next token in a sequence.

Admin
11 min read
On this page

Large Language Models (LLMs) have transformed the way we interact with technology. From generating code and writing essays to powering AI agents and video generation, modern systems like ChatGPT, Gemini, and Claude are capable of performing tasks that once seemed — uniquely human.

By the end of this article, you'll realize that beneath all the hype, LLMs generate remarkable outputs using a deceptively simple objective: predicting the next word.

The Core Idea Behind LLMs

Despite their complexity, all LLMs are trained to perform a single task:

Predict the next token in a sequence.

Consider the sentence:

The capital of France is ___

Most humans would naturally complete it with "Paris." LLMs learn to make similar predictions by pattern recognition.

How the AI Learns the "Pattern"

To predict "Paris," the AI didn't consult a map or a geography database. Instead, it digested trillions of web pages and noticed that the words "France," "capital," and "Paris" constantly orbit each other.

In its raw training data, it encountered this relationship in hundreds of different formats:

The SourceWhat the AI actually read
Encyclopedia Entries"Paris is the capital and most populous city of France..."
Travel Blogs"We finally arrived in the French capital, Paris, for our vacation."
News Articles"Speaking from the capital today, the French President announced..."
Trivia & Q&A Sites"Q: What is the capital of France? A: Paris."

By processing these variations millions of times, the AI builds a mathematical map. It learns that whenever the words "capital" and "France" appear together, there is a near 100% statistical probability that "Paris" is the next logical piece of the puzzle. It isn't thinking about geography: it is just completing the pattern.

Initially, the model's predictions are essentially random. However, after processing trillions of examples, it becomes increasingly effective at determining which token is most likely to appear next in a given context.

Let’s Begin

Before we dive into the inner workings of an LLM, it's useful to understand the three major stages involved in creating one.

At a high level, modern language models are built through a pipeline that looks like this:

  1. Pretraining: The model learns language, facts, reasoning patterns, and world knowledge by processing vast amounts of internet text and performing next-token prediction.
  2. Post-Training: The base model is refined using instruction tuning, human feedback, and alignment techniques to make it more helpful, conversational, and safe.
  3. Inference: Once training is complete, the model is deployed and begins generating responses for users one token at a time.

Pretraining

Every impressive capability of an LLM can be traced back to its pretraining phase. This is where the model learns from vast amounts of text and develops an understanding of language through pattern recognition.

Let's start by examining how the training data is collected.

Step 1: Collecting the Training Data

Before a model can learn language, it requires an enormous amount of text.

Training datasets typically contain information gathered from:

  • Websites
  • Books
  • Research papers
  • Documentation
  • Source code repositories
  • Online discussions

The resulting dataset can contain trillions of words and represent a significant portion of publicly available knowledge on the internet.

FineWeb is one of the best sources of categorized data on the internet

FineWeb is one of the best sources of categorized data on the internet

At this stage, however, the model does not understand language. It simply has access to a massive collection of text from which patterns can be learned.

Step 2: Converting Text into Tokens

Computers cannot directly process words.

Instead, text is broken down into smaller units called tokens.

For example:

ChatGPT is amazing.

becomes

Tokens in a sentence

Tokens in a sentence

GPT-5.x representation of the above text using Tokenizer.

Tokens can represent:

  • Complete words
  • Parts of words
  • Punctuation marks
  • Numbers
  • Symbols

Tokenization allows language to be represented numerically, enabling neural networks to process and learn from it.

Step 3: Transforming Tokens into Numbers

After tokenization, each token is converted into a numerical representation known as an embedding. By the time, ChatGPT is Amazing! reaches the AI's "brain," the human words have completely vanished. The neural network only receives and processes this sequence of numbers:

[4152, 318, 4932, 0, 34]

Here is how the AI translates those text fragments into a mathematical string:

TokenIDs corresponding to the sentence (Representational)

TokenIDs corresponding to the sentence (Representational)

Embeddings place words into a high-dimensional mathematical space where semantically related concepts appear closer together.

For example:

  • King and Queen appear near each other
  • Dog and Puppy occupy similar regions
  • Car and Vehicle are closely related

Capturing relationships between concepts using numbers

Capturing relationships between concepts using numbers

These embeddings allow the model to capture relationships between concepts without explicitly storing definitions.

Step 4: The Transformer Architecture

The breakthrough that enabled modern LLMs arrived in 2017 with the introduction of the Transformer architecture.

Transformer Architecture

Transformer Architecture

Source: Medium

Transformers rely on a mechanism called self-attention.

Self-attention allows the model to determine which parts of a sentence are most important when predicting the next token.

Consider the sentence:

The trophy didn't fit into the suitcase because it was too small.

What does "it" refer to?

To answer correctly, the model must understand relationships between multiple words in the sentence. Attention enables the model to focus on the relevant words and establish these connections.

Self-Attention in Transformers

Self-Attention in Transformers

This capability allows transformers to process language far more effectively than previous neural network architectures.

Step 5: Pretraining the Model

Pretraining is where the model acquires most of its knowledge.

During this stage, the model repeatedly performs next-token prediction across trillions of examples.

For instance:

Input:

Artificial intelligence is transforming the ___

Target:

world

The model predicts a token, compares it with the correct answer, calculates the error, and updates its internal parameters.

This process is repeated billions of times.

Pretraining

Pretraining

Eventually, the model learns:

  • Grammar
  • Facts
  • Reasoning patterns
  • Coding syntax
  • Writing styles

Remarkably, none of these skills are taught explicitly. They emerge naturally from large-scale next-token prediction.

At this point, the pretraining phase is complete.

The result is what researchers call a base model.

A base model is a language model that has completed pretraining and learned patterns from massive amounts of data, but has not yet undergone instruction tuning, alignment, or human-feedback-based post-training.

To help you understand how a base model works, here’s the response of a base model, when compared to a modern AI assistant like Gemini.

Gemini 3.1-Pro (Standard Thinking)

Gemini 3.1-Pro (Standard Thinking)

Ernie-4.5-VL-424B-A47B-Base-PT

Ernie-4.5-VL-424B-A47B-Base-PT

Where an instruction tuned model like Gemini was able to reply to this query simply, a base model went bonkers over something this trivial. This happens because a base model has never been explicitly taught how to answer questions, follow instructions, write helpful responses, or behave safely.

This is exactly what the next stage of training aims to solve.

Post-Training: Turning a Base Model into an AI Assistant

While pretraining teaches a model about the world, post-training teaches it how to interact with humans.

By the end of pretraining, we have a knowledgeable base model. It understands language, has absorbed facts from vast amounts of internet data, and can generate coherent text. However, knowledge alone is not enough.

As we’ve learnt, a base model is a loquacious talker. It would talk in length about anything. Even if the answer is one word, it would still give a paragraph.

This is where post-training comes in.

Step 1: Instruction Tuning

The first stage of post-training is known as Instruction Tuning.

During pretraining, the model learned to continue text.

For example, if given:

What is the capital of France?

A pretrained model might continue generating text similar to what it encountered on the internet:

The capital of France is Paris. France is a country located in Western Europe. Paris is known for its cultural heritage...

While technically correct, the response is simply continuing a pattern instead of helping the user.

Instruction tuning changes this behavior.

Instruction Tuning

Instruction Tuning

Researchers create thousands or even millions of prompt-response pairs that demonstrate what a good answer looks like.

By repeatedly training on these examples, the model learns a new skill:

Following instructions instead of merely continuing text.

This is the first major step that transforms a raw language model into an assistant.

Step 2: Learning from Human Preferences

Even after instruction tuning, the model still has a problem.

There are often many possible answers to a question.

Humans selecting the ideal response for a scenario

Humans selecting the ideal response for a scenario

Both answers are technically correct.

Adults might prefer the detailedness of the 1st prompt while kids might prefer the simplicity of the 2nd response. Since it is meant for a “10-year-old” the 2nd one prevailed as the winner.

The challenge is that helpfulness, clarity, and tone are difficult to encode as hard rules.

Instead, researchers use human feedback.

Human reviewers compare multiple responses and rank them according to factors such as:

  • Helpfulness
  • Accuracy
  • Clarity
  • Harmlessness
  • Relevance

Response Evaluation Rubric

Response Evaluation Rubric

These rankings provide a signal that tells the model which responses people prefer.

Over time, the model learns not only to answer questions but to answer them in ways that humans find useful.

Reinforcement Learning from Human Feedback (RLHF)

One of the most important techniques used during post-training is Reinforcement Learning from Human Feedback (RLHF).

The idea is surprisingly simple.

Imagine the model generates three possible responses to the same prompt.

Prompt:

Give me tips for learning Python.

Response A:

Python is a programming language.

Response B:

Start with basic syntax, build small projects, and practice consistently.

Response C:

Learning Python is difficult and depends on many factors.

Human reviewers rank the responses. Think of it like grading a marksheet: from A to F. 

Let’s say most people would prefer Response B.

The model is then rewarded for producing answers similar to the preferred response and discouraged from generating less useful alternatives.

By repeating this process across thousands of prompts, the model gradually learns what humans consider a "good" answer.

This is one reason modern AI assistants feel significantly more helpful than earlier language models.

Safety and Alignment

Another important goal of post-training is safety.

A pretrained model has absorbed patterns from the internet, which means it has also encountered misinformation, harmful content, biased statements, and unsafe instructions.

Without additional safeguards, the model may reproduce some of these patterns.

Circumventing Guardrails of an AI

Circumventing Guardrails of an AI

To address this, researchers conduct extensive safety evaluations.

The model is exposed to prompts involving:

  • Dangerous activities
  • Misinformation
  • Harassment
  • Privacy concerns
  • Harmful advice

Human reviewers and automated systems evaluate how the model responds.

The model is then further refined to encourage safer and more responsible behavior.

This process is commonly referred to as alignment, because the goal is to align the model's behavior with human values and expectations.

The Final Result

After post-training is complete, the model is no longer an internet auto-complete.

It has learned:

  • How to follow instructions
  • How to communicate clearly (somewhat)
  • How to prioritize helpful responses
  • How to interact safely with users

Only after this stage does the base model become the AI assistant that we recognize as ChatGPT, Claude, Gemini, or other modern conversational AI systems.

If I were to summarize this entire process In simple terms:

  • Pretraining teaches the model knowledge.
  • Instruction tuning teaches it how to answer.
  • Human feedback teaches it what people prefer.
  • Alignment teaches it how to behave.

Together, these stages transform a raw language model into a practical AI assistant capable of helping millions of users every day.

Dystopian Future

Dystopian Future

On this subject, I would highly recommend watching Andrej Karapathy’s Deep Dive into LLMs like ChatGPT YouTube video on the same.

Frequently Asked Questions

What is the primary task of a Large Language Model (LLM)?

LLMs are fundamentally trained to perform a single objective: predicting the next token in a sequence by identifying statistical patterns in massive amounts of text data.

What is a token in machine learning?

A token is a smaller unit of text, which ranges from complete words to individual symbols, that allows language to be represented numerically for neural network processing.

How do word embeddings help an LLM understand meaning?

Embeddings place tokens into a high-dimensional mathematical space where semantically related concepts sit closer together, capturing contextual relationships without relying on explicit dictionary definitions.

Why do LLMs experience hallucinations?

Hallucinations occur because models optimize for statistically plausible text continuations based on probability, rather than verifying factual truth against an internal reference database.

How does an AI agent differ from a standalone LLM?

An AI agent uses the LLM as a core reasoning engine but hooks it up to memory systems, databases, and external APIs to execute autonomous workflows.

Last updated: Oct 17, 2026

Build your agent team in 30 seconds.

Build agent teams that work along with your team. Free to start, no card required.