0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · Lección

Detectar y medir alucinaciones

Aprenda técnicas prácticas para detectar cuándo un sistema RAG inventa datos que no están respaldados por el contexto recuperado y cómo cuantificar la tasa de alucinaciones como parte de la evaluación.

Detectar y medir alucinaciones es una lección gratuita de LLM Apps in Production (RAG + Vector DB + Caching) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de LLM Apps in Production (RAG + Vector DB + Caching), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de LLM Apps in Production (RAG + Vector DB + Caching) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

What Is a RAG Hallucination?

A hallucination is an answer that is fluent and confident but not supported by the retrieved context. In RAG, the cure is grounding: every claim should trace back to a source.

Measuring hallucination rate is essential to trust your system.

Faithfulness vs Correctness

Two different things to measure:

  • Faithfulness — is the answer supported by the provided context?
  • Correctness — is the answer factually true in the real world?

A RAG answer can be correct but unfaithful (right by luck) or faithful but wrong (the source was wrong).

Claim Decomposition

To check faithfulness, break the answer into atomic claims, then verify each against the context.

answer = 'Paris is the capital of France and has 5 million people.'
claims = [c.strip() for c in answer.replace(' and ', '. ').split('.') if c.strip()]
for c in claims:
    print('CLAIM:', c)

Context Entailment

For each claim, ask: does the retrieved context entail it? An NLI model or an LLM judge labels each claim as supported, contradicted, or not-mentioned.

  • Supported = grounded
  • Not-mentioned = potential hallucination
  • Contradicted = definite error

LLM-as-Judge for Faithfulness

A common pattern: prompt a strong model with the context, the answer, and ask it to score whether the answer is fully supported. Calibrate the judge against human labels.

Computing Hallucination Rate

Hallucination rate = fraction of claims (or answers) that are unsupported.

labels = ['supported', 'supported', 'not_mentioned', 'contradicted']
bad = sum(1 for x in labels if x != 'supported')
rate = bad / len(labels)
print('Hallucination rate:', round(rate, 2))

Citation Coverage

If your system outputs citations, you can measure citation coverage: the share of sentences that point to a retrieved chunk that actually supports them. Low coverage signals hallucination risk.

Detecting Missing Context

Many hallucinations happen because retrieval failed and the model filled the gap. Track cases where the context lacks the answer but the model still answered confidently instead of saying 'I do not know'.

A Simple Faithfulness Score

Aggregate per-claim labels into a single score per answer.

def faithfulness(labels):
    return sum(1 for x in labels if x == 'supported') / len(labels)

print(faithfulness(['supported', 'supported', 'not_mentioned']))

Reducing Hallucinations

Once measured, reduce hallucinations by:

  • Improving retrieval recall
  • Instructing the model to abstain when unsupported
  • Requiring inline citations
  • Post-hoc filtering of unsupported claims

Tracking Over Time

Add hallucination rate to your regular eval runs. Watch it on every prompt or model change so a regression is caught before it reaches users.

Quick Check

Test your understanding of faithfulness.

Recap

You learned to detect hallucinations by separating faithfulness from correctness, decomposing answers into claims, checking entailment against context with an LLM judge, and computing a hallucination rate. Track it over time and reduce it with better retrieval, abstention, and citations.

Preguntas frecuentes

¿La lección «Detectar y medir alucinaciones» es gratis?

Sí — el texto completo de «Detectar y medir alucinaciones» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de LLM Apps in Production (RAG + Vector DB + Caching), actualiza a CoddyKit PRO. El curso de LLM Apps in Production (RAG + Vector DB + Caching) incluye 4 lecciones en total.

¿Qué aprenderé en «Detectar y medir alucinaciones»?

Aprenda técnicas prácticas para detectar cuándo un sistema RAG inventa datos que no están respaldados por el contexto recuperado y cómo cuantificar la tasa de alucinaciones como parte de la evaluació… Practicas LLM Apps in Production (RAG + Vector DB + Caching) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar LLM Apps in Production (RAG + Vector DB + Caching)?

No se requiere experiencia previa. LLM Apps in Production (RAG + Vector DB + Caching) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Detectar y medir alucinaciones»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de LLM Apps in Production (RAG + Vector DB + Caching)?

Sí. Cada lección de LLM Apps in Production (RAG + Vector DB + Caching) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Métricas clave del rendimiento de RAG
  2. Desarrollo de pruebas de referencia
  3. Pruebas A/B y ciclos de comentarios de los usuarios
  4. Detectar y medir alucinaciones
← Volver a LLM Apps in Production (RAG + Vector DB + Caching)