1 / 18
1
ComplianceGPT Lab · AI Innovation & Diffusion REU 2026

LLM Foundations in Practice

From next-token predictor to research instrument — the bridge between Week 2's transformer math and Week 3's prompting work

Week 2
Attention, tokenization, forward pass
Today
Decoding · tuning · scale · limits
Week 3
Prompting strategies
Later
Agentic pipelines · RAG
2

Where We Left Off, Where We're Going

You already know how a transformer computes one forward pass. Today is about what happens around that forward pass to turn it into something you can talk to.

You already know (Week 2)

  • Tokenization — text → integers
  • Self-attention — every token attends to every token
  • Positional encoding, Q/K/V, the full forward pass
  • Gemma3-4B's real architecture & parameter count

Today adds

  • How raw next-token probabilities become one chosen word (decoding)
  • How a base model becomes an assistant that follows instructions
  • What "context window" really limits, and why prompts are long
  • Why bigger models cost more and fail differently, not just "less"
3
Part 1 — From Predictor to Assistant

Three Training Stages, One Set of Weights

The forward pass you studied in Week 2 never changes. What changes across these stages is what the model was trained to predict.

1. Pretraining

Predict the next token over trillions of words of raw internet/book/code text. No notion of "question" or "answer" — just completion.

"The capital of France is" → "Paris"

2. Instruction Tuning

Fine-tune on (instruction, good response) pairs so the model completes instructions, not just text, and adopts a chat format.

"Summarize this." → does it, not "Summarize what?"

3. RLHF / Preference Tuning

Humans (or a reward model) rank multiple candidate responses; the model is nudged toward the ranked-higher style — helpful, harmless, on-format.

Same facts, safer / more useful phrasing

Every model we benchmark — Gemma, Llama, Qwen, GPT-OSS, Claude — has been through some version of stages 2 and 3. That is why you can prompt them in plain English at all.

4
Part 1 — From Predictor to Assistant

The Chat Template — What's Really Under the Hood grounded in our code

There is no "system / user / assistant" data structure inside the model. It's still one flat token sequence — with special marker tokens the instruction-tuned model was trained to respect.

## what you write in connector/llm1_extractor.py system_prompt = "You are a HIPAA disclosure extraction engine..." user_msg = f"Scenario: {question}" ## what actually gets tokenized and fed to the forward pass <start_of_turn>system You are a HIPAA disclosure extraction engine...<end_of_turn> <start_of_turn>user Scenario: Dr. Smith faxes records to...<end_of_turn> <start_of_turn>model

The model then does exactly the next-token prediction from Week 2 — it just happens to have been trained so that the highest-probability continuation after <start_of_turn>model looks like a helpful reply instead of more internet text.

5
Part 2 — Decoding

The Forward Pass Ends in a Probability Distribution. Then What?

Every step, the model outputs one probability over the entire vocabulary (~150k tokens for Gemma/Qwen). Decoding is the separate algorithm that turns that distribution into an actual chosen token.

Greedy decoding

Always pick the single highest-probability token. Deterministic — same input, same output, every time.

Used for: extraction, classification, anything you need to reproduce and audit.

Sampling

Draw randomly from the distribution instead of always taking the max. Same prompt can give different answers on different runs.

Used for: creative writing, brainstorming, chat — anywhere variety beats reproducibility.

6
Part 2 — Decoding

Temperature, Top-k, Top-p — the Knobs on Sampling

ParameterWhat it doesEffect at extremes
temperatureRescales the distribution before sampling. Divides logits by T before softmax.T→0: greedy. T→∞: uniform random token.
top_kOnly sample from the k highest-probability tokens.k=1: greedy. k=vocab size: no restriction.
top_pSample from the smallest set of tokens whose probabilities sum to p ("nucleus").p=1.0: no restriction. p small: very conservative.

Why every extraction call in our pipeline uses temperature 0

We need the same scenario to produce the same extracted facts every time we re-run an experiment — otherwise "accuracy" isn't measurable and a bug fix can't be verified. Reproducibility is a research requirement, not a preference. This is exactly what week3_prompting.ipynb asks you to confirm empirically on Wednesday.

7
Part 3 — Context, Memory, Scale

The Context Window Is the Only Memory the Model Has

Between API calls, the model remembers nothing. Every fact it uses — your system prompt, the scenario, few-shot examples, prior turns — must physically fit inside one token sequence, every single call.

What consumes context

  • System prompt (ours: 150+ lines of extraction rules)
  • Few-shot examples, if you add them
  • The scenario text itself
  • Retrieved passages, if using RAG
  • The model's own output, as it's generated

What happens near the limit

  • Hard cutoff: oldest content silently dropped or the call errors
  • "Lost in the middle": models attend less reliably to content buried in a long context, even before the hard limit
  • Cost and latency scale with context length — longer prompts are not free
8
Part 3 — Context, Memory, Scale

In-Context Learning: "Training" Without Updating a Single Weight

When you show the model 2 worked examples before your real question (few-shot), nothing in the network's parameters changes. The model is pattern-matching the shape of the prompt itself, at inference time.

