Skip to content
learn
  1. Learn
  2. Artificial Intelligence
  3. AI Safety, Security and Governance

AI AI-13.05

Bias and fairness in LLMs

Where bias in language models comes from, how to measure it with paired prompts that change only a name, and which mitigations work for sensitive uses.

Intermediate 9 min Last reviewed

The big idea

A lettings agency uses a language model to pre-screen rental applications. The prompt describes each applicant: job, income, references. Nobody told the model to consider anything else. Yet if you send the same application twice, once with one name and once with a name more common in a different group, the model's answer can change. Nothing in the prompt is wrong; the difference comes from patterns the model learned.

That is biasA systematic difference in how a model treats people or describes groups that is not justified by the task, for example different answers to the same request depending on a name, gender or age. in a language model. It shows up in two main ways: in decisions (approve or reject, rank, score), where one group is treated worse or better than another with identical facts, and in generated text (descriptions, stories, completions) that repeat stereotypes.

Think of a hiring manager who reads thousands of old files before starting a new job. They never decide to be unfair, but if the old files mostly showed one kind of person in senior roles, that pattern seeps into their sense of who "looks right". The fix is not to tell them to be fair once. It is to test their decisions with matched cases, give them clear criteria and check the results.

How it works

Where bias comes from

  • Training data. Models learn from huge amounts of human writing, which reflects historical inequality and stereotypes. Even word embeddings trained on news articles were shown to encode gender stereotypes, associating occupations such as receptionist with women.
  • Who is represented. Groups, dialects and languages that are rare in the data get less accurate and less nuanced treatment.
  • Fine-tuning and feedback. The people who write examples and rate answers bring their own judgements, and the model learns those too, in whichever direction they lean.
  • The deployment. The prompt, the retrieved documents and the task framing all matter. A model asked to "pick the best candidate" from free text will use whatever signals it finds, including names, addresses and hobbies.

Measure with paired prompts

The most direct test for decisions is a counterfactual evaluationAn evaluation that sends pairs of inputs identical except for one attribute, such as a name or a stated age, and measures how much the output changes.: hold everything constant and change only the attribute you are testing.

A counterfactual bias evaluation Step 1: write decision templates with placeholders for name, age or gender. Step 2: fill each template several times, identical except for the demographic detail. Step 3: send every version to the model and record the probability of a yes. Step 4: compare within each pair: the average gap per group, how many decisions flip, and whether the pattern holds across templates. scores template [NAME], 34, nurse version A name from group A version B name from group B model P(yes) compare pairs gap, flips, consistency report per group, per task Figure 1. Change one detail, keep everything else fixed, and measure the difference (glowing). Step through it.
  1. Write templates Realistic decision prompts for your use case, with a placeholder for the attribute under test: a name, an age, a stated gender.
  2. Fill them in pairs Each template becomes several prompts that differ only in that detail. Names test what the model infers; explicit attributes test what it does when told directly.
  3. Score every version Record the probability of yes, or the score, rank or text the model returns, for every version.
  4. Compare within pairs Average the gap for each group, count decisions that flip, and check whether one group is favoured across most templates or only in a few.

Because everything else is identical, any difference within a pair comes from the attribute. Report three things: the average gap for each group (on a log-odds scale so small and large probabilities compare fairly), the number of flipped decisions, and consistency, whether one group is favoured across most templates or only a few. Bias can run in either direction: published evaluations have found models both favouring and disfavouring particular groups.

For generated text, benchmarks ask questions where a stereotype would supply an answer the context does not. The BBQ benchmark, for example, describes two people from different backgrounds in a short scene, such as two customers at a computer store, and asks a question the scene does not answer, such as which of them has never used a computer. The right answer is "unknown"; a model that picks a person along stereotyped lines shows bias. Public benchmarks are a starting point. Your own templates, in your own domain, are what tell you whether your feature is fair.

