Sub topic 8
Check labeling quality with annotator agreement
This chapter assumes you can read the JSONL case schema from earlier chapters, write basic Python, and compute a proportion. You also need to know that each case has a reference answer or a rubric label. Here you check whether those labels can be trusted.
Why labels need a quality check
A reference answer is a human judgment. If two careful people would label the same case differently, the case is ambiguous or the labeling guideline is unclear. Scoring an app against that label measures noise.
Annotator agreement is the rate at which independent labelers give the same label to the same case. It tells you three things:
- Whether the task definition is precise enough to grade.
- Whether the guideline is usable by someone who did not write it.
- How high a score any grader can reach. A judge model that matches humans as often as humans match each other is doing as well as the labels allow.
Check agreement before you grade apps against the labels. Disagreement found after you have published scores means the scores must be redone.
Raw agreement is not enough
The simplest measure is the fraction of cases where two annotators give the same label. It looks good even when it is not. If 90% of cases are "pass", two people who guess "pass" almost every time will agree most of the time.
Cohen's kappa corrects for this. It compares the observed agreement to the agreement expected if each annotator labeled at random while keeping their own label frequencies:
kappa = (observed − expected) / (1 − expected)
A kappa of 1 means perfect agreement. A kappa of 0 means agreement no better than chance. A negative kappa means worse than chance, which usually signals a systematic mix-up, such as one annotator reading the scale backwards.
A worked example
Two annotators label 100 cases as pass or fail:
| Row | B: pass | B: fail |
|---|---|---|
| A: pass | 85 | 5 |
| A: fail | 5 | 5 |
Observed agreement is (85 + 5) / 100 = 0.90. Each annotator says "pass" on 90 cases. Expected agreement is 0.9 × 0.9 + 0.1 × 0.1 = 0.82. Kappa is (0.90 − 0.82) / (1 − 0.82), about 0.44.
Raw agreement of 90% sounds strong. Kappa of 0.44 says the annotators agree only moderately on the cases where the label is not obvious. The code below reproduces this result.
def cohens_kappa(a, b):
n = len(a)
observed = sum(x == y for x, y in zip(a, b)) / n
labels = set(a) | set(b)
expected = sum((a.count(l) / n) * (b.count(l) / n) for l in labels)
if expected == 1:
return 1.0
return (observed - expected) / (1 - expected)
labels_a = ["pass"] * 85 + ["pass"] * 5 + ["fail"] * 5 + ["fail"] * 5
labels_b = ["pass"] * 85 + ["fail"] * 5 + ["pass"] * 5 + ["fail"] * 5
raw = sum(x == y for x, y in zip(labels_a, labels_b)) / len(labels_a)
print("raw agreement:", raw)
print("kappa:", round(cohens_kappa(labels_a, labels_b), 3))The output is a raw agreement of 0.9 and a kappa of 0.444.
The expected == 1 branch handles the case where both annotators use one identical label for everything. The formula would divide by zero there, and the function returns 1.0. Treat that result with suspicion. It means the sample contains no variation, so it cannot show whether the annotators agree on hard cases.
Reading the number
Common rules of thumb for kappa:
| Kappa | Rough reading |
|---|---|
| Below 0 | Worse than chance |
| 0.00 to 0.20 | Slight |
| 0.21 to 0.40 | Fair |
| 0.41 to 0.60 | Moderate |
| 0.61 to 0.80 | Substantial |
| 0.81 to 1.00 | Almost perfect |
These bands are conventions, not laws. A practical target for an eval label is 0.6 or higher, and 0.8 or higher for labels that gate a release. Lower values are acceptable for subjective qualities such as tone, as long as you know the label is noisy and you don't over-read small score differences.
Run an agreement check
The loop is the same each time you label a new batch:
- Pick a sample. Double-label a subset, not the whole dataset. Aim for at least 50 to 100 cases, and include cases from each important slice. A random sample hides rare slices, so sample per slice.
- Label independently. Annotators must not see each other's answers or discuss cases before submitting. Both use the same written guideline.
- Compute agreement. Calculate kappa overall and per slice.
- Read the disagreements. The disagreements show what is wrong. The number only tells you that something is.
- Fix the guideline, not the people. Add a rule or example for each recurring confusion, then relabel the affected cases.
- Adjudicate. A third person, ideally the domain owner, decides the final label for cases that stay contested.
Find disagreements and slice-level problems
Add these helpers to the same file as cohens_kappa. Labels are stored as dictionaries from case id to label, so cases match by their stable id rather than by list position.
from collections import defaultdict
def disagreements(labels_a, labels_b):
shared = sorted(set(labels_a) & set(labels_b))
return [(i, labels_a[i], labels_b[i]) for i in shared
if labels_a[i] != labels_b[i]]
def kappa_by_tag(cases, labels_a, labels_b):
by_tag = defaultdict(lambda: ([], []))
for c in cases:
i = c["id"]
if i in labels_a and i in labels_b:
for tag in c["tags"]:
by_tag[tag][0].append(labels_a[i])
by_tag[tag][1].append(labels_b[i])
return {tag: cohens_kappa(a, b) for tag, (a, b) in by_tag.items()}An overall kappa of 0.7 can hide a slice at 0.2. For example, "policy" cases might be easy while "returns" cases involving exceptions are contested. A low-kappa slice needs a clearer guideline, or it needs a note that scores on that slice are unreliable.
Print the disagreements and read them. Typical causes are:
- The question has two valid answers, so the case needs a better input or a rubric that accepts both.
- The guideline does not say how to treat partial answers or extra correct detail.
- The reference answer depends on information not in the input, such as a policy that changed.
- One annotator lacks domain knowledge, so the case needs an expert.
Edge cases and gotchas
More than two annotators. Cohen's kappa is defined for exactly two raters. For three or more, use Fleiss' kappa when every case gets the same number of ratings, or Krippendorff's alpha when some ratings are missing.
Ordered scales. On a 1 to 5 quality scale, a 4 versus a 5 is a smaller disagreement than a 1 versus a 5. Plain kappa treats all disagreements as equal. Weighted kappa gives partial credit to near misses. Alternatively, collapse the scale into pass or fail with a clear threshold, which often improves agreement.
Uncertainty on small samples. With 50 double-labeled cases, a kappa of 0.6 could plausibly be 0.4 or 0.8 on a different sample. Resample the paired labels and recompute kappa to get an interval, the same way you would bootstrap an accuracy score. Treat a point estimate near your threshold as undecided.
Shared mistakes. Agreement measures consistency, not correctness. Two annotators who misread the same policy will agree and both be wrong. Spot-check a few agreed labels against the source of truth.
Annotator drift. Agreement measured in week one can decay as people forget the guideline or as the product changes. Re-check on a fresh sample whenever you add annotators or revise the guideline.
Agreeing by sampling bias. If you double-label only easy cases, kappa will be high and uninformative. Include cases tagged difficulty: hard and adversarial inputs in the sample.
Connecting agreement to later steps
The agreement you measure here sets a ceiling for the grader you choose next. If humans reach kappa 0.65 with each other, expect an LLM judge to match humans at about that level at best. Calibrating a judge against human labels, covered later in this tutorial, uses the same kappa function with a human and a judge as the two raters.
Exercise
Two annotators label 50 cases as pass or fail. Both say pass on 30 cases. A says pass and B says fail on 5 cases. A says fail and B says pass on 5 cases. Both say fail on 10 cases. Compute raw agreement and Cohen's kappa. Is the labeling good enough for a release gate with a target of 0.8?
Show answer
Observed agreement is (30 + 10) / 50 = 0.80. Each annotator says pass on 35 of 50 cases, so each passes 0.70 of the time. Expected agreement is 0.7 × 0.7 + 0.3 × 0.3 = 0.58. Kappa is (0.80 − 0.58) / (1 − 0.58), about 0.52, which is moderate. It is below the 0.8 target, so review the 10 disagreements, revise the guideline, relabel, and recheck before using these labels to gate a release.
Exercise
An overall kappa is 0.72, but kappa_by_tag reports 0.25 for the tag "returns". What should you do first, and why not just average the two numbers or ignore the slice?
Show answer
Call disagreements and read the cases tagged "returns" to find the recurring confusion, then add a rule or example to the guideline and relabel those cases. The overall number is dominated by the easier, more common slices, so it hides the problem. Scores on the "returns" slice are unreliable until its agreement improves, and ignoring the slice would leave a weak spot in the dataset unreported.