Debug School

rakesh kumar
rakesh kumar

Posted on • Edited on

How LLMs Process a Prompt: Complete Workflow from Tokenization and Embeddings to Transformer, Attention, and Next-Token Prediction

Section 1 — Training / Model Creation
Section 2 — Inference / Actual LLM Processing
LLM Foundations Experiment Lab
Experiments to Build
Recommended Final Mini-Projects
Final Project — LLM Foundations Experiment Lab
Most Important Experiments

LLM Architecture is the internal structure and flow of a Large Language Model that explains how it takes text as input, understands context, and generates the next words as output.

In simple flow:

Input Text → Tokens → Embeddings → Transformer → Attention → Context Understanding → Next-Token Prediction → Output Text
Enter fullscreen mode Exit fullscreen mode

Example:

Input: “The cat is sitting on the…”

The LLM processes the words using tokens, embeddings, transformers, and attention, then predicts:

Output: “mat”

Section 1: how an LLM is created/trained.
Section 2: what happens when a user actually sends a prompt.

Section 1 — Training / Model Creation

These steps happen before you use ChatGPT-like models.

Section 1 in one line

Transformer Architecture
        ↓
Initialize Parameters
        ↓
Pre-training learns general patterns
        ↓
Optional Fine-tuning
        ↓
Instruction Tuning
        ↓
Trained LLM

Enter fullscreen mode Exit fullscreen mode

Section 2 — Inference / Actual LLM Processing

Now suppose you type:

“Explain photosynthesis in simple words.”

This is where actual runtime processing starts.

  1. Input Prompt
Explain photosynthesis in simple words.
Enter fullscreen mode Exit fullscreen mode

This is simply the text sent by the user.

Purpose: Tell the LLM what you want.

  1. Tokenization

The model cannot directly work with normal sentences. A tokenizer breaks text into smaller pieces.

For example, conceptually:

"Explain photosynthesis in simple words."


       ↓ Tokenization


["Explain", " photo", "synthesis", " in", " simple", " words", "."]
Enter fullscreen mode Exit fullscreen mode

Real tokenizers may split words differently.

Purpose: Convert human text into units the model can process.

  1. Tokens

Each token is represented by a numerical ID.

For example:

Explain        →  21435
photo          →   9821
synthesis      →  17420
in             →    287
simple         →   4382
words          →   2456
Enter fullscreen mode Exit fullscreen mode

So:

Text
 ↓
Tokenization
 ↓
Tokens
 ↓
[21435, 9821, 17420, 287, 4382, 2456]
Enter fullscreen mode Exit fullscreen mode

Purpose: Give every text piece a machine-readable identity.

  1. Context Window

The context window defines how many tokens the LLM can consider together.

Imagine the model can process:

Previous conversation
+
System instructions
+
User prompt
+
Retrieved information
Enter fullscreen mode Exit fullscreen mode

all within its available context.

Conceptually:

┌──────────── Context Window ───────────────┐
│ Previous conversation                    │
│ User: What is a plant?                   │
│ Assistant: ...                           │
│ User: Explain photosynthesis simply      │
└──────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

Purpose: Determines how much information the model can use while answering.

A small correction to your diagram: context window isn't really a transformation step like tokenization or embeddings. It is better thought of as the limit/boundary around the tokens being processed.

  1. Embeddings

Token IDs such as:

21435
9821
17420
Enter fullscreen mode Exit fullscreen mode

don't contain useful meaning by themselves.

The model converts them into vectors:

"plant"
   ↓
[0.21, -0.54, 0.88, 0.13, ...]
Enter fullscreen mode Exit fullscreen mode
"sunlight"
   ↓
[0.17, -0.49, 0.91, 0.08, ...]
Enter fullscreen mode Exit fullscreen mode

These vector representations allow the neural network to work with language mathematically.

Purpose: Convert token IDs into numerical representations that the Transformer can process.

So:

Text
 ↓
Tokenization
 ↓
Tokens / Token IDs
 ↓
Embeddings

Enter fullscreen mode Exit fullscreen mode

Transformer Layers

Now the embeddings enter many Transformer layers.

Conceptually:

Embeddings
   ↓
Transformer Layer 1
   ↓
Transformer Layer 2
   ↓
Transformer Layer 3
   ↓
...
   ↓
Transformer Layer N
Enter fullscreen mode Exit fullscreen mode

Every layer improves the model's representation of the text.

For our example:

Explain photosynthesis in simple words
Enter fullscreen mode Exit fullscreen mode

The model gradually understands relationships involving:

photosynthesis
sunlight
plants
water
carbon dioxide
oxygen
Enter fullscreen mode Exit fullscreen mode

Purpose: Perform the main language reasoning/processing.

  1. Attention

Attention happens inside Transformer layers.

This is extremely important.

For example:

“The plant uses sunlight to make its food.”
Enter fullscreen mode Exit fullscreen mode

While processing the word “its”, attention can connect it strongly with:

plant
Enter fullscreen mode Exit fullscreen mode

rather than:

sunlight
Enter fullscreen mode Exit fullscreen mode

Simplified:

The plant uses sunlight to make its food
    ↑                         ↑
    └──────── Attention ──────┘
Enter fullscreen mode Exit fullscreen mode

For your photosynthesis prompt, attention may establish relationships like:

