kb://library/in-context-learning2026-06-16

In-Context Learning

llmtransformersfew-shotmeta-learningmechanistic-interpretabilitypromptinginduction-headstask-vectors

In-Context Learning

What it is (intuition first)

Imagine you hand a coworker a sheet of paper with three solved examples:

sea otter => loutre de mer
cheese => fromage
peppermint => menthe poivrée
plush giraffe =>

They never studied French formally for this sheet, but they pattern-match the examples and answer girafe en peluche. They didn't learn in the usual sense — nobody adjusted their brain's wiring. They just read the examples and inferred the rule on the spot.

That is in-context learning (ICL): a large language model performs a new task by reading examples or instructions placed in its prompt — at inference time, with no gradient updates, no weight changes, no training. The "learning" happens entirely inside one forward pass, in the model's activations, and it evaporates the moment the context window scrolls past.

This is the single most surprising property of large language models, and it is why "prompting" became an engineering discipline instead of a curiosity. Three flavors, defined the way the GPT-3 paper defines them:

  • Zero-shot — you give only a task description, no examples. Translate English to French: plush giraffe =>
  • One-shot — one example, then the query.
  • Few-shot — a handful (typically 2–64) of examples, then the query.

Crucially, in all three there are zero gradient updates. The model's 175 billion (in GPT-3's case) weights are frozen. Everything the model "learns" about this task is read off the tokens in front of it. Contrast this with fine-tuning, where you actually run backpropagation and permanently change the weights (see [[lora-and-peft]] for the cheap version of that).

Why it matters

ICL is the capability that turned LLMs from "autocomplete" into "general-purpose task engine." Three consequences:

  1. One model, thousands of tasks, no retraining. Before GPT-3, adapting a model to a new task meant collecting a labeled dataset and fine-tuning. ICL collapses that loop to writing a prompt. The GPT-3 paper's headline result: few-shot prompting was "sometimes even reaching competitiveness with prior state-of-the-art fine-tuning approaches" — with no parameter updates.
  1. It is emergent and scale-driven. Small models barely do ICL; large ones do it well. GPT-3's central empirical finding was that the gap between few-shot and zero-shot performance widens as models grow — bigger models are not just better at tasks, they are better at learning tasks from context. This is one of the canonical "emergent abilities" tied to [[scaling-laws]].
  1. It is the substrate for everything downstream. Chain-of-thought prompting, [[tool-use-function-calling]], [[agentic-loops]], retrieval-augmented generation ([[graphrag]]), and few-shot evaluation harnesses ([[agent-evaluation]], [[llm-evaluation]]) all are ICL with a particular prompt structure. If you understand ICL, you understand why [[context-engineering]] is a real engineering field.

How it works (real mechanics)

The setup, formally

Give the model a prompt that is a sequence of input–output demonstrations followed by a query:

P = (x_1, y_1), (x_2, y_2), ..., (x_k, y_k), x_query

The model produces y_query by ordinary autoregressive decoding — predicting the next token given everything before it (see [[attention-and-transformers]] for the forward pass). No part of the model's parameters θ_model changes. So the puzzle is: how does a frozen function produce behavior that looks like it just trained on (x_1,y_1)…(x_k,y_k)?

There are two leading, complementary mechanistic theories. They are not rivals so much as descriptions at different altitudes.

Theory 1 — Induction heads (the mechanistic-interpretability story)

Olsson et al. (2022) traced a concrete circuit. An induction head is a pair of attention heads (one feeding the other across layers) that implements prefix-matching + copying:

Given a context containing ... [A][B] ... [A], attend back to the earlier [A], find what followed it ([B]), and raise the logit for [B] as the next token.

Mechanically, two properties:

  • Prefix matching — the head attends backward to a previous position whose preceding token matches the current token.
  • Copying — it then increases the output logit for the token that followed that earlier match.

Pseudocode for the behavior an induction head approximates:

def induction_head(context, current_token):
    # find earlier positions where the SAME token appeared
    matches = [i for i, t in enumerate(context) if t == current_token]
    if matches:
        j = matches[-1]                 # most recent prior occurrence
        return context[j + 1]           # copy whatever followed it
    return None                         # otherwise contribute nothing

This is "fuzzy" pattern completion — it generalizes beyond literal token copying to things like [A*][B*] ... [A] -> [B] (semantically similar A / B), which is enough to support translation, format-following, and analogy.

