liveBook · Manning

Agentic RAG

As explained in Domain-Specific Small Language Models

A retrieval-augmented generation setup that uses an AI agent to plan, select, and combine retrieval actions across one or more sources before generation.

Agentic retrieval-augmented generation (RAG) adds an AI agent to a retrieval-and-generation pipeline. Traditional RAG retrieves relevant external information and passes it to a language model. In agentic RAG, the agent can plan multiple retrieval steps, revise a query in response to results, and choose among search tools before producing an answer. section 14.2

The distinction is control over retrieval, not a particular search algorithm. An agent might search local documents first, then search the web if the local results are not relevant. It can also select different local retrieval methods or use more than one tool for a query. section 14.2

A practical setup gives the agent tools with distinct retrieval jobs:

  • BM25 search retrieves local document passages by keyword. section 14.2
  • Semantic search embeds a query and searches stored document embeddings in LanceDB. section 14.2
  • Hybrid search combines BM25 and semantic retrieval; an EnsembleRetriever can weight their results. section 14.2
  • Web search offers another source when local retrieval is insufficient. section 14.2

Local documents and web search

A Python example loads a PDF, splits it into overlapping chunks, and makes those chunks available to BM25 and embedding-based search. A smolagents CodeAgent using SmolLM2-1.7B-Instruct receives search tools and a three-step limit. Queries about smart-home technologies demonstrate local search; further runs illustrate choosing between hybrid local search and web search, or using multiple tools. section 14.2

from langchain.retrievers import EnsembleRetriever

def __init__(self, bm25_retriever, semantic_retriever, **kwargs):
        super().__init__(**kwargs)
        self.ensemble_retriever = EnsembleRetriever(
            retrievers=[bm25_retriever, semantic_retriever], 
            weights=[0.4, 0.6])

The hybrid-search constructor shows two retrievers combined with explicit weights of 0.4 for BM25 and 0.6 for semantic retrieval. This combination is a search tool the agent can choose, not the agent’s decision process itself. section 14.2

Agentic RAG fits information-gathering tasks such as customer support, research assistance, and knowledge management. Its practical value depends on the available sources and tools: adding an agent enables adaptive search, but does not by itself establish that a retrieved passage is relevant or an answer is accurate. section 14.2

How Domain-Specific Small Language Models covers it

How deep it goes
Works through PDF preparation and BM25, semantic, hybrid, and web-search tools, but does not detail agent planning internals.
Examples
Uses a chunked PDF and smart-home queries to demonstrate local retrieval, web search, and multiple-tool selection.
What it assumes
Assumes familiarity with RAG, AI agents, LanceDB, smolagents, and pypdf while introducing the combined setup through an example.
What you can do after
Apply it via smolagents CodeAgent with local and web-search tools, including a weighted hybrid retriever.
How it connects
Connects retrieval-augmented generation with AI agents, BM25, vector search, hybrid search, and web search.

Also explained in

Compare how 4 books explain agentic RAG →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.