AI Engineering7 min read

RAG Engineer

Builds production RAG pipelines - chunking strategies, embedding selection, vector search, reranking, and evaluation for LLM knowledge-retrieval systems.

RAGretrievalvector searchembeddingsLLMknowledge base

The RAG Engineer designs retrieval-augmented generation pipelines that give LLMs accurate, up-to-date knowledge without fine-tuning. It covers the full stack: document ingestion, chunking strategy, embedding model selection, vector store setup, retrieval, reranking, prompt assembly, and answer generation. Production RAG quality is dominated by retrieval quality - generation is rarely the bottleneck.

Chunking Strategy

Chunk size is the single most impactful RAG parameter. The right size depends on your document structure:

Document typeChunk sizeOverlapStrategy
API documentation512 tokens50Section-aware (split on H2/H3)
Legal / contracts256 tokens64Sentence-level with context window
Code filesFunction-level0AST-based (split on function boundaries)
PDFs / books1024 tokens128Paragraph-aware, preserve headings
Support ticketsWhole ticket0No chunking - tickets are already atomic

Retrieval Pipeline

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

async function ragQuery(userQuery: string, vectorStore: VectorStore): Promise {
  // Step 1: Embed the query with the SAME model used to embed documents
  const queryEmbedding = await embed(userQuery);

  // Step 2: Retrieve top-k candidates
  const candidates = await vectorStore.search(queryEmbedding, { topK: 20 });

  // Step 3: Rerank - LLM-based reranker is more accurate than cosine similarity alone
  const reranked = await rerank(userQuery, candidates, { topN: 5 });

  // Step 4: Assemble context
  const context = reranked
    .map((c, i) => `[Source ${i + 1}]: ${c.text}`)
    .join('\n\n');

  // Step 5: Generate answer
  const response = await client.messages.create({
    model: 'claude-sonnet-5',
    max_tokens: 1024,
    system: 'Answer the question using ONLY the provided sources. If the answer is not in the sources, say so explicitly.',
    messages: [
      { role: 'user', content: `Sources:\n${context}\n\nQuestion: ${userQuery}` }
    ],
  });

  return response.content[0].type === 'text' ? response.content[0].text : '';
}

Evaluation Framework

RAG quality is measured on three axes:

  • Retrieval recall - does the correct chunk appear in the top-5 results? Measure with a labeled eval set of question → expected source pairs.
  • Answer faithfulness - does the answer contain only claims supported by the retrieved sources? Score with an LLM judge: "Is this claim supported by the context? Yes/No"
  • Answer relevance - does the answer address the question? Score with an LLM judge or ROUGE-L against reference answers.

Target: retrieval recall > 0.85, faithfulness > 0.90 before shipping to production.

Common Failure Modes

  • Embedding model mismatch - query embedded with a different model than documents → cosine similarity is meaningless. Use the same model for both.
  • Chunk too large - retrieval returns the right section but the relevant sentence is buried in 2,000 tokens → LLM misses it. Reduce chunk size or use sentence windowing.
  • No reranking - vector search alone has ~70% precision; reranking with a cross-encoder pushes this to ~90%.
  • No metadata filtering - user asks about 2026 pricing but retrieves 2023 docs. Always add date, category, and source metadata to chunks and filter before retrieval.

Explore all agents

Browse the full library of specialized AI agents.