4.4.9Alignment, Prompting & RAG

In-context learning mechanisms

2,010 words9 min readdifficulty · medium3 backlinks

WHAT is in-context learning?

WHY does this even exist? During pretraining the model sees enormous text where a pattern established earlier in a document predicts later tokens (lists, tables, translations, Q&A threads). To minimise next-token loss it is forced to become good at "spot the pattern in the context, then continue it." ICL is that skill reused at inference time.


HOW does it work? Two mechanistic stories

Both stories are compatible: the model already contains the algorithm; the context selects and steers it.

Figure — In-context learning mechanisms

Derivation: why more demos help (Bayesian task inference)


Toy mechanism: attention as one gradient step


Worked examples


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a super-smart parrot that has read the whole internet. You don't retrain it. You just show it a few examples first: "cat→animal, rose→plant, dog→?" Because it has seen millions of little lists like this before, it figures out the game you're playing and finishes the pattern: "animal". It didn't study for a test — it just noticed the rule you were hinting at right there in front of it. That noticing-and-continuing is in-context learning.


Active recall

What is in-context learning?
A frozen LLM performing a new task by conditioning on demonstrations in the prompt, with no weight updates; the "learning" happens in the forward pass.
In ICL, what changes about the model's parameters?
Nothing — θ\theta stays fixed; only the transient activations depend on the context.
State the k-shot ICL objective.
y^=argmaxyPθ(yx1,y1,,xk,yk,xk+1)\hat y=\arg\max_y P_\theta(y\mid x_1,y_1,\dots,x_k,y_k,x_{k+1}).
Bayesian view: what do demonstrations do?
They shift the posterior p(cDk)p(c\mid D_k) over latent tasks, concentrating it on the true task.
Why does accuracy saturate with more shots?
Log-posterior-ratio grows kKL\approx k\cdot\mathrm{KL}, so p(cDk)1p(c^*\mid D_k)\to1 exponentially; extra demos give diminishing returns.
Why does a single GD step equal linear attention?
GD from W0=0W_0{=}0 gives y^=ηiyi(xixk+1)\hat y=\eta\sum_i y_i(x_i^\top x_{k+1}), the same form as attention with keys xix_i, values yiy_i, query xk+1x_{k+1}.
Why can flipped labels still give good ICL accuracy?
Demos mainly convey input distribution, label space, and format (task location); pretraining priors supply the mapping — until the task is novel/hard.
Zero-shot vs one-shot vs few-shot?
k=0,1,many demonstrations respectively.
Two biases that reveal ICL is conditioning not clean learning?
Recency bias (later demos weighted more) and majority-label bias.
ICL vs fine-tuning in one line?
Fine-tuning permanently edits weights; ICL is per-prompt, reversible conditioning.

Connections

  • Transformers and Attention — attention is the substrate that implements ICL.
  • Prompt Engineering — demo count, order, and format are the levers of ICL.
  • Chain-of-Thought Prompting — ICL extended with reasoning steps in the demos.
  • Retrieval-Augmented Generation (RAG) — retrieved passages act as context that steers the same conditioning mechanism.
  • Bayesian Inference — the task-posterior derivation.
  • Gradient Descent — the implicit-optimization interpretation.
  • Fine-tuning vs Prompting — when to change weights vs steer with context.

Concept Map

forces skill

requires

conditions on

defines

explained by

explained by

demos shift

update evidence

forward pass runs

concentrates with k

steers task

Pretraining on pattern-rich text

In-context learning ICL

Frozen weights theta fixed

k demonstrations plus query

k-shot zero one shot

Story 1 task location Bayesian

Story 2 implicit optimization

Posterior over latent task

Attention as gradient descent

Better predictions

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, in-context learning ka matlab hai: aap model ko train nahi kar rahe. Model ke weights bilkul frozen hain. Aap sirf prompt ke andar do-chaar examples (demonstrations) daal dete ho, aur model unko dekh kar "game samajh leta hai" aur pattern aage badha deta hai. Jaise "cat→animal, rose→plant, dog→?" — model bina kisi training ke bol dega "animal". Yeh sab ek hi forward pass ke andar ho raha hai.

Ab kyun hota hai? Pretraining ke time model ne internet par lakhon aisi lists, tables aur Q&A threads dekhe jahan pehle wala pattern baad ke tokens predict karta hai. Next-token loss kam karne ke liye model majboor tha ki "context mein pattern pehchano aur continue karo" — yehi skill inference time pe reuse hoti hai. Bayesian tareeke se sochein: model ke andar bahut saare latent tasks ka mixture hai, aur demos us posterior ko shift kar dete hain sahi task ki taraf. Jitne zyada saaf demos, utna posterior true task pe collapse — isliye accuracy pehle tez badhti hai, phir plateau ho jaati hai (exponential concentration, p(cDk)1p(c^*\mid D_k)\to1).

Ek gehri baat: attention actually ek chhota gradient descent step jaisa kaam karta hai. Keys ko inputs xix_i, values ko labels yiy_i, aur query ko test input maano — attention ka output iyi(xixk+1)\sum_i y_i (x_i^\top x_{k+1}) ban jaata hai, jo bilkul ek GD step ke barabar hai. Matlab model apne activations mein ek "virtual model" train kar leta hai, par asli weights kabhi nahi badalte.

Practical tip: labels galat ho jayein tab bhi ICL kaafi accha chalta hai, kyunki demos mostly format aur label-space batate hain — actual mapping pretraining prior se aa jaati hai. Lekin agar task naya/mushkil hai, tab correct labels zaroori ho jaate hain. Aur haan — demo ka order aur recency matter karta hai, majority label ki taraf bias hota hai. Isliye prompt design ek skill hai!

Go deeper — visual, from zero

Test yourself — Alignment, Prompting & RAG

Connections