API / combine-web-search-and-x-search

API

Combine web search and X search on the Grok API

Combine web search and X search on the Grok API

Put both tools in the same tools array. The model pulls traditional web coverage and live X posts in one agentic run — the docs call this out for news plus social. Official guide: Advanced Usage → Tool Combinations.

Goal Tools
News plus social Web Search + X Search
Research and analyze Web Search + Code Execution
Multi-source insights Web Search + X Search + Code Execution

Advanced multi-tool patterns need the xAI SDK or OpenAI-compatible Responses API. The Vercel AI SDK does not support these patterns yet.

For web + code only, see Combine web search and code execution. For the full trio, see Combine web search, X search, and code execution.

xAI SDK

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search, x_search

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.6",
    tools=[
        web_search(),
        x_search(),
    ],
    include=["verbose_streaming"],
)

chat.append(user("What is the latest update from xAI?"))

is_thinking = True
for response, chunk in chat.stream():
    for tool_call in chunk.tool_calls:
        print(
            f"Calling tool: {tool_call.function.name} "
            f"with arguments: {tool_call.function.arguments}"
        )
    if response.usage.reasoning_tokens and is_thinking:
        print(
            f"\rThinking... ({response.usage.reasoning_tokens} tokens)",
            end="",
            flush=True,
        )
    if chunk.content and is_thinking:
        print("\n\nFinal Response:")
        is_thinking = False
    if chunk.content and not is_thinking:
        print(chunk.content, end="", flush=True)

print("\n\nCitations:", response.citations)
print("Usage:", response.server_side_tool_usage)

Optional per-tool knobs still apply when both are present — for example web_search(allowed_domains=[...]) or x_search(from_date=..., to_date=...). See Web Search and X Search.

Responses API

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 is the latest update from xAI?"
    }
  ],
  "tools": [
    {"type": "web_search"},
    {"type": "x_search"}
  ]
}'

xAI runs both server-side. You do not execute them locally. Check server_side_tool_usage and citations on the response.

Pitfalls

  • Use a reasoning model that supports agentic tools (docs examples use grok-4.6).
  • X Search pricing changes on September 21, 2026 (posts/profiles fetched) — see Prepare for X Search pricing change.
  • Each server-side tool call bills under its own usage category.