RAGEngineering Hub
Chapter 1 · concept 3 of 4 · show all
  1. 1What RAG is, and why
    1. 1.1Why models need outside knowledge, and what hallucination means
    2. 1.2What RAG is: anatomy of a request
    3. 1.3RAG vs fine tuning vs long context and prompt caching
    4. 1.4Use cases and the quality, latency and cost triangle
    5. Chapter overview
  2. 2Tokens, cost and your first RAG
    1. 2.1Tokens and context windows for RAG
    2. 2.2The RAG lifecycle and your first RAG in miniature
    3. Chapter overview
  3. 3Getting documents in
    1. 3.1Content sources and file formats
    2. 3.2Parsing, layout and OCR
    3. 3.3Tables, images and mixed content
    4. 3.4Cleaning, normalization and quality gates
    5. 3.5Metadata, document identity and versioning
    6. 3.6Updates, deletes, reindexing and ingestion pipelines
    7. Chapter overview
  4. 4Chunking
    1. 4.1Why we chunk, and the main chunking strategies
    2. 4.2Structure aware chunking and tables
    3. 4.3Chunk size, overlap and token based sizing
    4. 4.4Contextual chunking and late chunking
    5. 4.5Parent child and multi representation indexing
    6. 4.6Chunk metadata, provenance and the chunking debugging playbook
    7. Chapter overview
  5. 5Embeddings
    1. 5.1Embeddings and similarity from first principles
    2. 5.2What embeddings cannot do
    3. 5.3Choosing an embedding model: size, cost, language
    4. 5.4Embedding model migration and index rebuilds
    5. Chapter overview
  6. 6Vector stores and scores
    1. 6.1Vector databases, ANN indexes and choosing a store
    2. 6.2Score calibration and thresholds
    3. 6.3Late interaction and multivector retrieval
    4. Chapter overview
  7. 7Finding the right chunks
    1. 7.1Query understanding, rewriting and expansion
    2. 7.2Dense retrieval and keyword search with BM25
    3. 7.3Hybrid retrieval and rank fusion
    4. 7.4topK, no good match, and metadata filtering
    5. 7.5Routing, diversity, deduplication and caching
    6. 7.6Reranking: two stage, cross encoder and LLM rerankers
    7. Chapter overview
  8. 8When retrieval fails
    1. 8.1Retrieval failure analysis playbook
    2. Chapter overview
  9. 9Building the answer
    1. 9.1Context selection, packing and citations
    2. 9.2Grounding prompts and abstention
    3. 9.3Lost in the middle and context management
    4. 9.4Conflicts, staleness and freshness rules
    5. 9.5Hallucination checks and answer verification
    6. 9.6Conversational RAG, answer UX and RAG APIs
    7. Chapter overview
  10. 10Measuring
    1. 10.1Why measure, and what to measure
    2. 10.2Building a golden dataset
    3. 10.3Retrieval metrics: precision, recall, MRR, nDCG
    4. 10.4Faithfulness and answer correctness
    5. 10.5LLM as judge, and judging the judge
    6. 10.6Offline and online evals, regression tests and slices
    7. Chapter overview
  11. 11Safety and operations
    1. 11.1Prompt injection and poisoned documents
    2. 11.2Access control, privacy and governance
    3. 11.3Index lifecycle operations
    4. 11.4Observability and tracing
    5. 11.5Latency, cost, caching and model routing
    6. 11.6Reliability, degraded modes and launch readiness
    7. Chapter overview
  12. 12Scale and advanced RAG
    1. 12.1Scaling, multi tenancy and enterprise architecture
    2. 12.2Choosing an architecture: naive, advanced, agentic, graph
    3. 12.3Agentic RAG and RAG as an MCP tool
    4. 12.4GraphRAG, structured data and multimodal RAG
    5. Chapter overview

RAG vs fine tuning vs long context and prompt caching

Four ways to give a model your knowledge, what each one is good at, and what they cost per question on the same corpus.

A real question

Should I train the model on my documents, paste them all in, or use RAG?

It depends on three numbers: how big your documents are, how often they change, and how many questions you get. Each option wins somewhere.

The problem

Why this matters

To make a model know our documents, we have to train it on them.
The belief

To make a model know our documents, we have to train it on them.

Training (fine tuning) shapes style and format. For facts that change, giving the model the right page at question time is faster to update and can cite its source.
What really happens

Training (fine tuning) shapes style and format. For facts that change, giving the model the right page at question time is faster to update and can cite its source.

Why not the simpler way

The question "should we use RAG?" often gets a habit for an answer. Some teams build a vector database for twenty documents. Others try to fine tune facts into a model and then cannot update them. With context windows of a million tokens, a third group pastes everything into every prompt. Each choice fits some cases and is expensive in others.

The idea

The idea in plain words

There are four ways to give a model knowledge. Fine tuning changes the model's weights with your examples. Long context puts all your documents in the prompt. Prompt caching does the same but keeps the processed prompt on the provider's side, so repeats cost less. RAG puts only the few passages that matter in the prompt.

Compare it to preparing for questions. Fine tuning is studying for months. Long context is bringing the whole library to every meeting. Caching is leaving the library in the meeting room. RAG is bringing the three pages you need.

How it works

