AI13 min read

RAG in production: infrastructure, data security and why your chatbot answers wrong

Wrong answers from a company chatbot usually do not originate in the language model, but in retrieval, data and permissions. An overview of what matters in operation and what to check first.

Summary

When a company chatbot answers wrong, the fault lies more often in retrieval and data than in the language model. In its own tests, Anthropic showed that combining contextual embeddings, contextual BM25 and reranking reduced the share of failed retrievals from 5.7% to 1.9%. Security deserves the same attention as quality: the EchoLeak vulnerability (CVE-2025-32711) in Microsoft 365 Copilot showed that a single document with planted instructions can exfiltrate internal data from a RAG system. Production RAG is therefore primarily an infrastructure and operations discipline – ingestion pipeline, access control, evaluation and monitoring.

Where the answer gets lost: seven failure points

RAG (retrieval-augmented generation) looks simple: documents are split into chunks and converted to vectors, the most similar chunks are retrieved for a query, and the model composes an answer from them. In production, however, the answer can get lost in several places, and each one needs a different fix.

A good map is provided by the study Seven Failure Points When Engineering a Retrieval Augmented Generation System (Barnett et al., January 2024), based on three case studies from research, education and biomedicine. It describes seven failure points:

Failure pointWhat happenedWhere to look for a fix
Missing contentThe answer is not in the documents at allSource coverage, the ability to say “I don't know”
Missed the top-ranked documentsThe answer exists but did not rank high enoughHybrid search, reranking
Not in contextThe document was found but dropped out when the context was assembledNumber and size of chunks, consolidation strategy
Not extractedThe answer was in the context, but the model did not find itLess noise in the context, prompt, model
Wrong formatThe model ignored the request for a table or listPrompt, structured output
Incorrect specificityThe answer is too general or too specificQuery handling, clarifying questions
Incomplete answerPart of the information from the context is missingBreaking the query into sub-questions

The first three points are purely a matter of data and retrieval. Swapping the language model for a newer one does nothing about them. The study's authors also reach two conclusions worth treating as default assumptions: a RAG system can only be validated during operation, and its robustness cannot be designed up front but evolves over time.

Retrieval: hybrid search, reranking and chunking

Pure vector search captures meaning well but fails on exact strings – a contract number, a product code, the acronym of an internal system. Full-text search (BM25) is the opposite. Hybrid search runs both queries in parallel and merges the results. Azure AI Search uses Reciprocal Rank Fusion for this: each document receives a score of 1/(rank + k) for its position in each list, and the scores are summed; according to the documentation, the algorithm works best with a small value of k, such as 60.

How much difference the individual layers make was measured by Anthropic in its post on Contextual Retrieval (September 2024). The metric was the share of queries for which the relevant chunk did not make it into the top 20 chunks:

  • embeddings enriched with document context: a drop from 5.7% to 3.7% (35% lower),
  • after adding contextual BM25: a drop to 2.9% (49% lower),
  • after adding reranking: a drop to 1.9% (67% lower).

These are a vendor's measurements on its own datasets, so the absolute numbers on your documents will come out differently. The order of steps, however, applies generally: hybrid first, then reranking, and only then model tuning.

Chunking is not a detail

The seven-failure-points study notes that chunking sounds trivial, yet its quality affects the embeddings and therefore how well chunks match queries. A typical problem: a paragraph pulled out of an internal policy does not say which policy, which version or which branch it applies to. Contextual Retrieval solves this by adding to each chunk, before indexing, a short description of where it sits in the document.

When you do not need RAG

In the same post, Anthropic states that a knowledge base smaller than 200,000 tokens (roughly 500 pages) can be put into the prompt in its entirety. For a small, stable set of documents, that is simpler than running an index, ingestion and evaluation. You pay for it in cost and latency on every query, and it does not solve document-level access control.

Agentic RAG

Compound questions (“compare the terms of two contracts”) cannot be covered by a single index query. The agentic approach breaks the query into sub-queries, runs them in parallel and merges the results. In Azure AI Search, according to documentation updated in September 2026, agentic retrieval is generally available in REST API version 2026-04-01, while LLM-based query planning and answer synthesis remain in preview. Expect higher latency and cost per query – each sub-query is reranked separately – and the fact that another LLM call becomes part of a chain you have to measure.

Vector database, pgvector or a managed service

The choice of store is often made first, yet it has less influence on answer quality than chunking and reranking. Operational criteria matter more: who will run it, how filtering by permissions works and whether the store supports hybrid search.

