Sub topic 7
Controlling web search: domains, location, and usage limits
This chapter assumes you can already enable a built-in web search tool and read its output items and citations. You should also be comfortable with JSON request bodies and Python. The earlier chapters on declaring tools and reading tool-call items cover that groundwork.
An unconfigured web search tool lets the model query anywhere, for any number of rounds, with no regional context. That is fine for a demo. In production you usually want to narrow the sources, tell the search engine where the user is, and cap the work done per request. This chapter covers each control and how they interact.
What you can control
Three kinds of settings shape a search request:
- Where results come from. Domain allow lists and block lists restrict which sites the provider may use.
- Who is asking. An approximate user location biases results toward a country, region, or city.
- How much work is done. Usage caps and context size limit the number of searches and how much page content flows back to the model.
Restricting domains
Domain filtering is the strongest way to keep answers inside sources you trust. A support bot for your product should search only your documentation site. A finance assistant might allow only a few regulators and news outlets.
OpenAI takes an allowed_domains list inside a filters object on the tool. Anthropic takes allowed_domains or blocked_domains directly on the tool definition. You can set one or the other on Anthropic, not both.
Some practical rules apply to both providers:
- Write bare domains such as
docs.python.org, withouthttps://. - A domain entry normally covers its subdomains. Check the current provider documentation for the exact matching rule before relying on it.
- An allow list is safer than a block list. A block list only removes sites you thought of, and the open web has endless others.
Here is an OpenAI request that limits searching to two documentation sites:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "web_search",
"filters": {
"allowed_domains": ["docs.python.org", "peps.python.org"],
},
}
],
include=["web_search_call.action.sources"],
input="What did PEP 703 propose?",
)
print(response.output_text)The include value asks the provider to return the full list of sources it consulted, not only the ones cited in the answer text. That list is useful for auditing. You can confirm every consulted URL fell inside your allow list.
Setting user location
Search results depend on place. "Best pharmacy open now" or "current VAT rate" means different things in Leeds and in Lyon. The user_location setting gives the search an approximate location without needing a precise address.
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "web_search",
"user_location": {
"type": "approximate",
"country": "GB",
"city": "London",
"region": "London",
"timezone": "Europe/London",
},
}
],
input="What is the standard VAT rate here?",
)
print(response.output_text)Keep these points in mind:
- The type is always
approximate. The field is a hint, not a guarantee, and results can still come from elsewhere. - Country should be a two-letter ISO code such as
GB. The other fields are free text. - Send only what you need. A country is often enough, and a city is more personal data than the task requires.
- The location is not inferred from the user's IP address. If you do not set it, the model gets no location context. Your application has to decide what to send.
Capping usage
One user question can trigger several searches. The model may search, read, refine the query, and search again. Each search costs money and adds latency, so set a cap.
On Anthropic, max_uses on the tool limits searches per request. If the model wants more, the extra attempts return an error result inside the response, and the model must answer with what it has.
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 2,
"allowed_domains": ["nature.com", "science.org"],
"user_location": {
"type": "approximate",
"city": "Boston",
"region": "Massachusetts",
"country": "US",
"timezone": "America/New_York",
},
}
],
messages=[
{"role": "user", "content": "Summarize recent research on perovskite solar cells."}
],
)
for block in message.content:
if block.type == "text":
print(block.text)On OpenAI, the request-level max_tool_calls parameter caps built-in tool calls for the whole response. The cap applies across all built-in tools, not per tool. If you combine web search and file search, they share one budget.
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "web_search"}],
max_tool_calls=3,
input="Compare the latest stable releases of Python and Node.js.",
)OpenAI also offers search_context_size on the tool, with values low, medium, and high. It controls how much retrieved page content goes to the model. A larger value can improve answers on hard questions, but it adds tokens, cost, and latency. Start at low or medium and raise it only if answers lack detail.
Settings compared
| Setting | OpenAI | Anthropic | Main effect |
|---|---|---|---|
| Allow list | `filters.allowed_domains` | `allowed_domains` | Restricts sources |
| Block list | Not available | `blocked_domains` | Removes named sources |
| Location | `user_location` | `user_location` | Biases results regionally |
| Search count cap | `max_tool_calls` (request level) | `max_uses` (tool level) | Bounds cost and latency |
| Content volume | `search_context_size` | Not a tool setting | Trades detail for tokens |
Provider options change often, so check the current documentation before shipping.
Common errors
- Setting both allow and block lists on Anthropic. The request is rejected. Pick one.
- Including a scheme or path in a domain. Entries like
https://example.com/docsmay fail validation or match nothing. Useexample.com. - Allowing too few domains. If the answer needs facts from outside the list, the model will say it found nothing or will answer from memory. Check the citations to see which happened.
- Assuming a cap is a target.
max_uses=3means up to three searches. Many requests use one. - Expecting a cap to guarantee the cost. Token charges for retrieved content still apply, and per-search fees apply per call. Test with representative questions and read your usage reports.
- Trusting location blindly. Verify with a few region-specific questions, because the field only nudges results.
Verifying the controls worked
After any change, inspect the response. Count the search-call items, then check that each cited URL belongs to an allowed domain. A short check in code makes that routine:
from urllib.parse import urlparse
ALLOWED = {"docs.python.org", "peps.python.org"}
def host_allowed(url: str) -> bool:
host = urlparse(url).hostname or ""
return any(host == d or host.endswith("." + d) for d in ALLOWED)
for item in response.output:
if item.type == "message":
for part in item.content:
for ann in getattr(part, "annotations", []) or []:
if ann.type == "url_citation":
print(host_allowed(ann.url), ann.url)Log the result in your tests. A False means your filter configuration or your assumption about it is wrong.
Exercise
A news summarizer must only use two named outlets, answer within a budget of at most two searches per request, and favor results for users in Canada. Using the Anthropic request shape from this chapter, which three tool fields do you set, and why would you choose an allow list over a block list?
Show answer
Set allowed_domains to the two outlets' bare domains, max_uses to 2, and user_location with type "approximate" and country "CA". An allow list is safer because it names the only permitted sources. A block list only removes sites you listed, so every other site on the web would remain eligible.
Exercise
You combine web search and file search in one OpenAI request and set max_tool_calls=2. The model needs one file search and two web searches. What happens, and how would you notice?
Show answer
The cap applies across all built-in tools, so only two calls total can be processed. The model must answer with the results it has, which may be incomplete. You would notice by counting the web_search_call and file_search_call items in response.output and seeing that they total two, then comparing the answer against what the question needed.