Mitigate in layers

  • Remove what the task does not need. If the decision should not depend on names, ages or photos, leave them out of the prompt. This removes the simplest path for bias, although other details (an address, a school, a hobby) can still act as proxies.
  • State the criteria and the rule. Give the model explicit criteria and say that demographic characteristics must not influence the decision. In one published study, prompt instructions of this kind brought measured discrimination close to zero while keeping decisions highly correlated with the originals.
  • Improve data and training. Model developers balance training data, write rating guidelines and train against biased outputs. You can do the same in your own fine-tuning data.
  • Keep people in the loop for high-stakes uses. Hiring, lending, housing, insurance and medical decisions need human review, an appeal path and regular re-testing. In many places they also come with legal duties ( Guardrails covers review tiers).

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

  1. Set the name effect to 0. Every pair overlaps and nothing flips. Now set it to 0.6, a modest push. How many of the 40 decisions flip, and which applications are they: strong, weak or borderline?
  2. At 0.6, look at the approval rates for the two groups. Would a manager who only reads the overall approval rate notice a problem?
  3. Add the instruction. The lab assumes it shrinks the effect to 15 percent of its size. How many flips remain at 0.6, and at 2.0? Why do you still need to test after adding the instruction?

The same comparison in Python, on six made-up templates:

pairs.pyruns with python3 (standard library)
# Counterfactual bias check: send each decision prompt twice, identical except
# for the applicant's name, and compare the model's probability of "yes".
# The probabilities below are made up for illustration; in practice they come
# from the model (for example the normalized probability of the token "yes").
from math import log

def logit(p):
    return log(p / (1 - p))

# template -> (P(yes) with a group-A name, P(yes) with a group-B name)
results = {
    "rent apartment":   (0.81, 0.74),
    "approve loan":     (0.55, 0.47),
    "invite interview": (0.62, 0.58),
    "raise credit":     (0.35, 0.33),
    "grant visa":       (0.70, 0.71),
    "approve return":   (0.92, 0.91),
}

gaps = {t: logit(a) - logit(b) for t, (a, b) in results.items()}
flips = [t for t, (a, b) in results.items() if (a >= 0.5) != (b >= 0.5)]
favours_a = sum(g > 0 for g in gaps.values())

for t, g in gaps.items():
    print(f"{t:17} logit gap {g:+.2f}")
print(f"mean logit gap (A minus B): {sum(gaps.values()) / len(gaps):+.2f}")
print(f"templates favouring A: {favours_a} of {len(gaps)}")
print(f"decisions that flip at 0.5: {flips}")

Output

rent apartment    logit gap +0.40
approve loan      logit gap +0.32
invite interview  logit gap +0.17
raise credit      logit gap +0.09
grant visa        logit gap -0.05
approve return    logit gap +0.13
mean logit gap (A minus B): +0.18
templates favouring A: 5 of 6
decisions that flip at 0.5: ['approve loan']

The average gap is small, +0.18 in log-odds, and five of six templates favour group A. Only one decision flips, the loan, because it is the one sitting near the threshold. Small, consistent gaps matter most exactly where the decision is close, which is where real applicants are hardest to judge.

Going deeper

Choosing a fairness measure

The classic metrics from Fairness and bias still apply when a language model makes the decision. Demographic parity compares approval rates; equalized odds compares error rates given the true outcome, which needs labelled data. Counterfactual pairs answer a different question: does this attribute, by itself, change the answer? They are easy to run without labels, but they do not tell you whether the base rates were fair to begin with. Use both where you can, and decide in advance which measure matters for your use case.

Names are an imperfect probe

Names stand in for gender, ethnicity and age only statistically. Use several names per group, check that effects do not come from one unusual name, and test explicit attributes as well. The same caution applies to proxies in real data: a postcode or a school can carry the same signal as a name.

Bias in generated text

For writing tasks, sample many outputs per prompt and look at patterns: which pronouns appear for which jobs, which adjectives describe which groups, whether quality or length differs by dialect of the request. An LLM judge can label outputs at scale ( LLM-as-judge ), but check its labels against people first, because the judge has biases of its own.

Trade-offs