photosynthesis
     ↓
sunlight
     ↓
plant
     ↓
water
     ↓
carbon dioxide
Enter fullscreen mode Exit fullscreen mode

Purpose: Determine which other tokens are relevant to the token currently being processed.

So the correct relationship is:

Transformer Layer
      │
      ├── Attention
      ├── Feed-Forward Network
      ├── Normalization
      └── Residual Connections
Enter fullscreen mode Exit fullscreen mode

Attention is not outside the Transformer. It is one of its core mechanisms.

  1. Parameters / Weights Applied

Remember the billions of weights learned during training?

They are now used throughout the Transformer calculations.

For example, during training the model may have learned strong patterns involving:

photosynthesis ↔ plants
photosynthesis ↔ sunlight
photosynthesis ↔ carbon dioxide
photosynthesis ↔ oxygen
Enter fullscreen mode Exit fullscreen mode

Those learned relationships influence its processing.

Purpose: Apply previously learned knowledge/patterns.

But technically, your diagram simplifies this. Parameters aren't really a separate step after Attention.

A more accurate view is:

             Learned Parameters
                    ↓
Embeddings → Transformer Layers → Output
                    ↑
                 Attention
Enter fullscreen mode Exit fullscreen mode

The weights operate throughout the Transformer.

  1. Inference

Inference means:

Using the already-trained model to answer a new request.
Enter fullscreen mode Exit fullscreen mode

Everything happening after you submit:

Explain photosynthesis in simple words.
Enter fullscreen mode Exit fullscreen mode

is inference.

Therefore inference is actually the name of the whole runtime process, not just one small processing box.

Think:

Training
= Learning


Inference
= Using what was learned
Enter fullscreen mode Exit fullscreen mode

Example:

TRAINING:


Read millions of science documents



INFERENCE:
User asks:


"What is photosynthesis?"



Model generates an answer
Enter fullscreen mode Exit fullscreen mode
  1. Next-Token Prediction

Ultimately, an autoregressive LLM repeatedly predicts the next token.

Suppose it has started:

Photosynthesis is the process by which plants...

The model computes probabilities for possible next tokens:

use        45%
make       20%
convert    15%
absorb     10%
other      10%
Enter fullscreen mode Exit fullscreen mode

One token is selected.

Then:

Photosynthesis is the process by which plants use

Now the model predicts again:

sunlight      70%
water         10%
energy         8%
food           5%
Enter fullscreen mode Exit fullscreen mode

Select one:

sunlight

Then repeat.

Photosynthesis
      ↓
Photosynthesis is
      ↓
Photosynthesis is the
      ↓
Photosynthesis is the process
      ↓
Enter fullscreen mode Exit fullscreen mode

Purpose: Generate the response token by token.

  1. Temperature

Temperature changes how adventurous/random token selection is.

Imagine:

Next-token probabilities:

sunlight      70%
energy        15%
water          8%
light          5%
other          2%
Enter fullscreen mode Exit fullscreen mode

Low temperature

Model strongly favors high-probability choices.

sunlight

Usually gives:

more predictable
more focused
more consistent output
Enter fullscreen mode Exit fullscreen mode

Higher temperature

Lower-probability tokens have a greater chance.

This can produce:

more variety
more creativity
sometimes more mistakes

Enter fullscreen mode Exit fullscreen mode

Purpose: Control randomness during generation.

  1. Top-P

Top-P is another token-sampling control.

Suppose:

sunlight     50%
light        20%
energy       15%
water         8%
plants        4%
other         3%
Enter fullscreen mode Exit fullscreen mode

With:

Top-P = 0.90
Enter fullscreen mode Exit fullscreen mode

the sampler considers enough high-probability tokens to reach roughly 90% cumulative probability.

Conceptually:

sunlight 50%
+
light    20%
+
energy   15%
+
water     8%
-------------
93%
Enter fullscreen mode Exit fullscreen mode

Lower-probability alternatives outside that selected probability mass can be excluded.

Purpose: Control how broad the candidate token pool is.

  1. Generated Response

After repeating next-token generation many times:

Token 1
 ↓
Token 2
 ↓
Token 3
 ↓
Token 4
 ↓
Enter fullscreen mode Exit fullscreen mode

you finally see something like:

Photosynthesis is the process plants use to make food. They use sunlight, water, and carbon dioxide to produce glucose and release oxygen.

That is the generated response.

  1. Hallucination

Hallucination is not a processing step.

It is a possible failure mode of the generated response.
Enter fullscreen mode Exit fullscreen mode

For example, imagine the LLM says:

“Plants perform photosynthesis mainly using oxygen.”

That would be incorrect.

The output may sound confident even though the information is wrong.

So your diagram correctly puts Hallucination separately as:

Generated Answer
       │
       └──── Possible Risk
                  ↓
             Hallucination
Enter fullscreen mode Exit fullscreen mode

Purpose in the diagram: Show an important limitation/risk of LLM generation.

The actual flow to remember

