Overview

7 Retriever: Returning more relevant results

As agentic systems grow beyond short, self-contained prompts, they run into hard limits on context size, latency, and cost. The chapter explains that an agent cannot realistically “read everything” in a large knowledge source, so it must rely on selective attention: first search cheaply, then spend expensive reasoning only on the most promising results. The Retriever pattern is presented as the practical way to bridge this gap by turning huge external data sources into a small, actionable context that an agent can actually use.

Retrieval is described as a pipeline with three main steps: representing the query in a search-friendly form, executing a broad and inexpensive search, and then refining the candidates with post-processing such as filtering or re-ranking. This structure makes it possible to search millions of items while keeping compute manageable, and it improves grounding by tying outputs to retrieved evidence rather than model guesswork. The chapter emphasizes the tradeoff between recall and precision, showing how a wide first pass can capture likely matches while a later stage removes noise and selects the most relevant items.

The chapter also shows where retrieval fits in real agent designs, especially retrieval-augmented generation, long-term memory, routing, and tool selection. It compares dense semantic search, sparse keyword search, hybrid retrievers, and active resampling approaches, explaining how different variants help in different situations. Through examples like insurance adjudication, technical support, dynamic few-shot example selection, and semantic tool routing, the chapter demonstrates that retrieval helps agents stay lean, accurate, and scalable while still reaching into vast bodies of information when needed.

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 AFD of a Hybrid Retriever. The query enters the retriever and is processed in two separate streams, each of a different kind. Once processed, two different kinds of searches are performed on the same database. The results of these searches are post-processed separately before being merged together. The merging of the results usually entails concatenation and some naive de-duplication. More sophisticated merging approaches involve IEs that estimate the relevance of the individual results to the query.
The AFD of a Resampling Retriever. The system retrieves data, evaluates its relevance, and branches: either returning the data or re-sampling the search query and attempting to find relevant results again.

Summary

  • The “scaling wall of context” and rising computational costs limit an agent’s ability to process vast amounts of data, making brute-force “read everything” approaches expensive and often technically unfeasible.
  • The Retriever pattern provides a principled pipeline for selective attention, bridging massive external datasets and the focused, actionable context required for execution.
  • Retriever instances typically operate as a multi-stage pipeline: query representation (for example, producing vector embeddings), inexpensive high-recall search execution, and more sophisticated post-processing for precise filtering or re-ranking.
  • Retriever provides drastic selectivity and supports semantic alignment by grounding actions in retrieved facts, which improves interpretability, reduces hallucinations, and lowers material costs.
  • Hybrid Retrievers fuse semantic and keyword-based searches to balance intent with exact-match precision, and Resampling Retrievers actively refine the search query when initial results lack relevance.
  • Across use cases such as automated insurance adjudication, dynamic few-shot exemplar selection, and semantic tool routing, Retriever serves as a foundational gateway that keeps agents lean and fast while enabling access to virtually unlimited knowledge sources.

FAQ

What problem does the Retriever pattern solve in agent design?It solves the “scaling wall of context” by letting an agent search large external data sources efficiently and bring only the most relevant information into the model’s limited context window.
Why can’t an agent simply read all available documents or records?Because context windows are finite and the cost and latency of model processing grow quickly with input length. Reading everything is often too expensive and sometimes technically infeasible.
How does the Retriever pattern mimic human selective attention?It performs a cheap, broad search first to narrow the field of view, then applies more expensive reasoning or filtering only to the most promising results.
What are the main stages of a Retriever pipeline?The typical Retriever pipeline has three stages: query representation or preparation, search execution against a database or index, and optional post-processing such as filtering or re-ranking.
What is the difference between dense and sparse retrieval?Dense retrieval uses semantic vector embeddings to match meaning, while sparse retrieval uses keyword-based signals like weighted terms or exact token matches. Dense search is better for intent and synonyms; sparse search is better for exact phrases, codes, and rare terms.
Why is re-ranking used after the initial retrieval step?Initial search is fast but can return noisy or superficially similar candidates. Re-ranking applies a more accurate, costlier relevance check to improve precision before the final results are returned.
How does retrieval help reduce hallucinations in agents?By grounding the model in retrieved facts, policies, manuals, or examples, retrieval gives the agent evidence to base its answers on rather than relying only on internal generation.
What is a hybrid retriever and when is it useful?A hybrid retriever runs multiple search strategies in parallel, usually semantic and keyword-based search, then fuses their results. It is useful when both meaning-based matching and exact match retrieval matter, such as technical support or error-code lookups.
Besides RAG, what are common uses of Retriever?Retriever can be used for long-term memory, routing and tool selection, action-history lookup, context browsing over long documents, and even retrieval over non-text modalities such as images, video, or audio.
How is Retriever used in dynamic few-shot prompting and tool selection?In dynamic few-shot prompting, it selects the most relevant examples to insert into the prompt for the current task. In tool selection, it matches the user’s intent against tool descriptions to choose the most appropriate tool without loading all tool schemas into context.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Agent Design Patterns ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Agent Design Patterns ebook for free