You type "Explain quantum computing like I'm 5" into ChatGPT. Two seconds later, words start streaming onto your screen.
In those two seconds, your message traveled through a global CDN, hit a load balancer, got routed to an authentication service, was tokenized, passed through a model with trillions of parameters running on a cluster of GPUs, generated tokens one at a time, and streamed back to your browser over a WebSocket connection.
And that's the simple version. The full stack is one of the most complex software architectures ever deployed. Let's trace every step.
The Architecture Overview
Your Browser
β
βΌCloudFlare CDN (DDoS protection, caching, edge routing)
β
βΌAPI Gateway (rate limiting, auth, routing)
β
βΌConversation Service (context management, history)
β
βΌSafety/Moderation Layer (content filtering, PII detection)
β
βΌPrompt Orchestration (system prompts, tool configs, model selection)
β
βΌInference Router (load balancing across GPU clusters)
β
βΌInference Cluster (GPU nodes running the actual model)
β
βΌToken Streaming (SSE/WebSocket back to browser)
β
βΌYour Screen
Let's walk through each layer.
Layer 1: The Edge (CloudFlare + CDN)
Before your message even reaches OpenAI's servers, it hits CloudFlare β a massive CDN and security layer.
What happens here:
DDoS protection β millions of requests/second during peak, many of them malicious
Geographic routing β connects you to the nearest edge server
TLS termination β your HTTPS connection is decrypted here
Rate limiting (first pass) β obvious abuse gets blocked immediately
WebSocket upgrade β your connection is upgraded from HTTP to WebSocket for streaming
OpenAI handles roughly 100 million weekly active users. Without the CDN layer, their servers would melt.
π§
Layer 2: API Gateway
The gateway handles authentication and routing:
Request arrives:
{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing like I'm 5"}],
"stream": true
}
What happens here:
Authentication β validate API key or session token
Rate limiting β per-user token limits (TPM, RPM)
Usage tracking β count tokens for billing
Model routing β send GPT-4o requests to GPT-4o cluster, GPT-3.5 to a different one
Feature flags β enable/disable features per user tier (Plus, Team, Enterprise)
For ChatGPT (the consumer product), this also handles:
Session management
Conversation persistence (your chat history)
Plugin/tool activation
Memory features (if enabled)
π
Layer 3: Safety and Moderation
BEFORE the model sees your prompt, it goes through content moderation:
python
Simplified moderation pipeline
def moderate_input(messages):
# Step 1: Fast classifier (lightweight model)
risk_score = safety_classifier.predict(messages)if risk_score > 0.95:
return BLOCK # Obviously harmful, don't even process# Step 2: PII detection
pii_found = detect_pii(messages)
if pii_found:
messages = redact_pii(messages) # Strip sensitive info# Step 3: Injection detection
injection_score = detect_prompt_injection(messages)
if injection_score > 0.8:
flag_for_review(messages)
# Still process but add safety system promptreturn ALLOWOpenAI uses a combination of:
Lightweight classifiers for fast filtering (sub-millisecond)
The model itself as a secondary safety check (the system prompt includes safety instructions)
Output filtering β even after generation, responses are checked before being sent to the user
This is why ChatGPT sometimes refuses to answer mid-response. The output filter caught something.
Layer 4: Prompt Orchestration
Your simple message gets wrapped in a much larger prompt:
[SYSTEM]: You are ChatGPT, a large language model trained by OpenAI.
Knowledge cutoff: April 2024. Current date: [today].
You have access to the following tools: [browser, code_interpreter, dalle]
When the user asks for images, use DALL-E.
When code execution is needed, use the code interpreter.
[SAFETY INSTRUCTIONS: Do not generate harmful content...]
[MEMORY: User prefers concise answers, works in software engineering...]
[CONVERSATION HISTORY]:
User (3 messages ago): "What's quantum computing?"Assistant: "Quantum computing uses quantum mechanical phenomena..."
User (1 message ago): "That's too complex"[CURRENT MESSAGE]:
User: "Explain quantum computing like I'm 5"
What started as a 7-word message is now potentially thousands of tokens, including:
System prompt with personality, safety rules, and tool configs
User memories and preferences
Full conversation history (up to context limit)
Tool definitions in function-calling format
This is why ChatGPT remembers your earlier messages β it's not actually "remembering." It's re-reading the entire conversation every time.
Layer 5: Inference Router
The orchestrated prompt needs to get to a GPU. But which one?
OpenAI operates thousands of GPU nodes across multiple data centers. The inference router decides where to send each request:
Routing decisions:
βββ Which data center? (closest with capacity)
βββ Which GPU cluster? (model-specific: GPT-4o cluster vs GPT-3.5 cluster)
βββ Which node in the cluster? (least loaded)
βββ Priority level? (Plus users > free tier)
βββ Batch or individual? (can this request be batched with others?)
Key optimization: Request batching. Instead of processing one request at a time, the inference server batches multiple requests together:
Without batching:
Request 1 β GPU β Response 1 (utilization: 30%)
Request 2 β GPU β Response 2 (utilization: 30%)
With batching (continuous):
[Request 1 + Request 2 + Request 3] β GPU β [Response 1, 2, 3]
(utilization: 90%)
This is continuous batching (from Episode 37). The GPU processes multiple requests simultaneously, dynamically adding new requests as others finish. It's what makes serving 100M users economically feasible.
π¬
Layer 6: The Inference Engine
This is where the magic actually happens. A GPU cluster running vLLM, TensorRT-LLM, or OpenAI's custom inference engine processes the prompt.
The Prefill Phase (Processing Your Input)
Input: ~2000 tokens (your message + system prompt + history)
All 2000 tokens processed in PARALLEL on the GPU
β One forward pass through all transformer layers
β KV cache populated for every token
β Ready to generate
Time: ~200ms for GPT-4o
This is fast because all input tokens are processed at once. The GPU loves parallel work.
The Decode Phase (Generating the Response)
Token 1: "Imagine" β 200ms + context reading
Token 2: "you" β 20ms (faster now, KV cache warm)
Token 3: "have" β 20ms
Token 4: "a" β 20ms...
Token N: "[done]" β 20msTotal: First token in ~200ms, then ~50 tokens/second
Each token is generated sequentially. The model:
Reads all previous tokens (using KV cache β no recomputation)
Predicts the next token probability distribution
Samples from the distribution (using temperature, top-p β Episode 6)
Appends the new token
Repeats
The KV cache (Episode 12) is critical here. Without it, the model would re-process all previous tokens for each new token. With it, only the new token requires a full computation pass.
GPU Memory Layout During Inference
GPU Memory (80GB A100):
ββββββββββββββββββββββββββββββββββββββ
β Model Weights ~40GB β (loaded once, shared across requests)
ββββββββββββββββββββββββββββββββββββββ€
β KV Cache (Batch) ~30GB β (grows with context length Γ batch size)
ββββββββββββββββββββββββββββββββββββββ€
β Activations ~5GB β (temporary, per forward pass)
ββββββββββββββββββββββββββββββββββββββ€
β Overhead ~5GB β (CUDA, framework, buffers)
ββββββββββββββββββββββββββββββββββββββ
For large models like GPT-4 (rumored ~1.8T parameters with MoE), the model is split across multiple GPUs using tensor parallelism and pipeline parallelism.
π‘οΈ
Layer 7: Token Streaming
As each token is generated, it's streamed back to your browser immediately. This is Server-Sent Events (SSE):
HTTP Response (streaming):
data: {"choices":[{"delta":{"content":"Imagine"}}]}
data: {"choices":[{"delta":{"content":" you"}}]}
data: {"choices":[{"delta":{"content":" have"}}]}
data: {"choices":[{"delta":{"content":" a"}}]}
data: {"choices":[{"delta":{"content":" box"}}]}
...
data: [DONE]
The browser receives each chunk and appends it to the UI. This is why you see ChatGPT "typing" β it genuinely doesn't know the next word until it generates it.
Why streaming matters:
Perceived latency drops dramatically. Time-to-first-token is ~200ms. Time-to-full-response might be 10 seconds. Without streaming, users stare at a blank screen for 10 seconds.
Users can read while it generates. By the time the model finishes, the user has already read the first half.
Early termination. If the response starts going off track, the user (or the system) can abort early, saving compute.
π¦
The Numbers at Scale
Metric Estimated Value
| Users | ~100M weekly active |
|---|---|
| Requests/second | ~10,000-50,000 (estimated) |
| GPUs | Tens of thousands of H100s |
| GPU clusters | Multiple data centers globally |
| Tokens processed/day | Billions |
| Inference cost per query | $0.01-0.10 (depending on model/length) |
| Revenue per query | $0.003-0.01 (free tier), $0.02+ (Plus) |
| Annual GPU spend | ~$2-5 billion |
The economics are tight. Free-tier users cost more per query than they generate. Plus users subsidize the free tier. API customers are the profit center.
π
What Makes ChatGPT Feel Fast
OpenAI employs several tricks to minimize latency:
Speculative decoding (Episode 14) β a small draft model generates candidates, the large model verifies in parallel
KV cache optimization β PagedAttention (vLLM) prevents memory waste
Quantization β INT8/INT4 models on some tiers to speed up inferenceGeographic routing β process requests in the nearest data center
Continuous batching β never let GPUs idle
Model-specific optimizations β custom CUDA kernels for attention
π
Practical Takeaways
ChatGPT isn't one model β it's a distributed system with 7+ layers between your keyboard and the response
Your 7-word prompt becomes thousands of tokens β system prompts, history, safety instructions, tool configs
Safety happens BEFORE and AFTER generation β both input and output are filtered
Streaming isn't a UI trick β it's how inference works β tokens are genuinely generated one at a time
The KV cache is what makes long conversations possible β without it, latency would grow quadratically
The economics are brutal β GPU costs are enormous, and most users don't pay enough to cover their usage
ποΈ
What's Next?
Episode 93: How Cursor/Copilot Works β AI coding assistants use a different trick: fill-in-the-middle. Instead of generating left-to-right, they predict what goes IN BETWEEN your existing code. Plus: how they gather context from your entire codebase, integrate with your editor's language server, and make suggestions that actually compile.
β Previous
Ep 91: Evaluation Frameworks
Next β
Ep 93: How Cursor/Copilot Works
Next: Episode 93 β How Cursor/Copilot Works
You pause for half a second. Grey text appears β completing your function, writing the next 5 lines. Here's how it actually works.
This is part of a 98-episode series covering AI engineering from tokens to production deployment.