Activation Patching
Activation patching is a mechanistic interpretability technique that isolates the causal contribution of specific internal states in a neural network. A researcher runs a model on two different inputs, captures the intermediate activations (the numeric values at a particular layer or neuron) from the first run, transplants them into the second run at a chosen point, then observes how the output changes. If swapping those activations shifts the output meaningfully, that component is considered causally important for the behaviour under study.
Think of it like swapping one ingredient between two batches of a recipe mid-cook. If changing only the spice from batch A into batch B makes batch B taste like A, you have isolated spice as the key differentiating factor—without having to change anything else.
A typical workflow patches the residual stream at a single layer to test whether a token's representation drives a factual recall:
# Pseudocode using TransformerLens
original_logits, cache = model.run_with_cache(clean_prompt)
patched_logits = model.run_with_hooks(
corrupted_prompt,
fwd_hooks=[(f"blocks.{layer}.hook_resid_post",
partial(patch_activation, pos=subj_pos, cache=cache))]
)
The difference in the target-token probability between the two runs gives a patch score that quantifies each component's causal importance.
Activation patching connects closely to Attention (attention heads are among the most common targets), Transformer internals, Embedding representations, and the broader goals of AI Alignment research. It also underpins newer variants such as path patching and causal tracing, which are used to study Hallucination sources.
You will encounter activation patching in mechanistic interpretability papers—most notably Meng et al.'s ROME work on factual associations—and in tools like TransformerLens, which is built around making such interventions straightforward to run.