Prompting and In-Context Learning

Dive into Deep Learning · §18.3

Prompting and In-Context Learning
chat templates · reading a label · demonstrations · order and labels · calibration · probes

Four Solved Examples Change What a Base Model Writes

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
  • Solved examples in a prompt are demonstrations. Acquiring a task from them, with no weight change, is in-context learning (ICL).
  • On 200 test headlines, 1 reply is a label word without demonstrations. With four, 83% are, and 65.5% are right.

The model continues a pattern of tokens. How much of a score does the pattern decide?

By the End of This Deck You Can

Goals

  • read a chat template as the token ids a model receives;
  • read a label from one next token, and check the label mass behind it;
  • design a paired few-shot experiment and read its intervals;
  • say what the number, order and labels of demonstrations and one line of instruction do to accuracy;
  • correct a prompt’s label prior, and compare a prompt with a probe at equal labeled data.

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.

The Prompt Is the First Thing a Score Depends On

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
  • Uses: a pinned loader and a greedy generator for released checkpoints.
  • Skips: answers of several tokens, reasoning before the answer, fine-tuning.

A Chat Model Reads Token Ids, and the Template Chooses Them

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.

The Default Thinking State Belongs to Each Template File

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.

A Language Model Becomes a Classifier by Reading One Next Token

Reading a label

p(y \mid x) = \frac{\exp(z_{t_y})}{\sum_{y'} \exp(z_{t_{y'}})}

  • x: the headline, inside a prompt. t_y: first token of the word for label y. z_t: logit of token t at the next position. The sum runs over the labels. This is the read-out.
  • Label mass: the probability that the full softmax gives to the label tokens together. The formula divides it out.
  • Four solved headlines, then the cup final: Sports 0.999, on a label mass of 0.89.

A renormalized distribution sums to one however little probability it started from.

One Label Word Is Two Different Tokens

word id after Topic: id after a newline
World 4337 10134
Sports 13180 40979
Business 8025 22727
Tech 17374 34097
  • A token carries its leading space: no id is shared between the two kinds of prompt.
  • Prompt ends in 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.
  • The dataset’s class name Sci/Tech is three tokens, so the label word is Tech.

Check: Which of These Read-Outs Is the Model’s Answer?

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.

One Set of Demonstrations Has Four Layouts

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
  • k demonstrations make a k-shot prompt. With none it is zero-shot.
  • In chat_multi the answer to the query follows the empty think block, and the demonstrated answers do not.
  • Separators, spacing and casing move accuracy by many points (Sclar et al., 2024).
  • Below, plain is plain and chat is chat_multi, thinking off.

Every Comparison Is Paired: Same 200 Items, Five Draws

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
  • 200 AG News test titles, 50 per class.
  • A draw: one random choice of demonstrations. 5 draws.
  • Unpaired interval at 200 items: \pm 7 points. Paired, only the items on which two prompts differ add noise.

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.

Demonstrations Teach the Form of the Answer First

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
  • Read-out, 0 to 4: +0.11, interval 0.06 to 0.15. 64 over 8: +0.045, interval 0.01 to 0.08.
  • The read-out asks which label the model prefers. Generation asks whether it names a label at all.
  • At k = 0 the four labels hold under 2% of the probability.

Four demonstrations: about 10 points for the read-out, and from almost no label words to about two right replies in three.

Four Tokens of an Empty Think Block Drop the Base Model to Chance

Templates

Base model, 8 demonstrations:

prompt accuracy
plain text 0.75
chat template, empty think block 0.26
chat template, default ending 0.77
  • 0.26 is guessing among four labels.
  • Without the block the template costs nothing: +0.03 against plain text, interval -0.01 to 0.06.
  • The base model reads the chat layout. It was never taught what <think>\n\n</think>\n\n means: prompt it in plain text.

One Instruction Line Helps the Base Model and Misleads Its Sibling

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
  • Base: the line alone equals 8 demonstrations (0.747).
  • Instruct, chat layout: 0.275 or 0.37 by label order. World is 97.5% or 73% of its predictions.
  • Why World? Not the read-out (the model writes it), not the place in the list, not the template. That leaves the sentence, and thinking.