For learning LLM processing, I recommend remembering it like this:

       USER PROMPT
                       ↓
                  Tokenization
                       ↓
                     Tokens
                       ↓
                  Embeddings
                       ↓
          ┌─────────────────────────┐
          │   TRANSFORMER LAYERS    │
          │                         │
          │   Attention             │
          │       +                 │
          │   Learned Weights       │
          │       +                 │
          │   Feed-Forward Network  │
          │       +                 │
          │   Other Components      │
          └────────────┬────────────┘
                       ↓
                Output Scores
                  / Logits
                       ↓
              Token Probabilities
                       ↓
            Temperature + Top-P
                       ↓
             Select Next Token
                       ↓
          Add Token to the Context
                       ↓
              Run Model Again
                       ↓
                  REPEAT
                       ↓
              Generated Answer
                       │
                       └── Possible Hallucination
Enter fullscreen mode Exit fullscreen mode
┌──────────────── CONTEXT WINDOW ────────────────┐
│                                                │
│ User Prompt + Previous Tokens + Generated Text │
│                                                │
└────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

One correction that makes your diagram much more technically accurate

Instead of remembering:

Inference
 ↓
Temperature
 ↓
Top-P
 ↓
Next-Token Prediction
Enter fullscreen mode Exit fullscreen mode

remember:

Transformer Processing
        ↓
Next-Token Scores / Logits
        ↓
Probabilities
        ↓
Temperature / Top-P
        ↓
Select Next Token
        ↓
Append Token
        ↓
Repeat Transformer Processing
Enter fullscreen mode Exit fullscreen mode

LLM Foundations Experiment Lab

Goal

Build one learning project that allows you to see what is happening inside an LLM, instead of treating the model like a black box.

User Prompt
    ↓
Tokenization
    ↓
Tokens / Token IDs
    ↓
Embeddings
    ↓
Transformer Layers
    ├── Self-Attention
    ├── Q / K / V
    ├── Feed-Forward Network
    ├── Residual Connections
    └── Normalization
    ↓
Hidden Representation
    ↓
Logits
    ↓
Probabilities
    ↓
Temperature
    ↓
Top-P
    ↓
Select Next Token
    ↓
Append Token to Context
    ↓
Run Transformer Again
    ↓
REPEAT
    ↓
Generated Response

Possible Failure
    ↓
Hallucination
Enter fullscreen mode Exit fullscreen mode

This follows the main inference workflow described in the blog.

Experiments to Build

  1. Experiment 1 — Prompt Input Inspector
  • Enter different prompts.

  • Print the raw prompt.

  • Print character count.

  • Print word count.

  • Later compare these numbers with token count.

  • Example:

     Explain photosynthesis in simple words.
    
  • Learn: Prompt, input text, inference request.

  1. Experiment 2 — Tokenization Explorer
  • Pass the same sentence through a real tokenizer.
  • Display each individual token.
  • Try:

     cat
     cats
     unbelievable
     photosynthesis
     AI
     Agentic AI
     MyHospitalNow
    
  • Compare normal words, rare words, punctuation and numbers.

  • Learn: Tokenization, subword tokens, tokenizer vocabulary.

Tokenizers convert text into model inputs and may split uncommon words into smaller subword units.

  1. Experiment 3 — Token → Token ID Explorer
  • Display:

     Token             Token ID
     --------------------------------
     Explain           1234
     photo             5678
     synthesis         9123
    
  • Decode every ID back to its token.

  • Verify:

     Text
       ↓
     Tokens
       ↓
     IDs
       ↓
     Tokens
       ↓
     Text
    
  • Learn: Vocabulary and machine-readable token identities.

  1. Experiment 4 — Token Count / Context Window Lab
  • Generate prompts of:

    • 10 tokens
    • 100 tokens
    • 500 tokens
    • 1,000+ tokens
  • Display:

     Context used: 812 tokens
     Context limit: ....
     Remaining capacity: ....
    
  • Add conversation history and observe token growth.

  • Learn: Context window, conversation history, prompt size.

The context window should be understood as the boundary around the tokens being processed rather than another neural transformation step.

  1. Experiment 5 — Embedding Vector Inspector
  • Take token IDs and retrieve their input embeddings.

  • Display a shortened version:

     "plant"
        ↓
     [0.21, -0.54, 0.88, ...]
    
     "sunlight"
        ↓
     [0.17, -0.49, 0.91, ...]
    
  • Print:

     Token
     Token ID
     Embedding dimensions
     First 10 vector values
    
  • Learn: Embeddings, vectors, dimensions.

  1. Experiment 6 — Embedding Similarity Experiment
  • Compare representations for words such as:

     doctor
     physician
    
     car
     vehicle
    
     king
     banana
    
  • Calculate cosine similarity where appropriate.

  • Important: distinguish the model's input token embeddings from sentence/document embeddings used in semantic search.

  • Learn: Vector representations and similarity.

  1. Experiment 7 — Position Information Experiment
  • Compare:

     Dog bites man.
     Man bites dog.
    
  • Tokens may be similar while their ordering changes the meaning.

  • Inspect how the model represents positions according to the architecture being used.

  • Learn: Why sequence position matters.

  1. Experiment 8 — Transformer Layer Inspector
  • Run:

     The animal didn't cross the street because it was tired.
    
  • Retrieve hidden states from each Transformer layer.

  • Display:

     Embedding representation
             ↓
     Layer 1 representation
             ↓
     Layer 2 representation
             ↓
     Layer 3 representation
             ↓
     ...
     Final representation
    
  • Compare how a token's vector changes between layers.

  • Learn: Transformer processing and contextual representations.

