How does citation linkage between retrieved chunks and the model output rely on context-engineering choices?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Attach a stable id (chunk_id or short tag like [S3]) inline with each chunk, tell the model to cite by that id, and map ids back to sources at render time, so citations survive reranking and trimming.
Imagine you give a kid a stack of recipe cards and ask which card they used to make dinner. If every card has a sticker on the corner that says S1, S2, S3, the kid can just say 'S2' and you know exactly which recipe. If the cards have no stickers, the kid has to describe the recipe back to you, and they will probably get the title slightly wrong. RAG citations work the same way. Give every chunk a sticker before you hand them to the model, tell the model to cite the sticker, and you always know which chunk the answer came from. No stickers and the model has to guess at titles and URLs, and it guesses badly.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 minutes: stable-id assembly pattern, why position-based numbering breaks, render-time mapping, name structured-citation APIs (Anthropic, OpenAI Responses, Cohere), and the hallucinated-tag validation step.
def assemble_context(chunks: list[Chunk]) -> str:
blocks = []
for i, c in enumerate(chunks, start=1):
tag = f"S{i}"
header = f"[{tag}] Source: {c.source_path} | section: {c.section}"
blocks.append(f"{header}\n{c.text}")
return "\n\n".join(blocks)
SYSTEM = (
"When you use information from a labelled source block, cite it inline "
"using its tag in square brackets (e.g. [S1]). Cite every claim that "
"depends on a source. Do not invent tags."
)
# Render-time validation
def validate_citations(answer: str, valid_tags: set[str]) -> list[str]:
import re
cited = set(re.findall(r"\[(S\d+)\]", answer))
return list(cited - valid_tags) # hallucinated tagsReal products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Asking the model to quote chunk text or paraphrase source titles. The model confabulates and the renderer cannot reliably link back to the underlying source row.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.