Interview

A practical framework for debugging hallucinations in production RAG systems—from retrieval and chunking to prompts, generation, evaluation, and governance

One of the most interesting questions I have seen in a GenAI interview is:

“Your RAG system is retrieving relevant chunks, but the LLM is still hallucinating. How would you debug it?”

This question sounds simple.

But there is a trap.

Many candidates immediately say:

“I would improve the embeddings.”

Or:

“I would change the LLM.”

Or:

“I would increase the top-K.”

Those might help.

But they are not necessarily the right first steps.

If the retriever is already returning relevant information, then the problem may exist somewhere else in the pipeline.

The real challenge is to systematically identify where the grounded information is being lost between retrieval and the final answer.

A production RAG system is not just:

User Query
Vector Database
LLM
Answer

It is closer to:

                   ┌───────────────┐
                   │   User Query  │
                   └───────┬───────┘
                           ↓
                   Query Processing
                           ↓
                ┌──────────┴──────────┐
                ↓                     ↓
          Dense Retrieval        Keyword Search
                ↓                     ↓
                └──────────┬──────────┘
                           ↓
                     Reranking
                           ↓
                  Metadata Filtering
                           ↓
                   Context Assembly
                           ↓
                     Prompt Layer
                           ↓
                        LLM
                           ↓
                  Response Validation
                           ↓
                     Final Answer

If hallucinations occur, any of these components could be responsible.

So let’s walk through how I would answer this interview question.


1. First, Define What “Hallucination” Means

Before debugging, I would clarify what type of hallucination we’re dealing with.

There are several possibilities.

Type 1: Unsupported Claims

The retrieved documents don’t support what the model says.

Retrieved Context:
"Patients should consult a physician."
LLM:
"Patients should take Medication X."

The model invented information.


Type 2: Misinterpretation

The information exists in the context, but the model interprets it incorrectly.

Document:
"Treatment A is recommended for adults."
LLM:
"Treatment A is recommended for children."

The information was retrieved correctly.

The model simply reasoned incorrectly.


Type 3: Context Ignorance

The relevant information exists in the prompt, but the LLM doesn’t use it.

Context
Contains Correct Answer
LLM
Generates Different Answer

This could be related to:

  • Prompt structure
  • Context length
  • Distracting information
  • Poor formatting
  • Long-context behavior

Type 4: Retrieval Mismatch

The system retrieves documents that appear semantically relevant but don’t actually contain the answer.

For example:

Query:
"What is the claim approval period?"
Retrieved:
General insurance policy information

The documents are related to the topic but don’t answer the specific question.

So the first question I would ask is:

Is the model hallucinating despite having the correct evidence, or is the retrieval system not actually providing sufficient evidence?

That distinction determines where I investigate next.


2. Start With the Retrieval Layer

Even if someone tells me:

“The retriever is working.”

I would verify it myself.

I would inspect the actual retrieved chunks.

For every query, I would log:

Query
Retrieved Documents
Document IDs
Chunk IDs
Similarity Scores
Metadata

For example:

Query:
"What is the claim settlement period?"
Top-K Results:
1. Policy_2026.pdf
Section: Claims
Similarity: 0.91
2. Policy_2025.pdf
Section: Claims
Similarity: 0.87
3. FAQ.pdf
Section: Claims
Similarity: 0.84

Then I would manually verify:

Does the retrieved context actually contain the answer?

This is important because a high similarity score does not necessarily mean that the chunk contains the required information.

A document can be semantically similar to the query without answering it.

So I would measure retrieval quality independently using metrics such as:

  • Recall@K
  • Precision@K
  • MRR
  • NDCG

This gives us a clear separation:

Retrieval Quality
+
Generation Quality
=
End-to-End RAG Quality

If retrieval is poor, fix retrieval.

If retrieval is good, move downstream.


3. Check the Chunking Strategy

The next thing I would investigate is chunking.

This is often underestimated.

Suppose the original document says:

“Claims must be submitted within 30 days of the incident. Exceptions may apply in cases involving hospitalization.”

Now imagine the document is split into:

Chunk 1:
Claims must be submitted within 30 days.
Chunk 2:
Exceptions may apply in cases involving hospitalization.

The system might retrieve only Chunk 1.

The answer becomes:

“Claims must be submitted within 30 days.”

