Sub topic 1
What Grounding Means and Why Citations Help
Prerequisites
This chapter assumes basic Python, familiarity with language-model APIs, and a general understanding of retrieval-augmented generation (RAG). RAG is a pattern where a system retrieves relevant text before asking a model to answer.
Grounding and Citations
An answer is grounded when its claims are supported by the information supplied to the model. Grounding limits unsupported guesses, often called hallucinations.
A citation identifies the source behind a claim. It improves traceability, but a citation alone does not prove grounding. A model might cite a real passage that does not support its statement, or attach the right source to the wrong claim. Your application must validate citations and, when possible, compare claims with source text.
Where Citations Fit in a RAG Pipeline
A typical pipeline works like this:
- Split documents into chunks.
- Retrieve the chunks most relevant to a question.
- Assign each retrieved chunk an identifier.
- Assemble the chunks into the model's context.
- Ask the model to answer and cite identifiers.
- Validate and render the citations.
Giving Every Chunk a Stable Identifier
Give each retrieved chunk a short identifier such as 1, 2, or 3. Keep its metadata, including its URL, document ID, and text, in your application. The identifier must remain consistent while the prompt is being built and the answer is being checked.
Do not ask the model to invent URLs. Let it cite identifiers, then resolve those identifiers in your code.
Building a Numbered Context Block
Numbered context makes the source boundary explicit:
def build_context(chunks):
"""Build prompt context from chunks containing url and text."""
blocks = []
for source_id, chunk in enumerate(chunks, start=1):
blocks.append(
f"[{source_id}] ({chunk['url']})\n{chunk['text']}"
)
return "\n\n".join(blocks)
chunks = [
{"url": "https://example.com/guide", "text": "Backups run nightly."},
{"url": "https://example.com/policy", "text": "Backups are retained for 30 days."},
]
print(build_context(chunks))The model can now refer to [1] and [2]. Later code can check that every marker points to a supplied chunk and turn valid markers into links.
Exercise
Why should an application assign citation identifiers instead of asking the model to produce source URLs?
Show answer
Application-assigned identifiers connect each citation to verified metadata and reduce invented or malformed URLs.