datarekha

Differential privacy for LLMs

LLMs memorize their training data — and can be coaxed into regurgitating verbatim secrets, PII, and copyrighted text. Differential privacy is the mathematical defense: a formal guarantee, tuned by a privacy budget epsilon, that the model's behavior barely changes whether or not any single record was in the training set.

8 min read Advanced Generative AI Lesson 35 of 63

What you'll learn

  • Why LLMs leak training data — memorization, extraction, membership inference
  • The differential-privacy guarantee and the epsilon privacy budget
  • The Laplace mechanism and the privacy/utility trade-off
  • DP-SGD for training, and the cost (utility, and disparate impact)

Before you start

Large models memorize. Trained on text that includes private emails, API keys, medical notes, and copyrighted books, a model can be prompted to regurgitate chunks of it verbatim — extraction attacks have pulled real names, phone numbers, and addresses out of production models. Two related risks: membership inference (figure out whether a specific record was in the training set) and data extraction (recover the record itself). Differential privacy (DP) is the rigorous defense — not a filter, but a mathematical guarantee.

The guarantee: epsilon bounds one person’s influence

DP makes a precise promise: the output of a computation is almost the same whether or not any one individual’s record is included. Formally, for two datasets differing by a single record, the probability of any outcome changes by at most a factor of exp(epsilon). That epsilon (the privacy budget) is the knob: small epsilon = strong privacy (the individual is well hidden) but more noise; large epsilon = weak privacy but a more accurate result.

The basic tool is the Laplace mechanism: to release a numeric query privately, add random noise scaled to sensitivity / epsilon, where sensitivity is how much one record can change the answer. A count has sensitivity 1 (adding one person changes it by 1):

import numpy as np
rng = np.random.default_rng(0)

true_count, sensitivity = 1000, 1
print("epsilon  scale_b  noisy_answer  (true=1000)")
for eps in [10.0, 1.0, 0.1]:
    b = sensitivity / eps                 # Laplace scale: smaller epsilon -> more noise
    noisy = true_count + rng.laplace(0.0, b)
    print(f"{eps:>6}  {b:>6.1f}  {noisy:>11.1f}")
epsilon  scale_b  noisy_answer  (true=1000)
  10.0     0.1       1000.0
   1.0     1.0        999.4
   0.1    10.0        975.0

At epsilon=10 the noise is tiny — the answer is essentially exact, but it barely hides anyone. At epsilon=0.1 the noise (±10ish) masks whether any single person was counted, at the cost of an answer that’s only roughly right. That dial — privacy bought with accuracy — is differential privacy.

Include Alice, or not? The outputs look the same≤ exp(ε)output WITH Alice’s recordoutput WITHOUT Alice’s recordnear-identical distributions → an observer can’t tell if Alice was in the datasmaller ε forces the two curves closer together (stronger privacy)
DP’s promise: swapping one person in or out barely moves the output distribution — so the output reveals almost nothing about any individual. ε bounds the gap.

Training privately: DP-SGD

To make the trained model itself private, you privatize the gradients. DP-SGD changes ordinary training in two ways each step: clip every per-example gradient to a maximum norm (so no single example can dominate the update), then add Gaussian noise to the summed gradients before applying them. No individual example moves the weights much, so the model can’t memorize any one of them.

The price is real. The noise lowers accuracy and training is slower; and crucially, the privacy cost accumulates over steps (composition) — each access to the data spends from the epsilon budget, so you track the total spent across the whole training run.

In one breath

  • LLMs memorize training data and can be made to leak it — membership inference (was this record used?) and extraction (recover the record, e.g. PII).
  • Differential privacy is a guarantee: including or excluding any one record changes the output probability by at most exp(epsilon) — so the output reveals almost nothing about any individual.
  • Epsilon is the privacy budget: small ε = strong privacy + more noise; large ε = weak privacy + more accuracy. The Laplace mechanism adds noise scaled to sensitivity / epsilon (the demo: ε=0.1 masks the count, ε=10 barely hides it).
  • DP-SGD trains privately by clipping per-example gradients and adding Gaussian noise; the privacy cost composes across steps, spending the budget.
  • DP costs utility and that cost is uneven — it hurts rare/minority patterns most — so it’s applied selectively and alongside dedup and PII scrubbing.

Quick check

Quick check

0/4
Q1What does differential privacy guarantee?
Q2What does the privacy budget epsilon control?
Q3How does DP-SGD train a model with differential privacy?
Q4What is a key cost of differential privacy beyond reduced accuracy?

Next

DP attacks the memorization that starts in the pretraining data; the noise it relies on is the Laplace/Gaussian distributions; and its uneven cost connects to broader questions of fairness in ML.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Related lessons

Explore further

Skip to content