advancedChapter 12 of 12

Search & Retrieval (RAG)

RAG patterns · Context injection · Document grounding

Retrieval-Augmented Generation

RAG is the pattern of retrieving relevant documents, then giving them to Claude as context for answering questions. It's the backbone of most knowledge-base chatbots.

The RAG Prompt Pattern

Here are some relevant documents retrieved for the user's question.

<retrieved_documents>
  <document source="FAQ page">
    {DOCUMENT_1}
  </document>
  <document source="Product manual, p.42">
    {DOCUMENT_2}
  </document>
</retrieved_documents>

<user_question>
{QUESTION}
</user_question>

Answer the question using ONLY the information in the retrieved documents. 
Cite your sources. If the documents don't contain the answer, say so.

Key Design Decisions

  1. Grounding instruction: Always tell Claude to answer based on the documents, not general knowledge.
  2. Citation format: Ask for source references so users can verify.
  3. Fallback behavior: What should Claude do when the documents don't have the answer?

Key Takeaways

  • Always instruct Claude to use only the provided context.
  • Include source metadata so Claude can cite references.
  • Define explicit fallback behavior for missing information.

Exercises