The First Token Knows: Single-Decode Confidence for Hallucination Detection
TL;DR Highlight
LLM이 답변의 첫 토큰을 생성할 때의 확률 분포만 봐도, 10번 샘플링하는 semantic self-consistency와 맞먹는 hallucination 탐지 성능이 나온다.
Who Should Read
LLM 서비스에서 hallucination 탐지 파이프라인을 구축 중인 백엔드/ML 엔지니어. 특히 추론 비용을 줄이면서도 신뢰도 측정이 필요한 상황에서 유용하다.
Core Mechanics
- 첫 토큰 confidence(ϕfirst)는 모델이 greedy decode로 답변을 생성할 때 첫 번째 의미 있는 토큰 위치에서 top-100 logit의 정규화된 엔트로피를 측정한다. 값이 1에 가까울수록 모델이 확신하는 것.
- ϕfirst는 단 1번의 forward pass만 필요한데, semantic self-consistency(greedy 1회 + 샘플링 10회 + NLI 클러스터링)와 비교해 생성 비용이 약 1/11 수준이다.
- Llama-3.1-8B, Mistral-7B-v0.3, Qwen2.5-7B 세 모델과 PopQA, TriviaQA 두 벤치마크(각 n=1000)에서 ϕfirst의 평균 AUROC는 0.820으로, semantic self-consistency(0.793)와 surface-form self-consistency(0.791)를 앞섰다.
- 6개 dataset-model 조합 중 5개에서 ϕfirst가 가장 높은 AUROC를 기록했고, 나머지 1개에서도 최강 방법과 0.002 차이 이내였다.
- ϕfirst와 semantic AU를 logistic ensemble로 합쳐도 AUROC가 평균 +0.021밖에 안 오른다. 즉, ϕfirst가 semantic AU의 정보를 이미 대부분 담고 있다는 뜻.
- 모델에게 '너 얼마나 확신해?'라고 직접 물어보는 verbalized confidence는 평균 AUROC 0.700으로 가장 낮아서, LLM의 자기 보고 신뢰도는 믿기 어렵다는 걸 재확인했다.
Evidence
- 전체 평균 AUROC: ϕfirst 0.820 vs semantic AU 0.793 vs AU-full 0.791. PopQA에서는 ϕfirst가 +0.036, TriviaQA에서는 +0.016 앞섰다.
- ϕfirst와 semantic AU의 Pearson 상관계수는 0.54~0.76(평균 0.67)이고, 둘을 합쳐도 AUROC 개선이 평균 +0.021에 불과해 subsumption(포함 관계)이 성립한다.
- paired bootstrap test에서 ϕfirst는 AU-full 대비 6개 셀 중 4개에서(p<0.05), semantic AU 대비 3개에서 유의미하게 우세했고, 나머지는 동등 수준으로 판정됐다.
- 답변 길이와 ϕfirst의 원시 상관계수는 -0.11~-0.25이지만, 정답 여부를 통제하면 PopQA에서는 거의 0에 수렴(-0.02~-0.04)해서 길이 편향이 실제로는 정답률 차이 때문임을 확인했다.
How to Apply
- 모델 API가 logit을 노출하는 경우(Hugging Face transformers 등), 답변 생성 시 첫 번째 의미 있는 토큰의 top-100 logit을 뽑아 정규화 엔트로피를 계산하면 별도 샘플링 없이 hallucination 위험도 점수를 얻을 수 있다.
- 기존에 temperature sampling을 10회 돌려서 self-consistency를 측정하고 있다면, 우선 ϕfirst를 baseline으로 먼저 측정해보고, 추가 비용 대비 성능 향상이 있는지 비교한 후 sampling 횟수를 결정하는 방식으로 비용을 아낄 수 있다.
- RAG가 아닌 closed-book QA(모델의 파라메트릭 지식으로만 답하는 상황)에서 답변 신뢰도 필터링이 필요할 때, ϕfirst 임계값(예: 0.7 이하면 '불확실' 태그)을 설정해 낮은 신뢰도 답변을 별도 처리하는 파이프라인을 구성할 수 있다.
Code Example
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = "meta-llama/Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
def compute_phi_first(question: str, K: int = 100) -> float:
"""
첫 번째 content-bearing 토큰의 정규화 엔트로피로 hallucination 위험도 측정
반환값: 0(불확실) ~ 1(확신)
"""
prompt = f"Answer the following question in a short phrase.\nQuestion: {question}\nAnswer:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model(**inputs)
# 마지막 입력 토큰 다음 위치의 logit (첫 번째 생성 토큰)
logits = outputs.logits[0, -1, :] # shape: [vocab_size]
# top-K logit 추출 및 재정규화
top_k_logits, _ = torch.topk(logits, K)
top_k_probs = torch.softmax(top_k_logits, dim=-1)
# 정규화 엔트로피 계산
import math
H = -torch.sum(top_k_probs * torch.log(top_k_probs + 1e-12)).item()
H_normalized = H / math.log(K)
phi_first = 1.0 - H_normalized # 1에 가까울수록 확신
return phi_first
# 사용 예시
question = "Who wrote Hamlet?"
phi = compute_phi_first(question)
print(f"ϕfirst: {phi:.3f}")
if phi < 0.5:
print("⚠️ 모델이 불확실합니다. 답변을 신뢰하기 어렵습니다.")
else:
print("✅ 모델이 확신합니다.")Terminology
Related Papers
Postmortem: TanStack NPM supply-chain compromise
2026년 5월 11일 TanStack의 42개 npm 패키지가 GitHub Actions cache poisoning과 OIDC 토큰 탈취를 조합한 공격으로 악성 버전이 배포됐으며, 공격 벡터와 대응 과정을 상세히 분석한 글이다.
Can LLMs model real-world systems in TLA+?
LLM이 TLA+ 명세를 작성할 때 문법은 잘 통과하지만 실제 시스템과의 동작 일치도(conformance)는 46% 수준에 그친다는 걸 체계적으로 검증한 벤치마크 연구로, AI 기반 형식 검증의 현실적 한계를 보여준다.
Natural Language Autoencoders: Turning Claude's Thoughts into Text
Anthropic이 LLM 내부의 숫자 벡터(활성화값)를 직접 읽을 수 있는 자연어로 변환하는 NLA 기법을 공개했다. AI가 실제로 무슨 생각을 하는지 해석하는 interpretability 연구의 새로운 진전이다.
ProgramBench: Can language models rebuild programs from scratch?
LLM이 FFmpeg, SQLite, PHP 인터프리터 같은 실제 소프트웨어를 문서만 보고 처음부터 재구현할 수 있는지 측정하는 새 벤치마크로, 최고 모델도 전체 태스크의 3%만 95% 이상 통과하는 수준에 그쳤다.
MOSAIC-Bench: Measuring Compositional Vulnerability Induction in Coding Agents
티켓 3장으로 쪼개면 Claude/GPT도 보안 취약점 코드를 53~86% 확률로 그냥 짜준다.
Refusal in Language Models Is Mediated by a Single Direction
Open-source chat models encode safety as a single vector direction, and removing it disables safety fine-tuning.
Original Abstract (Expand)
Self-consistency detects hallucinations by generating multiple sampled answers to a question and measuring agreement, but this requires repeated decoding and can be sensitive to lexical variation. Semantic self-consistency improves this by clustering sampled answers by meaning using natural language inference, but it adds both sampling cost and external inference overhead. We show that first-token confidence, phi_first, computed from the normalized entropy of the top-K logits at the first content-bearing answer token of a single greedy decode, matches or modestly exceeds semantic self-consistency on closed-book short-answer factual question answering. Across three 7-8B instruction-tuned models and two benchmarks, phi_first achieves a mean AUROC of 0.820, compared with 0.793 for semantic agreement and 0.791 for standard surface-form self-consistency. A subsumption test shows that phi_first is moderately to strongly correlated with semantic agreement, and combining the two signals yields only a small AUROC improvement over phi_first alone. These results suggest that much of the uncertainty information captured by multi-sample agreement is already available in the model's initial token distribution. We argue that phi_first should be reported as a default low-cost baseline before invoking sampling-based uncertainty estimation.