The killer evidence is the phase change. Early in training (across every model with more than one layer), induction heads suddenly form, visible as a distinct bump in the training-loss curve — and at that exact moment, the model's ability to do ICL jumps. Olsson et al. quantify ICL with a simple per-token heuristic:

ICL score = loss(500th token in context) − mean loss(50th token)

i.e. how much better does the model predict late tokens than early ones, purely because it has more context to lean on. A more negative score = stronger ICL. They marshal six arguments (co-occurrence at the phase change, co-perturbation when you move the phase change, direct ablation killing ICL in small models, etc.) linking induction heads to in-context learning. This is the flagship result of the [[mechanistic-interpretability]] program — a named, ablatable circuit tied to a macroscopic capability.

Theory 2 — ICL is implicit meta-learning / an in-context optimizer

A second line treats ICL as the model running a learning algorithm inside the forward pass. Garg et al. (2022) trained transformers from scratch on synthetic tasks: each prompt is a fresh function (a random linear map, a sparse linear function, a 2-layer net, a decision tree) sampled at training time, presented only as (x_i, f(x_i)) example pairs. Result: the trained transformer learns the function class in-context, with accuracy comparable to the optimal least-squares estimator for linear functions, and matching task-specific algorithms for the harder classes — and it still works under distribution shift between train and test prompts. The transformer is behaving like a general-purpose learner that was meta-trained to learn.

Akyürek et al. (2022) and von Oswald et al. push on which algorithm. They show, theoretically and empirically, that transformer ICL can implement standard algorithms — gradient descent and closed-form ridge regression — "by encoding smaller models in their activations, and updating these implicit models as new examples appear in the context." Roughly: a self-attention layer can approximate one step of gradient descent on an implicit regression objective, so a deep transformer is unrolled optimization. The "model" being fit lives in the residual stream, not the weights.

Theory 3 — Task vectors (the bridge between the two)

Hendel et al. (2023) offer a clean, testable middle ground. They hypothesize ICL separates into two phases:

  • a learning phase: the demonstrations S = {(x_i, y_i)} get compressed into a single task vector θ(S) — one activation vector at one layer;
  • an application phase: the model applies a rule f(x_query; θ(S)) to the query.

θ(S) ≈ ICL(S) → application. Concretely, you run the demos, read off the hidden activation at the position of a dummy separator, then — on a fresh prompt with no demos — patch that vector in, and the model performs the task anyway. The demonstrations were never needed at application time; their entire content had been squeezed into one vector. This maps ICL onto the textbook ML picture: "learn parameters from data, then apply." Follow-up work (function vectors, 2024–2025) localizes these to specific attention heads and finds that many such heads start life as induction heads during training before transitioning to the task/function-vector mechanism — so the three theories braid together: induction heads are the developmental seed, task/function vectors are the mature representation, and implicit-optimization is the algorithmic description.

Two distinct sub-abilities

A useful split (sharpened by 2024–2025 head-analysis work): ICL involves both

  • task recognition — figuring out which known task the prompt is asking for (works even with wrong labels in the demos, because the labels mostly signal the format/task, not new information), and
  • task learning — actually extracting the input→output mapping from the demo labels (degrades when labels are scrambled).

Different attention heads carry these. This explains the otherwise-baffling finding that randomizing demonstration labels often barely hurts classification accuracy: the demos are doing task recognition, not teaching.

Key ideas & tradeoffs

AxisIn-context learningFine-tuning
Weight updatesNone — frozen modelYes — backprop, permanent
Cost per new taskWrite a promptCollect data + train run
PersistenceLives only in the context windowBaked into weights
Data needed0–64 exampleshundreds–millions
Catastrophic forgettingImpossible (weights untouched)Real risk
Per-query costHigh — demos re-paid in tokens every callLow — demos amortized into weights
CeilingLower on truly novel/hard tasksHigher with enough data
AuditabilityPrompt is visible & editableBehavior hidden in weights

The honest summary: ICL trades training cost for inference cost and a lower ceiling. Few-shot demos are paid for in tokens (and latency, and context-window budget) on every single request. [[lora-and-peft]] sits in between — cheap parameter updates when ICL's ceiling or per-call cost becomes the bottleneck. The modern stack often uses both: light fine-tuning for the base behavior, ICL for the per-request specifics.

