← Back to articles Blog

RAG Explained: How Retrieval-Augmented Generation Works (And What It Costs)

Emmanuel Ekunsumi · 6 min read · 2026-07-12

Large language models have a knowledge cutoff — they can only answer based on what they learned during training. Retrieval-Augmented Generation (RAG) fixes this by giving the model access to external documents at query time, without retraining or fine-tuning.

How RAG works

RAG has two phases:

1. Indexing (done once)

  1. Split your documents into chunks (typically 200-500 tokens each)
  2. Embed each chunk using an embedding model (text-embedding-3-small is the standard)
  3. Store the embeddings in a vector database (Pinecone, Weaviate, pgvector, Chroma)

2. Retrieval + generation (at query time)

  1. Embed the user's query
  2. Find the most similar chunks in the vector database (cosine similarity)
  3. Inject the top 3-5 chunks into the LLM prompt as context
  4. The LLM generates a response grounded in the retrieved documents
// Simplified RAG pipeline
const queryEmbedding = await openai.embeddings.create({
  model: 'text-embedding-3-small',
  input: userQuery
})

const relevantChunks = await vectorDB.search({
  vector: queryEmbedding.data[0].embedding,
  topK: 5
})

const context = relevantChunks.map(c => c.text).join('\n\n')

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: `Answer using only this context:\n${context}` },
    { role: 'user', content: userQuery }
  ]
})

RAG vs fine-tuning

RAGFine-tuning
Knowledge updatesReal-time (re-index docs)Requires retraining
Setup costLow (hours)High (days + GPU cost)
Inference costHigher (extra context tokens)Same as base model
Hallucination riskLower (grounded in docs)Higher
Best forKnowledge retrieval, Q&AStyle, format, behavior

The token cost problem with RAG

RAG adds tokens to every query. If you inject 5 chunks of 400 tokens each, that's 2,000 extra input tokens per call. At scale, this compounds fast:

How to keep RAG token costs under control

The most expensive RAG systems are the ones that retrieve too many chunks and send them to the most expensive model. Right-sizing both cuts costs 80% with no quality loss.

Tip: Wrap your RAG pipeline's LLM calls with Tokoscope to see exactly how many tokens the context injection adds per query, and track whether the overhead is justified by output quality.

Track RAG token costs in production

Tokoscope shows you token usage per call, so you can see exactly what your context injection costs. Free to start.

Get started free →