Part XXIII — Modern Architectures, Generative Models & LLM Engineering · Chapter 8

Prompting & in-context learning

Hook

Every chapter in this course has changed a model's behavior by changing its weights — gradient descent, fine-tuning, LoRA (short for Low-Rank Adaptation), all of it. What if you could change what a model does without touching a single weight, just by changing what you show it first?

Intuition

A handful of demonstrated (input, output) pairs, and a new query. The query attends to the demonstrations by how close their inputs are to its own — exactly Part IV's attention mechanism — and blends their outputs accordingly. Swap the demonstrations, and the same frozen mechanism gives a different answer.

Formalize

In-context learning treats the demonstrations in a prompt as an implicit, tiny training set, processed entirely within a single forward pass — no gradient step, no weight update:

y^(xq)=iK(xq,xi)jK(xq,xj)yi,K(xq,xi)=exp ⁣((xqxi)22σ2)\hat{y}(x_q) = \sum_i \frac{K(x_q, x_i)}{\sum_j K(x_q, x_j)} \cdot y_i, \qquad K(x_q, x_i) = \exp\!\left(-\frac{(x_q - x_i)^2}{2\sigma^2}\right)
  • xqx_q — the query input: the thing actually being asked about.
  • xix_i — the input of the ii-th demonstration example shown in the prompt.
  • yiy_i — the output demonstrated alongside xix_i.
  • y^(xq)\hat{y}(x_q) — the model's predicted output for the query, blended from the demonstrations.
  • K(xq,xi)K(x_q, x_i) — a similarity kernel: how close the query is to a given demonstration's input.
  • σ\sigma — the kernel's bandwidth, controlling how fast similarity falls off with distance.
  • i,ji, j — indices ranging over the demonstration examples in the prompt.
  1. Attention as kernel regression

    This is a kernel-regression view of what attention over demonstrations computes: weight each example by similarity to the query, then blend their answers.

  2. Weights depend only on inputs, never on outputs

    The weights depend only on the inputs xix_i — completely independent of what pattern the outputs yiy_i happen to encode.

Play

Both prompts share identical demonstration inputs (1,2,31, 2, 3), so both produce the exact same attention weights for the same query. The only thing that differs is which outputs were demonstrated — and that alone flips the sign of the final answer.

Worked example

Query xq=2.5x_q = 2.5, two different sets of demonstrations at the same three input values:

  1. The weights come from the inputs alone

    Both prompts use x{1,2,3}x \in \{1, 2, 3\} against query xq=2.5x_q = 2.5, with bandwidth σ=1\sigma=1:

    • K(2.5,1)=exp((2.51)2/2)=exp(1.125)0.325K(2.5,1) = \exp(-(2.5-1)^2/2) = \exp(-1.125) \approx 0.325
    • K(2.5,2)=exp((2.52)2/2)=exp(0.125)0.882K(2.5,2) = \exp(-(2.5-2)^2/2) = \exp(-0.125) \approx 0.882
    • K(2.5,3)=exp((2.53)2/2)=exp(0.125)0.882K(2.5,3) = \exp(-(2.5-3)^2/2) = \exp(-0.125) \approx 0.882
    • Sum 0.325+0.882+0.882=2.090\approx 0.325+0.882+0.882 = 2.090
    • Normalized weights:
      • 0.325/2.0900.1550.325/2.090\approx0.155
      • 0.882/2.0900.4220.882/2.090\approx0.422
      • 0.882/2.0900.4220.882/2.090\approx0.422

    Both prompts produce weights [0.155,0.422,0.422]\approx[0.155, 0.422, 0.422] — identical, because the kernel only ever looks at how far xqx_q is from each xix_i.

  2. 'Double' demonstrations blend toward roughly double

    With y={2,4,6}y = \{2, 4, 6\} demonstrated, the weighted blend is 0.155(2)+0.422(4)+0.422(6)0.311+1.689+2.534=4.5340.155(2) + 0.422(4) + 0.422(6) \approx 0.311 + 1.689 + 2.534 = 4.534 — close to the true doubled value of 55, though not exact, since a weighted average can only interpolate, not perfectly extrapolate a slope.

  3. 'Negate' demonstrations, same weights, opposite answer

    With y={1,2,3}y = \{-1, -2, -3\} demonstrated instead, the identical weights blend to 0.155(1)+0.422(2)+0.422(3)0.1550.8451.267=2.2670.155(-1) + 0.422(-2) + 0.422(-3) \approx -0.155 - 0.845 - 1.267 = -2.267 — close to the true negated value of 2.5-2.5, and of the opposite sign entirely from the "double" case.

Checkpoint

Find the prompt, between the two candidates, that steers the query toward a negative answer.

Pick a prompt to try it
Summary
y^(xq)=iwi(xq)yi— same wi, different yi, different answer\hat y(x_q) = \sum_i w_i(x_q) \cdot y_i \qquad\text{— same } w_i\text{, different } y_i\text{, different answer}

Nothing about the model changed between the two prompts — same weights, same attention mechanism, same query. The entire behavior change came from what was placed in the context window. That's the practical power of prompting: a frozen, already-trained model can be steered toward wildly different behaviors purely through what it's shown, which is why so much of modern LLM engineering happens at the prompt level rather than through retraining. The next chapter turns to what happens after a prompt is chosen: making that frozen model actually fast enough to serve.