VB
Victor Buzin
Back to Blog
RAG Is a System, Not a Prompt: Retrieval, Context, Evaluation and Operations
2026-07-2622 min read

RAG Is a System, Not a Prompt: Retrieval, Context, Evaluation and Operations

Cluster: RAG, Evaluation & Reliability | 22 min read

RAG answer quality is determined at four separate stages, each testable independently; treating it as a single system that is either "working" or "not working" makes it impossible to improve systematically.


The RAG Quality Gap: Why Prompt Engineering Isn't Saving Your Demo

Most Retrieval-Augmented Generation (RAG) projects begin with a moment of pure developer magic. You index a few PDFs in a vector store, wire up a basic similarity search, and pipe the results into an LLM with a template like "Answer based on the context." The first three questions you ask work perfectly. The demo looks like a production-ready product.

Then you hand it to a subject matter expert (SME). Within ten minutes, they find a failure. The LLM hallucinates an answer that isn't in the docs, or it claims it doesn't know the answer despite the information sitting right there in the corpus.

The standard engineering reaction to this is "prompt tuning." You spend three days wordsmithing the system instructions, begging the LLM to be more faithful, adding "take a deep breath," or formatting the context in XML blocks. Sometimes it helps. More often, it fixes one edge case while breaking three others. You are treating the RAG pipeline as a black box — a monolithic prompt where you put data in and pray for quality out.

This is the "prompt fallacy." In an enterprise-grade system, the prompt is the least significant variable in the quality equation. RAG is not a prompt; it is a distributed system with at least four distinct failure surfaces: Retrieval, Context Augmentation, Generation, and Operations. If you aren't measuring these stages independently, you aren't engineering a system — you're just gambling with tokens.

Stage 0: Document Preparation — The Garbage In Gap

Before a single query is ever issued, your quality is already being constrained by your document ingestion pipeline. This is often the most neglected "Stage 0" of RAG.

If your system relies on technical PDFs, your biggest enemy is not the LLM — it's the PDF format itself. Columns, headers, footers, and complex tables often turn into a "text lasagna" when using standard scrapers. If a table's numbers are flattened into a single list of digits, no retrieval algorithm can reconstruct the relationship between those numbers.

Document Quality Metrics

  • Parse Integrity: Does the extracted text maintain the semantic hierarchy (headings, bullet points)?
  • Table Reconstruction: Are tables preserved in a format (like Markdown or JSON) that an LLM can understand?
  • OCR Accuracy: In scanned documents, what is the character error rate? Even a 5% error rate can turn TX-900 into TK-800, making retrieval impossible.

Stage 1: Retrieval Quality — The Invisible Ceiling

Retrieval is the bedrock of RAG. If your retrieval engine fails to surface the correct information, no amount of prompt engineering or "frontier model" intelligence can save the response. Retrieval failures often masquerade as generation failures. If the LLM says "I don't know," is it because it followed your instructions to be cautious, or because the relevant chunk was 11th in a list where you only retrieved 10?

Traditional vector search (dense embeddings) is excellent at capturing semantic relationships — the "vibe" of a query. It knows that "How do I fix a leaky pipe?" is related to "plumbing repair." However, it is notoriously poor at handling specific identifiers, SKU numbers, or rare technical terms.

In enterprise environments, common queries often look like: "What is the torque spec for the TX-900 bolt?" A vector embedding might find three different bolt manuals, but not necessarily the TX-900, because "bolt" and "torque" dominate the semantic space.

This is where Hybrid Search becomes mandatory. By combining vector search with keyword-based search (like BM25), you ensure that exact matches are prioritized alongside semantic matches.

The Role of Reranking

Retrieval often retrieves a "candidate set" of, say, 50 documents using simple vector math. Passing 50 documents to an LLM is a recipe for noise and high costs. However, taking the top 5 by cosine similarity is risky because embeddings are "lossy" representations.

A Reranker (a Cross-Encoder model) performs a "second pass" over the candidate set. It looks at the query and the document simultaneously to determine their match strength. This is more expensive than vector math, but doing it on 50 documents takes only 200–400 ms and significantly improves the quality of the "ingredients" you send to the LLM.

Advanced Retrieval Pattern: HyDE

If your user queries are short or poorly phrased ("policy for travel?"), they may not have enough semantic density to find good chunks. One technique to fix this is HyDE (Hypothetical Document Embeddings). You ask an LLM to "generate a fake, ideal answer to this query," then use the embedding of that hypothetical answer to search the database. Often, the fake answer is more semantically similar to your documents than the original query was.

Stage 2: Context Quality — Signals in the Noise

Once you have the right chunks, you must decide how to present them. Even with the right data, the LLM's ability to reason is affected by the context's structure and length.

The "Lost in the Middle" Problem

Research has shown that LLMs are significantly better at using information at the very beginning or very end of a context window. If the crucial piece of information is buried in the middle of a 20,000-token context, performance degrades.

To fight this, we use Context Shaping:

  • Relevance Sorting: Ensuring the reranker's top results are at the very top of the prompt.
  • Diversification: If you have 5 very similar chunks, don't include all of them. Include the most relevant one and pick others that cover different parts of the semantic space to avoid redundancy.

Noise Ratio and Context Coverage

  • Noise Ratio: If you retrieve 5 chunks but the answer is only in 1, your noise ratio is 80%. High noise ratios lead to "distraction hallucinations."
  • Context Coverage: If the answer requires facts A, B, and C, and your retriever only found A and B, the system will fail or hallucinate C. Measuring context recall ensures you have the "coverage" needed for a complete answer.

Stage 3: Answer Quality — Grounding and Faithfulness

At the generation stage, we are evaluating the LLM's adherence to the context.

