DEV Community

Cover image for What Happens Inside an LLM Before It Generates Your First Token?
RAJSHREE
RAJSHREE

Posted on • Originally published at rjshree.com

What Happens Inside an LLM Before It Generates Your First Token?

Every day, billions of people ask AI assistants questions. The answers appear almost instantly, making the process feel effortless. Yet before the very first word appears, one of the most sophisticated inference pipelines ever built has already executed billions of mathematical operations. This invisible journey is where modern AI truly comes alive.

From tokenization and embeddings to GPU inference, KV Cache, FlashAttention, and speculative decoding.

Part 1 — From Human Language to Machine Understanding

The first word an AI generates is actually the last step of a remarkably complex journey.

When you open ChatGPT, Claude, Gemini, or any modern Large Language Model, the interaction feels almost magical.

You type a question.

You press Enter.

A brief pause follows.

Then, words begin appearing one after another—as if the model is thinking in real time.

To most users, it feels like the AI is simply "reading" the prompt and immediately responding.

But that's far from what actually happens.

Between the moment you press Enter and the moment the very first token appears on your screen, an extraordinary pipeline unfolds inside the model.

That single pause hides millions—sometimes billions—of mathematical operations happening across GPUs.

The model isn't reading English.

It isn't reasoning with words.

It isn't storing paragraphs inside its memory like humans do.

Instead, it transforms language into mathematics.

Only after completing an intricate sequence of computations does it predict the first token.

This article is not another simplified explanation of "how ChatGPT works."

Instead, we'll walk through the same inference pipeline that powers production-grade Large Language Models used by companies like OpenAI, Anthropic, Google DeepMind, Meta, and Mistral.

By the end of this series, you'll understand not only what happens inside an LLM—but why every step exists.


The Journey Before the First Token

Before diving into individual components, it's helpful to visualize the complete pipeline.

flowchart LR

A["👤 User Prompt"]
-->B["Tokenizer"]

B-->C["Token IDs"]

C-->D["Embeddings"]

D-->E["Positional Encoding"]

E-->F["Transformer Layers"]

F-->G["Probability Distribution"]

G-->H["Sampling"]

H-->I["First Generated Token"]
Enter fullscreen mode Exit fullscreen mode

Although this diagram appears simple, every block represents an entire field of research.

Some stages execute only once.

Others repeat for every generated token.

In this first part, we'll focus on the earliest stages—the ones responsible for converting human language into something a neural network can understand.


Why LLMs Don't Understand Words

One of the biggest misconceptions about AI is that models understand language the way humans do.

They don't.

Humans process meaning.

Machines process numbers.

Imagine asking ChatGPT:

Explain recursion using a simple analogy.

You see a sentence.

The model does not.

Before anything else happens, your sentence must become numerical data.

Because neural networks cannot perform calculations on letters.

They only understand vectors, matrices, and tensors.

This transformation is the foundation of modern Natural Language Processing.

Without it, GPT, Claude, Gemini, and Llama simply cannot operate.


Step 1 — Receiving the Prompt

Let's use a simple prompt throughout this article:

Why is the sky blue?
Enter fullscreen mode Exit fullscreen mode

From the user's perspective, this looks like plain English.

Inside the inference server, however, the prompt arrives as raw Unicode text.

At this stage, the model hasn't processed anything.

No intelligence has been applied.

No prediction has been made.

The inference server simply receives a sequence of characters.

Conceptually, it looks like this:

| Human View           | Machine View           |
|----------------------|------------------------|
| Why is the sky blue? | Raw Unicode characters |
Enter fullscreen mode Exit fullscreen mode

This is still unusable for the neural network.

The next stage changes everything.


Step 2 — Tokenization

Tokenization is often described as "splitting text into words."

That explanation is convenient.

It's also inaccurate.

Modern LLMs rarely tokenize by words.

Instead, they use subword tokenization, allowing them to efficiently represent nearly every language, programming syntax, emoji, and even spelling mistakes.

Consider this sentence:

Artificial Intelligence
Enter fullscreen mode Exit fullscreen mode

A tokenizer might split it like this:

| Text        |     Token |
|------|------------------|
| Artificial  |       Art |
| Artificial  |   ificial |
| Intelligence |    Intel |
| Intelligence |  ligence |
Enter fullscreen mode Exit fullscreen mode

Different models use different vocabularies.

GPT, Claude, Gemini, and Llama all have their own tokenizer implementations.

The exact tokens differ.

The underlying principle remains the same.

Instead of understanding words, the model understands predefined pieces of text.


Why Not Store Every Word?

Imagine storing every possible English word.

Now add:

  • Hindi
  • Japanese
  • Chinese
  • Python code
  • JavaScript
  • Emojis
  • URLs
  • Mathematical equations
  • Misspellings
  • Company names
  • Future slang

The vocabulary would become impossibly large.

Subword tokenization solves this elegantly.

A small vocabulary can represent virtually unlimited text combinations.

That's one reason modern LLMs scale so effectively.


Real Example

Suppose you type:

Unbelievable
Enter fullscreen mode Exit fullscreen mode

A tokenizer might produce something similar to:

