liveBook · Manning

Retriever

As explained in Agent Design Patterns

An agent design pattern that retrieves relevant information from a larger data source to provide focused context for downstream processing.

An agent cannot read an entire knowledge base, years of logs, or every available tool description before each decision. Context windows are finite, and longer inputs increase cost and latency. A Retriever provides selective attention: it searches a large information pool and returns a small set of relevant items for the agent’s next step. section 7.0 section 7.1

Figure 7.1 The AFD of the Retriever pattern. The input (a query) is first processed into a representation suitable for search. This representation is used to query a data storage, producing a set of candidate results. These candidates are optionally passed through a result-processing step (e.g., a re-ranker or filter) to improve the selection before being returned as the final output.

The pipeline turns a query into a searchable representation, retrieves candidates from storage, then optionally filters or re-ranks them before returning results. section 7.2

A typical Retriever separates inexpensive broad search from more precise selection:

  • Prepare the query as an embedding, a weighted keyword vector, or another search representation. section 7.2
  • Search the data source for candidates; dense retrieval commonly uses approximate nearest-neighbor search against a vector database. section 7.2
  • Optionally filter or re-rank candidates using a more expensive relevance check. Return only the most useful results. section 7.2
Listing 7.1 retriever.py: A high-level sketch of a generic Retriever pipeline
from ..utils import E, IE
 
 
class Retriever(IE[[str], list[str]]):
  def __init__(
    self,
    embedder: IE[[str], list[float]],
    vector_database_search: E[
      [list[float], list[list[float]], int], list[str]
    ],
    reranker: (
      IE[[str, list[str]], list[str]] | None
    ) = None,
    top_k: int = 5,
  ):
    self.embedder = embedder
    self.vector_database_search = vector_database_search
    self.reranker = reranker
    self.top_k = top_k
 
  async def __call__(
    self, query: str, database: list[list[float]]
  ) -> list[str]:
    # process the query
    query_vector = await self.embedder(query)
 
    # execute large search
    fetch_k = (
      self.top_k * 10 if self.reranker else self.top_k
    )
    candidates = await self.vector_database_search(
      query_vector, database, fetch_k
    )
 
    # process the search results (re-rank)
    if self.reranker:
      final_results = await self.reranker(
        query, candidates
      )
      return final_results[: self.top_k]
 
    return candidates
 

The generic Python sketch makes the component boundary explicit: an embedder, vector search dependency, optional re-ranker, and top-k limit turn a query into a list of results. section 7.2.2

Search choice depends on the failure mode. Dense retrieval can match a problem description by meaning but miss a rare error code; keyword search can match the code but miss vague intent. A hybrid Retriever searches both indexes, fuses their ranked results, and can re-rank the combined candidates. If initial results lack relevance, a resampling variant refines the query. section 7.4.2 section 7.5

Retrieve before answering a support ticket

For a question about resetting a particular heater model, embed the ticket, fetch a broad set of candidate manual passages, and re-rank them to a few paragraphs. Pass those paragraphs to the generator rather than asking it to answer from its general knowledge. section 7.4.1

The returned items need not be documents. Retrieving past interactions supports long-term memory; retrieving similar solved tasks supplies dynamic few-shot examples; retrieving tool descriptions narrows a large toolkit to plausible choices. In each case, the Retriever selects context or options—it does not itself generate the final answer or execute the tool. section 7.2.3 section 7.4.3

How Agent Design Patterns covers it

How deep it goes
Details query preparation, candidate search, optional re-ranking, and a generic dependency-based implementation sketch.
Examples
Uses support-document lookup, technical-support error codes, dynamic exemplar selection, and semantic tool routing.
What you can do after
Builds it from scratch as a query–search–re-rank component and applies it to focused agent context selection.
How it connects
Connects to retrieval-augmented generation, long-term memory, tool selection, few-shot prompting, and hybrid search.

Also explained in

Compare how 6 books explain Retriever →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Agent Design Patterns. The section links point to the parts of the book it draws on.