Step by step

  1. You change the model's weights with examples. Good for tone and format; slow to update and cannot cite a source.
    1

    Fine tuning: teach a behaviour

    You change the model's weights with examples. Good for tone and format; slow to update and cannot cite a source.

  2. Simple when the documents are small. You pay for every token on every question, and facts in the middle are used less well.
    2

    Long context: paste everything

    Simple when the documents are small. You pay for every token on every question, and facts in the middle are used less well.

  3. A prompt cache makes the pasted part cheap while it stays warm. RAG sends only the few chunks that matter.
    3

    Cache it, or retrieve a few pages

    A prompt cache makes the pasted part cheap while it stays warm. RAG sends only the few chunks that matter.

In detail

Fine tuning is strong for behaviour: tone, output format, a narrow task. For new facts it is weaker. In a peer-reviewed comparison, retrieval beat unsupervised fine tuning for injecting new knowledge (Ovadia et al., EMNLP 2024). It also cannot cite a source, and every update means another training run.

Long context is the simplest when the corpus is small and stable. Cost and latency grow with every token on every call, and facts in the middle of a long prompt are used less well. Caching removes most of the cost of the repeated part, while the cache is warm. Loading a small knowledge base into the cache instead of retrieving has been called cache augmented generation (Chan et al., arXiv 2412.15605, 2024).

Token budget of one RAG call: 39 instruction tokens, about 1,026 tokens of retrieved chunks, about 13 question tokens, and up to 1,024 tokens reserved for the answer, all inside the context window.ONE CALL · ABOUT 2,102 TOKENS OF A MUCH LARGER CONTEXT WINDOWInstruction ~393 retrieved chunks~1,026Question ~13Answer reserve~1,024
Median prompt in the Mini RAG Lab: 1,078 tokens across 47 golden questions (recursive 512 chunks, top 3). The answer reserve is the max_tokens the lab sends.

RAG keeps the prompt small and the facts updatable: change a document, re-index its chunks, and the next answer uses it. The cost is the retrieval system you have to build and measure.

The code

Try it in Python

def cost_per_question(prompt_tokens, cached_tokens=0, price_in=4.0, price_cached=0.20):
    """USD per question for the input side, prices per million tokens."""
    return ((prompt_tokens - cached_tokens) * price_in + cached_tokens * price_cached) / 1e6

corpus, question, rag_prompt = 7592, 52, 1078
print(f"long context  {cost_per_question(corpus + question):.4f}")
print(f"RAG           {cost_per_question(rag_prompt):.4f}")
print(f"cached corpus {cost_per_question(corpus + question, cached_tokens=corpus):.4f}")

See it

What we measured

Measured · input cost per question, Claude Opus 5.5 list prices

Polder corpus, 7,592 tokens. Prices $4 per million input tokens and $0.20 per million cached input tokens (vendor price list, checked 2026-11-03). Cache writes and output tokens excluded. Tokens counted with o200k, not the priced model's tokenizer, so costs are estimates.

Whole corpus in every prompt (about 7,650 tokens)baseline$0.031
RAG, top 3 chunks (median 1,078 tokens)$0.031$0.0043
Whole corpus cached, question uncached$0.031$0.0017

Calculated 2026-11-03 from measured token counts. Caching wins on this tiny corpus only while the cache is warm; a cold call pays the full write. At 10,000 documents of this size (about 3 million tokens) the corpus no longer fits a 1M window and only RAG remains.

Try it on a real example

Input cost per question on our 7,592 token corpus (Claude Opus 5.5 list prices, checked 2026-11-03)

Option

About 7,650 tokens per question at $4 per million

≈ $0.031 per question · about $31 per 1,000 questions

The simplest option, and the most expensive one here.

Choosing

Use it when, avoid it when

Use it when
  • RAG: large or changing corpora, citations, per-user access
  • Caching: a small, stable corpus with steady traffic
  • Fine tuning: style, format and narrow skills
Avoid it when
  • Fine tuning to add facts that change
  • Long context without caching for high traffic
  • A vector database for a corpus that fits in one cached prompt

What it costs · Fine tuning costs training runs per update. Long context costs every token on every call. Caching costs a write per refresh. RAG costs an index and an evaluation set to keep it honest.

In production

Decide with three numbers: corpus size in tokens, how often it changes, and questions per day. A corpus that fits comfortably in the window, changes rarely and gets steady traffic is a good fit for caching. A corpus that is large, changes daily or needs per-user access control points to RAG. Many systems combine them: fine tune for format, retrieve for facts, cache the fixed instruction.

Watch out

Common mistakes

  • Long context makes RAG obsolete. It moves the line. Above the window size, or with daily changes and access rules, retrieval is still needed.
  • Fine tuning teaches the model your documents. It learns patterns from them; it cannot quote or cite them reliably.
  • RAG is always the cheapest. On a tiny corpus with a warm cache it was not.

Check yourself

Remember this

Remember these

  • Fine tuning changes behaviour; it is a poor way to add facts that change.
  • Long context is simple, but you pay for every token on every call.
  • Caching makes repeated context cheap while it stays warm.
  • RAG scales to large, changing documents and gives citations.

Say it in an interview

  1. 01Four options: fine tuning, long context, prompt caching and RAG.
  2. 02Fine tuning is for behaviour; retrieval is for facts that change and need citations.
  3. 03On my 7,592 token corpus, a warm cache cost $0.0017 a question against $0.0043 for RAG.
  4. 04Above the window size, or with daily updates and access rules, RAG is the only option.
  5. 05I decide with corpus tokens, change rate and traffic, not with a habit.

Quick quiz

Three questions

Question 1

Q1

On the 7,592 token Polder corpus, which option had the lowest input cost per question?

Question 2

Q2

When is fine tuning the right tool?

Question 3

Q3 · from RAG-03

Which of these count against the context window?

Saved on this device. No account needed.