Overview
Level 1: Foundations · Module 1: How LLMs Work for Frontend Engineers · 15 min read · Core Track
Why this matters
Frontend engineers are frequently told that LLMs are "reasoning engines," "databases with natural language interfaces," or "sentient agents." Believing these marketing metaphors leads directly to catastrophic bugs in production: relying on the model for deterministic business logic, trusting it to verify database foreign keys, or expecting it to "remember" state across requests. When you understand the true statistical mechanism—a sequence-to-sequence conditional probability distribution over discrete tokens—you instantly know what can be delegated to the model and what must stay in TypeScript.
Learning objectives
- Form an exact mechanical model of autoregressive token generation.
- Differentiate between next-token probability prediction and stateful execution.
- Understand why LLMs have no active memory between HTTP requests.
- Defend architectural decisions in frontend system design interviews when asked about model capabilities.
Concept explanation: Plain language first, then engineering mechanics
At its most fundamental level, a Large Language Model (LLM) is a stateless mathematical function that maps a sequence of input token integers to a probability distribution over the next token integer.
f(tokens_in) -> P(next_token | tokens_in)It is not an active process continuously executing in the background. It is not an agent sitting in memory waiting for events. When an HTTP request hits /api/chat, your server passes an array of tokens to the model weights. The neural network computes matrix multiplications across billions of parameters in a single forward pass, produces logits for every token in its vocabulary (typically ~100k-200k tokens), and samples exactly one token.
To produce a full sentence, this operation is called autoregressively:
- You pass
["The", "sky", "is"] - The model outputs
"blue"(highest probability) - The server appends
"blue"to the sequence:["The", "sky", "is", "blue"] - The server runs the entire forward pass again to sample the next token
"." - The loop repeats until the model samples an
<|endoftext|>stop token or reaches yourmax_tokensceiling.
graph LR
A["Input Tokens: ['The', ' sky', ' is']"] --> B["Transformer Forward Pass<br>(Weights & Attention)"]
B --> C["Logits over Vocabulary<br>(~128,000 tokens)"]
C --> D["Softmax + Sampling<br>(T, Top-P)"]
D --> E["Single Sampled Token: ' blue'"]
E --> F["Append & Repeat Loop"]What an LLM is NOT
- It is NOT a database: It cannot query tables, verify foreign keys, or guarantee that data stored 5 turns ago is retrieved with 100% precision.
- It is NOT a JavaScript runtime: It does not "execute" algorithms; it approximates the lexical output of algorithms it encountered in training text.
- It has NO persistent memory: Every single request is completely isolated. The illusion of a continuous multi-turn chat occurs solely because the frontend or server re-sends the entire conversation history on every turn.
Code walkthrough: Simulating the Autoregressive Turn
Here is how the autoregressive inference turn maps cleanly to TypeScript:
// types/llm.ts
export interface TokenProbability {
token: string;
tokenId: number;
probability: number;
}
export interface InferenceStep {
contextTokens: number[];
predictedToken: string;
isStopToken: boolean;
}
/**
* Conceptual simulation of the autoregressive generation loop.
* In production, provider SDKs (Anthropic, OpenAI) stream this via SSE.
*/
export async function* simulateAutoregressiveGeneration(
prompt: string,
maxTokens: number = 50
): AsyncGenerator<string, void, unknown> {
const STOP_TOKEN = "<|end_of_turn|>";
let currentContext = prompt;
for (let step = 0; step < maxTokens; step++) {
// 1. Matrix multiplication forward pass (simulated)
const nextChunk = await fakeModelForwardPass(currentContext);
// 2. Stop condition check
if (nextChunk === STOP_TOKEN) {
break;
}
// 3. Yield immediately to streaming client
yield nextChunk;
// 4. Autoregressive append: The model has no memory, so context must grow!
currentContext += nextChunk;
}
}
async function fakeModelForwardPass(context: string): Promise<string> {
await new Promise((res) => setTimeout(res, 40)); // 40ms per token (~25 tok/sec)
const tokens = [" A", " production", " frontend", " handles", " latency", " gracefully.", "<|end_of_turn|>"];
const wordIndex = Math.min(context.split(" ").length % tokens.length, tokens.length - 1);
return tokens[wordIndex];
}Common mistakes frontend developers make
- Assuming the model "remembers" the user across messages: Forgetting to pass previous messages in the
messagesarray causes the model to have amnesia. - Treating math or date calculations as reliable: Expecting an LLM to accurately calculate
new Date("2026-10-14") - new Date("2023-01-01")directly in prose instead of using a deterministic tool or JavaScript expression. - Over-indexing on "sentience": Writing conversational prompts like "Please try your absolute hardest to be correct" instead of structural constraints like "Respond with valid JSON adhering to schema X. If uncertain, return null."
Interview angle: How this appears in interviews
Interviewer: "We want to build a real-time form validator that uses an LLM to validate credit card numbers and calculate shipping taxes. How would you design this?"
Winning Answer:
"I would strongly push back against using an LLM for credit card validation or tax calculation. An LLM is a probabilistic next-token predictor, not an arithmetic engine or regex validator. Tax calculations require 100% deterministic arithmetic and local tax table lookups, and credit cards require the Luhn algorithm. We should run Luhn validation and tax logic purely in client/server TypeScript. We should only invoke the LLM for subjective, fuzzy tasks—such as parsing unstructured address notes into standard fields, or generating conversational assistance."
Quick check
- True or False: When you chat with ChatGPT or Claude, the model server keeps an active Node.js session storing your chat variables in RAM.
(Answer: False. The model is stateless; the entire history is sent with each request.) - What triggers an LLM to conclude generation?
(Answer: Sampling an explicit Stop Token, such as<|endoftext|>, or reachingmax_tokenslimit.) - Why does an LLM struggle with exact character counting (e.g. 'how many letter r's in strawberry')?
(Answer: Because the model processes multi-character tokens, not individual raw characters.)
Summary
- An LLM is a stateless conditional probability function that maps token sequences to the next token.
- Generation is autoregressive: each sampled token is appended to the input context for the next forward pass.
- There is zero session memory inside the model weights; context must be transmitted on every request.
- Deterministic arithmetic, business logic, and security validation belong in TypeScript, not in prompt prose.
- Next up: We will dissect tokens, the Byte-Pair Encoding algorithm, and why tokens dictate your latency and cost.