LLM Field Guide 12 — Putting It All Together¶
Every post in this series is one stage a single request passes through. This capstone follows one real request, a Thai customer asking about a refund, from keystroke to streamed answer. It names the post that governs each step and the number that matters there, then gives you the framework for when something goes wrong.
In this post:
- The system we're tracing — every stack decision and the post that justifies it
- Steps 1–7 — gateway, prompt assembly, tokenization, prefill and KV math, decode and tool calls, approval, evaluation
- How the training side produced this model — the weight pipeline, arrow by arrow
- The symptom → lever framework — where to start when production breaks
- Five ideas that explain most of the series
LLM Field Guide — all 12 posts
The system we're tracing: a Thai support assistant on one H100¶
A Thai e-commerce company runs a support assistant on its own GPUs. Here's the stack, with the post behind each choice:
| Decision | Choice | Why | Post |
|---|---|---|---|
| Base model | Qwen3.5-9B, Apache 2.0 | License allows commercial self-hosting; the most token-efficient Thai tokenizer in the series' tests | 07, 01 |
| Domain adaptation | No continued pretraining | Their gap is policy and tone, not language knowledge | 08 |
| Behavior | QLoRA SFT on 3,000 real support chats | Teaches the house format and escalation rules | 08 |
| Preference | DPO on 1,500 agent-ranked answer pairs | Fewer "I'll check" non-answers | 08 |
| Knowledge | Hybrid RAG over policy docs and FAQs | Policies change weekly; retraining for that would be absurd | 05 |
| Actions | Two MCP tools: lookup_order, create_refund_ticket | Real data and real actions, with approval on writes | 06 |
| Serving | vLLM, FP8 weights + FP8 KV cache, one H100 | Native FP8 on Hopper; halves both weights and cache | 09, 10 |
| Safety | Injection classifier on retrieved/tool content, output PII redaction, Rule of Two on refunds | Untrusted content meets a write action | 11 |
| Quality gate | 600-question Thai golden set, paired comparison in CI | Every prompt, adapter, or quant change must prove itself | 11 |
The request: "สั่งของไปตั้งแต่วันจันทร์ ออเดอร์ A1023 ยังไม่ได้เลย ขอคืนเงินได้ไหมคะ" — "I ordered on Monday, order A1023 still hasn't arrived. Can I get a refund?"
Step 1 — Gateway and input rails (post 11)¶
Before the model sees anything, the gateway enforces a token cap and rate limit, and the user's text goes through a content classifier.
The dangerous input isn't the user's message; it's the retrieved policy chunks and tool results that flow into the context later. Those get the injection classifier too.
Step 2 — Prompt assembly (post 05)¶
The application builds the context window in a deliberate order: stable content first, volatile content last, so the prefix stays cacheable.
┌──────────────────────────────────────────────┐
│ System prompt + rules + tool schemas ~3,000 │ ← identical every call → prefix-cache hit
├──────────────────────────────────────────────┤
│ Retrieved policy chunks (5 × ~600) ~3,000 │ ← hybrid search: BM25 + vectors, RRF, rerank
├──────────────────────────────────────────────┤
│ Conversation so far ~800 │
├──────────────────────────────────────────────┤
│ User's new message ~40 │ ← question last
└──────────────────────────────────────────────┘
total ≈ 6,840 tokens
Retrieval is a miniature of posts 01 and 05. A sentence-embedding model turns the Thai query into a vector, BM25 catches the literal A1023 and "คืนเงิน" (refund), Reciprocal Rank Fusion merges the two rankings, and a cross-encoder picks the top 5.
If the answer is wrong, check retrieval first. Measure recall@k on your golden set before you touch the prompt or the model.
Step 3 — Tokenization (post 01)¶
The chat template wraps every turn in special tokens, and the tokenizer turns the text into ~6,840 integer IDs.
Language matters here. Post 01 measured Thai at 1.0× to 1.8× the English token count across six 2026 tokenizers. On a weak tokenizer, the same Thai conversation costs up to 80% more context, KV cache, and latency — which is why the tokenizer was a kill criterion in model selection (post 07).
Step 4 — Prefill: one parallel pass (post 02, post 04)¶
vLLM's scheduler finds the first ~3,000 tokens already in the prefix cache and skips them. It runs a forward pass only over the ~3,840 new tokens.
Inside that pass, each token:
- Becomes a 4,096-dim vector via the embedding table (post 01)
- Flows through 32 decoder blocks: RMSNorm → token mixing → residual add → RMSNorm → SwiGLU FFN → residual add (post 02)
- In 8 of those blocks, gets rotated by RoPE and reads every earlier token through full attention with 4 shared KV heads (post 02, GQA in post 04). The other 24 use Gated DeltaNet linear attention, which keeps a fixed-size state instead of a growing cache1
Only the full-attention layers write to the KV cache. From Qwen3.5-9B's config.json (8 full-attention layers, 4 KV heads, head dim 256)1, using the sizing formula from post 04:
KV per token = 2 (K,V) × 8 full-attn layers × 4 heads × 256 dims × 1 byte (FP8)
= 16,384 bytes = 16 KiB
This request = 6,840 tokens × 16 KiB ≈ 107 MiB (+ a fixed DeltaNet state per sequence)
Same request on Qwen3-8B (36 full-attn layers, 8 KV heads, dim 128, FP8):
= 72 KiB/token × 6,840 ≈ 481 MiB → 4.5× more
The KV cache, not the weights, usually decides how many conversations fit at once — and hybrid attention is the 2026 answer. FP8 weights take ~10 GB of the H100's 80 GB, leaving room for hundreds of requests this size; at that point compute and vLLM's max-num-seqs cap become the limit. Don't trust arithmetic alone: read the real capacity from vLLM's startup log.
Prefill's output is a probability distribution for the first answer token. That's the time-to-first-token (TTFT) your users feel.
Step 5 — Decode: thinking, then a tool call (post 03, post 06)¶
Now the generation loop from post 01 runs, one token per forward pass.
The model runs in thinking mode at low effort (post 06) and reasons briefly: order status unknown → look it up before quoting refund policy. Sampling follows the model card's thinking settings, not greedy (post 03).
Then it emits a tool call in its trained format, and vLLM's tool parser turns that into structured tool_calls:
Your application validates the arguments against the schema and calls the MCP server:
That result is untrusted, so it passes the injection classifier before it's appended. The second model call reuses the prefix cache for everything before the tool result — agents are only affordable because of this.
Why decode is the slow part¶
Each decode step reads every weight from HBM to produce one token (post 04). At ~9.7 GB of FP8 weights and the H100 SXM's 3.35 TB/s2, the single-stream ceiling is ~345 tokens/s; real numbers are lower. Three levers attack it:
| Technique | What it does here | Post |
|---|---|---|
| Continuous batching | 50 other conversations share each weight read | 10 |
| Speculative decoding (MTP/EAGLE-3) | Verifies ~2–3 drafted tokens per pass at low concurrency | 09 |
| FP8 weights | Halves bytes read per token vs BF16 | 09 |
Step 6 — The answer, the guardrail, and the approval (post 11)¶
With the tool result, the model drafts a Thai reply: the parcel is in transit, the ETA is tomorrow, and per the retrieved policy a refund can be filed if it hasn't arrived 7 days after shipping. It offers to open a ticket.
If the user says yes, the model calls create_refund_ticket. This agent has private data (order history), untrusted content (retrieved docs, tool output), and an external action (refunds) — all three legs of the Rule of Two. So the write requires human or policy-engine approval, enforced in code, not in the prompt.
The output rail redacts PII and validates the format, and the tokens stream to the user.
Step 7 — Observe and evaluate (post 11)¶
The trace records TTFT, inter-token latency, tokens in and out, cache hit rate, retrieved chunk IDs, tool calls, and guard verdicts. That's the raw material for the next improvement.
A week later someone proposes NVFP4 weights on new Blackwell GPUs. Run both on the 600-question Thai golden set with the same harness, and compare paired with a bootstrap CI and McNemar's test. Ship only if the difference is real — at 600 questions, a 1-point change is almost certainly noise.
How the training side produced this model¶
The request ran on weights that came from their own pipeline:
Pretraining (vendor) post 03 trillions of tokens, next-token loss, 6·N·D FLOPs
│
├─(optional) Continued pretraining post 08 only if language/domain knowledge is missing
▼
Instruct checkpoint (vendor) post 07 chosen on license, size, tokenizer, your eval
▼
QLoRA SFT (you) post 08 3,000 chats, LoRA r=16, assistant-only loss
▼
DPO (you) post 08 1,500 ranked pairs, beta 0.1
▼
(optional) Distill → smaller post 08 when the 9B is too slow or costly
▼
Quantize → FP8 post 09 evaluate before and after
▼
Serve: vLLM + spec decode posts 09–10
Every arrow is a place quality can drop silently, so each arrow gets a before/after eval (post 11).
The symptom → lever framework: start with the cheapest fix¶
Most production problems map to one or two posts. Start here:
| Symptom | First lever | Then | Posts |
|---|---|---|---|
| Wrong facts, outdated answers | Fix retrieval (recall@k, chunking, hybrid search) | Contextual retrieval, reranking | 05 |
| Right facts, wrong format or tone | Prompt: explicit rules, examples, structured output | SFT | 05, 08 |
| Invalid JSON or tool calls | Constrained decoding, correct tool parser | SFT on tool traces | 03, 06 |
| Fails multi-step reasoning | Thinking mode / higher effort | GRPO on verifiable tasks, distill from a reasoning teacher | 06, 08 |
| Weak in Thai or your domain's jargon | Better base model / tokenizer | Continued pretraining with replay | 07, 08 |
| Loses track in long conversations | Context discipline: compaction, sub-agents | Model with better effective (not advertised) context | 05, 04 |
| TTFT too high | Prefix caching, stable prompt prefix | Chunked prefill tuning, P/D disaggregation | 04, 10 |
| Inter-token latency too high | Speculative decoding, FP8/FP4 weights | TP across NVLink | 09, 10 |
| Can't fit enough concurrent users | FP8 KV cache, cap --max-model-len | GQA/MLA or hybrid model, more replicas (DP) | 04, 10 |
| Model doesn't fit on the GPU | Quantize | MoE with offload, TP/PP | 09, 10 |
| Cost per token too high | Smaller distilled model, quantization, caching | MoE for low-batch speed | 08, 09 |
| Harmful output or injection incidents | Guard classifiers on all untrusted inputs | Rule of Two, least privilege, red teaming | 11 |
| "Did that change help?" | Paired eval with confidence intervals | Larger golden set, LLM judge validated against humans | 11 |
Pick the cheapest lever first. The order is almost always prompt → retrieval → sampling and decoding settings → model choice → fine-tuning → continued pretraining. Each step down costs more time, data, and GPU hours than the last, and is harder to undo.
Five ideas that explain most of the series¶
- It's next-token prediction in a loop (01, 03). Reasoning, tool use, and chat are all patterns of generated tokens.
- Memory bandwidth is the bottleneck for decode (04, 09). Quantization, batching, speculative decoding, and MoE are all ways to move fewer bytes per token.
- The KV cache sets your concurrency (04). GQA, MLA, hybrid attention, FP8 KV, PagedAttention, and prefix caching all exist to shrink or reuse it.
- Knowledge goes in context; behavior goes in weights (05, 08). RAG for facts that change, SFT for format and behavior, CPT only for missing language or domain knowledge.
- Nothing is better until the eval says so (11). Small benchmarks are noisy; paired tests on your own data decide.
Summary¶
Your learning path, as a checklist:
- Foundations: run the next-token script and count your language's tokens (01). Read a model's
config.json(02) and compute its KV per token (04). - Using LLMs: build a hybrid RAG pipeline with a 50-question golden set (05). Add one MCP tool with schema validation and a step budget (06).
- Adapting: QLoRA-fine-tune a small model on 500 examples, then run DPO on 200 pairs (08). Evaluate before and after.
- Making it fast: serve it with vLLM in FP8 with prefix caching. Benchmark TTFT and ITL at 1, 8, and 32 concurrent requests (10). Turn on speculative decoding and measure again (09).
- Shipping: put an injection classifier on retrieved content, and gate every change on a paired eval with a 95% CI (11).
When something breaks, go back to the symptom → lever table above.
Next: start over at post 01 with fresh eyes, or go to the production deep-dives below.
Go deeper
- vLLM: Production LLM Serving from Zero to Scale
- Serving an LLM on Kubernetes in 2026: the operations checklist
- Building a Production RAG App
- Building Effective AI Agents
- How Much VRAM Does Your LLM Actually Need?
- Building an LLM from Scratch in PyTorch
- Self-Hosted LLM: The Real Cost After 30 Days
-
Qwen, Qwen3.5-9B config.json — 32 layers,
full_attention_interval4 (layers 4, 8, …, 32 are full attention; the restlinear_attention), 4 KV heads, head_dim 256, hidden_size 4096. ↩↩ -
NVIDIA, H100 Tensor Core GPU — H100 SXM: 80 GB HBM3, 3.35 TB/s memory bandwidth. ↩
Discussion
Have thoughts on this post? Share them below — questions, corrections, or your own experience are all welcome.