Large Language Model (LLM)
A transformer trained to predict the next token over trillions of tokens, then tuned to follow instructions: the engine under chat, agents and RAG.
A large language model is a program that has read an enormous amount of text and learned one skill extremely well: given some text, guess what comes next. It works one small piece at a time, where each piece is a Token (roughly a word fragment). Ask it a question and it continues your text with an answer, one token after another.
Chatbots, coding assistants and agents are all built by wrapping this next-token predictor with instructions, tools and data.
An extraordinarily well-read autocomplete: it has seen so many sentences that its guesses about what comes next often look like reasoning.
Treating the model as a database that looks facts up. It generates the most plausible continuation; without grounding it will hallucinate in the same confident tone it uses for correct answers.
A modern LLM is a decoder-only Transformer: stacked self-Attention and feed-forward (often Mixture of experts (MoE)) blocks. Pretraining on 10–40T tokens teaches Next-token prediction (Llama 3 used over 15T); post-training with Instruction tuning, RLHF (reinforcement learning from human feedback) or Preference optimization (DPO), and RL on verifiable rewards produces instruction following, tool use and reasoning.
Inference has two phases. Prefill runs the whole prompt through the network in one parallel pass and is compute-bound; it sets Time to first token (TTFT). Decode emits one token per step and is memory-bandwidth-bound: every step reads all weights plus the KV cache. An 8B model in BF16 is 16 GB, so on an H100 (3.35 TB/s) a single stream tops out near 200 tokens/s no matter how many FLOPs the chip has; Batching & throughput many streams is how servers recover throughput.
Each step produces Logits & probabilities over a 100k–250k vocabulary; a sampler (Temperature & sampling, Top-p (nucleus) sampling) picks one token and the loop repeats. Knowledge is frozen in the weights at the Knowledge cutoff, which is why production systems add retrieval, tools and Structured output / decoding.
Hosted frontier model behind an LLM gateway with Model fallback & routing to a second provider; streaming on; temperature 0 for extraction and classification, 0.7 for drafting; max_tokens set per route from measured p99 output + 30%; prompts in git with Prompt versioning; log input, output and reasoning tokens plus stop_reason on every call.
A code-search assistant gets: 'Where do we retry failed S3 uploads?' The prompt is 6,000 tokens: 1,500 of instructions plus 8 retrieved code snippets and the question. On a hosted frontier model, prefill returns the first token in 300–600 ms; decode streams a 250-token answer at ~100 tokens/s, so the reply finishes in about 3 s. At an assumed $3 / M input and $15 / M output, the call costs $0.018 + $0.004 ≈ $0.022. Output tokens are 4% of the tokens but 17% of the bill.
Default to a hosted frontier model behind a gateway for v1; move one narrow route to an 8–70B Open-weights vs closed models model on vLLM once it burns more than ~50M tokens/day and the Golden set gap is under 2 points. Expect the follow-up 'where does the latency go?': split it into TTFT (prefill, grows with prompt length) and inter-token latency (decode, grows with model size and batch), and say which one your product actually feels.
Numbers worth knowing
- Decode speed, hosted API
- 50–150 tokens/s per streamsingle request on frontier models, Oct 2026; reasoning models spend hidden tokens before the first visible one
- Weights memory
- 2 bytes × parametersBF16: 8B ≈ 16 GB, 70B ≈ 140 GB; FP8 halves it, 4-bit quarters it
- Single-stream decode ceiling
- ≈ HBM bandwidth ÷ weight bytes8B BF16 on one H100 (3.35 TB/s) ≈ 200 tokens/s upper bound
- Output vs input price
- 4–5× per tokentypical hosted list prices, e.g. $3 in / $15 out per M (illustrative)
How to decide
| When | Do this |
|---|---|
| Open-ended drafting or reasoning, under ~10M tokens/day | Use a hosted frontier model; self-hosting will not pay back the GPU and on-call cost. |
| Narrow high-volume task (classification, extraction, rewriting) and a small model is within 2 points on the golden set | Move it to a smaller hosted model or distil onto an 8B open-weights model on vLLM. |
| Answers must be current, private, permissioned or cited | Add Retrieval-Augmented Generation (RAG); never rely on the weights for those facts. |
| Users wait on long outputs | Stream, cap max_tokens, and pick the model by inter-token latency, not benchmark rank. |
Cost maths
Cost per call = input_tokens × p_in + (output + reasoning tokens) × p_out. Assume $3 / M input and $15 / M output (illustrative list prices, Oct 2026). 6,000 in + 250 out = $0.018 + $0.00375 ≈ $0.022. At 200k calls/day that is ≈ $4,400/day, ≈ $132k/month. Switch the same route to a reasoning model that emits 2,000 hidden tokens and each call adds 2,000 × $15 / M = $0.03, more than doubling the bill. Prompt caching on the 1,500-token instruction prefix and the provider's batch API (~50% off) are the first two levers.
Operating it
- Spend on one route passes ~$20k/month and a smaller model is within 2 points on its golden set
- TTFT p95 breaches the SLO because prompts routinely exceed 30k tokens
- Provider 429 or 5xx rate above 1% of requests for a week
- TTFT p95 per route: alert above 2× the 7-day baseline
stop_reason = max_tokensrate: alert above 0.5%- Cost per 1k requests per route: alert on +25% week over week
- Provider error rate (429/5xx): alert above 1% over 15 minutes
When it goes wrong
A contract-review assistant returned truncated JSON on ~4% of long master services agreements the week after a model version upgrade.
Parse-failure alarm on the extraction route, then a log query showing every failure had stop_reason = max_tokens.
The new model version wrote longer clause summaries; max_tokens stayed at 1,024, so outputs were cut mid-object.
Set max_tokens from measured p99 output + 30% per route, moved extraction to Structured output / decoding, and added stop_reason distribution to the model-upgrade Eval gate (CI).
Related terms
Where it shows up
Sources
- Attention Is All You Need — Vaswani et al., 2017paper
- Training language models to follow instructions with human feedback — Ouyang et al., 2022paper
- Efficient Memory Management for Large Language Model Serving with PagedAttention — Kwon et al., 2023paper
Check yourself
Three questions. Every option is explained once you answer.