Tools
Web Search
The Web Search tool enables Grok to search the web in real-time and browse web pages to find information. This powerful tool allows the model to search the internet, access web pages, and extract relevant information to answer queries with up-to-date content.
SDK Support
| SDK/API | Tool Name |
|---|---|
| xAI SDK | web_search |
| OpenAI Responses API | web_search |
| Vercel AI SDK | xai.tools.webSearch() |
This tool is also supported in all Responses API compatible SDKs.
Basic Usage
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
const { text, sources } = await generateText({
model: xai.responses('grok-4.7'),
prompt: 'What is SpaceXSI?',
tools: {
web_search: xai.tools.webSearch(),
},
});
console.log(text);
console.log('Citations:', sources);Web Search Parameters
| Parameter | Description |
|---|---|
allowed_domains | Only search within specific domains (max 5) |
excluded_domains | Exclude specific domains from search (max 5) |
enable_image_understanding | Enable analysis of images found during browsing |
enable_image_search | Enable image search results that can be embedded in responses |
user_location | The user's approximate location, used to localize search results. See Set the User's Location |
Only Search in Specific Domains
Use allowed_domains to make the web search only perform the search and web browsing on web pages that fall within the specified domains.
allowed_domains cannot be set together with excluded_domains in the same request.
const { text } = await generateText({
model: xai.responses('grok-4.7'),
prompt: 'What is SpaceXSI?',
tools: {
web_search: xai.tools.webSearch({
allowedDomains: ['grokipedia.com'],
}),
},
});Exclude Specific Domains
Use excluded_domains to prevent the model from including the specified domains in any web search tool invocations.
response = client.responses.create(
model="grok-4.7",
input=[{"role": "user", "content": "What is SpaceXSI?"}],
tools=[
{
"type": "web_search",
"filters": {"excluded_domains": ["grokipedia.com"]},
},
],
)Enable Image Understanding
Setting enable_image_understanding to true equips the agent with access to the view_image tool, allowing it to analyze images encountered during the search process.
When enabled, you will see SERVER_SIDE_TOOL_VIEW_IMAGE in response.server_side_tool_usage along with the number of times it was called.
Enabling this parameter for Web Search will also enable the image understanding for X Search tool if it's also included in the request.
const { text } = await generateText({
model: xai.responses('grok-4.7'),
prompt: "What is included in the image in SpaceXSI's official website?",
tools: {
web_search: xai.tools.webSearch({
enableImageUnderstanding: true,
}),
},
});Enable Image Search
Setting enable_image_search to true lets Grok search for relevant images and include them in the response as Markdown image embeds such as .
After Grok searches for images, the returned images are included in the model context used to write the response. This is separate from enable_image_understanding, which lets Grok inspect images it finds while browsing regular web pages.
const { text } = await generateText({
model: xai.responses('grok-4.7'),
prompt: 'Show me images of Starship on the launch pad.',
tools: {
web_search: xai.tools.webSearch({
enableImageSearch: true,
}),
},
});
console.log(text);
A response can include Markdown image embeds directly in the output text:
Output

Here are several high-quality images of SpaceX's Starship on the launch pad at Starbase in Boca Chica, Texas.
In the xAI SDK, successful image search executions appear in response.server_side_tool_usage as SERVER_SIDE_TOOL_IMAGE_SEARCH.
Set the User's Location
Use user_location to tell the search where the user is, so queries like "weather today" or "restaurants near me" return local results. The Responses API takes an object with "type": "approximate"; the xAI SDK takes the same fields as separate keyword arguments.
| Field | xAI SDK parameter | Description |
|---|---|---|
country | user_location_country | Two-letter ISO 3166-1 alpha-2 country code, such as US |
city | user_location_city | City name |
region | user_location_region | Region or state name |
timezone | user_location_timezone | IANA time zone, such as America/Chicago |
All fields are optional. The Vercel AI SDK (@ai-sdk/xai 5.0.49) has no option for this field.
response = client.responses.create(
model="grok-4.7",
input=[{"role": "user", "content": "What is the weather like today?"}],
tools=[
{
"type": "web_search",
"user_location": {
"type": "approximate",
"country": "US",
"city": "Austin",
"region": "Texas",
"timezone": "America/Chicago",
},
},
],
)
print(response.output_text)
Citations
For details on how to retrieve and use citations from search results, see the Citations page.
Last updated: November 24, 2026