Ever wondered how Large Language Models (LLMs) like ChatGPT can write poems, debug code, or help…
What Is a Large Language Model (LLM)?

How LLM (Large Language Model) works.
Ever wondered how Large Language Models (LLMs) like ChatGPT can write poems, debug code, or help you sound smarter in emails — all without demanding coffee breaks? These digital marvels aren’t fueled by magic (though it may seem like it). Instead, they rely on math, mountains of data, and a sprinkle of machine learning wizardry. In this blog, we’ll peel back the curtain on how LLMs really work — no PhD required. Whether you’re a curious newbie or someone just trying to sound cool at tech meetups, buckle up! We’re diving into token soup, neural networks, and why “attention” isn’t just something your dog wants.
- What Is a Large Language Model (LLM)?
A Large Language Model, or LLM, is like that overachieving friend who’s read everything — books, articles, code snippets, memes — and somehow remembers it all. At its core, an LLM is a type of artificial intelligence trained to understand and generate human-like language. It doesn’t “understand” the way we do, but it’s exceptionally good at predicting the next word in a sentence based on patterns it learned from massive text datasets.
Think of it like autocomplete on steroids — but instead of just finishing your sentence, it can write you an entire essay, haiku, or Python script… sometimes all at once!
2. How Are LLMs Trained?
Training a Large Language Model (LLM) is a bit like teaching a hyperactive parrot to complete your sentences — only this parrot has a neural network for a brain and is trained on half the internet.
Let’s break it down:
2.1 Pretraining: The Big Read
The process starts with pretraining, where the model is exposed to a massive corpus of text — ranging from books and Wikipedia to Stack Overflow and (occasionally questionable) Reddit threads. The goal? Learn the statistical structure of language.
This is done in a self-supervised fashion: no humans label anything. Instead, the model learns by trying to predict the next word (token) in a sentence. This is called causal language modeling.
For example, given:
The quick brown ___
It should ideally guess “fox,” not “hedgehog” (unless you’re reading fan fiction).
2.2 Tokenization: Breaking Down the Words
Before any of this magic happens, raw text is broken into tokens — sub-word units using methods like Byte-Pair Encoding (BPE) or WordPiece. This lets the model handle rare and unknown words more effectively.
For instance:
“unbelievable” → [“un”, “believ”, “able”]
This helps reduce vocabulary size while maintaining expressive power.
2.3 Objective: Minimize Loss (Like Emotional, But for Models)
The model learns by minimizing a loss function — typically cross-entropy loss — which quantifies the difference between the model’s predicted probability distribution over tokens and the actual next token.
If it predicts “fox” with 10% confidence but the correct answer was “fox,” the model feels bad (figuratively) and updates its internal parameters using backpropagation.
2.4 Parameters: Millions? Try Billions.
During training, the model updates billions (or even trillions) of weights — the knobs and dials inside neural network layers. These are optimized using gradient descent variants like Adam or LAMB, with help from techniques like learning rate warmup, layer normalization, and dropout to prevent overfitting or training collapse.
2.5 Infrastructure: Not Your Average Laptop Job
Training is done on clusters of high-end GPUs/TPUs. Think thousands of NVIDIA A100s humming in parallel, gulping terabytes of text, and producing petaflops of compute — all to let a chatbot eventually write haikus or debate Shakespearean characters.
This pretraining phase results in a model that has seen a lot, but isn’t exactly street-smart yet. That’s where fine-tuning may come in — task-specific training using curated datasets (like instruction-following or safety alignment), but that’s a story for another section.
3. How LLMs Generate Text (Conditional Probability)
Once trained, how does an LLM actually generate text? For this we must understand conditional probability.
Let’s consider a population of 14 individuals:

View of Population
- Some of them like Tennis
- Some like Footbal
- A few like both
- And few like none
Conditional probability is a measure of the probability of an event given that another event has occurred.

Conditional Probability
If the events are A and B, we denote this as P(A|B).
This reads as “probability of A given B”
For instance, if we’re predicting whether it will rain today (event A), knowing that it’s cloudy (event B) might impact our prediction.
As it’s more likely to rain when it’s cloudy, we’d say the conditional probability P(A|B) is high.
That’s conditional probability!
In an LLM, this same idea applies: the model estimates the probability of the next word (A) given all previous words (B). So when you type ‘Once upon a’, it calculates which next word — ‘time’, ‘dream’, or ‘planet’ — has the highest conditional probability P(next_word | previous_words).

Transformer architecture.
4. What’s Under the Hood? (Hint: It’s Transformers, Not Magic)
Now that we’ve fed our LLM enough text to make it the world’s most well-read entity, let’s peek under the hood and see how it processes all that information.
At the core of modern LLMs lies the Transformer architecture, first introduced in the 2017 paper “Attention is All You Need.” And yes, that paper basically turned the entire field of NLP on its head.
4.1 Transformer: A Language Assembly Line
A Transformer model consists of stacked layers — each layer contains:
Multi-head Self-Attention: Helps the model decide which words to focus on when interpreting a sentence. For example, in:
The bird saw the man with the telescope.
The model uses self-attention to figure out whether “with the telescope” modifies “saw” or “man.”
Feedforward Networks (FFN): Each token’s representation is passed through a small neural network to enrich its encoding. Think of it like applying makeup: each token gets dressed up for the next stage.
Layer Normalization & Residual Connections: These keep training stable and help gradients flow, reducing the chances of catastrophic breakdown halfway through training.
4.2 Self-Attention: The Secret Sauce
Self-attention lets the model look at all the words in a sequence simultaneously and assign weights to how important each word is in relation to others. It uses query, key, and value vectors (Q, K, V) for every token to calculate attention scores.
The formula looks scary:

But here’s a simpler metaphor:
Query = “What am I looking for?”
Key = “What do I offer?”
Value = “What should I contribute?”
The model compares every token with every other token using these roles and blends their values accordingly. It’s like a brainstorming session where each word in a sentence decides who it should listen to most.
4.3 Positional Encoding: Transformers Have No Memory (Literally)
Unlike RNNs, Transformers don’t process text sequentially — they look at everything in parallel. So how do they know the order of words?
Enter positional encodings: mathematical patterns added to token embeddings to give the model a sense of sequence. These are often sine/cosine waves with varying frequencies, helping the model distinguish between “The cat sat” and “Sat the cat.”
4.4 Decoder-Only Models (like GPT)
GPT and similar LLMs use decoder-only transformers. This means:
- They read input left-to-right (causal masking).
- They never “see the future” during training.
- Each layer stacks up to refine the understanding of what comes next.
So, in essence, LLMs don’t ‘understand’ language the way we do — they predict it. But by stacking billions of parameters, self-attention layers, and data-rich training, they’ve learned to mimic human expression with uncanny accuracy. Whether it’s writing code or composing poetry, LLMs are just probability machines with great taste in words.
References
메타데이터
- post_id
- fcb27f9553f0
- slug
- ever-wondered-how-large-language-models-llms-like-chatgpt-can-write-poems-debug-code-or-help-fcb27f9553f0
- url
- https://medium.com/@kashyaprajan056/ever-wondered-how-large-language-models-llms-like-chatgpt-can-write-poems-debug-code-or-help-fcb27f9553f0
- canonical_url
- https://medium.com/@kashyaprajan056/ever-wondered-how-large-language-models-llms-like-chatgpt-can-write-poems-debug-code-or-help-fcb27f9553f0
- author_url
- https://medium.com/@kashyaprajan056
- status
- ok
- fetched_at
- 2026-07-21 13:44:03