OptionWins whenLoses when
Remove demographic details from prompts Simple; removes the most direct path Proxies remain; some tasks legitimately need the data
Instruct the model not to use them Cheap; can sharply reduce measured gaps Not guaranteed; must be re-tested on every model change
Counterfactual pair evals No labels needed; isolates one attribute Names are imperfect proxies; says nothing about base rates
Outcome metrics on real data Measures what actually happens to people Needs labels and demographic data you may not hold
Human review for high-stakes decisions Accountability and an appeal path Slow; reviewers bring their own biases

Common mistakes

  • Testing a few examples by hand instead of hundreds of matched pairs across realistic templates.
  • Looking only at overall accuracy or approval rate, which can hide a consistent gap between groups.
  • Assuming that removing names or gender from the prompt removes bias, when addresses, schools or writing style carry the same signal.
  • Fixing bias with a prompt instruction once and never re-running the evaluation after model or prompt changes.
  • Using a language model as the sole decision-maker for hiring, lending or housing.

In the wild

Tamkin et al., Anthropic, 2023 (paper)

Evaluating and Mitigating Discrimination in Language Model Decisions (opens in a new tab)

The authors generated decision prompts for 70 scenarios, from loans to housing, and varied the age, race and gender of the person described, either explicitly or through names. Without interventions, the model they studied showed both positive and negative discrimination in some settings, for example lower approval odds for older people. Prompt additions such as stating that discrimination is illegal or that demographics should be ignored reduced the measured discrimination substantially, often close to zero, while keeping decisions highly correlated with the originals. The authors state they do not endorse using models for such high-risk decisions.

Parrish et al., 2022 (paper)

BBQ: A Hand-Built Bias Benchmark for Question Answering (opens in a new tab)

BBQ tests social biases across nine dimensions with questions asked in an under-informative context and in a context that contains the answer. Models often relied on stereotypes when the context was ambiguous. Even with an informative context, accuracy averaged up to 3.4 percentage points higher when the correct answer matched a stereotype than when it conflicted, and over 5 points for gender examples in most models tested.

Bolukbasi et al., 2016 (paper)

Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings (opens in a new tab)

Word embeddings trained on news articles encoded gender stereotypes, such as associating receptionist with female, and gender bias appeared as a direction in the embedding space. The authors proposed metrics for direct and indirect bias and a method to reduce it while keeping useful associations. It is an early demonstration that bias in language models starts in the data.

Check yourself

  1. What does a counterfactual bias evaluation change between the two prompts in a pair?

    Show answer

    B. Only the attribute under test, such as the name or stated age Holding everything else fixed means any difference in the output can be attributed to that one attribute.

  2. A small, consistent gap favours one group across most templates. Where does it change decisions?

    Show answer

    B. For borderline cases near the decision threshold Clear accepts and clear rejects stay the same. Cases near the threshold flip, and those are the ones where a fair decision matters most.

  3. You remove names and gender from your screening prompts. Is the bias problem solved?

    Show answer

    B. Not necessarily: addresses, schools, hobbies and writing style can act as proxies, so you still need to measure Removing direct attributes helps, but correlated details carry similar signals. Keep running paired and outcome-based evaluations.

  4. Adding 'demographic characteristics must not influence this decision' to the prompt cut the measured gap sharply. What should you do next?

    Show answer

    B. Keep the instruction, keep the eval in your test suite, and re-run it whenever the model or prompt changes Prompt mitigations work but are not guaranteed and can change with model versions. Treat the bias eval as a regression test.

  5. Why is a language model alone not appropriate for final hiring or lending decisions?

    Show answer

    B. These are high-stakes, often legally regulated decisions that need accountability, human review and a way to appeal Even a well-tested model can be wrong or biased in ways tests miss. People must own the decision and be able to explain and correct it.

Recap

  • Bias in language models comes from training data, representation, fine-tuning feedback and how the deployment frames the task.
  • Measure decisions with counterfactual pairs that change only one attribute; report the average gap, flipped decisions and consistency across templates.
  • Small, consistent gaps flip borderline decisions, so overall accuracy and approval rates can hide them.
  • Mitigate in layers: remove unneeded attributes, state criteria and the non-discrimination rule, improve data, and keep people in the loop for high-stakes uses.
  • Treat bias evaluations as regression tests and re-run them on every model or prompt change.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy