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

API

Combine web search, X search, and code execution on the Grok API

Combine web search, X search, and code execution on the Grok API

Activate all three in one tools array when you need web + X coverage and then arithmetic, charts, or other Python analysis on what came back. Official guide: Advanced Usage → Tool Combinations → “Extract insights from multiple sources.”

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

Pair-only how-tos: Combine web search and X search, Combine web search and 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.

xAI SDK

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

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

chat.append(
    user(
        "Gather the latest public numbers and X discussion on US EV sales, "
        "then compute month-over-month percent change for the top three makers."
    )
)

for response, chunk in chat.stream():
    for tool_call in chunk.tool_calls:
        print(tool_call.function.name, tool_call.function.arguments)
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)
print("Tool usage:", response.server_side_tool_usage)

Suggested setups from the docs:

from xai_sdk.tools import web_search, x_search, code_execution

research_setup = [web_search(), code_execution()]
news_setup = [web_search(), x_search()]
comprehensive_setup = [web_search(), x_search(), code_execution()]

Responses API

In Responses payloads, code execution is "type": "code_interpreter"; web and X stay "web_search" and "x_search".

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": "Gather the latest public numbers and X discussion on US EV sales, then compute month-over-month percent change for the top three makers."
    }
  ],
  "tools": [
    {"type": "web_search"},
    {"type": "x_search"},
    {"type": "code_interpreter"}
  ]
}'

xAI runs all three server-side. For custom functions mixed with these tools, see Mix client and server-side tools.

Pitfalls

  • Naming differs by surface: xAI SDK code_execution() vs Responses "code_interpreter".
  • Use a reasoning model that supports agentic tools (docs examples use grok-4.6).
  • Three tool families each add usage — inspect server_side_tool_usage before you ship production quotas.
  • X Search pricing changes on September 21, 2026 — see Prepare for X Search pricing change.