Skip to main content

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.

SlotBudget
System prompt and instructions500 tokens
Retrieved chunks, 5 x 7003,500 tokens
Conversation history, trimmed2,000 tokens
Reserved for the answer1,000 tokens
recall accuracyposition of the answer in the contextstartendchunk 1chunk 2the sagchunks 3, 4, 5

Three rules for assembling the chunks:

  1. 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.
  2. 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.
  3. Label every chunk. Prefix each with a number, its breadcrumb, its doc_id, and updated_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:

ClauseWhy
Answer only from the numbered contextScopes the model to what you retrieved
Cite the chunk numbers used, like [1] or [2, 3]Makes the answer auditable
Do not use outside knowledgeCloses the gap the first clause leaves open
If the context lacks the answer, say so in these exact wordsGives 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.

Best practice

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.