Technically, the answer is correct.

But incomplete.

Now consider the opposite problem.

Suppose we create extremely large chunks:

Chunk:
10,000 tokens containing multiple policies,
exceptions, definitions, and unrelated sections.

The relevant information may be buried inside a large amount of irrelevant context.

The model now has to identify the correct information itself.

So chunking involves a trade-off.

Too Small
Context Fragmentation
Missing Information
Too Large
Context Noise
Reduced Precision

I would therefore investigate whether the chunking strategy preserves the semantic boundaries of the documents.

For example:

  • Insurance documents → clause-aware chunking
  • Technical documents → heading-aware chunking
  • FAQs → question-answer pairs
  • Legal documents → section and clause boundaries

The key principle is:

A chunk should ideally represent a meaningful unit of information, not just an arbitrary number of tokens.


4. Check Metadata and Versioning

In enterprise RAG systems, the problem may not be semantic retrieval.

It could be metadata.

Imagine the system has:

Policy_2024
Policy_2025
Policy_2026

The query asks:

“What is the current policy?”

But retrieval returns:

Policy_2024

The information may be factually correct for 2024.

But it’s wrong for the user’s current context.

This is where metadata becomes critical.

Useful metadata might include:

document_name
document_type
section
clause
version
effective_date
expiration_date
department
region
access_level

The retrieval pipeline can then apply filters.

For example:

Query
Retrieve Relevant Documents
Filter by:
├── Version
├── Effective Date
├── Region
└── User Permissions
Final Context

This is especially important for dynamic enterprise knowledge.

A technically relevant document is not necessarily the correct document.


5. Check for Conflicting Context

Another common cause of hallucination is conflicting information.

Imagine the LLM receives:

Document A:
Claims must be submitted within 30 days.
Document B:
Claims must be submitted within 60 days.

The model now has conflicting evidence.

It may select one arbitrarily.

Or worse, it may combine the two and generate:

“Claims should generally be submitted within 30–60 days.”

That answer sounds reasonable.

But it may be completely wrong.

So I would add source authority and version information.

For example:

Current Policy
Higher Priority
Archived Policy
Lower Priority

The system should know which document is authoritative.

The retrieval layer should ideally return:

Source
Version
Effective Date
Confidence

Then the generation layer can prioritize authoritative information.


6. Inspect the Prompt

Suppose we have confirmed that:

  • The right documents were retrieved.
  • The chunks contain the correct answer.
  • Metadata is correct.
  • There are no conflicting sources.

But the LLM still hallucinates.

Now I would inspect the prompt.

A weak prompt might be:

Answer the user's question.
Context:
{context}

A better prompt would explicitly define the model’s behavior:

You are a domain-specific assistant.
Answer the user's question using only the provided context.
Rules:
1. Do not introduce facts that are not supported by the context.
2. If the context does not contain enough information, say so.
3. Do not make assumptions.
4. Cite the relevant source.
5. Clearly state uncertainty when evidence is incomplete.
Context:
{context}
Question:
{question}

The goal is to reduce the model’s tendency to fill missing information with its pretrained knowledge.

The model should understand:

Evidence Available
Answer
Evidence Missing
Abstain / Ask for Clarification

This is one of the most important design principles in production RAG.

A reliable AI system should know when it doesn’t know.


7. Check Context Formatting

Even with the correct documents and a good prompt, context formatting can affect generation quality.

Suppose we provide:

Context:
Chunk 1...
Chunk 2...
Chunk 3...
Chunk 4...
Chunk 5...

The model may not clearly understand:

  • Which source is authoritative
  • Where one document ends
  • Which information supports which claim

Instead, we can structure the context.

For example:

SOURCE 1
Document: Policy_2026
Section: Claims
Effective Date: January 2026
Content:
...
SOURCE 2
Document: FAQ
Section: Claims
Content:
...

This provides additional structure.

The model now receives:

Content
+
Metadata
+
Source Attribution

This can make grounding more reliable.


8. Investigate the “Lost in the Middle” Problem

Suppose the model receives a very large context.

The relevant information might be:

Beginning
Relevant Information
Middle
Relevant Information
End
Relevant Information

Long contexts can create challenges where information located in the middle is less effectively used than information near the beginning or end.

This is one reason simply increasing top_k isn’t always a good solution.

