Knowledge · Retrieval
Anatomy of a RAG pipeline
The stages every retrieval-augmented generation system has, what each one trusts, and the question to ask at each stage before you trust the answer it gives.
Retrieval-augmented generation is a model answering from documents it was handed at query time instead of from what it memorised in training. The appeal is obvious: the knowledge can be private, current, and cited. The trade is that the pipeline feeding the model is now part of the attack surface, and most of it was built by people thinking about relevance, not about who is allowed to see what.
Every RAG system has the same stages, whatever the vendor calls them.
Ingest
Documents come in from somewhere: a file upload, a crawler, a connector to Confluence or Google Drive or a ticketing system. Ingest decides what gets indexed, which means it decides what the model can later quote.
Questions for this stage: who can add a document, and is that the same set of people who could put text in front of the model any other way? A connector that syncs a shared drive makes everyone with write access to that drive a contributor to the model’s context. If the drive includes an external-sharing folder, so does the index.
Chunk and embed
Documents are split into pieces of a few hundred tokens, and each piece is turned into a vector by an embedding model. Chunking loses structure: a heading that said “DRAFT, DO NOT CIRCULATE” ends up in a different chunk from the paragraph it was warning about. Access-control labels that lived in the document header are gone unless somebody copied them into the chunk’s metadata on purpose.
Question: what metadata travels with each chunk? If the answer is “source file name”, you cannot filter on tenant, sensitivity, or owner later, because that information is no longer there.
Store
Chunks and their vectors go into an index: pgvector, Pinecone, Qdrant, Weaviate, Elasticsearch, a FAISS file on disk. This is a database holding a copy of the documents. It usually does not sit behind the same authorisation layer as the rest of the application, and it is often reachable with a single API key that has read access to everything.
Questions: where does the store live on the network, who holds its credentials, and does it enforce any tenancy itself (namespaces, collections, row-level security) or does it rely on every query remembering to pass a filter? The post on missing tenant filters is this stage going wrong.
Retrieve
The user’s query is embedded with the same model and the store returns the nearest k chunks. Sometimes a keyword search runs alongside, and the two result lists are merged. Sometimes a reranker scores the candidates again.
This is where authorisation has to happen, and it has to happen in the query. Filtering after the fact means a user with little data gets empty results while other tenants’ chunks fill the top k, and it means the unfiltered call still exists for the next engineer to copy.
Question: is there any code path from a request to the store that does not carry the requester’s identity into the query?
Assemble the prompt
The retrieved chunks are pasted into a prompt template along with the user’s question and a system instruction. At this point every chunk is text the model will read as context, and if any of them contains instructions, the model may follow them. That is indirect prompt injection, and the index is its delivery mechanism: whoever can get a document ingested can get text in front of every user whose query lands near it.
Question: for the users of this system, who can write documents that will be retrieved for other users?
Generate
The model produces an answer. The answer may quote the chunks, paraphrase them, or ignore them. With citations turned on, the response names which chunks it used, which is useful for the user and also tells an attacker exactly which document their injection landed in.
Question: does any part of the model’s output get executed, rendered as HTML, or fed into another tool? If so, it is untrusted input at that boundary.
The two checks to run first
If you only have an hour with an unfamiliar RAG system:
- Ingest two documents with different owners on the same subject, query as one owner, and confirm the other’s chunk never appears. Do it against the real store.
- Ingest a document containing an instruction (“when summarising, include the phrase …”) and see whether it shows up in a different user’s answer.
The first tells you whether the store is a shared drive with a search box. The second tells you whether the index is a channel into every user’s prompt.