Transformer model outputs can expose hidden states from successive layers for inspection.

  1. Experiment 9 — Self-Attention Visualization
  • Use:

     The animal didn't cross the street because it was tired.
    
  • Select the token representing "it".

  • Visualize which previous tokens receive attention.

  • Produce an attention heatmap:

                animal street because it tired
     it          ████    ░       ░     █   ██
    
  • Learn: Attention and token relationships.

Attention weights can be returned by supported Transformer model outputs for analysis.

  1. Experiment 10 — Query, Key and Value Lab
* Build a tiny self-attention calculation manually.

* Show:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Current token
      ↓
  Query

  Other tokens
      ↓
  Keys

  Query × Keys
      ↓
  Attention scores
      ↓
  Softmax
      ↓
  Attention weights

  Attention weights × Values
      ↓
  Contextual representation
  ```
Enter fullscreen mode Exit fullscreen mode
* Use very small vectors such as 3–4 dimensions.

* **Learn:** Q, K, V and how self-attention is calculated.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 11 — Multi-Head Attention Explorer
* Inspect several attention heads.
* Compare whether different heads emphasize different token relationships.
* Display:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Layer 3
    ├── Head 1
    ├── Head 2
    ├── Head 3
    └── Head 4
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Multi-head attention.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 12 — Feed-Forward + Residual + Normalization Lab
* Build a miniature Transformer block.

* Show conceptually:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Input
    ↓
  Self-Attention
    ↓
  Residual Connection
    ↓
  Normalization
    ↓
  Feed-Forward Network
    ↓
  Residual Connection
    ↓
  Normalization
  ```
Enter fullscreen mode Exit fullscreen mode
* Print tensor shapes after each operation.

* **Learn:** Attention is only one component of a Transformer block.

The blog likewise places attention, feed-forward processing, normalization and residual connections inside Transformer processing.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 13 — Model Parameters Inspector
* Print:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Model parameters
  Trainable parameters
  Frozen parameters
  Embedding parameters
  Transformer-layer parameters
  Output-head parameters
  ```
Enter fullscreen mode Exit fullscreen mode
* Inspect several actual weight matrices.
* **Learn:** Parameters/weights and where learned information resides.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 14 — Random Model vs Pretrained Model
* Initialize the same architecture with random parameters.

* Give it:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Paris is the capital of
  ```
Enter fullscreen mode Exit fullscreen mode
* Compare against the pretrained version.

* Expected learning result:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Random parameters
       ↓
  meaningless predictions

  Learned parameters
       ↓
  useful language predictions
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Why pre-training matters.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 15 — Tiny Pre-training Experiment
* Do **not** try to train a real billion-parameter LLM.

* Create a tiny Transformer language model.

* Train it on a tiny text corpus.

* Record:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Epoch
  Loss
  Next-token accuracy/example predictions
  ```
Enter fullscreen mode Exit fullscreen mode
* Compare predictions before and after training.

* **Learn:** Pre-training, loss, optimization, parameter updates.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 16 — Fine-Tuning Experiment
* Start with a pretrained causal language model.

* Fine-tune it on a tiny domain dataset such as:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Hospital FAQs
  Travel FAQs
  DevOps questions
  Product documentation
  ```
Enter fullscreen mode Exit fullscreen mode
* Compare:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Base Model
      VS
  Fine-Tuned Model
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Fine-tuning and domain adaptation.
Enter fullscreen mode Exit fullscreen mode

Hugging Face provides an official causal-language-modeling workflow showing training/fine-tuning and subsequent generation.

  1. Experiment 17 — Instruction-Tuning Concept Lab
* Prepare examples such as:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Instruction:
  Explain Docker simply.

  Response:
  Docker packages an application...
  ```
Enter fullscreen mode Exit fullscreen mode
* Compare ordinary continuation data:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Docker is...
  ```
Enter fullscreen mode Exit fullscreen mode
  against instruction-response data:
Enter fullscreen mode Exit fullscreen mode
  ```text
  User: Explain Docker simply.
  Assistant: ...
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Difference between pre-training data and instruction-style training data.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 18 — Inference Inspector
* Put the model in inference/evaluation mode.

* Submit a new prompt.

* Ensure no training or parameter update occurs.

* Display:
Enter fullscreen mode Exit fullscreen mode
  ```text
  TRAINING
  Data → Loss → Backpropagation → Update weights

  INFERENCE
  Prompt → Existing weights → Prediction
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Training vs inference.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 19 — Logits Inspector
* Enter:
Enter fullscreen mode Exit fullscreen mode
  ```text
  The capital of France is
  ```
Enter fullscreen mode Exit fullscreen mode
* Run one forward pass.

* Inspect output logits for the final position.

* Display top candidate tokens:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Token       Logit
  -----------------
  Paris       ...
  Lyon        ...
  France      ...
  London      ...
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Logits / raw next-token scores.

Model outputs expose logits, making this stage directly inspectable.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 20 — Logits → Probability Experiment
* Apply softmax manually.
Enter fullscreen mode Exit fullscreen mode
  ```text
  Logits
     ↓
  Softmax
     ↓
  Probabilities

  Paris       72%
  Lyon         8%
  London       4%
  ...
  ```
