How to use an AI agent to select useful search results (Research, version 3)?
A search returns ten results, and some of them are not worth keeping. One page barely mentions the population at all, and another is ten years old. This version adds a second tool whose whole job is to make that judgement call.
The agent now has two tools, and the system prompt fixes the order:
search_internet first, then hand the results to
generate_draft. The search tool works exactly as
before. generate_draft reads the saved results
and keeps only the ones that pass its rule.
A place to keep the search results
# Keeps the latest search response so generate_draft can use it
last_search = {}
Tools are separate functions. When one finishes, its local variables disappear. If the filter is called after the search, how does it see what the search found?
The answer is a variable that lives outside both tools:
last_search. The search tool writes its response into it, and
the filter tool reads from it.
Tool one: the search, and one new line
@tool
def search_internet(query: str):
"""
Search the internet for up-to-date information about the query
and return the full Tavily search response.
"""
global last_search
response = tavily_client.search(query, max_results=10)
last_search = response
return response
Three lines matter here. global last_search is the one that
needs an explanation. Inside a function, an assignment normally makes a
brand-new local variable: last_search = response would fill that
local copy and throw it away when the function ends. Declaring
last_search as global tells Python that this is the same
variable we use for the whole run of the program, not a fresh one made
inside the function.
last_search = response then saves the ten results into that
shared variable, and return response sends them to the agent as
well, because the model still needs to see what came back.
Tool two: generate_draft keeps only the useful results
@tool(return_direct=True)
def generate_draft():
"""
Go through the results from the latest search_internet call.
Keep results with a score of 0.5 or higher, but never keep Wikipedia.
Return the selected results.
"""
selected = []
for r in last_search.get("results", []):
if r["score"] >= 0.5 and "wikipedia.org" not in r["url"]:
selected.append(r)
return {"query": last_search.get("query"), "results": selected}
The docstring says all of this in plain English, and that is deliberate: the docstring is what the model reads when deciding whether the tool fits and what it does.
The filter takes no input. Everything it needs is in
last_search. It makes an empty list, walks through the ten
results one by one, and keeps a result only when both conditions are true.
The rule is written as one if with two parts joined by
and. The first part, r["score"] >= 0.5, keeps
pages the search engine rated as matching. The second part,
"wikipedia.org" not in r["url"], drops Wikipedia no matter how
well it scores. A result passes only if both are true, and then
selected.append(r) adds it to the list.
Dropping Wikipedia is a deliberate choice. Harvard’s What’s Wrong with Wikipedia? puts it this way:
When you’re doing academic research, you should be extremely cautious about using Wikipedia. As its own disclaimer states, information on Wikipedia is contributed by anyone who wants to post material, and the expertise of the posters is not taken into consideration. Users may be reading information that is outdated or that has been posted by someone who is not an expert in the field —or by someone who wishes to provide misinformation. While Wikipedia editors do correct misinformation, observers have found that they don’t catch everything—at least not right away. Sometimes inaccurate information is posted to Wikipedia on purpose, as a hoax. In some well-documented cases, this inaccurate information continues to spread when people take it from Wikipedia and use it in books and articles.
Harvard College Writing Program, What’s Wrong with Wikipedia?
The AI Agent
agent = create_agent(
model=my_model,
tools=[search_internet, generate_draft],
system_prompt=(
"You are a helpful assistant. "
"Always call search_internet, using the user's message exactly as the query. "
"Do not rephrase, shorten, translate, or add anything to it. "
"Then call generate_draft to select the useful results."
),
)
The system prompt now ends with one extra sentence: "Then call
generate_draft to select the useful results." The model reads
the question, calls the search, receives ten results, and then calls
generate_draft on them.
What comes out
The screen prints both tools, one after the other. First the search, with all ten results:
Tool: search_internet
Query: What is the population of Hong Kong?
1. Hong Kong Population (2026) - Worldometer (score: 0.92)
https://www.worldometers.info/world-population/china-hong-kong-sar-population
The current population of the Hong Kong Special Administrative Region of the People's Republic of China is 7,371,564 as of Sunday, October 18, 2026, based on Worldometer's elaboration of the latest Un...
2. Hong Kong Population 2026 (score: 0.94)
https://worldpopulationreview.com/countries/hong-kong
### Request Data Please enter your email address to receive this data in your inbox. ## Hong Kong By the Numbers As of 2026, Hong Kong has a total populationof 7,378,602, ranking as the 107th most...
...
Then generate_draft, with only the results it
selected. To keep this page short, the content line under each result is left
out here; the program prints it.
Tool: generate_draft
Query: What is the population of Hong Kong?
1. Hong Kong Population (2026) - Worldometer (score: 0.92)
https://www.worldometers.info/world-population/china-hong-kong-sar-population
2. Hong Kong Population 2026 (score: 0.94)
https://worldpopulationreview.com/countries/hong-kong
3. Hong Kong Map, History & Population | Study.com (score: 0.93)
https://study.com/academy/lesson/hong-kong-facts-population.html
4. Hong Kong Population (1950-2026) (score: 0.92)
https://www.macrotrends.net/global-metrics/countries/hkg/hong-kong/population
5. Hong Kong - Population and Demography Country Profile | Our World in Data (score: 0.91)
https://ourworldindata.org/profile/population-demography/hong-kong
6. GovHK: Hong Kong – the Facts (score: 0.85)
https://www.gov.hk/en/about/abouthk/facts.htm
7. Race Relations Unit - Information Centre - Demographics (score: 0.78)
https://www.had.gov.hk/rru/en/info/demographics.htm
Seven, not ten. The three that disappeared:
- Wikipedia (score 0.92) — kept out by the URL rule, even though its score is high.
- World Bank (score 0.39) — below the 0.5 line.
- Population Estimates (score 0.11) — also below the line.
generate_draft removed results
and left the rest untouched, so the numbers and links survive exactly as the
search service returned them.
The whole program
# RESEARCH, version 3: two tools, one after the other.
# Tool 1 searches. Tool 2 filters: score 0.5 or higher, never Wikipedia.
# last_search lets the second tool see what the first one found.
import os
import json
from dotenv import load_dotenv
from tavily import TavilyClient
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
load_dotenv()
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
# Keeps the latest search response so generate_draft can use it
last_search = {}
@tool
def search_internet(query: str):
"""
Search the internet for up-to-date information about the query
and return the full Tavily search response.
"""
global last_search
response = tavily_client.search(query, max_results=10)
last_search = response
return response
@tool(return_direct=True)
def generate_draft():
"""
Go through the results from the latest search_internet call.
Keep results with a score of 0.5 or higher, but never keep Wikipedia.
Return the selected results.
"""
selected = []
for r in last_search.get("results", []):
if r["score"] >= 0.5 and "wikipedia.org" not in r["url"]:
selected.append(r)
return {"query": last_search.get("query"), "results": selected}
my_model = ChatOpenAI(
model="deepseek-flash",
api_key=DEEPSEEK_API_KEY,
base_url="https://api.deepseek.com",
temperature=0,
)
agent = create_agent(
model=my_model,
tools=[search_internet, generate_draft],
system_prompt=(
"You are a helpful assistant. "
"Always call search_internet, using the user's message exactly as the query. "
"Do not rephrase, shorten, translate, or add anything to it. "
"Then call generate_draft to select the useful results."
),
)
prompt = "What is the population of Hong Kong?"
result = agent.invoke({
"messages": [{"role": "user", "content": prompt}]
})
for message in result["messages"]:
if message.type == "tool":
response = json.loads(message.content)
print(f"Tool: {message.name}")
print(f"Query: {response['query']}\n")
for i, r in enumerate(response["results"], start=1):
print(f"{i}. {r['title']} (score: {r['score']:.2f})")
print(f" {r['url']}")
print(f" {r['content'][:200].replace(chr(10), ' ')}...\n")
Running it on your own computer
Download the demo program
(Folder-AI-Research03.zip). The zip holds three files:
research03.py (the full script), pyproject.toml and
README.md. This version calls a language model once, so it
needs both a Tavily key and a DeepSeek key.
- Open the folder in Visual Studio Code.
-
Create a file named
.envin the folder with two lines:
A Tavily key is available from the Tavily dashboard, and a DeepSeek key from the DeepSeek console. Usage is billed by each provider, so check the current pricing terms there before you run it.TAVILY_API_KEY=your-tavily-key DEEPSEEK_API_KEY=your-deepseek-key - In the Terminal, run these three lines, one at a time:
uv python pin 3.12.10uv syncuv run research03.py - You should see two tools printed in turn: ten results from the search,
then seven from
generate_draft. One model call takes a few seconds.
If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.
Limitations
- The rule is simple for illustration. The score 0.5 is only an example. Raise it to keep fewer results; lower it to keep more. A useful page that scores 0.49 is still thrown away, and a page that passes the test can still be wrong or out of date.
-
The model still has to follow the order. The prompt asks
for
search_internetfirst andgenerate_draftsecond. A model can depart from an instruction, so the printed output is what you should check. -
The tools talk through one shared variable.
last_searchholds only the most recent search. If a later search runs, the filter silently uses the new results. - Keeping the sources is still a human job. Seven results are fewer than ten, but nothing has been read yet. Choosing between them is the next version's job.
Further reading
- How to use an AI agent to search the web (Research, version 2)? — the single-tool agent this version grows from.
- How to search the web from Python (Research, version 1)? — the search itself, with no agent.
- LangChain documentation — tools and
create_agent. - Tavily documentation — the
searchcall and its response fields.