Chapter 9 · concept 1 of 6 · show all
2Tokens, cost and your first RAG
4Chunking
- 4.1Why we chunk, and the main chunking strategies
- 4.2Structure aware chunking and tables
- 4.3Chunk size, overlap and token based sizing
- 4.4Contextual chunking and late chunking
- 4.5Parent child and multi representation indexing
- 4.6Chunk metadata, provenance and the chunking debugging playbook
- Chapter overview
7Finding the right chunks
8When retrieval fails
Context selection, packing and citations
Turning ranked chunks into a prompt: how many, in what order, labelled how, and how to make every claim point at a numbered source. With measured context sizes and a citation check.
A real question
We have the right chunks. How should they go into the prompt?
Few, deduplicated, best first, each labelled with a number and its source. Then ask for a citation after every claim, so both readers and scripts can check where each fact came from.

The problem
Why this matters

Paste the retrieved text into the prompt and the model will sort it out.
Unlabelled text gives the model no way to say where a fact came from, and you no way to check it. Numbered, labelled sources make every answer traceable to one chunk.
Why not the simpler way
Retrieval ends with a ranked list; the model needs a prompt. Many systems join the chunks with blank lines and move on. That loses the source of every fact, wastes tokens on duplicates, and hides the best chunk among the rest. Packing is a small step with a large effect on cost, trust and debuggability.
The idea
The idea in plain words
Selection picks which chunks go in. Packing decides order and format. Citations link each claim in the answer to a numbered source. Together they make the context small, readable and checkable.
A good report has footnotes. The reader can follow any claim to its source, and an editor can check every footnote.
How it works
Step by step
1Select
Take the reranked top few. Drop near copies and old versions. Stop when the token budget is reached.
2Label
Give each chunk a number and a header: document title, version, section. Keep the original text.
3Ask for citations
Instruct the model to put [n] after every claim, and check each [n] exists and holds the claim.
In detail
Budget. Decide the context budget in tokens (RAG-03), then add chunks in rank order until it is full. Our Mini RAG prompt used about 1,026 tokens of context for 3 chunks.
Order. Best first. Models use text at the start of the context more reliably than in the middle (RAG-35).
Format. [n] (document, version, section) then the chunk text. Our Mini RAG Lab uses: "Answer the question using only the sources below. Cite sources like [1]. If the sources do not contain the answer, say so."
Citation check. Parse every [n]; reject numbers that do not exist; for each sentence with a number, code or date, check that value appears in the cited chunk (RAG-37).
The code
Try it in Python
import re
def pack(chunks, budget_tokens=1200, count=len_tokens):
out, used, seen = [], 0, set()
for c in chunks: # already reranked, best first
key = c.text[:200]
if key in seen:
continue
cost = count(c.text)
if used + cost > budget_tokens:
break
seen.add(key); used += cost
out.append(f"[{len(out) + 1}] ({c.doc}, version {c.version}, {c.section})\n{c.text.strip()}")
return "\n\n".join(out)
def bad_citations(answer: str, n_sources: int) -> list[int]:
return [int(n) for n in re.findall(r"\[(\d+)\]", answer) if not 1 <= int(n) <= n_sources]
See it
What we measured
Measured · context size and answers present, hybrid search
Polder corpus, 34 recursive 512 token chunks, 47 golden questions, BM25 + bge-small with RRF. Tokens are the chunk texts only, averaged per question.
Measured 2026-11-04 with corpus/scripts/measure-parts-4-7.mts and measure-parts-6-7.mts. Citation behaviour of a real model was not measured (not measured yet: needs an API key).
Try it on a real example
The same question, two ways to pack the context
Prompt
Three documents run together. The answer “30 days” cannot be traced to the tariff sheet, or told apart from the insurance text next to it.
Choosing
Use it when, avoid it when
- Numbered, labelled sources, best first
- A token budget and dedup before packing
- A script that checks citations
- Joining chunks without labels
- Filling the context window because it is there
What it costs · Labels add a few tokens per chunk. Every extra chunk adds about 300 tokens on our corpus.
In production
Return citations as structured data (source id, document, version, link), not just text, so the interface can show them and logs can store them (RAG-38). Measure citation coverage: the share of answer sentences with a valid citation. Track context tokens per question; it is usually the largest part of the cost.
Watch out
Common mistakes
- Citations prove the answer is right. They make it checkable; the check is a separate step.
- More context is safer. Top 3 already held all 47 answers here.
- Order does not matter. It does, especially for long contexts (RAG-35).
Check yourself
Remember this
Remember these
- Pack few chunks, best first: top 3 held every answer in about 900 tokens.
- Label each chunk with a number, document, version and section.
- Ask for a citation after every claim.
- Check citations with a script: does source [n] contain the claimed value?
Say it in an interview
- 01I pack few chunks, deduplicated and best first, within a token budget.
- 02Each chunk gets a number and a source header; the answer cites [n] per claim.
- 03On my corpus the top 3 held every answer in about 900 tokens.
- 04A script checks that each citation exists and that the cited chunk holds the value.
- 05Citations go back to the client as structured data, not just text.
Quick quiz