| Token |
|--------|
| Un |
| believe |
| able |
Enter fullscreen mode Exit fullscreen mode

Instead of memorizing the entire word, the model builds meaning from reusable pieces.

This dramatically reduces vocabulary size while increasing flexibility.


Tokens Are Not Words

This distinction is surprisingly important.

Consider these examples.

| Input          | Approximate Tokens |
|----------------|------------------|
| Hello          |                 1 |
| Good morning |                 2–3 |
| Artificial Intelligence |      3–5 |
| 👋                      |      1–3 |
| `console.log()`          | Multiple |
Enter fullscreen mode Exit fullscreen mode

This is why API pricing is based on tokens, not words.

A thousand words can produce significantly more—or fewer—than a thousand tokens.

Understanding this difference becomes essential when optimizing AI applications for latency and cost.

We'll revisit token economics later in this series.


Engineering Insight

The tokenizer is not part of the neural network.

This surprises many developers.

The tokenizer is simply a preprocessing component.

It converts text into token IDs before the Transformer begins inference.

Only after tokenization does the actual LLM start working.


Step 3 — Converting Tokens into IDs

Tokens themselves are still text fragments.

The neural network cannot process strings either.

Each token is therefore mapped to an integer.

Example:

| Token   | Token ID |
|---------|----------|
| Why     |     4921 |
| is      |      318 |
| the     |      262 |
| sky     |     6766 |
| blue    |     4171 |
| ?       |       30 |
Enter fullscreen mode Exit fullscreen mode

These numbers have no mathematical meaning by themselves.

They simply act as unique identifiers inside the model's vocabulary.

Think of them like dictionary indexes.

The model still hasn't begun "thinking."

It has only converted language into references.


Why IDs Alone Are Meaningless

Suppose someone tells you:

4921
318
262
6766
4171
30
Enter fullscreen mode Exit fullscreen mode

Could you infer that this means:

Why is the sky blue?

Of course not.

These numbers contain no semantic information.

They merely point to entries in a vocabulary table.

Meaning enters the system in the next stage.


Step 4 — Embeddings: Where Language Becomes Mathematics

This is where the magic truly begins.

Each token ID is transformed into a high-dimensional vector called an embedding.

Instead of representing "blue" as:

4171
Enter fullscreen mode Exit fullscreen mode

the model converts it into something conceptually like:

[-0.18, 1.42, -0.77, 0.56, ...]
Enter fullscreen mode Exit fullscreen mode

Not four numbers.

Not forty.

Modern LLM embeddings often contain thousands of dimensions.

Each dimension captures subtle statistical relationships learned during training.

Words with similar meanings naturally occupy nearby regions in this mathematical space.

For example:

King
Queen
Prince
Princess
Enter fullscreen mode Exit fullscreen mode

end up close together.

Similarly,

Python
Java
JavaScript
C++
Enter fullscreen mode Exit fullscreen mode

form another neighborhood.

The model doesn't understand these concepts linguistically.

It understands them geometrically.

Meaning becomes distance.

Similarity becomes direction.

Language becomes linear algebra.


Visualizing Embedding Space

Imagine a simplified two-dimensional world.

                    Animal

                       🐶
                  🐺
         🦊

--------------------------------------------

                 🚗

                          🚀

Technology
Enter fullscreen mode Exit fullscreen mode

Real embeddings don't exist in two dimensions.

They exist in hundreds or thousands.

But the intuition remains valid.

Concepts that frequently appear in similar contexts become neighbors inside vector space.


Why Embeddings Changed AI Forever

Before embeddings, NLP relied heavily on handcrafted rules and sparse representations.

Embeddings introduced something revolutionary.

Instead of explicitly defining relationships, models learned them automatically from enormous amounts of text.

The model isn't told that:

Doctor and Physician are similar.

It discovers this statistically.

That capability transformed Natural Language Processing.

Embeddings became the foundation upon which modern Transformers were built.

Without embeddings, today's LLMs simply wouldn't exist.


Engineering Note

An embedding is not a definition.

It is a learned numerical representation whose position reflects how language behaves across billions of examples.


Where We Stand So Far

At this point, the model has still not generated a single token.

Yet it has already completed several critical stages.

| Stage | Completed           |
|--------|--------------------|
| Prompt received |       ✅ |
| Tokenization |          ✅ |
| Token IDs created |     ✅ |
| Embeddings generated |  ✅ |
| First token predicted | ❌ |
Enter fullscreen mode Exit fullscreen mode

Everything so far has been preparation.

The real computation is about to begin.


What's Coming Next

So far, we've transformed human language into mathematical vectors.

But vectors alone don't create intelligence.

The model still has no understanding of context.

It doesn't know which words relate to each other.

It doesn't know what part of the sentence is important.

It doesn't know whether "bank" refers to a financial institution or the side of a river.

That understanding emerges inside the Transformer—the architecture that revolutionized artificial intelligence.

In Part 2, we'll step inside the Transformer itself and explore:

  • Why positional encoding is necessary.
  • How self-attention allows every token to "look" at every other token.
  • Why multi-head attention exists.
  • How Feed Forward Networks refine representations.
  • Why residual connections and layer normalization make training deep models possible.

