Sub topic 7
Controlling Web Search: Domains, Location, and Usage Limits
This chapter assumes you can already enable the web search tool and read its output items, as covered earlier. Here you will narrow what the tool may search, tell it where the user is, and cap how much it can do.
Why add controls
By default, the model decides what to search and which sites to read. That can cause three problems:
- The answer cites low-quality or irrelevant sites.
- Results ignore the user's region, for example when they ask about "local" laws or stores.
- One request runs many searches, which raises cost and latency.
Each control below addresses one of these.
Restricting domains
A domain filter limits which sites the tool may use. OpenAI offers an allow list. Anthropic offers an allow list and a block list, but you can use only one of them in a request.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "web_search",
"filters": {"allowed_domains": ["python.org", "docs.python.org"]},
}
],
include=["web_search_call.action.sources"],
input="What changed in the latest stable Python release?",
)
print(response.output_text)The include parameter asks the API to return the full list of sources the search consulted, not only the ones cited in the text. Use it when you audit whether the filter worked.
Setting an approximate location
A location hint shifts results toward a region. It is approximate, so give a country, region, and city, not an address.
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,
"blocked_domains": ["example-content-farm.com"],
"user_location": {
"type": "approximate",
"city": "London",
"region": "England",
"country": "GB",
"timezone": "Europe/London",
},
}
],
messages=[
{"role": "user", "content": "Which bank holidays are coming up?"}
],
)
for block in message.content:
if block.type == "text":
print(block.text)Here max_uses caps the search count at 2. If the model needs more, the search result block carries an error such as max_uses_exceeded, and the model must answer with what it has.
Limiting effort
Usage limits differ by provider:
| Control | OpenAI | Anthropic |
|---|---|---|
| Allow list | `filters.allowed_domains` | `allowed_domains` |
| Block list | Not available | `blocked_domains` |
| Location | `user_location` | `user_location` |
| Search count cap | Not a direct parameter | `max_uses` |
| Result volume | `search_context_size`: `low`, `medium`, `high` | Not available |
On OpenAI, a lower search_context_size retrieves less page text. This is cheaper and faster but can give thinner answers.
Common errors
- Sending both
allowed_domainsandblocked_domainsto Anthropic. The API rejects the request. - Including
https://or a path in a domain entry. Use the bare domain, such aspython.org. - Treating a filter as a security boundary. It narrows search, but pages from allowed sites can still contain hostile text. Security is covered in a later chapter.
- Setting
max_usesto 1 for questions that need comparison. The model cannot cross-check sources.
Exercise
A support bot must answer only from your company's documentation site, docs.acme.example, and must never run more than two searches per request. Which controls do you set on Anthropic, and why can't you also add a block list?
Show answer
Set allowed_domains to ["docs.acme.example"] and max_uses to 2 on the web_search_20250305 tool. Anthropic accepts either allowed_domains or blocked_domains in one request, not both, and an allow list already excludes every other site.