liveBook · Manning

Vector index

As explained in AI Agents in Action, Second Edition

An approximate nearest neighbor index accelerates similarity retrieval by finding vectors that are close to a query without exhaustively comparing every stored vector.

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

How AI Agents in Action, Second Edition covers it

How deep it goes
Names ANN indexes as a retrieval optimization but does not explain their construction or search mechanics.
Examples
Shows a retrieval function configured for top-k passages with ANN search and metadata filtering.
What you can do after
Apply it via a retrieval tool to fetch top-k passages, with metadata filtering as a complementary option.
How it connects
Pairs it with metadata filtering, embeddings, reranking, hybrid search, and RAG.

Also explained in

Compare how 8 books explain vector index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.