Technical Guide

Running Counterfactual Name-Swap Bias Tests on a Live LLM in Production

A counterfactual name-swap test sends matched prompt pairs to the live model that differ only in a person's name, repeats each pair enough times to absorb sampling variability, and compares outputs with paired statistics. To run it safely in production, isolate the test traffic from real data and downstream actions, and retain the full configuration, inputs, outputs, and analysis as audit evidence. The test shows whether outputs differ. It does not show why.

Name-swap test protocol

Treat name-swap testing as a controlled experiment. The protocol has five stages, each covered in the sections that follow.

StageWhat it covers
1. DesignName sets and matched templates where only the name changes.
2. SampleRepeated calls per pair with recorded model and parameters.
3. IsolateSynthetic inputs, tagged traffic, no real downstream actions.
4. AnalyzePaired metrics, confidence intervals, and a baseline for noise.
5. RetainA reproducible evidence record for governance and audit review.

Design the name sets and matched templates

Build matched prompt pairs where only the person's name changes. Fix the hypotheses, name-selection rationale, templates, metrics, and thresholds before the run, so the plan is not adjusted after results are seen.

Control for non-deterministic responses

Repeat each pair enough times to absorb sampling variability. Make repeated calls for every pair, and record the model version and parameters on each call so the run can be reproduced. Include a control-pair baseline to show how much variation appears without any name change.

Isolate test traffic from production side effects

Run the test with synthetic inputs, tagged traffic, and no real downstream actions. Apply the following controls:

  • Synthetic inputs only: Build prompts from fabricated profiles and scenarios. Do not reuse real candidate, customer, or employee records.
  • Dedicated test identity: Use separate credentials so test requests are attributable and can be filtered or revoked independently.
  • Tool and action containment: Disable tool calls for the test identity or route them to stubs, so no tickets, emails, or record updates occur.
  • Traffic tagging: Mark every request so analytics, feedback loops, and any retraining or tuning pipelines exclude it.
  • Controlled load: Apply rate limits and schedule runs to avoid affecting live users or distorting capacity metrics.
  • Separate log store: Write test requests and responses to a distinct, access-controlled store that can be linked to the run record.

Choose metrics and statistical tests that fit the output type

Compare outputs with paired statistics, since each name-swapped prompt has a matched counterpart. Report paired metrics with confidence intervals, and read them against the control-pair baseline so that ordinary sampling noise is not mistaken for a difference.

Interpret results within the method's limits

A name-swap test measures whether outputs differ when only the name changes. Findings, sign-off, remediation actions, and the date of the follow-up re-test should be recorded with the run.

Evidence to retain for each test run

Retain a reproducible evidence record for governance and audit review. For each run, keep:

  • Test plan: hypotheses, name-selection rationale, templates, metrics, and thresholds fixed before the run
  • System configuration: model version, parameters, system prompt, and endpoint, as recorded on each call
  • Raw data: every prompt, response, timestamp, and sample index, linked to the run ID
  • Isolation record: test identity, tool and action restrictions, and traffic tags in force
  • Analysis: code or procedure, statistical outputs, confidence intervals, and control-pair baseline
  • Decisions: findings, sign-off, remediation actions, and the date of the follow-up re-test

Run bias tests with runtime controls in place

If you are planning name-swap testing against a live LLM deployment, talk to us about how runtime policy enforcement and audit logging can support safe execution and evidence capture.

Talk to an Expert