The "Attribution Error"

A common RAG failure is when the LLM uses its training data to "fill in the gaps" of the provided context. If your manual says "Product A is blue" but the LLM's general knowledge says "Product A is often red," it may get it wrong. This is an attribution error.

We measure this using Faithfulness:

  1. Extract every claim made in the LLM's answer.
  2. Check each claim against the retrieved context.
  3. Faithfulness = (Claims supported by context) / (Total claims).

Answer Relevance vs. Context Recall

Your answer could be 100% faithful (contains no lies) but still irrelevant if it didn't actually answer what the user asked. Conversely, a relevant answer might have 0% context recall if the LLM ignored your "only answer based on the context" rule and just used its pre-existing knowledge. You need both metrics to tell the full story.

Stage 4: Operational Quality — The Price of Production

Operational quality is where developer productivity meets budget and user satisfaction.

P99 Latency vs. Average Latency

In RAG, your latency "long tail" is brutal. If one document retrieval takes longer, or the LLM generates a longer-than-usual explanation, the user waits.

  • Time-to-First-Token (TTFT): This is what makes a UI feel "snappy."
  • Generation PPS (Packets Per Second): How fast the answer streams once started.
  • Reranker Overhead: If your reranker adds 1 s to every query, is the quality gain worth the friction?

Semantic Caching

To reduce costs, you can implement a semantic cache. Before running a retrieval/LLM loop, check if a "semantically identical" query has been answered recently. If a user asks "What is the Wi-Fi password?" and an hour ago someone asked "how to join the guest network?", you can likely serve the same answer.

The Quality Model Artifact: A Four-Stage Framework

Stage What it Measures Metric Examples What it Tells You What it Misses
Retrieval Searching for the right data. Recall@k, MRR, Hit Rate. Do we have the raw ingredients for the answer? If the information is formatted in a way the LLM can use.
Context Preparing the data for the LLM. Context Precision, Noise Ratio. Are we overwhelming the model with noise or redundant data? If the LLM is smart enough to generate a correct response.
Answer Factual accuracy and grounding. Faithfulness, Hallucination Rate. Is the LLM being honest and helpful relative to the context? Why the answer is wrong (retrieval vs. logic).
Operations Real-world reliability and cost. P99 Latency, Cost/Query, TTFT. Is the system viable for production users and scale? The actual correctness of the answers.

Strategy: Automated Metrics vs. Human Review

When to trust an automated metric (RAGAS / LLM-as-judge)

  1. For Regression Testing: Use RAGAS in your CI/CD pipeline. If a code change drops your Faithfulness score across a set of 100 test queries, stop the deployment.
  2. For Hyperparameter Tuning: Use automated metrics to decide if top_k=5 is better than top_k=7. Humans cannot look at enough data to make this statistically valid.

When Human Review is necessary

  1. Golden Set Initialization: An expert must define the first 50–100 queries and their "ideal" source documents. This is the only way to "ground" your automated metrics.
  2. Logic-Heavy Tasks: Automated judges often fail to spot complex missing logic (e.g., "The model forgot to adjust for the tax rate mentioned in Section 4").

10 Common RAG Failure Modes (And How to Spot Them)

  1. The Silent Miss: Retrieval found nothing, but you have no "out of scope" detector, so the LLM guesses. (Stage 1)
  2. Token Overload: You sent 15,000 tokens of context and the LLM "forgot" the facts in the middle. (Stage 2)
  3. The "Stale Chunk": Retrieval found an old version of a document. (Stage 0)
  4. Semantic Drift: Your embedding model wasn't trained on your technical jargon, so it thinks "Apple" (the fruit) and "Apple" (the company) are the same thing. (Stage 1)
  5. Formatting Sabotage: You passed a poorly parsed table that looks like a wall of text. (Stage 0)
  6. Prompt Instruction Conflict: You told the LLM to "be concise," so it cut out half the required facts. (Stage 3)
  7. Latency Death Spiral: You added a reranker and HyDE, and now every query takes 12 seconds. (Stage 4)
  8. The False Positive: RAGAS says the answer is 100% faithful because the LLM cited the docs correctly, but the docs themselves are wrong. (Stage 0)
  9. Budget Bleed: You are indexing 1M documents that no one ever queries, paying for massive vector store clusters. (Stage 4)
  10. The Citation Lie: The LLM provides a correct answer but points to the wrong document as the source. (Stage 3)

Conclusion: Engineering the System

If you take only one thing from this article, let it be this: stop changing your prompt until you have measured your retrieval recall.

RAG is not a magic solution to information search; it is an assembly line. Every stage of that line — Document Prep, Retrieval, Context Shaping, and Generation — can fail. Treating the whole thing as a single black box is a recipe for frustration and unreliable production systems.

By measuring each stage independently, you turn a "vibe-based" AI project into a predictable engineering project. You can debug failures, you can set performance SLAs, and most importantly, you can sleep at night knowing your system isn't just a very expensive hallucination engine.


Implementation Checklist for Seniors

  1. Retrieval: Implement a "Golden Set" (50+ queries) and measure your Recall@k. If it's below 80%, stop prompt tuning and start hybrid search / reranking.
  2. Context: Monitor your "Noise Ratio." If you're sending 10 chunks to get 1 answer, your context is too noisy.
  3. Generation: Run an automated "Faithfulness" check on a 10% sample of production queries. Watch for when the LLM brings in outside knowledge.
  4. Operations: Track P99 latency and Cost-per-Query. Use a semantic cache if you have high volume on repetitive queries.

Related Articles:

Open Source Projects:

  • Agent Shaker — task-oriented multi-agent orchestration for .NET.

Discuss an enterprise AI architecture or delivery challenge → techbuzzz.me

Related Articles