Dive into Deep Learning · §18.3
Prompting and In-Context Learning
chat templates · reading a label · demonstrations · order and labels · calibration · probes
The problem
Qwen3-1.7B-Base is a base model: trained only to predict the next token.
| prompt | the base model continues with |
|---|---|
What is the topic of this news headline? Local team wins cup final |
The topic of this news headline is a local team winning a |
four solved headlines, then Headline: Local team wins cup final Topic: |
Sports, then another headline of its own |
The model continues a pattern of tokens. How much of a score does the pattern decide?
Goals
Running example: the headline “Local team wins cup final”, four topics (World, Sports, Business, Tech), 200 AG News titles, and two checkpoints of 1.72 billion parameters. Qwen3-1.7B-Base (base) was trained only to predict the next token. Qwen3-1.7B (instruct) was trained further on conversations and instructions.
Where this fits
| movement | section |
|---|---|
| open a checkpoint | Pretrained Models with Hugging Face |
| A Released Checkpoint in the Book’s Own Code | |
| measure it | Prompting and In-Context Learning |
| Scoring Language Models | |
| Evaluation Suites, Judges and Error Bars | |
| inside and outside | Interpretability |
| Models behind an API |
Prompts as token sequences
The chat template, a program shipped with the tokenizer, turns messages with roles into tokens and opens the reply with the generation prefix <|im_start|>assistant\n. Thinking is reasoning in a think block before the answer; enable_thinking=False writes an empty one.
What follows <|im_start|>assistant\n when enable_thinking is not given, for our two checkpoints and two models of the Qwen3.5 family:
| template of | continues with | thinks by default |
|---|---|---|
| Qwen3-1.7B | nothing | yes |
| Qwen3-1.7B-Base | nothing | yes |
| Qwen3.5-2B | <think>\n\n</think>\n\n |
no |
| Qwen3.5-4B | <think>\n |
yes |
With enable_thinking=False all four end in the empty block.
Pass enable_thinking explicitly. The base model ships a version of its sibling’s template: the column describes the file, not a model that was never taught to think.
Reading a label
p(y \mid x) = \frac{\exp(z_{t_y})}{\sum_{y'} \exp(z_{t_{y'}})}
A renormalized distribution sums to one however little probability it started from.
| word | id after Topic: |
id after a newline |
|---|---|---|
| World | 4337 | 10134 |
| Sports | 13180 | 40979 |
| Business | 8025 | 22727 |
| Tech | 17374 | 34097 |
Topic:: label mass 0.89 for the cup final, 0.11 to 0.16 for three other headlines. We type the space ourselves: 0.000.Sci/Tech is three tokens, so the label word is Tech.Check
One prompt without demonstrations: a line that names the four labels, then “Local team wins cup final”.
World Sports Business Tech label mass
instruct, chat template 1.000 0.000 0.000 0.000 0.378
instruct, plain text 0.999 0.001 0.000 0.000 0.985
base, chat template 0.782 0.004 0.183 0.032 0.000
base, chat template, no think block 0.384 0.327 0.133 0.156 0.019
Answer. Only the instruct rows. The base rows rest on a label mass of 0.000 and 0.02: a preference among words the model will not write. The instruct model’s greedy reply names World: the answer is its own, and wrong.
Few-shot prompting
| layout | a demonstration | the prompt ends with |
|---|---|---|
plain |
Headline: … Topic: Sports |
Topic: |
qa |
Q: … A: Sports |
A: |
chat_multi |
a user turn, then an assistant turn Sports |
the generation prefix and think block |
chat_single |
the plain text, in one user message | the generation prefix and think block |
chat_multi the answer to the query follows the empty think block, and the demonstrated answers do not.plain and chat is chat_multi, thinking off.The experiment
| factor | values |
|---|---|
| model | base, instruct |
| layout | plain, chat, chat without think block |
| demonstrations k | 0, 1, 2, 4, 8, 16, 32, 64 |
| instruction line | none, labels named, labels reversed |
| order | 4 orders of the same 8 |
| labels | fraction wrong; relabeled; natural or abstract words |
An effect: the 95% bootstrap interval of the paired difference excludes 0 and 4 of 5 draws agree in sign. The interval resamples items, not draws.
Number of demonstrations
| base model, plain layout | k = 0 | k = 4 | k = 8 | k = 64 |
|---|---|---|---|---|
| read-out over four labels, accuracy | 0.65 | 0.76 | 0.75 | 0.79 |
| generation, reply is a label word | 0.005 | 0.83 | ||
| generation, exact match | 0.005 | 0.655 |
Four demonstrations: about 10 points for the read-out, and from almost no label words to about two right replies in three.
Templates
Base model, 8 demonstrations:
| prompt | accuracy |
|---|---|
| plain text | 0.75 |
| chat template, empty think block | 0.26 |
| chat template, default ending | 0.77 |
<think>\n\n</think>\n\n means: prompt it in plain text.Instructions
No demonstrations, plain layout, accuracy of the read-out:
| instruction line | base | instruct |
|---|---|---|
| none | 0.65 | 0.68 |
| “Name the topic of each news headline: World, Sports, Business or Tech.” | 0.755 | 0.31 |
| the same, labels reversed | 0.70 | 0.485 |
World is 97.5% or 73% of its predictions.World? Not the read-out (the model writes it), not the place in the list, not the template. That leaves the sentence, and thinking.Instruct model, chat layout, no demonstrations; “…” stands for “World, Sports, Business or Tech”.
| instruction line | thinking | accuracy | most predicted |
|---|---|---|---|
| “Name the topic of each news headline: …” | off | 0.275 | World, 97.5% |
| “Which topic does this news headline belong to: …?” | off | 0.31 | World, 85.5% |
| “Classify the news headline into one of four topics: …” | off | 0.605 | World, 44.5% |
| the same, then “Answer with the topic only.” | off | 0.635 | World, 44.5% |
| “Name the topic of each news headline: …” | on | 0.70 | Business, 27% |
Telling a chat model its labels works. One sentence broke it: measure what a system line does.
No demonstrations, plain layout: mean label mass over the 200 titles, and accuracy of the read-out.
| instruction line | base: mass | accuracy | instruct: mass | accuracy |
|---|---|---|---|---|
| none | 0.016 | 0.65 | 0.008 | 0.68 |
| labels named | 0.84 | 0.755 | 0.685 | 0.31 |
Print the label mass beside a read-out. After raising it, measure accuracy again.
Check
Two prompts differ in correctness on 15% of the items and by 3 points in accuracy. How many items give a 95% half-width of 3 points when both prompts are scored on the same items? And on different items?
Answer. Paired: 1.96\sqrt{0.15/n} = 0.03 gives n \approx 640. Unpaired, accuracies near one half: 1.96\sqrt{0.5/n} = 0.03 gives n \approx 2{,}100 for each prompt. With 200 paired items the half-width is 5 points.
Sensitivity to the demonstrations
foo: 0.31 and 0.335 at best. Guessing: 0.25.Calibration
Read-out as evidence q from the headline x times a prior \pi induced by the rest of the prompt, with normalizer Z:
\log p(y \mid x) = \log q(x \mid y) + \log \pi(y) - \log Z(x)
Assume that a content-free input x_0 such as N/A returns the prior (the next slide tests this). Subtract it:
\log p_\textrm{cal}(y \mid x) = \log p(y \mid x) - \log p(y \mid x_0) + \textrm{const}
p(\cdot \mid x) = (0.6, 0.4) and p(\cdot \mid x_0) = (0.8, 0.2) give ratios (0.75, 2) and calibrated (0.27, 0.73). The prediction flips (Zhao et al., 2021).
One demonstration, plain layout:
| base | instruct | |
|---|---|---|
| share of the most predicted label, before | 0.52 | 0.44 |
| after calibration | 0.35 | 0.33 |
| accuracy, before | 0.64 | 0.70 |
| after calibration | 0.71 | 0.73 |
| draws in which accuracy rises | 2 of 5 | 4 of 5 |
Balanced demonstrations remove the cause.
In-context learning
32 sequences of 64 uniformly random tokens, each repeated once, through the base model:
32 sequences of 2 x 64 tokens; bound ln 151,643 = 11.93 nats
first copy: 14.25 nats per token (SD over sequences 0.20)
second copy: 0.33 nats per token (SD over sequences 0.19)
Behavioral evidence: the model copies from its context. No component is identified.
Prompt or probe
The prompt starts from what the model knows about the word Sports. The probe starts from nothing.
The probe reads the headline alone, 10 tokens. The prompt reads its k demonstrations again with every query.
| k labeled titles | prompt tokens per query | against the probe’s 10 | KV cache |
|---|---|---|---|
| 0 | 15 | 1.5 | 1.8 MB |
| 4 | 90 | 9 | 10 MB |
| 16 | 293 | 29 | 34 MB |
| 64 | 1,163 | 117 | 133 MB |
Check
Case A: 12 labeled headlines and 50 queries. Case B: 2,000 labeled headlines and a million queries a day. Which method for each?
Answer. A: the prompt. At 16 examples the probe trails by 17 points, and 50 queries make the longer prompt cheap. B: a fitted classifier. With 256 examples the probe already matches the 8-shot prompt, and it reads 10 tokens per query, not hundreds.
Next: the same models under different ways of reading an answer.