OptionWhen it makes senseWhat to watch out for
pgvector in PostgreSQLThe team already runs PostgreSQL; permissions and metadata live in the same database; a JOIN and transactions are enoughIndexing of the vector type only up to 2,000 dimensions; HNSW has a better speed/recall trade-off than IVFFlat but takes longer to build and uses more memory; you assemble hybrid search and result fusion yourself
Azure AI SearchMicrosoft environment, documents in SharePoint and Azure Storage; hybrid search, RRF and semantic ranker in one serviceNative ACLs for ADLS Gen2 and SharePoint are still in preview
Amazon Bedrock Knowledge BasesAWS environment; you want managed ingestion and to keep the choice of store openHybrid search only on some stores; you have to trigger data synchronization yourself
Dedicated vector databaseLarge volumes, high throughput, specific filtering requirementsAnother system to operate, back up, secure and audit

The pgvector details come from the project documentation. For Bedrock Knowledge Bases, the AWS documentation lists the supported stores as OpenSearch Serverless, managed OpenSearch Service clusters, S3 Vectors, Aurora with pgvector, Neptune Analytics, Pinecone, Redis Enterprise Cloud and MongoDB Atlas. Hybrid search, however, is according to the same documentation supported only for Amazon RDS, OpenSearch Serverless and MongoDB with a filterable text field; elsewhere the query silently falls back to pure semantic search. By default, the service returns at most five chunks.

The counterargument to “just use pgvector”: as soon as you need high-quality full-text search in Czech, result fusion and reranking, you are writing and maintaining a piece of a search engine yourself. If the team lacks the capacity, a managed service works out cheaper despite the higher running cost.

Data security: permissions must be enforced at retrieval time

The most common RAG security flaw is not an exotic attack but an index with broader permissions than the user asking the question. Ingestion runs under a service account that reads everything, and the chatbot then happily summarizes payroll documents for anyone. OWASP ranks this among the top risks in LLM08:2025 Vector and Embedding Weaknesses: unauthorised access and data leakage, cross-context leaks in a shared vector database, embedding inversion and data poisoning. As defenses it lists fine-grained access control with logical partitioning of datasets, source validation, data classification and immutable retrieval logs.

The principle is simple: the permission filter belongs in the index query, not in the prompt. The instruction “do not reveal confidential documents” is not a security control. What never reaches the model's context, the model cannot leak.

In Azure AI Search, the security filter pattern is generally available: group identifiers are stored with each document at indexing time, and the application adds them to the query filter.

POST /indexes/securedfiles/docs/search?api-version=2026-04-01
{
  "search": "contract termination terms",
  "filter": "group_ids/any(g:search.in(g, 'group_id1, group_id2'))"
}

The documentation states explicitly that in this pattern the identity is just a string in the filter – no authentication or authorization takes place on the service side. Your application is therefore responsible for building the filter correctly. The variants in which the service compares the user's token with permissions inherited from the source (ACLs and RBAC for ADLS Gen2, ACLs from SharePoint, Microsoft Purview sensitivity labels) are, according to the access control overview, still in preview as of September 2026.

Permissions go stale just like content

When you revoke someone's access to a folder or delete a document, the index does not know until a synchronization runs. Bedrock Knowledge Bases, for example, according to the documentation, require a sync to be started after every change to the source (in the console or by calling StartIngestionJob); it is incremental and removes deleted documents from the store. Treat the time between a change in the source and its reflection in the index as a security parameter, and measure it.

Indirect prompt injection: the document as an attack vector

By design, RAG puts text into the model's context that neither the user nor the developer wrote. OWASP LLM01:2025 describes this as indirect prompt injection: the model accepts input from an external source, and the content in it alters the model's behavior. OWASP also states that neither RAG nor fine-tuning fully eliminates this vulnerability.

EchoLeak showed that this is not theory. The vulnerability CVE-2025-32711, published on 11 June 2025, is described in the NVD as AI command injection in M365 Copilot that allows an unauthorised attacker to disclose information over a network. Microsoft gave it a CVSS score of 9.3 (critical), NIST 7.5. According to an analysis published on arXiv (Reddy and Gujral, September 2025), a crafted e-mail and no user action were enough: the attack chained a bypass of the prompt injection classifier, a bypass of link redaction via Markdown reference-style links, auto-loaded images and a Teams proxy allowed by the content security policy.

The second category is knowledge base poisoning. The paper PoisonedRAG (Zou et al., accepted at USENIX Security 2025) reports a 90% attack success rate when injecting five malicious texts per target question into a knowledge base of millions of texts; according to the authors, the defenses they evaluated were insufficient. This is a lab result, but the lesson is practical: whoever can write to the sources you index can influence the answers.

Defense in layers

  • Separate sources by trustworthiness. Incoming e-mails, customer tickets and web content do not belong in the same index and context as internal policies.
  • Limit what the output can do: do not automatically render images and links to external domains, and set a strict content security policy.
  • Give the RAG application the least privileges possible and require human approval for risky actions.
  • Only people you would trust to edit the chatbot's answers should be able to write to indexed sources.
  • Include adversarial tests in regular testing – a document with an embedded instruction belongs in the test set.

None of these measures is sufficient on its own; in the EchoLeak case, both input and output filters were bypassed. The goal is that a successful injection has nothing to reach and no way to get data out.

