AI Agentic Handbook
Foundations

Large Language Model (LLM)

Budding levelReviewed Oct 2026In 29 interview questionsIn 8 posts

A program that has read a staggering amount of text and learned one trick very well: guess what comes next. Chat, agents and search all run on that trick.

In plain English

A large language model is a program that has read an enormous amount of text — books, code, websites — and learned one skill extremely well: given some text, guess what comes next. It works one small piece at a time, and each piece is a Token (roughly a word or a word fragment). Ask it a question and it simply keeps writing, one token after another, until an answer has appeared.

That sounds far too simple to be useful, and honestly it surprised everyone that it works this well. Chatbots, coding assistants and agents are all this same next-word guesser, wrapped in instructions, tools and data.

Analogy

Autocomplete that read the entire library. It has seen so many sentences that its guesses about what comes next can look a lot like thinking.

Common pitfall

The easy trap is treating it like a database you look facts up in. It is not fetching anything — it is writing the most plausible next words. With no real documents to lean on it will make things up, in exactly the same confident voice it uses when it is right.

How it actually works

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.

What I'd ship on day one

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.

Worked example

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.

In the interview

What they are really checking is whether you can pick a model without hand-waving. Say it plainly: a hosted frontier model behind a gateway for v1, and 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. The follow-up is almost always '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), then say which one your product actually feels. Naming the one that matters to users is the part that lands.

App
Tokenizer
GPU prefill
GPU decode
1
6,000-token promptApp → Tokenizer
2
6,000 ids, one passTokenizer → GPU prefill
3
KV cache + token 1GPU prefill → GPU decode
4
TTFT ≈ 300–600 msGPU prefill → App
5
1 token/step, ~10 msGPU decode → GPU decode
6
stream 250 tokensGPU decode → App
Prefill reads the whole prompt in parallel; decode loops one token at a time, re-reading weights and KV cache every step.
6k prompt, 250 out
2,950 ms
60k prompt, 250 out
5,500 ms
Reasoning model
17,950 ms
Prefill (TTFT)Visible outputHidden reasoning

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

WhenDo this
Open-ended drafting or reasoning, under ~10M tokens/dayUse 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 setMove it to a smaller hosted model or distil onto an 8B open-weights model on vLLM.
Answers must be current, private, permissioned or citedAdd Retrieval-Augmented Generation (RAG); never rely on the weights for those facts.
Users wait on long outputsStream, 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

Time to scale up when…
  • 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
What to monitor
  • TTFT p95 per route: alert above 2× the 7-day baseline
  • stop_reason = max_tokens rate: 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

What users saw

A contract-review assistant returned truncated JSON on ~4% of long master services agreements the week after a model version upgrade.

How it was caught

Parse-failure alarm on the extraction route, then a log query showing every failure had stop_reason = max_tokens.

Root cause

The new model version wrote longer clause summaries; max_tokens stayed at 1,024, so outputs were cut mid-object.

Fix

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

Where it shows up

Sources

  1. Attention Is All You Need — Vaswani et al., 2017paper
  2. Training language models to follow instructions with human feedback — Ouyang et al., 2022paper
  3. 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.

1ConceptAn 8B model in BF16 serves a single stream on one H100 and tops out near 200 tokens/s even though the GPU has FLOPs to spare. Why?
2ScenarioA narrow classification route burns 60M tokens/day on a hosted frontier model. An 8B open-weights model scores 1.5 points lower on its golden set. What does the page recommend?
3CalculationA route sends 6,000 input tokens and gets 250 visible output tokens at $3/M input and $15/M output. You switch it to a reasoning model that also emits 2,000 hidden reasoning tokens. Roughly what does one call cost now?