How LLMs Work for Engineers: Tokens, Context Windows and Sampling
What engineers need to know about large language models: tokens, the transformer at a high level, context windows, temperature and sampling, and pricing.
You don't need to train a large language model to design systems around one, but you do need a working model of how an LLM turns input into output, because it explains latency, cost, context limits and many failure modes. This guide covers tokens, the transformer at the level a system designer needs, context windows, sampling (temperature and top-p), how generation happens one token at a time, and how all of that becomes per-token pricing.
Tokens
LLMs read and write tokens: chunks of text produced by a tokenizer, often a whole common word, part of a rarer word, or punctuation. In English a token averages roughly three-quarters of a word, so 1,000 tokens is about 750 words. Code, numbers and non-English text often use more tokens per word.
- Limits (context window, max output) are in tokens.
- Price is per token, usually per million, with output tokens costing several times more than input tokens.
- Latency grows with output tokens, because they're generated one at a time.
What happens inside, briefly
Each token becomes an embedding vector. A stack of transformer layers updates every token's vector using attention, which lets each position look at all earlier positions to decide what's relevant. The final layer produces a score for every token in the vocabulary; those scores become a probability distribution over the next token. That's all an LLM does: predict the next token, repeatedly.
Two phases matter for serving: prefill reads the whole prompt in one parallel pass (fast per token, sets the time to first token), and decode generates output tokens one by one (sets the streaming speed). Serving systems keep each token's attention keys and values in a KV cache so they aren't recomputed every step. See LLM serving.
Context windows
The context window is the maximum number of tokens (prompt plus output) the model can attend to in one call. Everything the model knows about this request has to fit: system instructions, conversation history, retrieved documents, tool results. The model remembers nothing between calls; your application resends what it needs each time.
- Long contexts cost more (you pay for every input token) and are slower to prefill.
- Models use information in very long contexts unevenly; facts buried in the middle are recalled less reliably than those at the start or end.
- So even with large windows, selecting the right context (RAG, memory) usually beats stuffing everything in.
Sampling: temperature and top-p
| Setting | What it does | Use |
|---|---|---|
| Temperature 0 (or near) | Almost always picks the most likely token | Extraction, classification, code, anything that needs consistency |
| Temperature 0.7-1.0 | More varied, creative output | Brainstorming, writing, multiple candidate answers |
| Top-p (nucleus) | Samples only from the smallest set of tokens whose probability sums to p | Cuts off very unlikely tokens while keeping variety |
| Max tokens | Hard cap on output length | Cost and latency control; truncation must be handled |
| Stop sequences | End generation when a string appears | Delimited formats, agent loops |
Even at temperature 0, outputs aren't guaranteed identical across calls or model versions. Design for variation: validate outputs, retry, and pin model versions in production.
From base model to assistant
- Pretraining on a huge text corpus teaches next-token prediction and broad knowledge.
- Instruction tuning (supervised fine-tuning on examples of instructions and good answers) makes it follow requests.
- Preference tuning (from human or AI feedback) makes answers more helpful and safer.
The knowledge has a cutoff date and isn't your private data, which is why products add retrieval and tools. Adapting models yourself is covered in fine-tuning vs RAG.
Why LLMs fail the way they do
- Hallucination: the model produces fluent, plausible text whether or not it knows the answer. Grounding with retrieval and citations, and asking it to say when it doesn't know, reduce it.
- Instruction conflicts: text in the prompt from untrusted sources can be read as instructions. See prompt injection.
- Format drift: free-text output doesn't reliably match a schema. See structured outputs.
- Arithmetic and exact recall: better handled by tools (a calculator, a database query) than by the model.
Turning tokens into cost and latency
Request: 2,000 input tokens, 400 output tokens
Cost = 2,000 × input_price + 400 × output_price
e.g. at $3 / $15 per 1M → $0.006 + $0.006 = $0.012 per request
Latency ≈ time_to_first_token + output_tokens ÷ decode_speed
e.g. 0.5 s + 400 ÷ 60 tok/s ≈ 7.2 s total, first text at 0.5 s (stream it)Where it’s used
Token counts per request turn user numbers into GPU or API budgets; see cost optimization.
Knowing the window and attention behaviour shapes how context is selected and ordered.
Non-determinism and hallucination drive validation, retries, grounding and evals.
In an interview
- Estimate tokens per request and convert them to cost and latency.
- Explain prefill vs decode and why output length dominates latency.
- Choose temperature by task: near zero for extraction, higher for creative work.
- Say why selecting context beats filling the window.
Frequently asked questions
What is a token in an LLM?
A chunk of text produced by the model's tokenizer, often a word or part of a word. In English one token is roughly three-quarters of a word. Context limits, latency and pricing are all measured in tokens.
What is a context window?
The maximum number of tokens, prompt plus output, a model can process in one call. Everything the model needs for a request must fit inside it, because the model keeps no memory between calls.
What does temperature do?
It controls randomness when sampling the next token. Values near zero give consistent, most-likely output for extraction and code; higher values give more varied output for creative tasks.
Further reading
- Attention Is All You NeedVaswani et al., NeurIPS 2017
- Lost in the Middle: How Language Models Use Long ContextsLiu et al., 2023