Enter fullscreen mode Exit fullscreen mode
* Confirm probabilities sum approximately to 1.

* **Learn:** How output scores become candidate probabilities.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 21 — Greedy Next-Token Predictor
* Select the token with the highest probability.
Enter fullscreen mode Exit fullscreen mode
  ```text
  Prompt
    ↓
  Model
    ↓
  Logits
    ↓
  Softmax
    ↓
  Highest probability
    ↓
  Next token
  ```
Enter fullscreen mode Exit fullscreen mode
* Print the top 10 candidates each time.

* **Learn:** Next-token prediction.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 22 — Temperature Lab
* Generate the same prompt repeatedly:
Enter fullscreen mode Exit fullscreen mode
  ```text
  AI will change software development by
  ```
Enter fullscreen mode Exit fullscreen mode
* Test several temperature settings.

* Compare:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Lower temperature → generally more concentrated sampling
  Higher temperature → generally more diverse sampling
  ```
Enter fullscreen mode Exit fullscreen mode
* Save every output for comparison.

* **Learn:** Temperature and randomness.

Generation APIs expose temperature as one of the decoding controls.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 23 — Top-P Lab
* Take candidate probabilities:
Enter fullscreen mode Exit fullscreen mode
  ```text
  A   40%
  B   25%
  C   15%
  D   10%
  E    5%
  F    5%
  ```
Enter fullscreen mode Exit fullscreen mode
* Manually calculate the nucleus retained by different `top_p` settings.

* Then test generation with those settings.

* **Learn:** Nucleus sampling / Top-P.

Top-P keeps a sufficiently probable candidate set whose cumulative probability reaches the configured threshold.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 24 — Temperature vs Top-P Comparison
* Hold one constant while varying the other.
* Record:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Prompt
  Temperature
  Top-P
  Output
  Repetition
  Diversity
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** These parameters influence sampling in different ways.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 25 — Manual Autoregressive Generation Loop
* This is one of the **most important experiments**.

  Implement:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Prompt
    ↓
  Tokenize
    ↓
  Transformer
    ↓
  Logits for last position
    ↓
  Probabilities
    ↓
  Sample next token
    ↓
  Append token
    ↓
  Feed enlarged sequence back into model
    ↓
  Predict again
    ↓
  REPEAT
  ```
Enter fullscreen mode Exit fullscreen mode
* Do **not** initially use the high-level `generate()` method.

* Write the loop yourself.

* Print:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Step 1
  Context: "AI is"
  Predicted: "changing"

  Step 2
  Context: "AI is changing"
  Predicted: "the"

  Step 3
  Context: "AI is changing the"
  Predicted: "world"
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Autoregressive generation.

The blog identifies this repeating append-and-run-again loop as the central idea behind autoregressive text generation.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 26 — Context Growth Visualizer
* Extend Experiment 25.

