22.7196°N — 75.8577°E / IST
← WRITING

B·03 / ARTIFICIAL INTELLIGENCE

Generative AI, Explained: How Machines Learned to Create

A practical, no-hype breakdown of how generative AI actually works — transformers, self-attention, RLHF, diffusion models — and where it's genuinely useful versus overhyped.

SEP 07, 2026 · 6 MIN READ

GENERATIVE-AI · MACHINE-LEARNING · LLM · TRANSFORMERS · DEEP-LEARNING

A few years ago, "AI" mostly meant a model that could classify an image or predict a number. Today it means something that can write your emails, draft your code, generate an image from a sentence, or hold a conversation that feels almost human. That shift has a name: Generative AI. Here's a practical, no-hype breakdown of what it actually is, how it works under the hood, and where it's genuinely useful versus where it's overhyped.

What "generative" actually means

Traditional machine learning models are mostly discriminative — given an input, they predict a label. Is this email spam or not? Is this a cat or a dog? Generative models flip the problem: instead of predicting a label, they learn the underlying probability distribution of the data itself, so they can produce brand new samples that look like they belong to that distribution.

Feed a generative model millions of sentences, and it learns the patterns of language well enough to write new sentences. Feed it millions of images, and it learns to paint new images. That's the core idea — everything else is engineering on top of it.

The architecture that changed everything: Transformers

Almost every major generative AI system today — GPT, Claude, Gemini, Stable Diffusion's text encoder — is built on the Transformer architecture, introduced in the 2017 paper "Attention Is All You Need."

The key innovation is self-attention: instead of processing text word-by-word in strict order (like older RNNs), a transformer looks at an entire sequence at once and learns which words matter most to each other, regardless of distance.

PYTHON
1import torch
2import torch.nn as nn
3
4class SelfAttention(nn.Module):
5    def __init__(self, embed_dim):
6        super().__init__()
7        self.query = nn.Linear(embed_dim, embed_dim)
8        self.key = nn.Linear(embed_dim, embed_dim)
9        self.value = nn.Linear(embed_dim, embed_dim)
10        self.scale = embed_dim ** 0.5
11
12    def forward(self, x):
13        Q = self.query(x)
14        K = self.key(x)
15        V = self.value(x)
16
17        scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale
18        weights = torch.softmax(scores, dim=-1)
19
20        return torch.matmul(weights, V)

In plain terms: for every word, the model asks "which other words in this sentence should I pay attention to?" and weighs them accordingly. Stack enough of these attention layers, train on enough text, and the model starts capturing grammar, facts, reasoning patterns, and style — all without anyone explicitly programming rules.

How large language models are actually trained

Training a model like GPT or Claude happens in stages:

  1. Pretraining — the model is fed massive amounts of text (web pages, books, code) and trained on a simple objective: predict the next token. Nothing more. This single objective, applied at enormous scale, is enough to teach the model grammar, facts, and reasoning patterns implicitly.

  2. Supervised fine-tuning (SFT) — the base model is then trained on curated examples of good responses to instructions, so it learns to behave like an assistant instead of just autocompleting text.

  3. Reinforcement learning from human feedback (RLHF) — humans rank multiple model outputs, and a reward model is trained on those rankings. The base model is then fine-tuned to produce outputs that score higher against that reward model, aligning it with what people actually find helpful and safe.

JAVASCRIPT
1// Simplified mental model of next-token prediction
2function predictNextToken(context, model) {
3  const logits = model.forward(context);        // raw scores per token
4  const probabilities = softmax(logits);         // convert to probabilities
5  const nextToken = sampleFrom(probabilities);    // pick one (with some randomness)
6  return nextToken;
7}
8
9function generateText(prompt, model, maxTokens = 100) {
10  let context = tokenize(prompt);
11  const output = [];
12
13  for (let i = 0; i < maxTokens; i++) {
14    const nextToken = predictNextToken(context, model);
15    if (nextToken === "<END>") break;
16    output.push(nextToken);
17    context = [...context, nextToken];
18  }
19
20  return detokenize(output);
21}

This is, at its core, what's happening every time you get a response from ChatGPT or Claude — one token predicted at a time, each prediction conditioned on everything before it.

Beyond text: diffusion models for images

Image generators like Midjourney, DALL·E, and Stable Diffusion use a different technique: diffusion. The idea is almost the reverse of what you'd expect —

  1. Take a real image and gradually add random noise to it over many steps, until it's pure static.
  2. Train a neural network to reverse that process — predict and remove a little bit of noise at each step.
  3. To generate a new image, start from pure random noise and run the trained model backward, step by step, until a coherent image emerges.

Combine that with a text encoder (often a transformer) that guides the denoising process toward what you described in your prompt, and you get text-to-image generation.

Where generative AI is genuinely useful

  • Code assistance — autocomplete, boilerplate generation, refactoring suggestions, explaining unfamiliar code
  • Drafting and editing — first drafts of emails, documentation, marketing copy that a human then refines
  • Summarization — condensing long documents, meeting transcripts, or research papers
  • Search and retrieval-augmented generation (RAG) — combining a model with your own data so it can answer questions grounded in real documents instead of just its training data
  • Rapid prototyping — generating UI mockups, sample data, or test cases in seconds

Where to be skeptical

  • Hallucination — these models are fundamentally next-token predictors, not fact databases. They will confidently state things that are completely wrong, especially about niche or recent topics.
  • No true understanding — a model can write a flawless explanation of a bug without actually "understanding" your codebase the way a human engineer does. Treat it as a very well-read collaborator, not an oracle.
  • Data and bias — models inherit biases and gaps present in their training data. They're a reflection of what they were trained on, not an objective source of truth.
  • Cost and latency — running these models at scale is expensive and slow compared to traditional software; not every problem needs a 70-billion-parameter model to solve it.

A simple example: calling an LLM API

Here's what using a generative model actually looks like in code — no magic, just an API call:

JAVASCRIPT
1async function askAI(prompt) {
2  const response = await fetch("https://api.anthropic.com/v1/messages", {
3    method: "POST",
4    headers: {
5      "Content-Type": "application/json",
6      "x-api-key": process.env.ANTHROPIC_API_KEY,
7      "anthropic-version": "2023-06-01",
8    },
9    body: JSON.stringify({
10      model: "claude-sonnet-5",
11      max_tokens: 1024,
12      messages: [{ role: "user", content: prompt }],
13    }),
14  });
15
16  const data = await response.json();
17  return data.content[0].text;
18}
19
20const answer = await askAI("Explain generative AI in one paragraph.");
21console.log(answer);

Everything discussed above — attention, next-token prediction, RLHF — is hidden behind that single API call. That's the whole point: the complexity is enormous, but the interface can be a single function.

Wrapping up

Generative AI isn't magic — it's next-token prediction and iterative denoising, trained at a scale large enough to make the output feel intelligent. Understanding the mechanics doesn't make it less impressive; if anything, it makes it easier to use well: you know when to trust it, when to double-check it, and when a simpler tool would do the job better. As these models get faster, cheaper, and more capable, the engineers who understand what's actually happening under the hood will be the ones who use them most effectively.