This is where the model begins constructing context—and where the journey toward the very first generated token truly starts.


Part 2 — Inside the Transformer: Where an LLM Actually Begins to "Think"

Everything you've seen so far was preparation.

Now the real computation begins.

In Part 1, we transformed human language into mathematical vectors.

At this point, the model has:

✅ Received your prompt

✅ Tokenized the text

✅ Converted tokens into IDs

✅ Generated embeddings

Yet something important is still missing.

The model has no understanding of context.

Imagine reading the sentence:

"Apple released a new chip."

Now read:

"Apple fell from the tree."

The word Apple appears in both sentences.

Humans instantly know they refer to completely different things.

How?

Because of context.

An embedding alone cannot determine which meaning is correct.

Context is created inside one of the greatest inventions in modern AI:

The Transformer


Why Transformers Changed Everything

Before 2017, language models processed text sequentially.

Word after word.

Like reading a book with one eye closed.

If the sentence became too long, models gradually "forgot" earlier information.

Long conversations became difficult.

Dependencies were lost.

Training was slow.

Then came the famous research paper:

Attention Is All You Need

The Transformer completely changed Natural Language Processing.

Instead of processing one word at a time...

Every token could look at every other token simultaneously.

That single idea transformed AI forever.

Today, GPT, Claude, Gemini, Llama, Mistral, DeepSeek and almost every modern LLM are based on this architecture.


A Bird's-Eye View

flowchart LR

A[Embeddings]
-->B[Positional Encoding]

B
-->C[Transformer Layer 1]

C
-->D[Transformer Layer 2]

D
-->E[Transformer Layer N]

E
-->F[Logits]
Enter fullscreen mode Exit fullscreen mode

Notice something interesting.

There isn't just one Transformer layer.

There are dozens.

Sometimes hundreds.

GPT-3 contains 96 Transformer blocks.

Larger models contain even more.

Each layer gradually refines the understanding of the sentence.


Step 5 — Positional Encoding

Imagine receiving these words:

Dog
Bites
Man
Enter fullscreen mode Exit fullscreen mode

Now rearrange them.

Man
Bites
Dog
Enter fullscreen mode Exit fullscreen mode

Exactly the same words.

Completely different meaning.

Embeddings alone cannot distinguish these two sentences.

Because embeddings don't know order.

Without additional information, the model sees only a collection of vectors.

That's why positional information is added.

Every embedding receives another vector representing its position inside the sequence.

Conceptually:

| Token   | Position |
|---------|----------|
| Dog     | 1        |
| Bites   | 2        |
| Man     | 3        |
Enter fullscreen mode Exit fullscreen mode

This positional information becomes part of the embedding itself.

Now the model understands not only what the token is...

...but also where it appears.


Engineering Insight

Without positional encoding:

I love AI

AI love I
Enter fullscreen mode Exit fullscreen mode

would appear almost identical to the model.

Word order matters.

Language depends on sequence.

Transformers must be taught that sequence.


Step 6 — Self-Attention

Now comes the most famous component of every LLM.

Self-Attention.

The name sounds complicated.

The idea is surprisingly intuitive.

Imagine reading this sentence.

"The cat sat on the mat because it was warm."

What does it refer to?

The cat?

Or the mat?

Humans naturally connect it with the mat because of context.

The Transformer performs something remarkably similar.

Every token asks:

"Which other tokens should I pay attention to before deciding my meaning?"

Hence the name:

Self-Attention.


Visualizing Attention

graph LR

A["The"]
B["Cat"]
C["Sat"]
D["On"]
E["The"]
F["Mat"]
G["It"]

G --> F
G --> B
C --> B
F --> D
Enter fullscreen mode Exit fullscreen mode

The arrows represent attention.

Each token decides which other tokens contain useful information.

Not all tokens are equally important.

Some receive stronger attention.

Others receive almost none.


Query, Key and Value

This is where many explanations become unnecessarily mathematical.

Let's simplify it without losing accuracy.

Every token creates three vectors.

| Vector   |            Purpose             |
|----------|--------------------------------|
| Query    | What am I looking for?         |
| Key      | What information do I contain? |
| Value    | What should I contribute?      |
Enter fullscreen mode Exit fullscreen mode

Think of a search engine.

Query:

"What am I searching?"

Key:

"Which documents match?"

Value:

"Return the information."

The Transformer performs a similar matching process.

Every token compares its Query with every other token's Key.

The stronger the similarity...

The higher the attention score.


Attention Matrix

Imagine four tokens.

The

Cat

Sat

Mat
Enter fullscreen mode Exit fullscreen mode

The model computes something conceptually like:

| | The | Cat  | Sat  | Mat |
|-------|------|------|------|------|
| The   |0.12  |0.21  |0.18  |0.49|
| Cat   |0.05  |0.60  |0.30  |0.05|
| Sat   |0.11  |0.55  |0.22  |0.12|
| Mat   |0.20  |0.10  |0.15  |0.55|
Enter fullscreen mode Exit fullscreen mode

These numbers represent attention weights.

Rows sum to approximately 1.

Higher values mean stronger attention.

Every generated token depends on this matrix.


Why Attention Is Powerful

Suppose your prompt is:

