Back to Patterns

Retrieval-Augmented Generation

Agent Patterns

Summary

Retrieval-Augmented Generation (RAG) combines information retrieval with text generation. Relevant documents are retrieved from a knowledge base based on user queries, then provided as context for LLM response generation. This grounding reduces hallucinations and ensures responses reflect current, authoritative information.

How it works

  1. Query Processing: Transform user query into retrieval format
  2. Document Retrieval: Search knowledge base for relevant passages
  3. Context Assembly: Combine retrieved documents with user query
  4. Generation: LLM produces grounded response from augmented prompt
  5. Verification: Optional citation or source attribution

RAG architectures

  • Naive RAG: Retrieve-then-read pipeline
  • Agentic RAG: Iterative retrieval with planning and tool use
  • Hybrid RAG: Combine dense and sparse retrieval
  • Graph RAG: Leverage knowledge graph relationships

Component considerations

  • Retriever: BM25, dense embeddings, hybrid approaches
  • Index: FAISS, Pinecone, Weaviate, Elasticsearch
  • Chunking: Fixed-size, semantic, recursive strategies
  • Retrieval: Top-k, MMR, similarity thresholds

Build This Pattern

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.

Build a RAG (Retrieval-Augmented Generation) system with hybrid retrieval. ROLE: You are a retrieval-augmented generation system that ingests documents, indexes them for search, retrieves relevant context, and generates grounded answers with citations. CONSTRAINTS: - Document chunking: 1000 character chunks with 200 character overlap - Embedding model: configurable; default to text-embedding-3-small - Vector database: pgvector for storage; hybrid search combining semantic similarity and keyword matching - Maximum document size: 50MB; larger documents must be split into parts - Citation requirement: every answer must cite at least one source document TOOL CALLING: - Use function calling for: ingest_document(file_path?, text?, metadata?), index_document(document_id, chunks[]), search_context(query, top_k?, filters?), generate_answer(query, context[]), get_index_stats() - Each tool returns structured JSON with retrieval data and metadata STRUCTURED OUTPUT: - Document ingestion must return JSON: { document_id: string, filename: string, chunk_count: number, total_characters: number, indexed_at: string } - Search results must return JSON: { query: string, results: [{ chunk_id: string, document_id: string, content: string, score: number, metadata: Record<string, any> }], total_results: number, search_time_ms: number } - Generated answer must return JSON: { answer: string, sources: [{ document_id: string, chunk_id: string, content_snippet: string }], confidence: number, generation_time_ms: number } - Index stats must return JSON: { total_documents: number, total_chunks: number, index_size_mb: number, avg_chunk_size: number } CHAIN OF THOUGHT: - Ingestion: receive document → validate format → extract text → chunk content → generate embeddings → store in vector DB - Indexing: process chunks → create embeddings → store with metadata → update search index - Retrieval: parse query → generate query embedding → perform hybrid search → rank by relevance → return top-k - Generation: assemble context from retrieved chunks → prompt LLM with query and context → generate answer with citations FEW-SHOT EXAMPLES: Document: 'AI Agents Guide.pdf' (15 pages) Ingestion: { document_id: 'doc_123', filename: 'AI Agents Guide.pdf', chunk_count: 42, total_characters: 42000, indexed_at: '2025-01-15T10:30:00Z' } Query: 'What are the benefits of AI agents?' Search: [{ chunk_id: 'chunk_45', content: 'AI agents provide automation, 24/7 availability...', score: 0.89 }] Answer: { answer: 'AI agents provide businesses with automation, 24/7 availability, and cost reduction...', sources: [{ document_id: 'doc_123', chunk_id: 'chunk_45', content_snippet: 'AI agents provide automation...' }], confidence: 0.91 } EVALUATION CRITERIA: - Retrieval precision: percentage of retrieved chunks that are topically relevant - Answer grounding: percentage of answer claims supported by retrieved sources - Citation accuracy: percentage of citations that correctly reference source content - Index completeness: percentage of ingested documents successfully indexed The system should: 1) Implement pipeline with separate modules for ingestion, indexing, retrieval, and generation, 2) For ingestion: accept document uploads (PDF, DOCX, TXT, MD), use recursive chunking (1000 char chunks, 200 overlap) with document-level metadata tracking, 3) Generate embeddings using configurable embedding model and store in vector database with pgvector, 4) For retrieval: implement hybrid search combining semantic similarity and keyword matching, 5) For generation: pass retrieved chunks with source metadata to LLM for answer generation with inline citations, 6) Handle unsupported file formats with clear error messages, 7) If embedding generation fails, queue documents for retry, 8) Handle empty search results by returning 'no relevant sources found' response instead of hallucinating, 9) Handle very large documents with progress tracking across chunks, 10) Deduplicate overlapping chunks from multiple documents at retrieval time.