Logprobs — Confidence & Routing
Turning "The Model Said X" Into a Number You Can Route On
AI engineeringAI engineering / evaluation & reliability
The problem
Your classifier returns "refund_request", and downstream you auto-approve refunds under $50. You need to know: did it barely pick that label, or was it certain? Asking the model to "also output a confidence 0-1" gives you 0.95 every time — that's the model role-playing confidence, not measuring it. Logprobs are the real measurement.
Build it in 5 lines (OpenAI-style API)
r = client.chat.completions.create(
model=M, messages=msgs, max_tokens=1,
logprobs=True, top_logprobs=5)
tok = r.choices[0].logprobs.content[0]
print(tok.token, math.exp(tok.logprob))
print([(t.token, round(math.exp(t.logprob),3)) for t in tok.top_logprobs])
Sample output:
refund 0.62
[('refund', 0.62), ('billing', 0.31), ('ship', 0.04)]
What each line teaches:
- max_tokens=1 — you only need the DECISION token. One token, full signal, near-zero cost.
- logprob is a natural log; exp() makes it a probability. -0.0001 ≈ certain, -2.3 ≈ 0.10.
- top_logprobs is the gold: the runner-up tells you WHAT it almost said. 0.62 vs 0.31 is nearly a coin flip. 0.98 vs 0.01 is not.
The gate
if p_top < 0.80 or (p_top - p_second) < 0.30: route_to_human()
Fit those two numbers on a labeled set. Don't guess them.
Where people get this wrong
- Averaging logprobs over a whole answer. A 400-token reply's mean logprob is dominated by filler ("the", ","), which is always high-probability. You end up measuring fluency, not correctness. Score the token that carries the decision.
- Sum vs mean across candidates. Summing silently penalizes longer outputs — fine for ranking same-length labels, wrong for free text. Length-normalize, or only compare same-shape outputs.
- Confidence ≠ accuracy. Models are usually overconfident. Bucket your eval set by p and check real accuracy per bucket: if p ∈ [0.9, 1.0] is only 72% correct, a gate at 0.80 is theater.
- Label tokenization. " refund" and "refund" are different tokens, and "escalate" may split into two. If three labels share a first token, you're reading the probability of a prefix, not a label. Choose single-token labels, or pin the exact casing/spacing.
- Temperature won't fix calibration. It rescales the distribution; the ranking you read at the decision token is the same.
🅰️ Provider doesn't expose logprobs? (Anthropic currently doesn't)
Approximate with self-consistency: ask for the label 5x at temperature 1 and use the vote share (3/5 → 0.6). On a 1-token completion that's still cheap, and it correlates well enough to drive a routing gate.
Your exercise
Take a classification prompt you already run. Re-run 50 examples with max_tokens=1, logprobs=True, top_logprobs=3. Bucket by p_top into <0.6, 0.6–0.9, >0.9 and compute accuracy in each bucket.
Tell me: where does your accuracy actually fall off, and what share of traffic would a human-review gate at that threshold divert?