"The capital of France is"

To predict the next word...

The model should probably pay attention to:

France

capital

is

It doesn't need to focus much on "The".

Attention automatically learns these relationships during training.

No human manually programs them.


Multi-Head Attention

One attention mechanism isn't enough.

Language contains multiple relationships simultaneously.

Consider this sentence:

"The engineer fixed the server because it had crashed."

Different attention heads may focus on different things.

Head 1

Grammar

Head 2

Subject

Head 3

Verb

Head 4

Pronouns

Head 5

Long-distance dependencies

Instead of one perspective...

The model observes the sentence through many.

Hence the name:

Multi-Head Attention


flowchart TD

Embedding

-->Head1

Embedding

-->Head2

Embedding

-->Head3

Embedding

-->Head4

Head1-->Merge

Head2-->Merge

Head3-->Merge

Head4-->Merge

Merge-->Output
Enter fullscreen mode Exit fullscreen mode

Each head specializes.

Together, they produce a richer representation.


Engineering Note

Attention heads are not manually assigned.

Nobody tells Head 7:

"You will learn grammar."

They naturally specialize during training.

Some detect syntax.

Others capture semantics.

Some focus on punctuation.

Others track long-range dependencies.

Researchers still discover surprising behaviors inside attention heads.


Feed Forward Networks

Once attention updates the representation...

Each token passes through another neural network.

Called the Feed Forward Network (FFN).

Think of attention as gathering information.

Think of FFN as processing that information.

Every token independently flows through:

Linear

↓

Activation Function

↓

Linear
Enter fullscreen mode Exit fullscreen mode

This stage introduces additional non-linearity.

Without it...

The Transformer would be dramatically less expressive.


Residual Connections

Deep neural networks suffer from a problem.

As layers increase...

Training becomes unstable.

Information gradually disappears.

Residual connections solve this elegantly.

Instead of replacing the previous representation...

The Transformer keeps it.

Conceptually:

Output

=

Attention(x)

+

x
Enter fullscreen mode Exit fullscreen mode

The original information never completely disappears.

Every layer refines.

None starts from scratch.


Layer Normalization

Imagine training billions of parameters.

Small numerical differences quickly explode.

Layer Normalization keeps activations stable.

Every Transformer block includes normalization before moving forward.

Without it...

Training massive LLMs becomes extremely difficult.


One Transformer Layer

Putting everything together:

flowchart TD

A[Input Embedding]

A-->B[LayerNorm]

B-->C[Multi Head Attention]

C-->D[Residual Add]

D-->E[LayerNorm]

E-->F[Feed Forward Network]

F-->G[Residual Add]

G-->H[Output]
Enter fullscreen mode Exit fullscreen mode

This single block repeats...

Again.

Again.

Again.

Sometimes nearly one hundred times.

Each pass creates richer contextual understanding.


Does the Model Understand Meaning?

Not in the human sense.

It builds increasingly sophisticated mathematical representations.

After enough layers...

The embedding for:

Apple
Enter fullscreen mode Exit fullscreen mode

inside

Apple released a new iPhone.

becomes completely different from

Apple
Enter fullscreen mode Exit fullscreen mode

inside

Apple fell from the tree.

Same token.

Different context.

Different vector.

That's contextual intelligence.


Where We Stand

After Part 2, the model has now:

| Stage |                    Status |
|----------|------------------------|
| Prompt Received |           ✅ |
| Tokenization |              ✅ |
| Token IDs |                 ✅ |
| Embeddings |                ✅ |
| Positional Encoding |       ✅ |
| Multi-Head Attention |      ✅ |
| Feed Forward Layers |       ✅ |
| Context Built |             ✅ |
| First Token Generated |     ❌ |
Enter fullscreen mode Exit fullscreen mode

The model now possesses a deep contextual understanding of the prompt.

But one critical question remains.

How does it actually decide that the next token should be:

blue

instead of

green

or

beautiful

or

impossible?

That decision happens in the final stage of inference.

It involves probability distributions, logits, temperature, Top-K sampling, nucleus sampling, and one of the most fascinating pieces of engineering inside every modern LLM.

That's exactly where we'll continue in Part 3, where the model finally generates its very first token.


Part3 - Predicting the First Token: From Mathematics to Language

So far, the model has done something remarkable.

It has transformed your prompt from plain text into contextual representations enriched through dozens of Transformer layers.

At this stage, the model understands the relationships between tokens.

It knows grammar.

It knows context.

It knows semantics.

But it still hasn't generated a single word.

One final question remains.

How does the model decide what to write first?

This is where probability, statistics, and decision-making come together.

The answer lies in five critical stages:

  1. Logits
  2. Softmax
  3. Temperature
  4. Top-K & Top-P Sampling
  5. First Token Prediction

Together, these stages determine every word an LLM ever produces.


Step 7 — Logits: Every Token Becomes a Candidate

Imagine you're asking ChatGPT:

The capital of France is
Enter fullscreen mode Exit fullscreen mode

After processing the entire prompt, the Transformer outputs a giant vector.

Not a sentence.

Not a word.

Just numbers.

Lots of numbers.

Suppose the model's vocabulary contains 100,000 tokens.

