Give your agent a retrieval tool so it answers from your documents instead of its memory. Build the RAG pattern — a search tool the model calls — then point it at real data.
Why: Retrieval-Augmented Generation means the agent looks up relevant text and answers from it, so it can cite your private data and stay current. When: use it whenever answers must come from documents the model was never trained on. Where: to the agent, retrieval is simply one more tool it can call.
search_tool = {
"name": "search_docs",
"description": "Search the company knowledge base for relevant passages. "
"Call this before answering any question about company policy.",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "What to search for"},
},
"required": ["query"],
},
}Why: the tool returns the passages most relevant to the query, which the model then reads and answers from. When: this keyword version is enough to learn the pattern; production swaps it for vector search (embeddings + a vector database) so it matches by meaning, not exact words.
DOCS = [
"Refunds are available within 30 days of purchase with a receipt.",
"Digital downloads are non-refundable once accessed.",
"Support hours are 9am–5pm, Monday to Friday.",
]
def search_docs(query):
# Toy keyword match. Production: embed the query and DOCS, then
# return the nearest passages from a vector database.
hits = [d for d in DOCS if any(w in d.lower() for w in query.lower().split())]
return "\n".join(hits) or "No matching passages found."Why: the point of RAG is answers grounded in retrieved text, not the model's memory — so instruct it to use only what it found and say when the answer is not there. When: this single instruction is what prevents confident, wrong answers. Where: put it in the system prompt so it applies to every turn.
SYSTEM = (
"Answer ONLY from the passages returned by search_docs. "
"If the answer is not in them, say 'I don't have that information.' "
"Do not use outside knowledge."
)
answer = run_agent_with_system(
"Can I get a refund on a downloaded e-book?",
tools=[search_tool],
system=SYSTEM,
)
print(answer) # Grounded in the "Digital downloads are non-refundable" passage.