Skip to main content
AI on Kubernetes · GPU/NPULesson 17 of 20

How a RAG Pipeline Answers From Your Documents

Follow one question through retrieval-augmented generation (RAG): documents → chunks → embeddings → vector DB, then question → same embedding model → search → rerank → prompt → LLM. See why the answer is only as good as the chunks that reach the model.

Indexed chunks0
Retrieved—
Prompt tokens—
Answer—
Ingestion · runs ahead of timeQuery · runs per questionDocumentsWiki · PDF · ticketsChunkerSize · overlapEmbedding modelText → vectorVector DBHNSW · metadataUserAsks a questionRAG serviceOrchestratesRerankerTop 20 → top 5LLMModel servingQuestion
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Why a model needs your documents handed to it at question time
  • How chunking and embedding turn documents into searchable vectors
  • Nearest-neighbor search, hybrid keyword search and reranking
  • Bad chunks, stale indexes and mismatched embedding models
  • Filtering by permission before retrieval, not after

When answers go wrong​

FailureSymptomFix
Bad chunkingThe fact exists but is never retrieved, or tables and lists come back cut in halfSplit along headings, paragraphs and table rows. Tune size and overlap, and test with real questions
Stale indexAnswers quote an old version of a documentRe-index on change (events or a frequent CronJob). Delete old chunks by document id when a document changes
Embedding model mismatchSimilarity scores look random after an upgrade, or the query fails on vector dimensionsStore the model name with each chunk, and rebuild the whole index when the model changes
Exact terms missedQuestions about codes, SKUs or names get unrelated chunksAdd keyword search (BM25) and merge results (hybrid search)
Context overflowRequest rejected for length, or the answer ignores later chunksCount tokens, keep fewer and better chunks after reranking, leave room for the answer
No permission filterA user sees content from documents they cannot openStore allowed groups per chunk and filter in the vector query
Model ignores sourcesAnswer contradicts the retrieved chunks or cites nothingTell the model to answer only from the sources and to say when they are not enough. Measure faithfulness

Try it yourself​

# pgvector in a local PostgreSQL (the pgvector image ships the extension)
docker run -d --name pg -e POSTGRES_PASSWORD=pw -p 5432:5432 pgvector/pgvector:pg17
psql postgresql://postgres:pw@localhost:5432/postgres <<'SQL'
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE chunks (id bigserial PRIMARY KEY, doc_id text, groups text[], body text, embedding vector(3));
INSERT INTO chunks (doc_id, groups, body, embedding) VALUES
('refund', '{all}', 'Opened items: 30 days', '[0.9,0.1,0.0]'),
('salary', '{hr}', 'Salary bands 2026', '[0.1,0.9,0.2]'),
('ship', '{all}', 'Shipping takes 3 days', '[0.2,0.1,0.9]');
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops); -- approximate index
-- <=> is cosine distance (smaller is closer). Filter by the user's groups in the same query.
SELECT doc_id, body, embedding <=> '[0.8,0.2,0.1]' AS distance
FROM chunks WHERE groups && '{all,sales}' ORDER BY distance LIMIT 2;
SQL

# Embeddings from an OpenAI-compatible server (many model servers expose this endpoint)
curl -s http://<embedding-server>/v1/embeddings -H 'Content-Type: application/json' \
-d '{"model":"<embedding-model>","input":"How long is the return window?"}' | jq '.data[0].embedding | length' # vector dimension

Real embeddings have hundreds or thousands of dimensions. The three-number vectors above only show how the query works.

Check yourself

  1. The RAG assistant confidently gives a wrong return window, although the right fact exists in your documents. What should you check first?
  2. The team switches queries to a newer embedding model but keeps the stored chunks as they are. What happens?
  3. Questions about an error code like "RMA-114" keep getting unrelated chunks. What fixes this?

Further reading: pgvector · Qdrant docs · Milvus docs · Lewis et al. 2020, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks · How an Answer Is Built One Token at a Time · From API Request to Response