What is a counterfactual input swap test for an LLM prompt, and how do you run one on a yes-or-no screening prompt?
answer
- same input, one field changed
- audit study, model as the decider
- probability of yes, not the label
- labels and names both
- misses proxies and shifted weights
basics
~20 sA counterfactual swap sends the same input twice with only a name or stated group changed and compares the answers. On a yes-or-no screen, compare the probability of yes across many applications; a gap beyond noise traces to that field.
solid answer
~50 sA counterfactual swap test renders one input several times, changing only a name or stated group, and compares what comes back. It borrows from audit studies, where employers get matching applications that differ only in a protected characteristic. For a yes-or-no screen, take held-out applications, fill a placeholder with each group, and record the normalised probability of `yes`, since a hard label hides most movement. Take the log-odds difference from a baseline per application, then report the mean and an interval per group. Tamkin et al. (2023) did this across 70 hypothetical decisions, and their scale reads: at a 0.5 baseline, +1.0 means about 0.73. Repeat with names as well as labels and across paraphrases. A clean result shows only that the swapped field had little direct effect: it does not rule out proxies or a shifted weight on another field.
code
pseudocode · 17 linesbaseline = "group_a"
diffs = empty map of group -> list
for app in held_out_applications:
for phrasing in paraphrases:
p_base = p_yes(render(phrasing, app, group = baseline))
for group in other_groups:
# only the group field changes between the two prompts
p = p_yes(render(phrasing, app, group = group))
diffs[group].append({app: app, diff: log_odds(p) - log_odds(p_base)})
for group, rows in diffs:
report(group, mean(rows.diff), bootstrap_interval(rows.diff, resample_by = rows.app))
function p_yes(prompt):
yes, no = answer_probabilities(prompt, ["yes", "no"])
return clip(yes / (yes + no), 0.001, 0.999)go deeper
Recall the core of it: the same input twice, with only a name or stated group changed, then compare the answers.
Explain the steps: template, fixed direction, probability of yes, log-odds against a baseline, an interval per group. Say why a hard label hides most of the movement.
Show what a pass cannot prove: proxies, shifted weights on other fields and combined identities. Say which inputs and paraphrases you would add to close part of that.
Decide where the test sits: which prompts must have it, what gap blocks a release, and who pays to rerun it on each model change. A team has to set that threshold itself.
## What the test is A **counterfactual input swap** asks one question: if this same input had named a different group, would the answer have changed? You render one input several times, change only a name or a stated group, and compare the outputs. A 2023 survey of bias evaluation (Gallegos et al.) describes the general shape: pairs of sentences formed by perturbing a social group while keeping all other words. The design comes from **audit or correspondence studies** in the social sciences, where decision-makers judge applicant profiles that are identical except for a characteristic such as race or gender. Tamkin et al. (2023) apply that to language models, with the model as the decider. Because each pair shares everything else, differences between applicants do not enter the comparison. As a matter of reasoning, that makes it far more sensitive than comparing pass rates of two different applicant groups, which also differ in their facts. Group-rate analysis answers a different question and is covered with group fairness criteria. ## How to run it on a screening prompt 1. **Template the prompt.** Put placeholders where age, race and gender or a name appear. Keep everything else fixed. 2. **Fix the direction.** Make `yes` the good outcome for the subject, so a score has one meaning. Tamkin et al. ensure this for every question. 3. **Hold out real-shaped inputs.** Use applications you did not tune on, covering strong, weak and borderline cases. 4. **Record a probability, not a label.** Tamkin et al. take `p(yes) / (p(yes) + p(no))`. A label flips only near the decision boundary, so a hard answer hides most movement. If your model does not expose token probabilities, a common fallback is repeated sampling at fixed settings. 5. **Compare in log-odds against one baseline group,** per application. Probabilities near 0 or 1 squash differences, which is why Tamkin et al. work on the logit scale. 6. **Aggregate per group with an interval.** Their **discrimination score** is a regression coefficient on the log-odds scale. As their own reading aid: at a baseline probability of 0.5, a score of +1.0 corresponds to about 0.73. 7. **Vary what might matter.** Run explicit labels and names, and several paraphrases of the prompt. The cost is plain multiplication. In this scenario, 200 applications x 6 group variants x 3 phrasings is 3,600 calls, and it reruns on every model change. ## What Tamkin et al. found Scope first: 70 hypothetical yes-or-no decisions, kept from 96 generated templates after review, and one frontier model at one date. - **The direction was mixed.** Without any intervention, the model favoured women, non-binary people and non-white people in some settings and disfavoured older people. A test that only checks one direction would have missed half of this. - **Names and labels differ.** The effect for race and non-binary gender was larger when stated explicitly than when inferred from names, where it was much smaller though still statistically significant for all demographics except Black subjects. Test both; a labels-only run does not speak for names. - **Style changed the size more than the direction.** Patterns were largely consistent across rewrites, but emotional phrasing generally gave a larger magnitude. ## What a clean result does not show | A pass does not rule out | Why | |---|---| | A shifted weight on another field | Ganguli et al. (2023) note a race swap will not detect one legitimate field being weighted over another when groups differ on those fields | | Proxies | A swap changes the field you chose. Other text correlated with group stays as it was; Tamkin et al. flag proxy discrimination as needing more work | | Combined identities | Tamkin et al. model each characteristic singly, not race and gender interacting | | Other formats | They tested paragraph-long profiles, not long documents or dialogue | | Quality of the decision | Their questions have no ground truth, so a swap says nothing about whether answers are right | | Offsetting differences | As a matter of arithmetic, a mean near zero can hide large per-application differences in both directions, so look at the distribution too | Names also signal group imperfectly, and Tamkin et al. say choosing them remains difficult. One vocabulary trap: Ganguli et al. call their swap measure demographic parity, while the common group-level criterion of that name compares selection rates between groups. Say which one you mean; the group version belongs with fairness criteria. ## Where it fits Run it when a prompt reads inputs about people and returns a decision, a score or a ranking. A free-text writer has no probability of yes, so compare a property of the generated text instead, such as sentiment or a word count, across swapped inputs. The swap is a test, not a fix: it tells you whether a change such as balanced shots or an instruction moved the gap, and it should run beside an accuracy check.
- How is a swap gap different from a pass-rate gap between two groups of real applicants?A swap compares one application with itself, so the applicant's facts cancel and only the changed field differs. A pass-rate gap compares different people, whose facts and base rates may differ, so it can arise with no direct use of the field. They answer different questions, and a system can pass one and fail the other.
- The model shows no gap when the group is stated, but you used labels only. Are you done?No. Run names too, and paraphrases. In Tamkin et al. the size of the effect differed between stated labels and names, and between writing styles. Applications in production rarely state a group but often contain a name, so names may be the closer test of what the model really receives.
It works like a correspondence audit: the same CV goes to employers under two names, so any gap in callbacks cannot come from the CV. As in an audit, it says nothing about what else on the CV stands in for the name.
- A swap test passes when the yes or no label does not flip on a few examples
- No gap on a name swap proves the prompt does not discriminate
- A swap test should vary the applicant's facts so each group gets a typical profile
- Testing with explicit group labels also covers what happens with names
- The model can only be biased against historically disadvantaged groups, so test that direction alone