* Display:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Step 0:
  [The] [cat]

  Step 1:
  [The] [cat] [is]

  Step 2:
  [The] [cat] [is] [sitting]

  Step 3:
  [The] [cat] [is] [sitting] [on]

  Step 4:
  [The] [cat] [is] [sitting] [on] [the]
  ```
Enter fullscreen mode Exit fullscreen mode
* Display token count after every generation step.

* **Learn:** Generated tokens become part of subsequent context.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 27 — Prompt Sensitivity Experiment
* Compare:
Enter fullscreen mode Exit fullscreen mode
  ```text
  Explain Transformer.

  Explain Transformer simply.

  Explain Transformer to a 10-year-old.

  Explain Transformer to a senior AI engineer.
  ```
Enter fullscreen mode Exit fullscreen mode
* Compare generated tokens and answers.

* **Learn:** Context changes probability distributions and output.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 28 — Previous Conversation Context Experiment
* Try:
Enter fullscreen mode Exit fullscreen mode
  ```text
  User: My favorite language is Python.
  Assistant: Understood.

  User: Which language did I mention?
  ```
Enter fullscreen mode Exit fullscreen mode
* Then remove the previous context.

* Compare.

* **Learn:** Context-window information versus model parameters.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 29 — Hallucination Lab
* Ask questions about deliberately fictional entities or unavailable facts.
* Record whether the model:

  * admits uncertainty,
  * gives unsupported information,
  * invents details.
* Compare different prompts.
* **Learn:** Hallucination as a generation failure rather than a Transformer processing stage.

That distinction matches the blog: hallucination appears after generation as a possible risk, not as part of the forward-processing pipeline.
Enter fullscreen mode Exit fullscreen mode
  1. Experiment 30 — Full LLM Workflow Visualizer
* Combine all previous experiments into one application.

  User enters:
Enter fullscreen mode Exit fullscreen mode
  ```text
  The cat is sitting on the
  ```
Enter fullscreen mode Exit fullscreen mode
  Application displays:
Enter fullscreen mode Exit fullscreen mode
  ```text
  STEP 1 — INPUT
  The cat is sitting on the

        ↓

  STEP 2 — TOKENS
  ["The", " cat", " is", " sitting", " on", " the"]

        ↓

  STEP 3 — TOKEN IDs
  [....]

        ↓

  STEP 4 — EMBEDDINGS
  Token × embedding_dimension matrix

        ↓

  STEP 5 — TRANSFORMER
  Layer 1
  Layer 2
  ...
  Layer N

        ↓

  STEP 6 — ATTENTION
  Attention heatmap

        ↓

  STEP 7 — FINAL HIDDEN STATE

        ↓

  STEP 8 — LOGITS

        ↓

  STEP 9 — PROBABILITIES
  mat       42%
  floor     18%
  chair      8%
  ...

        ↓

  STEP 10 — SAMPLING
  Temperature = ...
  Top-P = ...

        ↓

  STEP 11 — SELECT
  "mat"

        ↓

  STEP 12 — NEW CONTEXT
  The cat is sitting on the mat

        ↓

  REPEAT
  ```
Enter fullscreen mode Exit fullscreen mode
* **Learn:** Complete LLM prompt-processing workflow.
Enter fullscreen mode Exit fullscreen mode

Recommended Final Mini-Projects

After completing the experiments, convert them into four small projects:

Project A — Token & Embedding Explorer

Covers:

Prompt
Tokenization
Tokens
Token IDs
Context length
Embeddings
Vector similarity
Enter fullscreen mode Exit fullscreen mode

Project B — Transformer Inside Viewer

Covers:

Transformer layers
Hidden states
Self-attention
Q/K/V
Multi-head attention
FFN
Residual connections
Normalization
Parameters
Enter fullscreen mode Exit fullscreen mode

Project C — Next Token Prediction Playground

Covers:

Logits
Softmax
Probabilities
Greedy decoding
Temperature
Top-P
Next-token prediction
Context growth
Autoregressive repetition
Enter fullscreen mode Exit fullscreen mode

Project D — Tiny Training & Hallucination Lab

Covers:

Parameters
Pre-training
Fine-tuning
Instruction tuning
Training vs inference
Prompt sensitivity
Hallucination
Enter fullscreen mode Exit fullscreen mode

Final Project — LLM Foundations Experiment Lab

Combine A + B + C + D into one application:

                    LLM FOUNDATIONS LAB
                            │
          ┌─────────────────┼─────────────────┐
          │                 │                 │
    INPUT EXPLORER    MODEL INSPECTOR   GENERATION LAB
          │                 │                 │
      Prompt            Embeddings          Logits
      Tokens            Layers              Probabilities
      IDs               Attention           Temperature
      Context           Hidden states        Top-P
                        Parameters           Next token
                                             │
                                             ↓
                                      Generation Loop

                            │
                            ↓

                     TRAINING LAB
                            │
                ┌───────────┼───────────┐
                │           │           │
           Pre-training  Fine-tuning Instruction
                                      tuning

                            │
                            ↓
                   HALLUCINATION LAB
Enter fullscreen mode Exit fullscreen mode

Recommended Technology

Python
   +
Jupyter Notebook
   +
PyTorch
   +
Hugging Face Transformers
   +
Matplotlib
Enter fullscreen mode Exit fullscreen mode

Hugging Face provides tokenizer, model-output and generation interfaces suitable for inspecting tokenization, logits, hidden states, attentions and generation behavior.

Most Important Experiments

If you don't want to implement all 30 initially, start with:

Experiment 2  → Tokenization
Experiment 3  → Token IDs
Experiment 5  → Embeddings
Experiment 8  → Transformer hidden states
Experiment 9  → Attention
Experiment 13 → Parameters
Experiment 19 → Logits
Experiment 20 → Probabilities
Experiment 22 → Temperature
Experiment 23 → Top-P
Experiment 25 → Manual next-token loop
Experiment 26 → Context growth
Experiment 29 → Hallucination
Experiment 30 → Complete workflow
Enter fullscreen mode Exit fullscreen mode

These give you the strongest practical foundation before moving to RAG → AI Agents → Agentic AI.

Project 0 — LLM Inside-Out Lab

Project goal

Build a small LLM processing simulator + debugger where a user enters a prompt and can watch the complete journey:

Prompt
  ↓
Tokenization
  ↓
Token IDs
  ↓
Embeddings
  ↓
Positional Information
  ↓
Transformer Layers
  ↓
Self-Attention
  ↓
Q / K / V
  ↓
Hidden Representations
  ↓
Logits
  ↓
Softmax Probabilities
  ↓
Temperature + Top-P
  ↓
Next Token
  ↓
Append Token to Context
  ↓
Repeat
  ↓
Final Response
Enter fullscreen mode Exit fullscreen mode

Then the same project also includes:

Parameters
Pre-training
Fine-tuning
Instruction tuning
Context Window
Inference
Hallucination
What the application looks like

User opens:

LLM INSIDE-OUT LAB

and enters:

The capital of France is

Press:

Analyze Prompt

Your application shows the entire processing pipeline.

Module 1 — Prompt Inspector

Input:

The capital of France is
Enter fullscreen mode Exit fullscreen mode

Show:

Characters: 24
Words: 5
Tokens: 5
Context Used: 5 / 1024
Enter fullscreen mode Exit fullscreen mode

Concepts covered:

Prompt
Input
Context window
Inference
Enter fullscreen mode Exit fullscreen mode

Module 2 — Tokenization Explorer

Also allow:

Encode → Decode

Text
 ↓
Tokens
 ↓
Token IDs
 ↓
Decode
 ↓
Original Text
Enter fullscreen mode Exit fullscreen mode

Concepts covered:

Tokens
Tokenization
Vocabulary
Token IDs
Enter fullscreen mode Exit fullscreen mode

Module 3 — Embedding Inspector

Click any token:

France
Enter fullscreen mode Exit fullscreen mode

Show:

Token ID: 4881
Enter fullscreen mode Exit fullscreen mode

Embedding:

[
  0.023,
 -0.182,
  0.517,
 ...
]
Enter fullscreen mode Exit fullscreen mode

Also show:

Embedding Dimension: 768

Add comparison:

doctor    vs physician
car       vs vehicle
doctor    vs banana
Enter fullscreen mode Exit fullscreen mode

Calculate cosine similarity.

Concepts:

Embeddings
Vectors
Dimensions
Semantic representation
Enter fullscreen mode Exit fullscreen mode

Module 4 — Position Explorer

Compare:

Dog bites man
Enter fullscreen mode Exit fullscreen mode

with:

Man bites dog
Enter fullscreen mode Exit fullscreen mode

Show:

Same/similar words
       +
Different positions
       ↓
Different contextual meaning
Enter fullscreen mode Exit fullscreen mode

Concept:

Positional information
Module 5 — Transformer Visualizer

Show:

Embeddings
    ↓
Transformer Layer 1
    ↓
Transformer Layer 2
    ↓
Transformer Layer 3
    ↓
...
    ↓
Final Layer
Enter fullscreen mode Exit fullscreen mode

User can click:

Layer 1
Layer 2
Layer 3
...
Enter fullscreen mode Exit fullscreen mode

and inspect hidden representations.

Concepts:

Transformer
Transformer layers
Hidden states
Contextual representations
Enter fullscreen mode Exit fullscreen mode

Module 6 — Self-Attention Viewer

Use a good example:

The animal didn't cross the street because it was tired.
Enter fullscreen mode Exit fullscreen mode

Click:

it



Enter fullscreen mode Exit fullscreen mode

Display an attention heatmap showing how strongly "it" relates to:

The
animal       █████████
didn't
cross
street
because
it
was
tired        ███
Enter fullscreen mode Exit fullscreen mode

Concepts:

Attention
Self-attention
Context
Token relationships
Enter fullscreen mode Exit fullscreen mode

Module 7 — Q/K/V Playground

Make this interactive.

Token
  ↓
Query

Enter fullscreen mode Exit fullscreen mode

Other tokens:

Tokens
  ↓
Keys
  ↓
Values
Enter fullscreen mode Exit fullscreen mode

Then:


Query × Keys
     ↓
Attention Scores
     ↓
Softmax
     ↓
Attention Weights
     ↓
Weights × Values
     ↓
Contextual Representation
Enter fullscreen mode Exit fullscreen mode

Show the actual small matrices.

Concepts:


Query
Key
Value
Attention score
Attention weights
Enter fullscreen mode Exit fullscreen mode

Module 8 — Transformer Block Inspector

Show one Transformer block:

Input Representation
        ↓
Multi-Head Attention
        ↓
Residual Connection
        ↓
Normalization
        ↓
Feed Forward Network
        ↓
Residual Connection
        ↓
Normalization
        ↓
Output Representation
Enter fullscreen mode Exit fullscreen mode

Concepts:

Multi-head attention
Feed-forward network
Residual connection
Normalization
Enter fullscreen mode Exit fullscreen mode

Module 9 — Parameters Inspector

Display:

Model: GPT-2 Small


Total Parameters: ~124M



Embedding Parameters
Transformer Parameters
Attention Parameters
Feed Forward Parameters
Output Head Parameters
Enter fullscreen mode Exit fullscreen mode

Allow the user to inspect some weight shapes:

Wq = [...]
Wk = [...]
Wv = [...]

Enter fullscreen mode Exit fullscreen mode

Concept:

Parameters
Weights
Learned information
Enter fullscreen mode Exit fullscreen mode

Module 10 — Next-Token Predictor

The capital of France is
Enter fullscreen mode Exit fullscreen mode

Flow:

Transformer Output
       ↓
Logits
       ↓
Softmax
       ↓
Probabilities
       ↓
Next Token

Enter fullscreen mode Exit fullscreen mode

Concepts:

Logits
Softmax
Probabilities
Next-token prediction
Enter fullscreen mode Exit fullscreen mode

Module 11 — Temperature Playground

Provide a slider:

Temperature


0.1 ─────────────── 2.0
Enter fullscreen mode Exit fullscreen mode

Prompt:

The future of AI is
Enter fullscreen mode Exit fullscreen mode

Generate repeatedly.

Compare:

Temperature = 0.2
More predictable


Temperature = 0.8
Balanced


Temperature = 1.5
More diverse/random
Enter fullscreen mode Exit fullscreen mode

Concept:

Temperature
Enter fullscreen mode Exit fullscreen mode

Module 12 — Top-P Playground

Provide another slider:

Top-P


0.1 ─────────────── 1.0
Enter fullscreen mode Exit fullscreen mode

Visualize:

Token A → 40%
Token B → 25%
Token C → 15%
Token D → 10%
Token E → 5%
Enter fullscreen mode Exit fullscreen mode

If:

Top-P = 0.80
Enter fullscreen mode Exit fullscreen mode

show which candidates remain eligible.

Concept:

Top-P / nucleus sampling
Enter fullscreen mode Exit fullscreen mode

Module 13 — Autoregressive Generation Visualizer

This should be the main feature of the project.

Prompt:

AI is
Enter fullscreen mode Exit fullscreen mode

Your application shows:

Generation Step 1


Context:
[AI] [is]


Prediction:
changing


        ↓


Generation Step 2


Context:
[AI] [is] [changing]


Prediction:
the


        ↓


Generation Step 3


Context:
[AI] [is] [changing] [the]


Prediction:
world
Enter fullscreen mode Exit fullscreen mode

Then:

AI is changing the world
Enter fullscreen mode Exit fullscreen mode

This visually explains:

Predict
 ↓
Append
 ↓
Predict again
 ↓
Append
 ↓
Repeat

Enter fullscreen mode Exit fullscreen mode

Concepts:

Autoregressive generation
Next-token prediction
Context growth
Repeated inference
Enter fullscreen mode Exit fullscreen mode

Module 14 — Context Window Monitor

While generation occurs show:

Context Window


████████████░░░░░░


621 / 1024 tokens
Enter fullscreen mode Exit fullscreen mode

Add long text until the limit is reached.

Concepts:

Context window
Context length
Prompt + generated tokens
Enter fullscreen mode Exit fullscreen mode

Module 15 — Training Playground

Do not train a large LLM.

Include a Tiny Transformer inside the same project.

Start with random weights.

Prompt:

The sky is
Enter fullscreen mode Exit fullscreen mode

Before training:

Output:
chair banana running...
Enter fullscreen mode Exit fullscreen mode

Train on a tiny dataset.

After training:

The sky is blue
Enter fullscreen mode Exit fullscreen mode

Show:

Epoch 1   Loss 4.91
Epoch 2   Loss 3.82
Epoch 3   Loss 2.71
...
Epoch 20  Loss 0.81
Enter fullscreen mode Exit fullscreen mode

Concepts:

Training
Parameters
Loss
Backpropagation
Parameter updates
Pre-training
Enter fullscreen mode Exit fullscreen mode

Module 16 — Fine-Tuning Lab

Take a small pretrained model and fine-tune it on one domain.

For example:

DevOps FAQ dataset

Before fine-tuning:

Q: What is Jenkins?
Generic response

After fine-tuning:

Q: What is Jenkins?
Better domain-specific response

Concept:

Fine-tuning
Module 17 — Instruction-Tuning Demo

Training dataset:

Instruction:
Explain Docker to a beginner.


Response:
Docker is a tool...
Enter fullscreen mode Exit fullscreen mode

Compare:

Normal language modeling

The Docker platform is...

versus:

Instruction tuned

User asks → Model follows instruction

Concept:

Instruction tuning
Module 18 — Training vs Inference

Create two clearly separated modes.

TRAIN MODE

Dataset
 ↓
Forward Pass
 ↓
Prediction
 ↓
Loss
 ↓
Backpropagation
 ↓
Update Parameters

Enter fullscreen mode Exit fullscreen mode

versus:

INFERENCE MODE

Prompt
 ↓
Existing Parameters
 ↓
Forward Pass
 ↓
Next Token
 ↓
Response
Enter fullscreen mode Exit fullscreen mode

This removes a major beginner confusion.

Module 19 — Hallucination Lab

Ask:

Who invented ProfesNow in 1895?

or intentionally use fictional facts.

Show:

Model Output
      ↓
Verify Against Known Data
      ↓
Supported?
   /       \
 Yes       No
           ↓
Possible Hallucination
Enter fullscreen mode Exit fullscreen mode

Allow comparison between:

Normal prompt

vs

"Say you don't know if information is unavailable."

Concept:

Hallucination
Model limitations
Enter fullscreen mode Exit fullscreen mode

Final Combined Dashboard

┌─────────────────────────────────────────────────────┐
│                LLM INSIDE-OUT LAB                   │
├─────────────────────────────────────────────────────┤
│ Enter Prompt                                        │
│                                                     │
│ "The capital of France is..."                       │
│                                                     │
│ [Analyze] [Generate]                                │
├─────────────────────────────────────────────────────┤
│                                                     │
│ 1. Tokens                                           │
│ 2. Token IDs                                        │
│ 3. Embeddings                                       │
│ 4. Position                                         │
│ 5. Transformer                                      │
│ 6. Attention                                        │
│ 7. Q/K/V                                            │
│ 8. Hidden States                                    │
│ 9. Parameters                                       │
│ 10. Logits                                          │
│ 11. Probabilities                                   │
│ 12. Temperature                                     │
│ 13. Top-P                                           │
│ 14. Next Token                                      │
│ 15. Context Growth                                  │
│                                                     │
├─────────────────────────────────────────────────────┤
│ TRAINING LAB                                        │
│                                                     │
│ Pre-training | Fine-tuning | Instruction Tuning    │
├─────────────────────────────────────────────────────┤
│ HALLUCINATION LAB                                   │
└─────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

That repeating loop is the most important idea to understand about how an autoregressive LLM generates text.
chatgpt
chatgpt

prompt
for this what iput in frontend and output in after submit form in shory summary

Top comments (0)