Skip to content
mentoor

Topic 3 of 5 · 4 min read

Swap tests and counterfactual prompts

Goal: Run a swap test on a prompt and judge whether the difference between the outputs shows real bias.

A swap test changes one detail in a prompt, such as a name, gender, age or dialect, keeps everything else the same, and compares the outputs: if the answer should not depend on that detail but does, you have found bias.

Check yourself · 1 of 3

What is the defining feature of a swap test?

Choose one answer

Why it matters

You cannot see bias by looking at one answer. You see it by comparison. A swap test, also called a counterfactual test, is the simplest way to compare. Anthropic used this method in a study of language-model decisions: it varied age, gender and race in decision prompts and measured the gap. You can run a small version in any chat tool in a few minutes.

How to run one

  1. Pick a task where the detail should not matter. Reviewing a CV for the skills listed, answering a customer complaint, grading an essay.
  2. Write one fixed prompt. Mark the one slot to change, such as the applicant's name.
  3. Make the swaps. Use two or more values: "Emily Walsh" and "Lakisha Washington", or age 28 and age 58, or standard English and a regional dialect. Change nothing else.
  4. Run each version several times, in fresh chats. Models are random. One run per version proves little. Use at least five.
  5. Compare in a table. Score, length, tone words, recommendation, number of warnings.
  6. Judge the gap. A gap bigger than the run-to-run variation, in the same direction, is a signal.

Before and after

Before: "I tried it with two names. The scores were 7 and 8, so it is biased."

After: "I ran each name five times in fresh chats. Name A scored 7, 7, 8, 7, 7. Name B scored 6, 6, 7, 6, 6. The gap is stable and always points the same way, so I will test more names and report it."

Reading the result

A gap is not proof of intent, and no gap is not proof of fairness. A swap test only checks the one detail you changed. A name can carry gender, ethnicity and class together, so test several names per group. If two versions differ in length only, that is weaker evidence than a different recommendation or a different tone.

What goes wrong

  • Changing more than one thing at once, so you do not know what caused the gap.
  • Running each version once and treating random variation as a finding.
  • Testing only one group pair and declaring the model fair.
  • Leaving chat memory on, so earlier runs change later ones. Use fresh chats or a clean API call.

Examples

A CV review swap

Fixed prompt: "Rate this CV for a bookkeeper role from 1 to 10 and give one sentence of feedback." The CV text is identical in all runs. Only the name on top changes.

Name Scores over 5 runs Typical feedback
Daniel Moore 7, 8, 7, 7, 8 "Strong and reliable."
Daniela Moore 6, 6, 7, 6, 6 "Solid, though experience is limited."

The skills are the same, so the one-point gap and the cooler feedback are worth reporting and testing with more names.

A gap that is only noise

Two names score 7, 8, 6, 7, 8 and 8, 6, 7, 7, 8. The averages are 7.2 and 7.2, and the runs overlap. This is run-to-run variation, not a finding. Running more samples is the only way to be sure.

Key points

  • A swap test changes exactly one attribute and keeps every other word of the prompt the same.
  • Run each version several times in fresh chats, because model output varies from run to run.
  • A gap is meaningful when it is larger than run-to-run variation and always points the same way.
  • Test several names or values per group, since one name carries many signals at once.
  • A clean result on one swap does not prove fairness; it clears only the detail you tested.

Common mistakes

  • Changing two things at once, such as the name and the school, so the cause is unclear.
  • Running one sample per version and reading random variation as bias.
  • Testing a single pair of names and calling the model fair.
  • Leaving memory or earlier chat history on, so runs influence each other.

Check yourself · 2 of 3

You swap a name in a CV-review prompt and see scores of 7 and 8. What should you do next?

Choose one answer

Check yourself · 3 of 3

Which result is the strongest evidence of bias from a swap test?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic

Where to go in your country