When to reach for it
- Outputs are free-form, so schema validation does not apply.
- A numeric reject/allow boundary is needed.
- Topical drift or low-confidence fabrication must be caught before a response reaches a user.
Reject on the numbers: cosine-distance drift and token-entropy confidence.
Quantitative, model-agnostic checks sit between a non-deterministic agent and the user and reject outputs on statistical signals rather than schema or rules. Two complementary checks: semantic-drift detection embeds each response and measures its cosine distance from a set of "safe" baseline examples, flagging outliers by z-score (off-topic drift, persona shifts, likely hallucination or toxicity); and confidence gating computes the Shannon entropy of the generated tokens' probabilities — high entropy means the model was guessing among many low-probability tokens, a proxy for low-confidence or fabricated answers.
In practiceA medical-information agent runs every generated answer through a cosine-distance drift detector; responses that stray semantically from the safe-answer baseline are flagged and routed to a human reviewer before delivery.
Without itWithout statistical guardrails, an answer that is schema-valid but semantically drifted reaches the user unflagged, because structural validators see nothing wrong and the drift only shows up once a complaint surfaces it.
Two numbers are computed for every free-text answer: how far it lies from known good answers, and how unsure the model was while writing it. An answer outside the limits is held back.
1 · Calibrate
2 · On every answer
| Answer | z-score | Entropy | Outcome |
|---|---|---|---|
| The dosage of a common painkiller | 0.4 | 1.1 | Delivered |
| Side effects, in unusual wording | 1.9 | 1.6 | Delivered |
| An investment tip in the middle of the answer | 3.8 | 1.4 | Held: drift away from the topic |
| A drug name that does not exist, with invented details | 1.2 | 3.7 | Held: low confidence |
import numpy as np
def calibrate(safe_answers):
vectors = np.array([embed(answer) for answer in safe_answers])
centroid = vectors.mean(axis=0)
distances = [cosine_distance(vector, centroid) for vector in vectors]
return centroid, np.mean(distances), np.std(distances)
def check(answer, token_probabilities, baseline, max_z=2.5, max_entropy=3.0):
centroid, mean, std = baseline
# semantic drift
z = (cosine_distance(embed(answer), centroid) - mean) / std
# per token, in bits
entropy = np.mean([-sum(p * np.log2(p) for p in dist if p > 0)
for dist in token_probabilities])
if z > max_z:
return "hold: drift"
if entropy > max_entropy:
return "hold: low confidence"
return "deliver"When to reach for it
When it backfires
The z-score baseline was built from last quarter's safe answers. The product's scope expanded, legitimate new topics now read as drift, and the guardrail rejects correct outputs.
Countermeasure
Refresh the baseline corpus as scope changes, and alert on rejection-rate spikes so a drifting threshold is caught before it blocks real traffic.
Low token entropy reads as high confidence, but a fluent hallucination is exactly that — confident and wrong. The statistical gate passes it because confidence is not correctness.
Countermeasure
Never treat low entropy as a truth signal. Pair confidence gating with a grounding or verification check (retrieval, a fact-check tool, or LLM-as-judge) before trusting the output.
EU law
Duties from EU law that this pattern helps meet, or that land on it. A reading aid, not legal advice.
An engineer's reading aid, not legal advice. The legal texts and their interpretation are authoritative.
Keep going
Search patterns, frameworks, and pages.