liveBook · Manning

Web search

As explained in Build an AI Agent (From Scratch)

Web search is a tool capability that retrieves information from internet-accessible search indexes or pages.

Web search gives an agent a way to look up information instead of relying on an LLM’s training data. This matters when an answer must be precise, current, or checked against a named source. If a user asks for a value on a particular web page, recalling a plausible value is not the same as looking it up. section 2.4.3

A minimal Python tool takes a query, calls Tavily’s search API, and returns its results. Install the Tavily client, put the API key in a .env file as TAVILY_API_KEY, load it with load_dotenv(), and create a TavilyClient. Starting with two results keeps development responses concise and limits API costs. section 3.3.1

Listing 3.12 Basic web search function using Tavily
import os
from tavily import TavilyClient
from dotenv import load_dotenv

load_dotenv()

tavily_client = TavilyClient(os.getenv("TAVILY_API_KEY"))

def search_web(query: str, max_results: int = 2) -> list:

    response = tavily_client.search(query, max_results=max_results)
    return response.get("results")

The basic search_web function exposes the essential data flow: query in, Tavily search call, results out. section 3.3.1

Check a search result

Searching for Kipchoge’s marathon record returns results with titles, URLs, snippets, and relevance scores. A Wikipedia result’s snippet reports his official marathon best as 2:01:09 at Berlin in 2022. The URL and snippet let the agent identify where that answer came from. section 3.3.1

Extend the wrapper only when the task needs more control:

  • Use max_results to control how many results come back; use topic for general, news, or finance searches. section 3.3.1
  • Pass time_range or country when a request calls for a time period or geographic focus. section 3.3.1
  • Catch search exceptions and return an error message when the call fails. This changes the function’s possible return value from results alone to results or an error string, which callers must handle. section 3.3.1

In an agent loop, search is one tool among others. It cannot replace file reading when the needed information is in an attachment, and a model that recognizes it lacks information still needs the appropriate tool to obtain it. section 2.4.3 section 1.5.6

How Build an AI Agent (From Scratch) covers it

How deep it goes
Builds a minimal Python search wrapper, adds query options, and handles exceptions; does not detail search-index internals.
Examples
Tests a Tavily query about Kipchoge’s marathon record and inspects results containing URLs, snippets, and relevance scores.
What it assumes
Assumes basic Python function and environment-variable use while starting the search integration with a minimal API call.
What you can do after
Apply it via Tavily by configuring an API key and writing a Python function that returns search results.
How it connects
Connects tool use and agent loops to LLM limitations, file reading, and the GAIA benchmark.

Also explained in

Compare how 5 books explain web search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.