Tracing a Prompt Through a Real LLM, End to End

LLMs

Chapter 11 · Capstone: Tracing a Prompt Through a Real LLM, End to End

llm1-1 opened with a deliberately incomplete preview of "The capital of France is", promising the full trace would wait until every piece was built. Every piece is now built. Here is the full trace.

Step 1 — Tokenization (llm1-2)

prompt = "The capital of France is"
tokens = ["The", " capital", " of", " France", " is"]   # BPE subword tokens
token_ids = [464, 3139, 286, 4881, 318]                    # looked up in the vocabulary

llm1-2's own Byte-Pair Encoding breaks the raw prompt string into subword tokens, each mapped to an integer ID in the model's fixed vocabulary — no out-of-vocabulary risk, per that chapter's own byte-level fallback guarantee.

Step 2 — Embedding & Positional Encoding (llm1-3)

x = embedding_table[token_ids]              # each token ID -> its learned vector
x = x + positional_encoding[0:5]              # position 0..4 added, per token

Each token ID is looked up in the jointly-trained embedding table, then combined with its own positional encoding — the exact mechanism that gives the model any sense of order at all, resolving nlp1-8's own deferred gap. This produces the actual vectors that enter the first Transformer block.

Step 3 — Multi-Head Self-Attention Across Stacked Layers (llm1-4 / llm1-5 / llm1-6)

for layer in range(num_layers):                     # dozens of stacked blocks
    x = x + multi_head_attention(layer_norm(x), causal_mask=True)
    x = x + feed_forward(layer_norm(x))

At every layer, each token's own vector is reshaped by causally-masked (llm1-6) multi-head attention (llm1-5), built from the Query/Key/Value mechanism (llm1-4). The token at position 4 (" is") can attend to itself and every earlier token — never anything later, since nothing later exists yet in this generation step. By the final layer, its own vector has been shaped by learned relevance to "The", "capital", "of", and "France", repeatedly, across every stacked block.

Step 4 — Next-Token Prediction (llm1-7 / llm1-8)

logits = final_layer_output[-1] @ output_projection    # project to vocabulary size
probabilities = softmax(logits)
# probabilities["Paris"] ≈ highest, if pretraining (llm1-7) at sufficient scale (llm1-8) went well

The final vector at the last position is projected into a probability distribution over the entire vocabulary — llm1-7's own causal language modeling objective, at generation time. If the model was pretrained on enough data, at enough scale, following llm1-8's own measured power-law relationship, "Paris" receives the highest probability of any token in the vocabulary.

Step 5 — The Autoregressive Loop (llm1-6)

The predicted token is appended to the sequence, and the entire process — embed, attend across every layer, predict — repeats to generate the token after that, one token at a time, each one causally valid given only what has genuinely been generated so far.

An Honest Aside: Real Deployments Aren't Raw Completions

llm1-9's own fine-tuning, in practice
A real assistant deployment doesn't send the raw prompt shown above — it wraps it in a conversation format (system/user/assistant turn markers) the model was specifically trained to follow via llm1-9's own SFT and RLHF pipeline. The trace above shows the base mechanism; real assistant behavior is this exact mechanism, operating on text that's been formatted according to what fine-tuning taught the model to expect.

Chapter Attribution Table

ChapterWhat it contributed to this trace
llm1-1The scope-setting preview this capstone now completes
llm1-2BPE tokenization — turning the raw prompt into token IDs
llm1-3Token embeddings and positional encoding — the input vectors
llm1-4Query/Key/Value and scaled dot-product attention — the core computation
llm1-5Multi-head attention, feed-forward layers, residuals, and layer norm — one full Transformer block
llm1-6Causal masking and the decoder-only architecture — why generation stays coherent
llm1-7The next-token prediction objective — why "Paris" becomes the most probable output
llm1-8Why scale makes that prediction reliable — the measured power law behind pretraining quality
llm1-9Why a real deployment behaves like a helpful assistant rather than a raw text-completion engine
llm1-10Why this trace has a maximum possible length, and why the model might not "know" the answer at all

Closing the Loop on nlp1-10's Three Claims

Delivered, not just asserted
  • Scale — real numbers (llm1-7) and a real, measured power law (llm1-8), not a vague intuition.
  • Self-supervised pretraining taken further — llm1-7's own next-token objective, extending nlp1-5's/nlp1-9's own context-prediction signal across an entire corpus.
  • One flexible architecture vs. many task-specific pipelines — llm1-6's own decoder-only reframing, proven with a worked example replacing nlp1-6's and nlp1-7's own genuinely separate pipelines.

This is also the LLM analogue of what imgai1 did for image models, exactly as llm1-1 set out — a full mechanism, traced end to end, sitting underneath prompt1's and claude-adv1's own practical territory rather than replacing it.

Hands-On Exercises

Exercise 1

Using this chapter's own code, trace each step of the pipeline back to the specific chapter it came from, and explain why the trace stops being a "preview" (as in llm1-1) and becomes a genuine mechanical account here.

📄 View solution
Exercise 2

Explain why the token at position 4 ("is") can attend to every earlier token but nothing later, connecting your answer to llm1-6's own causal masking mechanism, and explain why this restriction is essential for the autoregressive generation loop in Step 5 to make sense.

📄 View solution
Exercise 3

For each of nlp1-10's own three named differences between its own pipelines and an LLM, explain specifically what in this capstone's own trace demonstrates that claim concretely, rather than merely restating it abstractly.

📄 View solution

Scope Note — What This Course Deliberately Doesn't Cover

Honestly out of scope
  • No from-scratch training of a production-scale model — this course explains the mechanism, not a runnable training pipeline at real scale.
  • No deployment or serving infrastructure — that's a separate, substantial engineering discipline of its own.
  • No coverage of any specific commercial API's own features or pricing — that territory belongs to claude-adv1, exactly as llm1-1 scoped from the start.

Chapter 11 Quick Reference — Course Summary

  • The full trace: tokenize (2) → embed + position (3) → multi-head causal attention across stacked layers (4/5/6) → predict next token (7, reliable per 8) → repeat (6) → shaped into an assistant (9), within a hard length limit (10)
  • What was diagnosed and fixed across the course: nlp1-9's OOV gap (2), nlp1-8's missing order (3) and undelivered full Transformer (5), nlp1-6/nlp1-7's separate pipelines (6), nlp1-10's three asserted-but-unproven claims (7/8/6)
  • This completes the LLMs course (11 chapters) — the fifth of six courses in the Data Science & ML subject
  • Remaining in the subject: Data Science & ML Projects (dsproj1/dsproj2)