A vector index uses approximate nearest neighbor (ANN) search to find vectors close to a query without comparing every stored vector. In retrieval-augmented generation (RAG), it helps accelerate retrieval. section 11.3
Treat the index as one part of retrieval, alongside:
- Metadata filters, domain-tuned embeddings, and reranking. section 11.3
- Domain sharding and hybrid search, which combines semantic, keyword, and hierarchical or graph search. section 11.5
Listing 11.6 07_rag_agent_tips.py
from agents import Agent, function_tool
@function_tool
def retrieve(query: str, corpus: str = "product_docs", top_k: int = 5) -> list[dict]:
"""Return top-k passages with metadata from the selected index."""
# ... ANN + metadata filtering ...
return [{"text": "...", "source": "KB-123", "section": "Refunds"}]
answerer = Agent(
name="RAG-Answerer",
instructions=(
"Use ONLY the passages provided via `retrieve`. " #1
"If none answer the question, reply: "
"'I don't know based on the available documents.' " #2
"Return a short summary and cite sources." #3
),
tools=[retrieve],
)
See a retrieval tool that returns top-k passages and uses ANN search with metadata filtering. section 11.3