Ingestion as a pipeline, evaluation as a test

Ingestion is a build. It has inputs (documents, parser version, chunking strategy, embedding model), an output (the index), and it should be reproducible. The same rules apply as for any other CI/CD pipeline: configuration in version control, separate environments, controlled access to secrets and the ability to roll back to a previous version of the index.

  • Version the chunking and the embedding model. Changing either of them means re-indexing everything; plan it as a migration with a parallel index and a cutover.
  • Handle deletions and duplicates. An outdated version of a policy in the index is a common source of “wrong” answers that are actually faithful – just to an old document.
  • Store metadata. Source, owner, validity date, classification and groups with access; without them you can neither filter nor cite.
  • Check content at the input. Scan for secrets and personal data before they reach the index.

Evaluation: measure retrieval and generation separately

Without measurement, you cannot tell whether a chunking change helped. The Ragas framework offers RAG metrics for context precision and context recall (retrieval quality) and faithfulness and response relevancy (answer quality). Faithfulness is defined as the share of claims in the answer that are supported by the retrieved context, out of all claims in the answer.

Two caveats. LLM-based metrics call a model themselves, so they are not deterministic and they cost money. And high faithfulness does not mean a correct answer: the model can faithfully summarize an outdated or planted document. That is why you also need a set of questions with known correct answers and sources, put together with people from the business, and run it as a regression test on every pipeline change.

In operation, log for every answer the query, the user's identity, the IDs of the retrieved chunks, and the index and prompt versions. This is the basis for debugging as well as for incident investigation; more on the topic in our article on security monitoring and logging. Keep in mind, though, that query logs themselves contain sensitive data and need the same protection as the source documents.

What to do on Monday

A quick audit of your existing RAG solution

Find out which identity ingestion runs under and whether user permissions are enforced by a filter at retrieval time, not by the prompt.
Test it: using an account without access, ask about the contents of a confidential document.
Measure how long it takes for a document deletion and a permission revocation to be reflected in the index.
List everyone who can write to indexed sources, including external ones (e-mail, tickets, web).
Take 30–50 real queries with wrong answers and sort them by the seven failure points. It will show you where to invest.
Verify that search is hybrid and reranking is enabled; with Bedrock, check that your chosen store supports hybrid search at all.
Create a regression set of questions with correct answers and run it on every change to chunking, model or prompt.
Check whether the chatbot interface renders external images and links from the model's answers.

If the audit shows that documents are scattered, without owners and without classification, start there. RAG on top of disorganised data only makes the mess accessible faster – the same pattern as with infrastructure that is not ready for AI.

Key takeaways

  • Most wrong answers originate before the model. Missing content, poor result ranking and loss during context assembly are data and retrieval problems.
  • Hybrid search and reranking are the foundation, not an add-on. In Anthropic's measurements, together with adding context to chunks, they reduced the share of failed retrievals from 5.7% to 1.9%; verify the benefit on your own data.
  • Permissions are enforced in the index query. The prompt is not a security control, and synchronization lag is a security parameter.
  • Every indexed document is a potential instruction. EchoLeak demonstrated a zero-click data leak from a production system; defend yourself by separating sources, applying least privilege and restricting output.
  • Ingestion is a pipeline and evaluation is a test. Version everything, measure retrieval and generation separately, and expect validation to happen only in operation.
  • Choose the store based on operations. pgvector, Azure AI Search and Bedrock Knowledge Bases all work; the difference is what the service does for you and what is still only in preview.

Sources

  1. Barnett et al.: Seven Failure Points When Engineering a Retrieval Augmented Generation System (January 2024)
  2. Anthropic: Introducing Contextual Retrieval (September 2024)
  3. NIST NVD: CVE-2025-32711 (June 2025)
  4. Reddy, Gujral: EchoLeak – The First Real-World Zero-Click Prompt Injection Exploit in a Production LLM System (September 2025)
  5. OWASP GenAI Security Project: LLM08:2025 Vector and Embedding Weaknesses and LLM01:2025 Prompt Injection (2025)
  6. Zou et al.: PoisonedRAG – Knowledge Corruption Attacks to Retrieval-Augmented Generation of Large Language Models (February 2024, revised August 2024)
  7. Microsoft Learn: Document-level access control in Azure AI Search, Security filters, Hybrid search scoring (RRF) and Agentic retrieval (September 2026)
  8. AWS: Amazon Bedrock Knowledge Bases – supported vector stores, query configuration and data synchronization (accessed October 2026)
  9. pgvector: project documentation (accessed October 2026)
  10. Ragas: Available metrics and Faithfulness (accessed October 2026)

Is your company chatbot answering wrong, and nobody knows why?

DevSecOps.cz helps design and review RAG infrastructure in AWS and Azure – from the ingestion pipeline and document access control to evaluation and monitoring.

Or write to info@devsecops.cz. We process your data under our privacy policy.