More retrieved documents can mean:

More Context
More Noise
More Tokens
More Cost
Potentially Worse Generation

Instead of:

Top-K = 20

we might prefer:

Retrieve 20
Rerank
Select Best 5
Generate

This is where a reranker can be valuable.


9. Add a Reranking Layer

A typical retrieval pipeline might be:

User Query
Dense Retrieval
Top 50
Reranker
Top 5
LLM

The initial vector search is optimized for fast candidate retrieval.

The reranker then evaluates:

“How relevant is this specific document to this specific query?”

This can improve precision.

The architecture becomes:

                  Query
                    ↓
        ┌───────────┴───────────┐
        ↓                       ↓
  Vector Search             BM25 Search
        ↓                       ↓
        └───────────┬───────────┘
                    ↓
              Candidate Pool
                    ↓
                 Reranker
                    ↓
              Top-K Context
                    ↓
                   LLM

This is particularly useful when the system retrieves documents that are topically related but don’t directly answer the question.


10. Use Hybrid Retrieval Where Appropriate

Pure vector search isn’t always sufficient.

Consider a query:

“What does clause 4.2.1 of policy ABC-123 say?”

This query contains exact identifiers.

Keyword search may outperform semantic search.

So we can combine:

BM25
+
Dense Vector Search

The result:

Query
├───────────────┐
↓ ↓
BM25 Vector Search
│ │
└───────┬───────┘
Fusion
Reranking
LLM

Hybrid retrieval is useful because:

  • BM25 handles exact terms.
  • Vector search handles semantic meaning.

For enterprise systems, the combination can be more robust than relying exclusively on either approach.


11. Implement an Abstention Strategy

One of the biggest mistakes in GenAI systems is assuming:

“The model must always answer.”

It shouldn’t.

Suppose the user asks:

“What is the reimbursement policy for a procedure that doesn’t exist in our knowledge base?”

The correct answer may be:

“I couldn’t find sufficient information in the available sources to answer this question reliably.”

That’s better than hallucinating.

So we can introduce a confidence threshold:

Retrieval
Confidence Score
┌───────────────┐
│ │
High Low
│ │
▼ ▼
Generate Abstain
Answer / Escalate

In high-risk domains, abstention can be a feature, not a failure.


12. Add Claim-Level Grounding

A response may contain multiple claims.

For example:

“The claim must be filed within 30 days, the insurer will process it within 7 days, and the customer is eligible for reimbursement.”

These are three separate claims.

The system should ideally validate each one.

Conceptually:

Generated Answer
Claim Extraction
┌──────┼──────┐
↓ ↓ ↓
Claim Claim Claim
1 2 3
↓ ↓ ↓
Evidence Matching
Grounded?

This is more robust than checking whether the response “looks reasonable.”

Each claim should have supporting evidence.

This allows us to identify exactly where the hallucination occurred.


13. Add a Governance Layer

For production applications, I would introduce a final validation stage.

User Query
Retrieval
LLM Generation
Governance Agent
┌─────────────┐
│ │
Pass Fail
│ │
▼ ▼
Return Retry / Re-Retrieve
Answer │
Validate
┌────┴────┐
↓ ↓
Pass Fail
↓ ↓
Return Fallback /
Human Review

The Governance layer can check:

  • Groundedness
  • Hallucination
  • PHI leakage
  • Sensitive information
  • Policy violations
  • Citation correctness
  • Confidence

This is especially important for healthcare, finance, legal, and other high-risk domains.


14. Evaluate the Pipeline End-to-End

Once we’ve made changes, we need to evaluate whether the system actually improved.

I would create a golden dataset.

For example:

Question
Expected Answer
Relevant Document
Relevant Chunk
Expected Citation

Then evaluate the pipeline.

Retrieval Metrics

  • Recall@K
  • Precision@K
  • MRR
  • NDCG

Generation Metrics

  • Faithfulness
  • Answer Relevance
  • Groundedness
  • Citation Accuracy

System Metrics

  • Latency
  • Token Usage
  • Cost

Business Metrics

  • User Satisfaction
  • Resolution Rate
  • Containment Rate
  • Task Completion

This gives us a complete picture.

              RAG Evaluation
                    │
      ┌─────────────┼─────────────┐
      ↓             ↓             ↓
 Retrieval      Generation     Business
  Quality         Quality        Impact

