Overview
A large language model (LLM) is a neural network trained to predict the next token in a sequence. Given "The capital of France is", it assigns a probability to every token in its vocabulary and picks one, then repeats with the new token appended. Trained on trillions of tokens, this simple objective produces a model that can answer questions, summarize, translate, and write code.
An LLM goes through three stages. Pre-training on large text corpora teaches language and world knowledge. Instruction tuning on curated examples teaches it to follow requests. Preference tuning (for example reinforcement learning from human feedback) makes answers more helpful and safer. At serving time the model only runs inference, and its knowledge is frozen at the training cutoff unless you supply context in the prompt.
Phone autocomplete suggests the next word from your recent messages. An LLM is autocomplete that has read a large slice of the public internet and books, so its guesses can continue an essay, a program, or a conversation convincingly. It still only predicts what text plausibly comes next, which is why it can sound confident while being wrong.
When to use it
- Open-ended language tasks such as drafting, summarizing, rewriting, and answering questions.
- Turning unstructured text into structured data.
- Conversational interfaces and assistants.
- Code generation and explanation.
Where it shows up in interviews
Recognize it when: the design includes a chat, summary, or extraction feature.
- Design a ChatGPT-like system
- Design an AI chatbot
Recognize it when: answers must be accurate, fresh, or cheap at scale.
- Design a RAG system
- Design an LLM gateway
Where it is used in real software
Consumer and enterprise chat products wrap an LLM with conversation history, tools, and safety filters.
IDE plugins send surrounding code as context and stream back completions and edits.
LLMs draft replies grounded in help-center articles, and a human or policy layer approves them.
Key terms
- Token
- A chunk of text, often a word piece, that the model reads and writes. Roughly four English characters per token.
- Context window
- The maximum number of tokens the model can consider at once, covering both prompt and output.
- Parameters
- The learned weights of the network. More parameters generally mean more capacity and higher serving cost.
- Temperature
- A sampling setting. Low values make output focused and repeatable; high values make it more varied.
- Hallucination
- Fluent output that is not supported by facts or the provided context.
How it works, step by step
- 1Tokenize the input
The prompt is split into token IDs using the model's tokenizer.
- 2Embed and add position
Each token ID becomes a vector, combined with information about its position.
- 3Run transformer layers
Dozens of attention and feed-forward layers let each token gather context from earlier tokens.
- 4Predict the next token
The final layer outputs a probability for every vocabulary token; sampling picks one.
- 5Repeat until done
The new token is appended and the loop continues until a stop token or length limit. Cached keys and values (the KV cache) avoid recomputing earlier tokens.
STEP 1"The capital of France is" is split into tokens.
Sizing an LLM feature
A support tool summarizes 50,000 tickets per day. Each ticket averages 1,200 input tokens and a 150-token summary.
| Quantity | Calculation | Result |
|---|---|---|
| Input tokens per day | 50,000 x 1,200 | 60M |
| Output tokens per day | 50,000 x 150 | 7.5M |
| Peak requests per second | 50000 / 86400 x 5 peak factor | about 3 RPS |
| Latency per summary | prefill plus 150 output tokens | 1-3 seconds |
NOWQuantity: Input tokens per day | Calculation: 50,000 x 1,200 | Result: 60M
Token volume drives cost, and output tokens drive latency. Batch the job overnight if nobody waits on it.
Implementation
import OpenAI from "openai"; const client = new OpenAI(); // Stream tokens to the user as they are generated.const stream = await client.chat.completions.create({ model: "gpt-4o-mini", temperature: 0.2, // focused, repeatable answers max_tokens: 300, // cap cost and latency stream: true, messages: [ { role: "system", content: "You summarize support tickets in three bullet points." }, { role: "user", content: ticketText }, ],}); for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? "");Complexity and performance
Processed in parallel, so it is fast per token.
Sequential, so output length dominates latency.
KV cache grows with context length and batch size.
Trade-offs
Larger models reason better but cost more and respond slower. Many systems route easy requests to small models and hard ones to large models.
APIs are fast to adopt and always current; self-hosting open models gives data control and predictable cost at high volume but needs GPU operations.
Variants and related techniques
Accept images, audio, or video alongside text.
Spend extra tokens thinking before answering, trading latency for accuracy on hard problems.
Compact models that run on laptops or phones for private, low-latency tasks.
Common mistakes
- Trusting the model's knowledge for recent or private facts.
Fix: Provide the facts in the prompt through retrieval (RAG) or tool calls.
- Ignoring output token cost and latency.
Fix: Set max tokens, ask for concise formats, and stream responses.
- Parsing free-form text in code.
Fix: Request structured output (JSON schema) and validate it.
Interview questions
Why do LLMs hallucinate?
They are trained to produce plausible next tokens, not verified facts. When the answer is missing from training data or context, the most plausible continuation can still be wrong. Grounding with retrieved sources, asking for citations, and evaluation reduce it.
What dominates LLM latency?
Time to first token depends on prompt length (prefill), while total time depends mostly on the number of output tokens because decoding is sequential. Shorter outputs, streaming, smaller models, and caching help.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Estimate token cost and latency for a summarization feature | Easy | Token math. |
| Design a chat service that streams answers to 1M daily users | Hard | Serving, streaming, and cost control. |