Sub topic 1
Agentic RAG: Letting the Model Decide What to Retrieve
Prerequisites
Assume you know basic Python, LLM prompts, embeddings, vector similarity search, and classic retrieve-then-generate RAG.
Recap: Embeddings and Vector Search
An embedding is a list of numbers representing the meaning of text. Texts with similar meanings have vectors that are close together. A vector search system compares a question's embedding with stored document embeddings and returns the nearest chunks.
Vector search is useful for finding relevant passages, but it does not understand whether the passages fully answer a question.
The Classic RAG Pipeline
Classic RAG, or retrieval-augmented generation, always follows the same sequence:
- Embed the user's question.
- Retrieve the top
kmatching chunks. - Place those chunks in a prompt.
- Ask the model to answer from that context.
def answer_with_rag(question, embed, search, generate, k=4):
query_vector = embed(question)
chunks = search(query_vector, k)
context = "\n\n".join(
f"[{chunk['id']}] {chunk['text']}" for chunk in chunks
)
prompt = (
"Answer only from the context. If the answer is missing, say so.\n\n"
f"Context:\n{context}\n\nQuestion: {question}"
)
return generate(prompt)Where Single-Shot Retrieval Breaks Down
Single-shot retrieval runs once, using the user's original wording. It can fail when:
- The question contains several separate questions.
- The first query uses vague terms or the wrong vocabulary.
- Different facts live in different sources.
- The top results are related but do not answer the question.
- Retrieval is unnecessary, such as for a greeting.
An agentic RAG system can rewrite a query, search again, use another source, or stop without searching.
Tool Calling in One Page
Tool calling lets a model request a function using structured arguments. Your application executes the function and sends its result back to the model. The model sees the tool's name, description, and parameter schema, but it does not execute code itself.
{
"name": "search_docs",
"description": "Search product documentation for behavior, configuration, or limits. Do not use for greetings.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"top_k": {"type": "integer", "minimum": 1, "maximum": 8}
},
"required": ["query"]
}
}Turning Retrieval into a Tool
Wrap your retriever so the model receives short, identifiable passages rather than vectors or internal database fields.
def search_docs(query, top_k=4):
top_k = max(1, min(int(top_k), 8))
chunks = retriever.search(query, top_k)
return [
{"id": chunk["id"], "text": chunk["text"][:1200]}
for chunk in chunks
]The description is part of the control surface. State when to call the tool, when not to call it, and what each argument means. Validate limits in the function too, because model-generated arguments are untrusted input.
Exercise
Why can agentic RAG outperform a fixed top-k retrieval step?
Show answer
It can decide whether retrieval is needed, rewrite unclear queries, search multiple sources, and retry when the first results lack evidence.