Exclude handles from X Search
Exclude handles from X Search
Pass excluded_x_handles (max 20) on the x_search tool so Grok skips posts from those accounts while searching the rest of X. Base setup: Search X posts with the Grok API. To lock search to a short allowlist instead, see Restrict X Search to specific handles.
xAI SDK
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.6",
tools=[
x_search(excluded_x_handles=["spam_bot", "noisy_aggregator"]),
],
)
chat.append(user("What are people saying about Grok 4.6 on X?"))
response = chat.sample()
print(response.content)
OpenAI Responses
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 are people saying about Grok 4.6 on X?"
}
],
"tools": [
{
"type": "x_search",
"excluded_x_handles": ["spam_bot", "noisy_aggregator"]
}
]
}'
Vercel AI SDK
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
const { text } = await generateText({
model: xai.responses('grok-4.6'),
prompt: 'What are people saying about Grok 4.6 on X?',
tools: {
x_search: xai.tools.xSearch({
excludedXHandles: ['spam_bot', 'noisy_aggregator'],
}),
},
});
console.log(text);
Pitfalls
excluded_x_handlesandallowed_x_handlescannot share the same request.- Pass handles without
@, up to 20. - Pair with
from_date/to_datewhen you also need a window: Set an X Search date range. - Console API credits are separate from SuperGrok's weekly pool on grok.com.