The output layer now generates 100,000 scores.

One score for every possible token.

These raw scores are called logits.

Conceptually:

| Token | Logit |
|--------|-------:|
| Paris | 18.9 |
| London | 10.2 |
| Berlin | 9.4 |
| Pizza | -3.1 |
| Elephant | -8.5 |
| Galaxy | -11.0 |
Enter fullscreen mode Exit fullscreen mode

These values are not probabilities.

They're simply confidence scores.

Higher score means:

"I believe this token is more likely."

Lower score means:

"This token probably doesn't belong here."


Engineering Insight

The model doesn't search Google.

It doesn't query Wikipedia.

It doesn't retrieve a stored sentence.

Instead, it computes a score for every single token in its vocabulary.

Only then does it choose one.


Why Logits Aren't Enough

Consider two logits.

| Token | Logit |
|--------|-------:|
| Paris | 18 |
| London | 17 |
Enter fullscreen mode Exit fullscreen mode

Does that mean Paris is only 1% more likely?

10%?

100%?

Impossible to tell.

Logits have no intuitive meaning.

They can be:

-25

0

17

145

3.4
Enter fullscreen mode Exit fullscreen mode

They aren't constrained.

They're not normalized.

To convert them into usable probabilities, the model performs another operation.


Step 8 — Softmax: Turning Scores into Probabilities

Softmax transforms arbitrary scores into a probability distribution.

Before Softmax:

| Token | Logit |
|--------|-------:|
| Paris | 18 |
| London | 15 |
| Berlin | 12 |
Enter fullscreen mode Exit fullscreen mode

After Softmax:

| Token | Probability |
|--------|------------:|
| Paris | 92.1% |
| London | 6.4% |
| Berlin | 1.5% |
Enter fullscreen mode Exit fullscreen mode

Now everything makes sense.

The probabilities always sum to 100%.

92.1%

+

6.4%

+

1.5%

=

100%
Enter fullscreen mode Exit fullscreen mode

Every generated token begins with this probability distribution.


Visual Representation

Paris      ████████████████████████ 92%

London     ██ 6%

Berlin     ▏1%

Others     .
Enter fullscreen mode Exit fullscreen mode

This is what the model actually "sees."

A landscape of possibilities.


Why Doesn't the Model Always Pick the Highest Probability?

Because language isn't deterministic.

Suppose you ask:

Write a fantasy story.

If the model always selected the highest probability token...

Every user would receive nearly identical stories.

Creativity would disappear.

Responses would become repetitive.

This is where sampling strategies become essential.


Step 9 — Temperature: Controlling Creativity

Temperature controls how confident—or adventurous—the model becomes.

Think of it as adjusting the model's willingness to take risks.


Low Temperature (0.1)

The model strongly prefers the highest-probability token.

Paris

Paris

Paris

Paris
Enter fullscreen mode Exit fullscreen mode

Responses become:

  • factual
  • deterministic
  • repetitive

Perfect for:

  • documentation
  • coding
  • legal drafting
  • mathematics

Medium Temperature (0.7)

Now the model occasionally explores alternatives.

Responses become:

  • natural
  • conversational
  • varied

This is where most chat assistants operate.


High Temperature (1.5)

The probability distribution becomes flatter.

Suddenly, unlikely words gain a chance.

The model becomes:

  • creative
  • surprising
  • unpredictable

Useful for:

  • storytelling
  • poetry
  • brainstorming

Less useful for:

  • production code
  • financial advice
  • medical information

Example

Prompt:

Once upon a
Enter fullscreen mode Exit fullscreen mode

Temperature = 0.1

time
Enter fullscreen mode Exit fullscreen mode

Temperature = 0.8

time

storm

dream

morning
Enter fullscreen mode Exit fullscreen mode

Temperature = 1.8

nebula

dragon

dimension

violin
Enter fullscreen mode Exit fullscreen mode

Higher temperature increases diversity.

It doesn't increase intelligence.


Common Misconception

Temperature changes randomness—not knowledge.

A model with Temperature = 2.0 does not know more.

It simply explores less likely possibilities.


Step 10 — Top-K Sampling

Imagine the vocabulary contains 100,000 possible tokens.

Should the model really consider all of them?

Probably not.

Most are completely irrelevant.

For:

The capital of France is
Enter fullscreen mode Exit fullscreen mode

there's little reason to consider:

  • banana
  • spaceship
  • volcano
  • refrigerator

Top-K solves this elegantly.

Instead of considering every token...

The model keeps only the K highest-scoring candidates.

Example:

Top K = 5
Enter fullscreen mode Exit fullscreen mode

Remaining candidates:

| Token | Probability |

| Paris | 72% |

| Lyon | 12% |

| Marseille | 7% |

| Nice | 5% |

| Bordeaux | 4% |

Everything else is discarded.

The next token must come from this shortlist.


Why Top-K Exists

Benefits:

  • Faster sampling
  • Better quality
  • Fewer bizarre outputs
  • Reduced randomness

Step 11 — Top-P (Nucleus Sampling)

Top-K has one limitation.

Sometimes five candidates are enough.

Sometimes twenty are necessary.

A fixed number isn't always ideal.

