MODULE 14  Β·  Real-World Architectures

How ChatGPT Works: The Full Stack, End to End

You type a question. Two seconds later, words stream onto your screen. Here's everything that happens in between.

πŸ“… Mar 2026
⏱ 10 min read
🎯 Episode 92 of 98
ChatGPTFull StackArchitectureCase Study
In this episode

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

example
code
β”‚
    β–Ό

CloudFlare CDN (DDoS protection, caching, edge routing)

example
code
β”‚
    β–Ό

API Gateway (rate limiting, auth, routing)

example
code
β”‚
    β–Ό

Conversation Service (context management, history)

example
code
β”‚
    β–Ό

Safety/Moderation Layer (content filtering, PII detection)

example
code
β”‚
    β–Ό

Prompt Orchestration (system prompts, tool configs, model selection)

example
code
β”‚
    β–Ό

Inference Router (load balancing across GPU clusters)

example
code
β”‚
    β–Ό

Inference Cluster (GPU nodes running the actual model)

example
code
β”‚
    β–Ό

Token Streaming (SSE/WebSocket back to browser)

example
code
β”‚
    β–Ό

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": [

example
code
{"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):

example
code
# Step 1: Fast classifier (lightweight model)
    risk_score = safety_classifier.predict(messages)
example
code
if risk_score > 0.95:
        return BLOCK  # Obviously harmful, don't even process
example
code
# Step 2: PII detection
    pii_found = detect_pii(messages)
    if pii_found:
        messages = redact_pii(messages)  # Strip sensitive info
example
code
# 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 prompt
example
code
return ALLOW

OpenAI 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]:

snippet
code
User (3 messages ago): "What's quantum computing?"

Assistant: "Quantum computing uses quantum mechanical phenomena..."

snippet
code
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)

snippet
code
Token 1: "Imagine" β†’ 200ms + context reading
Token 2: "you"     β†’ 20ms (faster now, KV cache warm)
Token 3: "have"    β†’ 20ms
Token 4: "a"       β†’ 20ms

...

snippet
code
Token N: "[done]"  β†’ 20ms

Total: 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)
GPUsTens of thousands of H100s
GPU clustersMultiple data centers globally
Tokens processed/dayBillions
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

snippet
code
Quantization β€” INT8/INT4 models on some tiers to speed up inference

Geographic 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.

← Previous Ep 91: Evaluation Frameworks: How Do You Know If Your Model Is…