Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 1 of 12
From Prompt to Response
When you send a message to ChatGPT and a response appears, a specific multi-step process is happening. Understanding this pipeline demystifies how LLMs work.
The Complete Pipeline
Here is everything that happens when you send a message to an LLM:
- 1Your message (and the conversation history) is combined into a single prompt
- 2The text is tokenized — split into tokens and converted to token IDs
- 3Token IDs are converted to embedding vectors
- 4Embeddings pass through all transformer layers (attention + feed-forward)
- 5The final layer outputs a probability distribution over the vocabulary
- 6One token is selected from this distribution
- 7The selected token is added to the sequence
- 8Steps 4–7 repeat until a stop token is generated or the maximum length is reached
- 9All generated tokens are converted back to text and returned
This entire process happens in 1–30 seconds, with most of the time spent on the transformer computation (step 4).
Diagram
Loading diagram…
From Prompt to Response Diagram
Text Generation Pipeline
Text Generation Pipeline
# LLM text generation pipeline (conceptual)
def llm_generation_pipeline(prompt, max_tokens=20):
"""
Illustrates the full pipeline from prompt to generated text.
"""
print(f"Input prompt: '{prompt}'")
print()
# Step 1: Tokenize
tokens = prompt.split() # Simplified tokenization
print(f"Step 1 - Tokens: {tokens}")
# Step 2: Process through transformer (simplified)
print("Step 2 - Transformer processes all tokens via attention layers...")
# Step 3: Generate tokens one by one
generated_tokens = []
print("Step 3 - Generating response tokens:")
for step in range(1, max_tokens + 1):
# In a real LLM, the model outputs probabilities over the full vocabulary
# Here we simulate the selection process
context = tokens + generated_tokens
# Simulated next token selection
simulated_next = f"[token_{step}]"
if step <= 3: # Show first 3 steps
print(f" Step {step}: Context has {len(context)} tokens → generates '{simulated_next}'")
generated_tokens.append(simulated_next)
# Stop at end-of-sequence token (simplified)
if step == 3:
print(" ... (continues until stop token or max length)")
break
print()
print("Step 4 - Convert tokens back to text")
print("Step 5 - Return response to user")
print()
print("The key insight: the model generates ONE TOKEN AT A TIME.")
print(f"For a 100-token response, this loop runs 100 times.")
llm_generation_pipeline("What is the capital of France?")Finished this lesson?
0 of 12 in Module 6
PreviousStart of module
Next lessonInput Tokens