Reworded or Allowed to Think, the Instruct Model Classifies Again

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%
  • Off: the label is the next token. On: the first line of the reply after the think block.
  • “Classify”: +33 and +36 points. Thinking: +42, at a median of nearly 300 generated tokens.

Telling a chat model its labels works. One sentence broke it: measure what a system line does.

Label Mass Says Whose Answer a Read-Out Is, Not Whether It Is Right

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
  • Low mass: a preference among words the model will not write. It can be well informed.
  • High mass: the model’s answer. For the instruct model it is the worse of the two.
  • Instruct, chat layout, no line: mass below 0.01 even with 8 demonstrations. With the line and 4 demonstrations: 0.99 (one draw each).

Print the label mass beside a read-out. After raising it, measure accuracy again.

Check: How Many Items for a Half-Width of 3 Points?

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.

Order Alone Changes One Prediction in Eight

Sensitivity to the demonstrations

  • The same 8 demonstrations in 4 orders, for 5 draws (columns of dots).
  • Predictions that flip between two orders: 13% (base), 12% (instruct). Scoring the same prompts twice flips none.
  • Standard deviation of accuracy over orders: 0.033 and 0.023, more than if no order were better (outcomes shuffled among orders, p \le 0.004).
  • One draw of the base model: 0.655 to 0.78.

Wrong Labels Cost Accuracy, but Word Meaning Beats Eight Examples

  • Natural words, every label wrong: -0.30 (base), -0.22 (instruct).
  • Three labels in four wrong: the gain of 8 demonstrations is gone. Min et al. (2022) found that random labels barely hurt.
  • Abstract words such as foo: 0.31 and 0.335 at best. Guessing: 0.25.
  • Each label swapped for a fixed other one (open circles): true label in half of the items, demonstrated label in 0.16 (base), 0.14 (instruct).

Contextual Calibration Divides Out the Label Prior of the Prompt

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

Calibration Balances a One-Shot Prompt; Accuracy May Not Follow

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
  • In 9 of 10 draws the content-free prior gives the demonstrated label 0.74 to 0.98: more than its pull on real headlines.
  • Calibration overcorrects: that label falls to 7 to 17% of the base model’s predictions.

Balanced demonstrations remove the cause.

A Released Model Copies a Repeated Random Sequence

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)
  • First copy: no model can average below \log V = 11.93 nats for V = 151{,}643 equally likely tokens.
  • Second copy: 0.33 nats, a geometric-mean probability of about 0.7 for the right token.
  • The same behavior in the task: one demonstration’s label takes 41 to 65% of the predictions.

Behavioral evidence: the model copies from its context. No component is identified.

The Prompt Beats a Plain Probe at Every Size up to 64 Examples

Prompt or probe

  • Probe: a linear classifier on the frozen base model’s mean hidden state for the headline alone.
  • Lines: means of 5 draws. Bands: their range.
  • Prompt (blue) minus probe (red): 0.365 at 4 examples, 0.17 at 16, 0.07 at 64 (interval 0.03 to 0.11).
  • At 256 examples the probe reaches 0.76, as the 8-shot prompt does.

The prompt starts from what the model knows about the word Sports. The probe starts from nothing.

A 16-Shot Prompt Costs 30 Times the Tokens of a Probe

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
  • Cache per token: keys and values of 8 heads of width 128 in 28 layers, 2 \cdot 28 \cdot 8 \cdot 128 numbers of 2 bytes.
  • The demonstrations are a prefix shared by all queries. Cached once, a query costs about its own tokens.

Check: Prompt or Probe?

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.

Wrap-up

  • A prompt is token ids. The template chooses them, and four tokens of an empty think block took the base model from 0.77 to 0.26.
  • A label read from one next token needs the right label token and a label mass. With little mass it is a preference, not an answer.
  • Four demonstrations: +10 points in the read-out, 0 to 0.655 exact match in generation. One instruction line: +10 points for the base model, -37 for the instruct model, until the line says “Classify” or the model may think.
  • Order flips one prediction in eight. Word meaning beats eight consistent examples. Calibration balances a one-shot prompt.
  • The prompt beats a plain probe up to 64 labeled examples, at 30 times the tokens for 16 shots.

Next: the same models under different ways of reading an answer.