Lesson 2 of 3 Responsible AI in Practice

Evaluating Foundation Models for Fairness

This lesson covers fairness test design, metrics, A/B testing and automated evaluation that systematically measure the biases foundation models can reproduce from human-written text.

Learning outcomes

After completing this lesson, you will be able to:

  • Design counterfactual fairness tests.
  • Track fairness metrics across groups.
  • Run systematic A/B tests and LLM-as-a-judge evaluations.

Designing fairness tests

A practical approach for generative AI is counterfactual testing: create pairs or sets of prompts that are identical except for a sensitive attribute or its proxy, such as a name associated with a gender or ethnicity, an age, a nationality or a disability, and compare the outputs. Differences in recommendations, scores, refusal rates, tone, length or the presence of stereotypes indicate bias. Tests cover the application's real tasks, such as summarising candidate profiles or drafting loan explanations, rather than generic benchmarks.

The following diagram shows a counterfactual fairness evaluation of CV summaries.

A counterfactual fairness evaluation for CV summaries. A set of CVs that differ only in names and pronouns runs through two prompt variants; the difference in average fit scores and a stereotyping score from a Bedrock LLM-as-a-judge evaluation decide which variant is within the threshold.
The same test set is re-run whenever the prompt or model changes.
Scenario questions

Testing for bias

Exam skills 3.4.2

Practise designing fairness tests for generative applications.

Log in to try this activity Interactive activities are free with an account.

Fairness metrics

Fairness tests produce outputs for different groups; metrics turn the comparison into numbers that can be tracked and acted on. Typical metrics are the difference in favourable outcome rates between groups, for example the share of candidate profiles recommended for interview; differences in average sentiment or politeness of responses; differences in refusal rates; and the rate of stereotyping detected by an LLM judge.

Each metric has a threshold that defines an acceptable difference, agreed with the business and, where relevant, legal advisers. Publishing the metrics to Amazon CloudWatch with alarms turns fairness from a one-time check before launch into a monitored property, measured on evaluation runs after each change and on sampled production traffic, so that a regression introduced by a new model or prompt is detected promptly.

Systematic A/B testing and LLM-as-a-judge

Prompt Management variants and Bedrock Flows make it easy to run the same fairness test set through different prompts or models and compare results: for example, a variant with explicit instructions to ignore irrelevant personal attributes versus the original. Results are compared on fairness metrics and on task quality, because a fairer prompt that ruins accuracy is not a solution.

LLM-as-a-judge evaluations in Amazon Bedrock score responses at scale with built-in metrics such as stereotyping and harmfulness, or custom metrics with a rubric for the organisation's fairness criteria. Human reviewers check a sample to confirm the judge's assessments. The following table summarises the methods.

MethodMeasuresTooling
Counterfactual prompt setsOutput differences when only a sensitive attribute changesTest datasets in S3
Outcome rate differencesUnequal recommendations or decisionsCustom metrics in CloudWatch
A/B comparison of prompts or modelsWhich configuration is fairer at equal qualityPrompt Management variants, Flows
LLM-as-a-judgeStereotyping, harmfulness, custom fairness rubricBedrock evaluation jobs
Human review of samplesJudge accuracy and nuanced issuesBedrock human evaluation, A2I
True or false

Comparing for fairness

Exam skills 3.4.2 · 5.1.2

Check three claims about comparing prompts and models for fairness.

Log in to try this activity Interactive activities are free with an account.
Worked example

Candidate summaries

A recruiting tool summarises CVs and suggests a fit score. The company must show the scores do not vary by gender.

The team builds a counterfactual set of CVs that differ only in names and pronouns, runs it through two prompt variants in Prompt Management, measures the difference in average fit scores and the stereotyping score from a Bedrock LLM-as-a-judge evaluation, chooses the variant within threshold, and publishes the gap metric to CloudWatch for monitoring on sampled production data.

Check yourself

How can differences in outcome rates between groups be tracked over time?

Show answer

Publish them as CloudWatch metrics and alarm when the difference exceeds a threshold.

Exam Tips

Remember for the exam

  • Counterfactual prompts that change only a sensitive attribute reveal bias.
  • Track outcome rate differences between groups as CloudWatch metrics.
  • Compare prompts or models with Prompt Management variants or Flows.
  • Score stereotyping at scale with LLM-as-a-judge; validate with human samples.