AI Agents Fundamentals, v2

Creating a Web Search Tool

Scott Moss
Netflix
AI Agents Fundamentals, v2

Lesson Description

The "Creating a Web Search Tool" Lesson is part of the full, AI Agents Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates adding a web search tool, showing how to integrate it with existing tools and trigger compaction when token usage is high. He tests the tool by asking questions and verifying search result accuracy.

Preview

Transcript from the "Creating a Web Search Tool" Lesson

[00:00:00]
>> Scott Moss:How do we implement this compaction? We use a simple compaction strategy. We estimate the token usage before each turn. If over a threshold, we can pick a number, we'll say 80% of the context window, we trigger compaction. We then summarize the conversation history into a condensed form, we replace history with summary, and then we continue the conversation. OK. This isn't the most sophisticated approach, but it's reliable and easy to understand.

[00:00:25]
So let's do it. First, let's add our web search code. This is going to be the easiest tool you've ever written in your life, so let's do that. Um, I'm going to check out to lesson 7. We are just going to go to our tools. We're gonna make a new tool, we're gonna call it web search. It's gonna be super simple because we're not gonna make it at all because it's already made, but we're just making sure it fits.

[00:00:55]
Um, within our, you know, standard of how we do our tool composition, so we don't have to change any other code. So we'll just say export const webSearch equals in this case it'll just be OpenAI.tools.webSearch. And if you see, if you do this, you can see all the tools that we get from OpenAI on their service. So we get code interpreter, like I said, you could pass it a string for it to execute in Python and JavaScript, maybe other languages.

[00:01:22]
File search is if you have files on OpenAI because you can attach files to an LLM and they store them for you. This allows you to search it. Uh, image generation, if you're into, uh, Dolly, I guess, uh, it's not good. Local shell. This one's kind of cool, um, because they don't run the shell commands, but they give you the commands that you can then run, which is kind of cool, um, and we will be doing a shell tool but we won't be doing it this way.

[00:01:50]
Web search, that's what we're using, and then web search preview is just like their experiment version of web search, the one that they keep messing around with, um, so. We will do web search and we will give it no arguments. That's it. And then we go to our tools file. And we want to go ahead and import that. And add that in here. So we will say import webSearch from web search, added to our tools, webSearch.

[00:02:36]
And then, um, let's just export webSearch. You might see some other tools in there in the code. Those are other ones that we will write after this. Looks good. There we go. Cool. Now that we got the web search. We should just be able to use it right now and give it a try. Um, I think there might be a bug here in the agent somewhere where if it like calls tools after like 4 or 5 times, it's like getting a streaming error.

[00:03:12]
I don't know if it's this or if it's what it's gotta be this. It can't be AI SDK. It's definitely our stuff, but I don't know what it is, but it's not hard to work around, so, um. Let's build this. Cool, npm i again, which I don't think I need to do this, the install every time, but I just do it. And then I'm just gonna ask it. Dude. You have the ability to search the web. See what it says.

[00:03:55]
Yes, I can search the web using a browsing tool. OK, so let's ask you something. OK, who won the F1 Vegas Grand Prix in 2025, please only use one web search, no more. So I don't want this thing to break. Hopefully he listens to me. It's thinking, it's doing a web search. Please only do one. I think I only did one. I think by now I would have like shot off like 20 of them already.

[00:04:37]
There we go. Yeah, Max first happened, he did win, uh, it's pretty accurate, so, and they actually give you, again, maybe they give you the sources, so you can. You can click on this. And there we go, took me to the source where I got it from. It's pretty impressive. Now your agent is no longer bound by its training date cutoff. You can make it live, right? You can ground it. Um, so, pretty powerful, right?

Learn Straight from the Experts Who Shape the Modern Web

  • 250+
    In-depth Courses
  • Industry Leading Experts
  • 24
    Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now