Top-P uses a smarter strategy.

Instead of selecting a fixed number of tokens...

It selects enough tokens whose combined probability exceeds a threshold.

Example:

P = 0.90
Enter fullscreen mode Exit fullscreen mode
  1. Suppose probabilities are:
| Token     | Probability |

| Paris     | 55% |

| Lyon      | 20% |

| Marseille | 10% |

| Nice      | 8% |

| Bordeaux  | 3% |

| Others    | 4% |
Enter fullscreen mode Exit fullscreen mode

Cumulative probability:

Paris

55%

Paris + Lyon

75%

+ Marseille

85%

+ Nice

93%
Enter fullscreen mode Exit fullscreen mode

The model stops here.

Only these four tokens remain.

Everything else is ignored.


Why Modern LLMs Prefer Top-P

Because language is dynamic.

Sometimes only one answer is obvious.

Sometimes dozens are equally reasonable.

Top-P adapts automatically.

That's why many production LLMs combine:

  • Temperature
  • Top-P

rather than relying on Top-K alone.


Putting Everything Together

flowchart LR

A[Transformer Output]

-->B[Logits]

-->C[Softmax]

-->D[Temperature]

-->E[Top-K / Top-P]

-->F[Random Sampling]

-->G[First Token]
Enter fullscreen mode Exit fullscreen mode

This pipeline executes for every generated token.

Not just the first one.


Finally... The First Token Appears

Suppose your prompt is:

The capital of France is
Enter fullscreen mode Exit fullscreen mode

After everything we've discussed...

The model samples:

Paris
Enter fullscreen mode Exit fullscreen mode

That single word now becomes part of the context.

The updated prompt becomes:

The capital of France is Paris
Enter fullscreen mode Exit fullscreen mode

The entire inference process begins again.

Transformer.

Logits.

Softmax.

Sampling.

Next token.

Again.

Again.

Again.

This loop continues until the model predicts an end-of-sequence token or reaches the maximum generation length.


Why LLMs Generate One Token at a Time

One of the most common misconceptions is:

"The model writes the whole sentence internally and then streams it."

It doesn't.

It literally generates:

The
Enter fullscreen mode Exit fullscreen mode


The capital
Enter fullscreen mode Exit fullscreen mode


The capital of
Enter fullscreen mode Exit fullscreen mode


The capital of France
Enter fullscreen mode Exit fullscreen mode


The capital of France is
Enter fullscreen mode Exit fullscreen mode


The capital of France is Paris
Enter fullscreen mode Exit fullscreen mode

Every new token depends on every previous token.

The future doesn't exist until the model predicts it.

This autoregressive process is what gives LLMs both their flexibility and their computational cost.


Engineering Insight

The first generated token is often the most expensive.

Why?

Because the model must process the entire input prompt before making its first prediction.

Once that first token exists, modern inference engines reuse much of the previous computation instead of starting from scratch.

That optimization—called the KV Cache—is one of the biggest reasons today's LLMs can generate text at interactive speeds.

We'll explore exactly how KV Cache works, why the first token has the highest latency, and how production systems like GPT, Claude, Gemini, and Llama optimize inference in the next part.


Where We Stand

| Stage                | Status |
| Prompt Received      | ✅ |
| Tokenization         | ✅ |
| Embeddings           | ✅ |
| Positional Encoding  | ✅ |
| Transformer Layers   | ✅ |
| Logits Generated     | ✅ |
| Softmax Applied      | ✅ |
| Temperature Adjusted | ✅ |
| Top-K / Top-P Sampling | ✅ |
| **First Token Generated** | ✅ |
Enter fullscreen mode Exit fullscreen mode

At last, the model has spoken its very first word.

But the journey is far from over.

The next challenge is speed.

How can a model with billions of parameters generate dozens of tokens every second?

The answer lies in production inference engineering—KV Cache, GPU parallelism, batching, streaming, Flash Attention, and speculative decoding—the technologies that make modern AI feel almost instantaneous.

Part4 - Production Inference: Why the First Token Takes Longer Than the Rest

If you've ever used ChatGPT, Claude, or Gemini, you've probably noticed something interesting.

There's usually a brief pause before the first word appears.

After that, the response streams almost instantly.

This behavior isn't accidental.

It's the result of one of the most sophisticated engineering pipelines in modern computing.

To understand why, we need to move beyond neural networks and enter the world of production inference.

This is where software engineering meets deep learning.


Why the First Token Is the Slowest

Imagine asking ChatGPT:


Explain how quantum computers work in simple terms.

Enter fullscreen mode Exit fullscreen mode

Before generating even one word, the model must process the entire prompt.

That means:

  • Tokenization

  • Embeddings

  • Positional Encoding

  • Every Transformer Layer

  • Every Attention Head

  • Every Feed Forward Network

  • Logits

  • Sampling

Only after completing all of these computations can it predict the very first token.

This stage is called the Prefill Phase.


flowchart LR



A[User Prompt]

-->B[Tokenization]



B-->C[Embeddings]



C-->D[Transformer]



D-->E[Attention]



E-->F[Logits]



F-->G[Sampling]



G-->H["First Token"]

Enter fullscreen mode Exit fullscreen mode

