Generation & Grounding
Customer framing: Retrieval found the right chunk and the answer still cited the wrong document. Retrieval quality and answer quality are different problems, and this is the second one.
Official docs:
Everything here assumes a shortlist of roughly 5 chunks, produced by Retrieval.
Building the promptβ
Budget the context window explicitly. A table beats a guess.
| Slot | Budget |
|---|---|
| System prompt and instructions | 500 tokens |
| Retrieved chunks, 5 x 700 | 3,500 tokens |
| Conversation history, trimmed | 2,000 tokens |
| Reserved for the answer | 1,000 tokens |
Three rules for assembling the chunks:
- Order by attention, not by score. Models attend to the beginning and end of the context and sag in the middle, so ranked order is the wrong order. Put the top chunk first and the second best last.
- Deduplicate. Overlapping chunks from one document repeat text, waste budget, and make the model over-weight whatever got repeated. Collapse near-duplicates and merge adjacent chunks from the same section.
- Label every chunk. Prefix each with a number, its breadcrumb, its
doc_id, andupdated_atβ so the model can cite it, and you can resolve the citation back to a source.
[1] Security Policy > API Keys (doc:POLICY-4471, updated 2026-03-02) Keys must be rotated every 90 daysβ¦
Grounding and citationsβ
The prompt is a contract. Four clauses, and the last one is the one people leave out:
| Clause | Why |
|---|---|
| Answer only from the numbered context | Scopes the model to what you retrieved |
Cite the chunk numbers used, like [1] or [2, 3] | Makes the answer auditable |
| Do not use outside knowledge | Closes the gap the first clause leaves open |
| If the context lacks the answer, say so in these exact words | Gives it a way out, and a string you can match on |
The refusal path is a feature, not a failure. A system that never says "not found" is a system that hallucinates instead β and a fixed refusal string is what lets you count how often it happens.
Verify citations in code, not on trust. Parse the cited numbers out of the answer, check each one was actually in the shortlist, and route to escalation if any points at nothing. A citation to [7] when you sent five chunks is a hallucination that looks like diligence.
Render citations as links to the source document and section. Users trust an assistant they can check, and it turns every answer into a spot-check of your retrieval.
β See Evaluation & Guardrails for scoring groundedness rather than assuming it.
β Next: Evaluation & Guardrails, for scoring all of this rather than assuming it.