15. The Debugging Framework I Would Use

If I were answering this in an interview, I would summarize my debugging process like this:

                Hallucination
                     │
                     ▼
          Is relevant evidence
             actually retrieved?
               /           \
             No             Yes
             │               │
             ▼               ▼
       Fix Retrieval    Is context complete?
                             /      \
                           No        Yes
                           │          │
                           ▼          ▼
                     Fix Chunking   Check Prompt
                                      │
                                      ▼
                              Check Context Format
                                      │
                                      ▼
                              Check Conflicting Docs
                                      │
                                      ▼
                                  Reranking
                                      │
                                      ▼
                               Claim Validation
                                      │
                                      ▼
                                  Abstention
                                      │
                                      ▼
                                  Governance

This demonstrates that I’m not jumping randomly between models and embeddings.

I’m debugging the system layer by layer.


16. How I Would Answer This in a Real Interview

If the interviewer wants a concise answer, I would say:

“I would first separate the retrieval problem from the generation problem. Even if we’re told that retrieval is working, I would inspect the actual retrieved chunks and verify that they contain sufficient evidence to answer the query. I would evaluate retrieval independently using metrics such as Recall@K and MRR.

If retrieval is correct, I would check whether the chunks are complete, whether metadata and document versions are correct, and whether conflicting sources are being retrieved. Then I would inspect the prompt and context formatting to make sure the model is explicitly instructed to answer only from the provided evidence and abstain when information is missing.

I would also consider reranking to improve context precision, hybrid search for exact domain terms, and reducing the amount of irrelevant context. Finally, I would introduce a governance layer that validates generated claims against retrieved evidence and either retries, abstains, or escalates when the response isn’t sufficiently grounded.

So I would treat hallucination as an end-to-end pipeline debugging problem rather than immediately assuming that the LLM itself is the problem.”

That answer shows the interviewer that you understand:

  • RAG architecture
  • Retrieval evaluation
  • Prompt engineering
  • Chunking
  • Reranking
  • Hybrid search
  • Groundedness
  • Governance
  • Production reliability

The Bigger Lesson

This question looks like a question about hallucinations.

But it’s actually testing something deeper.

It’s testing whether you can debug a complex AI system systematically.

A production RAG application is a chain:

Data
Ingestion
Chunking
Embeddings
Indexing
Retrieval
Reranking
Context Construction
Prompt
LLM
Validation
User

If the final answer is wrong, the solution isn’t always:

“Use a better LLM.”

The failure could be anywhere in the chain.

The strongest AI Engineers understand this.

They don’t just ask:

“Which model should I use?”

They ask:

“At which layer did the system lose the evidence needed to produce the correct answer?”

That’s the difference between building a demo and engineering a production AI system.


Final Takeaway

When your RAG system hallucinates despite retrieving relevant documents, don’t immediately change the model.

Debug systematically:

  1. Verify retrieval quality
  2. Check whether retrieved chunks contain sufficient evidence
  3. Validate chunking and semantic boundaries
  4. Check metadata, versions, and access controls
  5. Look for conflicting sources
  6. Inspect prompt construction
  7. Improve context formatting
  8. Use reranking
  9. Consider hybrid retrieval
  10. Introduce abstention
  11. Validate claims against evidence
  12. Add governance and human escalation
  13. Evaluate the entire pipeline with a golden dataset

The key principle is simple:

A RAG system is only as reliable as the chain connecting data to the final answer.

And when debugging GenAI systems, the best engineers don’t guess.

They trace the evidence.


AI Engineer Interview Questions — Part 4

In the next part, we’ll tackle another common production GenAI interview question:

“When would you use Prompt Engineering vs. RAG vs. Fine-Tuning?”

We’ll build a practical decision framework covering:

  • Prompt engineering
  • RAG
  • Fine-tuning
  • LoRA and PEFT
  • Cost and latency trade-offs
  • Knowledge updates
  • Catastrophic forgetting
  • Model customization
  • When to combine multiple approaches

Because the real question isn’t:

“Which technique is best?”

It’s:

“Which technique solves this specific problem with the lowest cost and complexity while meeting the required quality?”

follow me on medium

Leave a Reply

Discover more from Geeky Codes

Subscribe now to keep reading and get access to the full archive.

Continue reading