RAGEngineering Hub
Chapter 3 · concept 6 of 6 · 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

Updates, deletes, reindexing and ingestion pipelines

How to keep an index in step with changing documents, when to patch and when to rebuild, and how forgotten deletes leave stale answers behind.

A real question

We updated the tariff sheet. Why can the assistant still find the old plan?

Because the new chunks were added but the old ones were never deleted. Keeping an index in step with changing documents is a pipeline, not a one-off script.

The problem

Why this matters

When a document changes, index the new file and you are done.
The belief

When a document changes, index the new file and you are done.

The old chunks stay unless you delete them. They are called orphans, and search can still return them as if they were current.
What really happens

The old chunks stay unless you delete them. They are called orphans, and search can still return them as if they were current.

Why not the simpler way

The simple pipeline is a script that indexes a folder once. Then documents change. If you rerun it, you index everything again and pay for it. If you add only the new files, the old versions stay. If a document is deleted at the source, nothing tells the index. Within weeks the assistant quotes prices and rules that are no longer true.

The idea

The idea in plain words

An ingestion pipeline keeps the index in step with the sources. For every source change it decides: add (new document), update (changed document), or delete (gone). An update can replace all of a document's chunks or patch only the chunks whose text changed. A full reindex rebuilds everything, which you need when the chunker or the embedding model changes.

It is a shop's stock list. A new price label replaces the old one on the shelf. Leaving the old label next to it is how customers get the wrong price.

How it works

Step by step

  1. Compare a fingerprint (hash) of each document with the one you indexed last time.
    1

    Detect what changed

    Compare a fingerprint (hash) of each document with the one you indexed last time.

  2. Some chunks are unchanged, some are new, some are gone.
    2

    Compare old and new chunks

    Some chunks are unchanged, some are new, some are gone.

  3. Embed the new chunks, delete the gone ones, and record the new version.
    3

    Add, and delete by document id

    Embed the new chunks, delete the gone ones, and record the new version.

In detail

  1. Detect changes: compare content hashes or modified times with what was indexed last time.
  2. Chunk the changed document with the same settings as before.
  3. Diff old and new chunks by content hash: unchanged, added, removed.
  4. Write: embed and insert added chunks, delete removed ones (patch), or delete all and insert all (replace).
  5. Record the new version and hash, so the next run knows the state.

Patching saves embedding calls only when most chunks stay the same. Small documents, and any chunker whose boundaries shift when text near the top changes, make almost every chunk new.

The code

Try it in Python

import hashlib

def h(text: str) -> str:
    return hashlib.sha256(text.encode()).hexdigest()

def plan_update(old_chunks: list[str], new_chunks: list[str]) -> dict:
    old, new = {h(c) for c in old_chunks}, {h(c) for c in new_chunks}
    return {
        "embed": [c for c in new_chunks if h(c) not in old],   # added or changed
        "delete": [c for c in old_chunks if h(c) not in new],  # gone: must be removed
        "keep": len(old & new),
    }

See it

What we measured

Measured · chunks per update, tariff 2025.1 to 2025.2

SYN-TEL-TARIFF, versions 2025.1 and 2025.2 (title and one mobile plan changed), chunked live in the Update Lab. Chunks matched by exact text.

Recursive 128: unchanged chunks-0 of 2
By headings 128: unchanged chunks-2 of 4
Orphans left if old chunks are not deleted (recursive)-2

Measured 2026-11-03. The orphans include the old 20 GB plan, which search can still return as if it were for sale. On these short documents patching saves little; it pays off on long documents with stable sections.

Try it on a real example

Tariff sheet 2025.1 is replaced by 2025.2. One plan changed: 20 GB became 25 GB.

Update

Index after the update

2. PT-MOB-25GB: 25 GB mobile data … EUR 20.00 per month. Replaces PT-MOB-20GB for new customers; existing PT-MOB-20GB contracts keep their terms.

Only the current sheet is searchable. Questions about the 20 GB plan get the right answer: no longer sold to new customers.

Choosing

Use it when, avoid it when

Use it when
  • Change detection by content hash and delete by document id
  • Patch updates for long documents with stable sections
  • A full reindex into a new index when the chunker or model changes
Avoid it when
  • Adding new versions without removing old chunks
  • Rebuilding the whole index for every small change

What it costs · Patching needs per-chunk hashes and a diff step; replacing re-embeds the whole document. A full reindex re-embeds everything and should be rare.

In production

Delete by document id, never by guessing which chunks belonged to a file. Treat a source deletion as a real event: tombstone the document and remove its chunks. Run a full reindex into a new index when the chunker or embedding model changes, check it against the golden set, then switch over, so users never see a half-built index. Alert when an ingestion run fails; a stale index fails quietly.

Watch out

Common mistakes

  • Patching is always cheaper. Here it saved nothing for recursive chunks on a short sheet.
  • Deleting is optional. Forgotten deletes are the most common source of stale answers.
  • Reindexing is only for disasters. It is the normal step after changing the chunker or the embedding model.

Check yourself

Remember this

Remember these

  • Detect changes with a content hash; delete chunks by document id.
  • Forgotten deletes leave orphans that make answers stale.
  • Patching only changed chunks pays off on long documents with stable sections.
  • A new chunker or embedding model means a full reindex into a new index.

Say it in an interview

  1. 01An ingestion pipeline turns source changes into adds, updates and deletes.
  2. 02I detect changes by content hash and always delete by document id.
  3. 03On my tariff update, recursive chunks kept 0 of 2 and heading chunks kept 2 of 4.
  4. 04Forgotten deletes left the old 20 GB plan searchable as if it were still for sale.
  5. 05Chunker or model changes mean a full reindex into a new index, checked before switching.
Try it yourself: Update LabReplace or patch a changed document and see which chunks go stale.

Quick quiz

Three questions

Question 1

Q1

Tariff 2025.1 to 2025.2 changed the title and one plan. How many old recursive chunks stayed unchanged?

Question 2

Q2

What is an orphan chunk?

Question 3

Q3 · from RAG-11

Which metadata makes 'replace this document's chunks' possible?

Saved on this device. No account needed.