Example 1: Scenario: A nurse tells a coworker about a patient's diagnosis for treatment purposes. JSON: {"purpose": "treatment", "phi_involved": true} Example 2: Scenario: A hospital sells patient records to a marketing firm. JSON: {"purpose": "marketing", "phi_involved": true} Now extract: Scenario: {question} // the model infers the JSON "shape" from the 2 examples above

This is the mechanism behind everything you'll do in Wednesday's few-shot exercises — you are not teaching the model new facts, you are teaching it the format and granularity of a good answer.

9
Part 3 — Context, Memory, Scale

Does Bigger Actually Mean Better? verified — RQ5

Real accuracy on our 137-row GoldCoin-HHS benchmark, same prompt/strategy, five models spanning two orders of magnitude in parameter count.

ModelParamsAccuracyMacro-F1Rank
Gemma2:2B2B78.2%0.7785th
Llama3.1:8B8B84.7%0.8394th
GPT-OSS:20B20B90.0%0.8973rd
Qwen2.5:72B72B92.7%0.9192nd
Claude-Sonnet-4.6~200B*94.2%0.9381st

Roughly monotonic here — but notice the curve is flattening: 2B→8B gained 6.5pp, 20B→72B gained only 2.7pp. *Proprietary size estimated. This is exactly the kind of data your own project could extend or complicate.

10
Part 3 — Context, Memory, Scale

Scale Is Not Free — Why We Rent GPUs for Qwen

Small models (Gemma2:2B)

  • Run locally, on a laptop CPU or small GPU
  • Fast — seconds per scenario
  • Weakest raw accuracy of the five

Large models (Qwen2.5:72B)

  • Needs an 80GB-class GPU — we rent one on vast.ai per experiment
  • Mean latency ~67 seconds per scenario (vs. ~1–5s for small models)
  • Strongest open-weight accuracy of the five, but ~13× the wait

This is a real engineering decision every project makes: your Week 4–5 experiment plan has to budget for the fact that a 137-row run on Qwen takes roughly 2.5 hours of wall-clock GPU time, not seconds.

11
Part 4 — Where Models Fail

Hallucination Isn't a Bug in the Usual Sense

The model was trained to always produce a fluent next token. It has no built-in "I don't know" state — it will complete a JSON field even when the scenario never mentions it.

What this looks like in extraction

  • Inventing a purpose the scenario never states
  • Filling extra_facts with plausible-sounding but unsupported predicates
  • Confidently citing the wrong subsection of §164

Why confidence ≠ correctness

Week 2's analysis already showed this on our own data: the model's stated confidence does not reliably predict whether the verdict is right. A wrong answer can be delivered exactly as fluently as a right one.

12
Part 4 — Where Models Fail

Open-Weight vs. Proprietary — Why It Matters for Research

Open-weight (Gemma, Llama, Qwen, GPT-OSS)Proprietary API (Claude)
Where it runsYour GPU / rented GPU, via OllamaSomeone else's servers
ReproducibilityFull — same weights foreverProvider can silently update the model
Cost modelCompute time (rent a GPU)Per-token API billing
InspectabilityCan quantize, fine-tune, inspect activationsBlack box — text in, text out

We evaluate both for a reason: open-weight models are what a compliance officer could actually self-host and audit; the proprietary model is our accuracy ceiling reference point.

13

Recap — Five Ideas to Carry Into Wednesday

  1. Instruction-tuning + RLHF is why plain-English prompts work at all
  2. Decoding parameters (temperature, top-k/p) control determinism — extraction wants temperature 0
  3. The context window is the model's entire memory — few-shot examples compete for that space
  4. Scale correlates with accuracy but with diminishing returns and real dollar/time cost
  5. Hallucination is the model doing exactly what it was trained to do: keep talking fluently
14

This Is Why Prompting Is an Engineering Discipline

You cannot open the model up and fix a wrong weight. The prompt — system instructions, examples, formatting constraints, reasoning scaffolding — is the only lever you have to shape behavior from outside the black box.

Zero-shot
Few-shot
Chain-of-thought
Wednesday's session
15

Looking Further Ahead — When One Prompt Isn't Enough

Every technique today assumed one call to the model. Our actual production pipeline calls the LLM up to four times per scenario, checking its own work in between.

Coming later: Agentic Systems

How connector/llm1_extractor.py's extract_with_reflection() turns a single extraction call into a four-pass self-checking loop — and why that loop helps a small model enormously but can backfire on a large one if you're not careful with retrieval.

16

Today — Read, Don't Code Yet

No notebook exercise today. Instead:

Before Wednesday

  • Skim connector/llm1_extractor.py lines 1–90 (system prompt setup)
  • Pick 1 wrong prediction from your Week 2 batch results to bring Wednesday

Wednesday's live notebook

notebooks/week3_prompting.ipynb — you'll write zero-shot, few-shot, and chain-of-thought prompts against that exact failure case.

17

Questions to Sit With

18

You Now Know the Whole Machine

Architecture (Week 2) + generation mechanics (today) = everything that happens before a single prompting choice you make on Wednesday even matters.