Everything above happens before the first visible word appears.


Prefill vs Decode

Modern LLM inference has two distinct phases.


| Phase       | Purpose          | Computational Cost  |

|--------     |----------|-----------------------------|

| **Prefill** | Process the entire prompt | Very High  |

| **Decode** | Generate one token at a time | Much Lower |

Enter fullscreen mode Exit fullscreen mode

Think of reading a book.

Before answering a question about Chapter 10, you first need to read Chapters 1–9.

That's the prefill phase.

Once you've read them, answering follow-up questions becomes much easier.

That's decoding.


Why Doesn't the Model Recompute Everything?

Imagine generating a 500-word answer.

Without optimization, the model would need to re-read the entire conversation for every new token.

For token #1

Read 100 tokens

For token #2

Read 101 tokens

For token #3

Read 102 tokens

Eventually:


100



101



102



103



104



...



600

Enter fullscreen mode Exit fullscreen mode

The computational cost would explode.

Fortunately...

Modern LLMs never do this.


KV Cache: The Hidden Hero of LLM Inference

One of the biggest innovations in production AI is something most users never hear about.

KV Cache.

Without it, ChatGPT would feel dramatically slower.


A Simple Analogy

Imagine reading a 400-page textbook.

Someone asks:

What's written on page 400?

You read the entire book.

Then they ask another question about page 401.

Would you start reading from page 1 again?

Of course not.

You'd continue from where you stopped.

KV Cache works the same way.

Instead of recomputing previous attention information...

The model remembers it.


What Does "KV" Mean?

During Self-Attention, every token produces three vectors:

  • Query (Q)

  • Key (K)

  • Value (V)

We already explored this in Part 2.

Here's the clever optimization.

Once Keys and Values are computed...

They almost never change.

So instead of recomputing them for every new token...

The model stores them in memory.

Hence the name:

Key-Value Cache


Without KV Cache

Suppose you've already generated:


Artificial intelligence is transforming

Enter fullscreen mode Exit fullscreen mode

Now you want to generate:


the

Enter fullscreen mode Exit fullscreen mode

Without caching:


Artificial



↓



intelligence



↓



is



↓



transforming



↓



the

Enter fullscreen mode Exit fullscreen mode

Every token would need to be processed again.

Again.

And again.

And again.


With KV Cache

The previous computations already exist.

Only the newest token requires fresh attention.


Cached



Artificial



✓



intelligence



✓



is



✓



transforming



✓



New Token



↓



the

Enter fullscreen mode Exit fullscreen mode

The speed improvement is enormous.


Visualizing KV Cache


flowchart LR



A["Prompt"]



-->B["Transformer"]



B



-->C["Key Cache"]



B



-->D["Value Cache"]



C-->E["Next Token"]



D-->E

Enter fullscreen mode Exit fullscreen mode

Notice something important.

The Transformer doesn't discard previous work.

It reuses it.

This is one reason modern LLMs can generate dozens—or even hundreds—of tokens every second.


Engineering Insight

KV Cache doesn't make the model smarter.

It makes inference dramatically faster by avoiding redundant computation.


The Cost of Long Conversations

Now let's explore something you've probably experienced.

Long chats often become slower.

Why?

Because the KV Cache keeps growing.

Suppose your conversation contains:


50 tokens

Enter fullscreen mode Exit fullscreen mode

Easy.

Now imagine:


5,000 tokens

Enter fullscreen mode Exit fullscreen mode

Every new token must attend to a much larger context.

More context means:

  • More GPU memory

  • Larger attention matrices

  • Higher latency

  • Increased computational cost

This is why context windows matter.


Context Window Isn't Just About Memory

People often think:

"A larger context window is always better."

Not necessarily.

A larger context allows the model to remember more information.

But it also means:

  • More memory consumption

  • Higher inference cost

  • Longer processing time

  • Greater GPU bandwidth requirements

Engineering is always about trade-offs.


GPU Inference: Why CPUs Aren't Enough

Could ChatGPT run on your laptop's CPU?

Technically...

Yes.

Practically...

Not at scale.

A modern LLM performs billions of matrix multiplications during inference.

Matrix multiplication is exactly what GPUs were designed for.

Unlike CPUs, which excel at sequential tasks...

GPUs execute thousands of mathematical operations simultaneously.

Think of it this way.

CPU


Task 1



↓



Task 2



↓



Task 3



↓



Task 4

Enter fullscreen mode Exit fullscreen mode

GPU


Task 1



Task 2



Task 3



Task 4



↓



All Execute Together

Enter fullscreen mode Exit fullscreen mode

This massive parallelism is why GPUs dominate AI workloads.


Why Matrix Multiplication Dominates AI

Inside every Transformer layer, operations like these occur repeatedly:


Embedding



×



Weight Matrix



↓



Attention



×



Projection Matrix



↓



Feed Forward



×



Parameter Matrix

Enter fullscreen mode Exit fullscreen mode

Almost every stage depends on matrix multiplication.

If matrix multiplication is slow...

The entire model becomes slow.


Continuous Batching: Serving Thousands of Users

Imagine an AI service receiving requests from:

  • Alice

  • Bob

  • Charlie

  • Diana

