Skip to content
AgenticAIWiki
  1. Learn
  2. Part 4
  3. 21. Naive RAG
  4. 21.2

Lesson 21.2 · 2 of 5 · 4 min read · Reviewed 24 Nov 2026

Putting retrieved text in the prompt

By the end, you can

  • Put retrieved text into a prompt well.
  • Ask for answers grounded in the sources with citations.

A grounded prompt

<documents>
  <document id="pol-4.2" source="returns-policy.pdf">
    Footwear may be returned within 30 days if unworn...
  </document>
  <document id="pol-4.3" source="returns-policy.pdf">
    Sale items are final sale unless faulty...
  </document>
</documents>

Answer the question using only the documents above.
Cite document ids in square brackets after each claim.
If the documents don't contain the answer, say so plainly.

Question: Can I return sale shoes I've worn once?

Why this layout

  • Tags and ids make each source distinct and citable (chapter 11).
  • Documents first, question last tends to work well with long inputs.
  • “Only from the documents” reduces answers from memory that may be wrong or outdated.
  • An explicit way out (“say so”) stops the model filling gaps with guesses (chapter 7).

Citations with Claude

Claude’s citations feature lets you pass documents as content blocksContent block: One typed piece of a message, such as text, image, document, tool_use or tool_result. and get answers whose claims point to exact passages in them. That saves you parsing your own citation format, and the cited text comes from the documents you supplied.

How many chunks

More isn’t always better. Five highly relevant chunks usually beat twenty loosely related ones, which add cost and distract. Chapter 23 covers rerankingReranking: Re-scoring an initial set of retrieved results with a more accurate model to put the best ones first. to pick the best few.

Key points

  • Wrap each retrieved chunk in clear tags with its source id, and put the question after the documents.
  • Tell the model to answer only from the sources and to say when they don't contain the answer.
  • Ask for citations to chunk ids, or use Claude's citations feature, so answers can be checked.
Hands-on labRuns in the Playground

Assemble a grounded prompt with citations

Build a prompt that tags every chunk with its id, puts the question last, and check that an answer only cites real ids.

You will have: A build_prompt() function and a citation checker run on two sample answers.

Steps

  1. Wrap each retrieved chunk in a <document id=...> tag inside <documents>.
  2. Add instructions: answer only from the documents, cite ids in square brackets, say so if the answer isn't there.
  3. Put the question after the documents.
  4. Write check_citations() to find cited ids that were never provided.

Done when

  • The printed prompt shows two document tags and the question on the last line.
  • The good answer has no unknown citations.
  • The bad answer is flagged for citing pol-9.9.

Open in the Playground No API key needed.

Starter code (Python)
import re

CHUNKS = [
    {"id": "pol-4.2", "text": "Footwear may be returned within 30 days if unworn."},
    {"id": "pol-4.3", "text": "Sale items are final sale unless faulty."},
]
QUESTION = "Can I return sale shoes I've worn once?"

def build_prompt(chunks, question):
    # TODO: <documents> with one <document id="..."> per chunk, then the instructions, then "Question: ..."
    return question

def check_citations(answer, chunks):
    # TODO: find every [id] in the answer and return the ones not in the chunk ids
    return []

print(build_prompt(CHUNKS, QUESTION))
for ans in ["No: sale items are final sale unless faulty [pol-4.3].", "Yes, within 90 days [pol-9.9]."]:
    print("unknown citations:", check_citations(ans, CHUNKS))
Show a solution
import re

CHUNKS = [
    {"id": "pol-4.2", "text": "Footwear may be returned within 30 days if unworn."},
    {"id": "pol-4.3", "text": "Sale items are final sale unless faulty."},
]
QUESTION = "Can I return sale shoes I've worn once?"

def build_prompt(chunks, question):
    parts = ["<documents>"]
    for c in chunks:
        parts.append(f'  <document id="{c["id"]}">{c["text"]}</document>')
    parts += ["</documents>", "",
              "Answer the question using only the documents above.",
              "Cite document ids in square brackets after each claim.",
              "If the documents don't contain the answer, say so plainly.", "",
              f"Question: {question}"]
    return "\n".join(parts)

def check_citations(answer, chunks):
    known = {c["id"] for c in chunks}
    return [cid for cid in re.findall(r"\[([^\]]+)\]", answer) if cid not in known]

prompt = build_prompt(CHUNKS, QUESTION)
print(prompt)
print("question is last:", prompt.splitlines()[-1].startswith("Question:"))
for ans in ["No: sale items are final sale unless faulty [pol-4.3].", "Yes, within 90 days [pol-9.9]."]:
    print("unknown citations:", check_citations(ans, CHUNKS))
Quick checkRetrieved chunks don't contain the answer to the user's question. What should the prompt tell the model to do?
How sure are you?
Pick an answer and how sure you are