AI · LLM · Backend · TypeScript
RAG Basics: Building an LLM App That Works with Your Own Documents
How Retrieval-Augmented Generation (RAG) works: chunking documents, embeddings, vector search, hybrid search, reranking, citing sources and measuring RAG quality.

Ask an LLM about your company's test documentation or product manual and it either says "I don't know" or, worse, confidently makes something up. Retraining the model is slow and expensive. Most of the time the right answer is RAG: find the passages relevant to the question and give them to the model as context.
The flow
- Preparation: split documents into chunks, compute an embedding for each, store them in a vector database.
- Query: compute the embedding of the user's question and fetch the most similar chunks.
- Generation: add the chunks to the prompt and ask the model to answer only from those sources.
1. Chunking
The step that affects quality most and gets the least attention. My rules:
- Split by headings and paragraph boundaries, not a fixed character count.
- Roughly 300–800 tokens per chunk, with a small overlap.
- Add context to every chunk: document title, section heading, URL. "This setting is off by default" is useless without knowing which setting.
2. Embeddings and storage
Small and medium projects don't need a separate vector database; pgvector in PostgreSQL is often enough:
CREATE EXTENSION vector;
CREATE TABLE chunks (id bigserial PRIMARY KEY, doc_url text, heading text, body text, embedding vector(1024));
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);
SELECT doc_url, heading, body FROM chunks
ORDER BY embedding <=> $1 LIMIT 8;
3. Hybrid search and reranking
Pure vector search is weak on queries that need exact matches: error codes, version numbers, product names. Combine keyword search (PostgreSQL full text or BM25) with vector search, then cut the top 20–30 results down to 5–8 with a reranker model. In practice this step gives the biggest quality jump.
4. Prompting and citations
const system = `Answer only from the sources below.
If the sources don't contain the answer, say "I couldn't find this in the docs".
End every claim with [source number].`;
const context = chunks.map((c, i) => `[${i + 1}] ${c.heading} (${c.doc_url})\n${c.body}`).join('\n\n');
Citations build user trust and let you trace a wrong answer back to the chunk it came from.
5. You can't improve what you don't measure
Build an evaluation set of 50–100 real user questions and measure two things on every change:
- Retrieval quality: is the right chunk in the top k results?
- Answer quality: is the answer correct, faithful to the sources, free of invention?
I describe how I automate this in LLM evals as test automation.
6. Common mistakes
- Not refreshing embeddings when documents change.
- Skipping permission checks: documents the user can't see must be filtered at retrieval time.
- Stuffing 50 chunks into the context: more context doesn't always mean better answers.
Conclusion
The secret of a good RAG system isn't the model but the retrieval pipeline: meaningful chunks, hybrid search, reranking and measurement. For prompt versioning and tool calling in production, see shipping LLM features to production.