Honest caveats

  • Prompt sensitivity is brutal and real. Zhao et al. (2021) showed GPT-3's few-shot accuracy can swing "from near chance to near state-of-the-art" purely by changing the example ordering, the prompt format, or which examples you pick. Three named biases drive it: majority-label bias (favoring answer labels that appeared more often in the demos), recency bias (favoring labels near the end of the prompt), and common-token bias (favoring tokens that are simply frequent in pretraining). Their contextual calibration fix — probe the model with a content-free input like N/A, measure its baseline answer distribution, and divide it out — recovered up to 30% absolute accuracy. Takeaway: a "few-shot result" without reporting variance across orderings/formats is close to meaningless.
  • "Learning" is a loaded word. Mechanistically the weights never change. Whether ICL is really running gradient descent or merely behaviorally resembles it is partly a definitional fight. The gradient-descent equivalence is cleanest on toy linear-regression setups; it does not mean a 70B chat model literally runs GD internally on your prompt. Be skeptical of confident one-line mechanistic claims.
  • The mechanism is plural, not settled. Induction heads, task/function vectors, and implicit optimization are different lenses on overlapping phenomena. 2025 work ("In-Context Learning Without Copying", multi-phase circuit-emergence studies) shows induction-head copying is sufficient but not necessary — ICL exists in regimes where copying is suppressed. Anyone claiming "ICL = induction heads, full stop" is over-claiming.
  • It scales with model size — which means small models barely do it. ICL is an emergent ability; you cannot assume a 1B model few-shots like a 70B one (see [[small-language-models]]). Budget accordingly.
  • It is bounded by the context window and degrades inside it. More demos help only until the window fills, and even within it, attention to the middle of long prompts is weak (the "lost-in-the-middle" effect — see [[long-context]]). Demos also compete with retrieved content and instructions for the same token budget.
  • Evaluation contamination. Because ICL needs no training data, it's tempting to test on web-scraped benchmarks the model may have memorized. The GPT-3 authors themselves flagged data-contamination as a methodological hazard ([[benchmark-contamination]]).

How it connects to OpenAlice + the Academy ladder

ICL is the daily-bread mechanism of Alice. Every personality block, soul prompt, few-shot exemplar, tool schema, and retrieved memory we place in Alice's context is an act of in-context learning — we are steering a frozen model's behavior without retraining it. Concretely:

  • [[context-engineering]] is applied ICL: ordering, formatting, and selecting what goes into the window is exactly the knob Zhao et al. showed is decisive. The prompt-sensitivity results are why OpenAlice treats prompt composition as byte-sensitive, version-controlled, regression-tested code — not freeform text.
  • [[tool-use-function-calling]] and [[agentic-loops]] rely on the model inferring from in-context schemas and examples how/when to call a tool — ICL over a structured grammar.
  • [[agent-memory-systems]] are an ICL delivery mechanism: retrieved memories are injected as context so the model "learns" the user's history per-turn, with no weight update.
  • When ICL's ceiling or per-call token cost becomes the constraint, the escalation is [[lora-and-peft]] then full fine-tuning ([[rlhf-and-alignment]]) — moving behavior from the prompt into the weights.

Academy ladder

  1. Foundations — [[attention-and-transformers]], [[tokenization]], [[embeddings]]. You cannot reason about ICL without the forward pass and the residual stream.
  2. The phenomenon — read the GPT-3 paper's few-shot section; build a tiny few-shot harness and measure your own variance across example orderings to feel the Zhao et al. instability first-hand.
  3. The mechanism — Olsson et al.'s induction-heads piece, then the function-class / task-vector papers. Pair with [[mechanistic-interpretability]] to actually find an induction head in a small model.
  4. The application — [[context-engineering]], then [[agentic-loops]] and [[agent-evaluation]] — ICL in production, where prompt sensitivity stops being a curiosity and becomes a reliability problem you own.
  5. The boundary — [[scaling-laws]] (why size unlocks ICL), [[long-context]] (its limits), [[lora-and-peft]] (when to stop prompting and start tuning).

ICL is the closest thing LLMs have to a free lunch — but the bill arrives in tokens, variance, and a ceiling you can't prompt your way past. Know all three before you bet a system on it.