Restrict web search to allowed domains
Restrict web search to allowed domains
Pass allowed_domains (max 5) on the web_search tool so Grok only searches and browses those hosts. Base setup: Add web search to a Grok API request. For image embeds alongside search, see Enable image search in web search.
xAI SDK
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.6",
tools=[
web_search(allowed_domains=["grokipedia.com"]),
],
)
chat.append(user("What does Grokipedia say about large language models?"))
response = chat.sample()
print(response.content)
OpenAI Responses
{
"model": "grok-4.6",
"input": [
{
"role": "user",
"content": "What does Grokipedia say about large language models?"
}
],
"tools": [
{
"type": "web_search",
"filters": {
"allowed_domains": ["grokipedia.com"]
}
}
]
}
curl https://api.x.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.6",
"input": [
{
"role": "user",
"content": "What does Grokipedia say about large language models?"
}
],
"tools": [
{
"type": "web_search",
"filters": {
"allowed_domains": ["grokipedia.com"]
}
}
]
}'
Vercel AI SDK
import { xai } from '@ai-sdk/xai';
const tools = {
web_search: xai.tools.webSearch({
allowedDomains: ['grokipedia.com'],
}),
};
Sibling filter: excluded domains
excluded_domains (also max 5) skips listed hosts while searching the rest of the web. allowed_domains and excluded_domains are mutually exclusive. Do not set both on the same web_search tool.
Pitfalls
- Max 5 entries in
allowed_domains. - Setting both
allowed_domainsandexcluded_domainson one tool fails. - Console API credits are separate from SuperGrok's weekly pool on grok.com.