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.
RAG has two phases:
text-embedding-3-small is the standard)// 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 | Fine-tuning | |
|---|---|---|
| Knowledge updates | Real-time (re-index docs) | Requires retraining |
| Setup cost | Low (hours) | High (days + GPU cost) |
| Inference cost | Higher (extra context tokens) | Same as base model |
| Hallucination risk | Lower (grounded in docs) | Higher |
| Best for | Knowledge retrieval, Q&A | Style, format, behavior |
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:
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.
Tokoscope shows you token usage per call, so you can see exactly what your context injection costs. Free to start.
Get started free →