Back to cheat sheet hub

RAG Cheat Sheet

A compact reference for retrieval-augmented generation: how we move from raw documents to grounded AI answers for QA use cases.

Chunking Embeddings Evaluation

RAG in One Line

Retrieve relevant context first, then ask the model to answer using that context.

Question -> retrieve documents -> build prompt with context -> generate grounded answer

Pipeline Steps

This is the standard mental model we use in class and projects.

1. Collect source documents
2. Clean and chunk text
3. Create embeddings
4. Store vectors in a vector DB
5. Retrieve top matches for a query
6. Send matches + question to the LLM

Chunking Tips

Bad chunks lead to bad retrieval. Keep chunks readable and semantically meaningful.

Typical starting point:
- chunk size: 500-1000 tokens
- overlap: 50-150 tokens
- split by heading / paragraph where possible
- preserve metadata like source, title, section

Embeddings

Embeddings convert text into vectors so semantically similar content can be found efficiently.

Use embeddings for:
- document chunks
- user queries

Compare vectors using similarity:
- cosine similarity
- dot product
- euclidean distance

Retrieval

Retrieval decides what context the model sees, so it directly affects answer quality.

Common strategies:
- top-k similarity search
- metadata filtering
- hybrid search
- reranking

Examples:
- feature = payments
- environment = staging

Prompt Assembly

Tell the model to answer from the retrieved context and avoid unsupported claims.

You are a QA assistant.
Answer the question using only the provided context.
If the context is insufficient, say so clearly.
Return the answer with source citations.

RAG for QA

RAG works especially well when answers must reflect project-specific process, not generic internet advice.

Good QA use cases:
- test strategy lookup
- bug triage guidance
- framework conventions
- past incident recall
- requirement clarification support

Evaluation Triad

These are the three most important quality checks for RAG outputs.

Context relevance:
Were the retrieved chunks relevant?

Groundedness:
Did the answer stay faithful to the context?

Answer relevance:
Did the answer actually solve the question?

Common Failure Modes

Most RAG issues come from retrieval quality or stale sources, not the model alone.

- chunks too large or too small
- weak metadata
- stale documents
- missing sources
- too much irrelevant context
- no evaluation after changes