Should the GPU process them one by one?

That would waste enormous computational power.

Instead, inference servers combine multiple requests into a single batch.


flowchart LR



A[Alice]



B[Bob]



C[Charlie]



D[Diana]



A-->GPU



B-->GPU



C-->GPU



D-->GPU



GPU-->Responses

Enter fullscreen mode Exit fullscreen mode

This technique is called Continuous Batching.

Instead of waiting for one request to finish before starting another...

The GPU keeps processing incoming requests continuously.

The result:

  • Higher throughput

  • Better GPU utilization

  • Lower infrastructure cost

  • Faster average response times


Why AI Companies Invest So Much in Inference

Training an LLM is incredibly expensive.

But surprisingly...

Inference often becomes even more expensive over time.

Why?

Because millions of users interact with the model every day.

Every conversation requires:

  • GPU memory

  • Compute cycles

  • Network bandwidth

  • Scheduling

  • KV Cache management

  • Token streaming

For large AI providers, optimizing inference by even 5% can save millions of dollars annually.

That's why so much engineering effort goes into making models faster—not just smarter.


Engineering Takeaway

At this point, we've moved beyond neural networks and into production systems.

Generating the first token isn't only a machine learning problem.

It's also a distributed systems problem.

It's a GPU scheduling problem.

It's a memory optimization problem.

It's an infrastructure problem.

The intelligence of an LLM comes from its parameters.

But the responsiveness you experience comes from world-class engineering.


Up Next

We've now uncovered why the first token is slower, how KV Cache avoids redundant computation, why GPUs are essential, and how AI companies efficiently serve millions of users simultaneously.

But we're still missing some of the most fascinating innovations behind modern LLMs.

In the final part, we'll explore:

  • FlashAttention — How engineers drastically reduce memory usage during attention.

  • Speculative Decoding — How two models collaborate to generate tokens faster.

  • Streaming Responses — Why words appear one by one instead of all at once.

  • Tensor Parallelism & Model Sharding — How trillion-parameter models run across multiple GPUs.

  • Common Misconceptions About LLMs — Separating popular myths from reality.

  • Final Engineering Insights — What every AI engineer should take away from the complete inference pipeline.

By the end, you'll not only understand what happens before the first token—but also why modern AI feels as fast and responsive as it does.

Final Thoughts

Artificial Intelligence often feels magical.

You ask a question.

A few moments later, an answer appears.

The entire interaction feels almost effortless.

But now you know the truth.

Behind that seemingly simple conversation lies an extraordinary engineering pipeline.

Before the very first token reaches your screen, your prompt has already been transformed into numerical representations, enriched through layers of attention, processed by billions of parameters, evaluated across an entire vocabulary, optimized by sophisticated sampling strategies, accelerated through GPU clusters, and refined by decades of research in machine learning, mathematics, and computer systems.

The next time an AI assistant pauses before answering, remember:

It isn't searching the internet.

It isn't reading your question like a human.

It isn't secretly writing the entire response before showing it.

It is performing one of the most remarkable sequences of computations ever engineered—transforming language into mathematics, mathematics into probabilities, and probabilities back into language, one token at a time.

Perhaps that's what makes modern AI so fascinating.

Not because it feels like magic.

But because, once you understand what's happening beneath the surface, you realize it's something even more impressive:

Brilliant engineering.


Key Takeaways

  • Large Language Models don't process words—they process tokens and vectors.
  • Understanding emerges through attention, not memorization.
  • Every generated token is the result of probability, not certainty.
  • The first token is the most computationally expensive because the entire prompt must be understood before generation begins.
  • Technologies like KV Cache, FlashAttention, continuous batching, and speculative decoding make modern AI practical at global scale.
  • Behind every conversation with an LLM lies a remarkable combination of machine learning, linear algebra, distributed systems, and high-performance computing.

Thank You for Reading

If you've made it this far, thank you for investing your time in understanding one of the most fascinating pieces of modern engineering.

My goal wasn't simply to explain how an LLM generates its first token, but to help you appreciate the incredible ideas, research, and engineering that make today's AI systems possible.

I hope this article helped turn what once felt like a mysterious black box into something a little more understandable.

If you found this article valuable:

  • Share it with a fellow developer, AI enthusiast, or student.
  • Start a conversation about it on LinkedIn or X.
  • And if you learned something new, consider sharing your own knowledge—because the best way to truly understand a concept is to explain it to someone else.

After all,

Code powers software.

Knowledge powers engineers.

And knowledge grows only when it's shared.

Happy learning, and I'll see you in the next deep dive.

About The Author

Hi, I’m RAJश्री (Rajshree), a Software Engineer passionate about building modern web applications with the MERN stack while exploring AI, machine learning, and web performance. I enjoy creating projects, writing about what I learn, and continuously improving as a developer.

🌐 Portfolio: https://rjshree.com

💼 LinkedIn: https://linkedin.com/in/rjshree

💻 GitHub: https://github.com/itsrjshree

If you enjoyed this article, consider following me for more writing on software engineering, AI, technology, and the journey of continuous learning.

Happy learning, and I'll see you in the next deep dive.

Thanks for reading!

Top comments (0)