Retrieval-Augmented Generation (RAG)
debt(d9/e7/b7/t7)
Closest to 'silent in production until users hit it' (d9). detection_hints.automated is no — there's no linter that flags 'you should use RAG here' or 'your chunks are too large.' Quality issues surface only when users get wrong answers.
Closest to 'cross-cutting refactor across the codebase' (e7). quick_fix describes building an embedding pipeline, vector DB, retrieval layer, and prompt restructuring — this is not a one-line patch but adding a whole subsystem (embeddings + vector store + retrieval + prompt assembly).
Closest to 'strong gravitational pull' (b7). applies_to spans web/cli/queue-worker; once RAG is the AI feature's foundation, chunking strategy, embedding model, and vector store choice shape every future AI feature change. Tagged vector-database — a load-bearing infrastructure choice.
Closest to 'serious trap' (t7). The misconception is explicit: developers believe RAG eliminates hallucination, but the LLM can still fabricate or ignore the retrieved context. This contradicts the intuitive promise of 'grounded generation' and is a documented, costly misunderstanding.
Also Known As
TL;DR
Explanation
RAG solves two LLM limitations: knowledge cutoff dates and hallucination on specific domain data. The pipeline: (1) documents are chunked and embedded as vectors, (2) stored in a vector database, (3) a user query is embedded, (4) similar vectors are retrieved, (5) the retrieved chunks are injected into the LLM prompt as context, (6) the LLM answers based on the provided context. This enables LLMs to answer questions about private or recent data without fine-tuning.
Common Misconception
Why It Matters
Common Mistakes
- Chunk size too large — chunks that are too big include irrelevant text, diluting signal; too small loses context.
- Not filtering retrieved chunks by relevance score — low-similarity chunks mislead the model.
- Not citing sources in the response — users cannot verify which document the answer came from.
- Using the LLM's training knowledge instead of retrieved context when context is provided — prompt must instruct the model to use only the provided context.
Code Examples
// RAG without relevance filtering — poor chunks mislead LLM:
$allDocs = vectorDB()->search($query, topK: 20); // Too many, low relevance
$context = implode('\n', array_column($allDocs, 'text'));
$response = $llm->complete('Answer based on context: ' . $context . '\n\nQuestion: ' . $query);
// RAG with relevance threshold:
$docs = vectorDB()->search($query, topK: 5, minSimilarity: 0.75);
if (empty($docs)) {
return 'No relevant documentation found for that question.';
}
$context = implode('\n---\n', array_column($docs, 'text'));
$prompt = "Answer the question using ONLY the context below. If the answer is not in the context, say so.\n\nContext:\n$context\n\nQuestion: $query";
return $llm->complete($prompt);