AI & GENERATIVE AI / SYSTEM CONCEPT BRIEF

Large Language Models

A large language model (LLM) is a neural network trained to predict the next token in a sequence.

IntermediatePhase 09 / Topic 6 of 30RequirementsTrade-offsFailure modes
01

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.

A very well-read autocomplete

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.

02

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

Where it shows up in interviews

LLM as a component

Recognize it when: the design includes a chat, summary, or extraction feature.

  • Design a ChatGPT-like system
  • Design an AI chatbot
Grounding and cost control

Recognize it when: answers must be accurate, fresh, or cheap at scale.

  • Design a RAG system
  • Design an LLM gateway
04

Where it is used in real software

Chat assistants

Consumer and enterprise chat products wrap an LLM with conversation history, tools, and safety filters.

Code assistants

IDE plugins send surrounding code as context and stream back completions and edits.

Customer support

LLMs draft replies grounded in help-center articles, and a human or policy layer approves them.

05

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

How it works, step by step

  1. 1
    Tokenize the input

    The prompt is split into token IDs using the model's tokenizer.

  2. 2
    Embed and add position

    Each token ID becomes a vector, combined with information about its position.

  3. 3
    Run transformer layers

    Dozens of attention and feed-forward layers let each token gather context from earlier tokens.

  4. 4
    Predict the next token

    The final layer outputs a probability for every vocabulary token; sampling picks one.

  5. 5
    Repeat 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.

One generation step at a time
Step 1 / 4
Prompt
Tokens
Transformer
Probabilities
Next token

STEP 1"The capital of France is" is split into tokens.

07

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.

Step 1 / 4
QuantityCalculationResult
Input tokens per day50,000 x 1,20060M
Output tokens per day50,000 x 1507.5M
Peak requests per second50000 / 86400 x 5 peak factorabout 3 RPS
Latency per summaryprefill plus 150 output tokens1-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.

08

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 ?? "");
09

Complexity and performance

PrefillO(n^2) attention over prompt length n

Processed in parallel, so it is fast per token.

Decodeone forward pass per output token

Sequential, so output length dominates latency.

Memoryweights plus KV cache

KV cache grows with context length and batch size.

10

Trade-offs

Bigger vs smaller models

Larger models reason better but cost more and respond slower. Many systems route easy requests to small models and hard ones to large models.

Hosted API vs self-hosted

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.

11

Variants and related techniques

Multimodal models

Accept images, audio, or video alongside text.

Reasoning models

Spend extra tokens thinking before answering, trading latency for accuracy on hard problems.

Small language models

Compact models that run on laptops or phones for private, low-latency tasks.

12

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.

13

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.

14

Practice problems

ProblemDifficultyWhat it trains
Estimate token cost and latency for a summarization featureEasyToken math.
Design a chat service that streams answers to 1M daily usersHardServing, streaming, and cost control.