Tools

Overview

The xAI API supports tool calling, enabling Grok to perform actions beyond generating text—like searching the web, executing code, querying your data, or calling your own custom functions. Tools extend what's possible with the API and let you build powerful, interactive applications.

Types of Tools

The xAI API offers two categories of tools:

TypeDescriptionExamples
Built-in ToolsServer-side tools managed by xAI that execute automaticallyWeb Search, X Search, Code Interpreter, Collections Search
Function CallingCustom functions you define that the model can invokeDatabase queries, API calls, custom business logic

Built-in tools run on xAI's servers—you provide the tool configuration, and the API handles execution and returns results. Function calling lets you define your own tools that the model can request, giving you full control over what happens when they're invoked.

Pricing

Tool requests are priced based on two components: token usage and tool invocations. Since the model may call multiple tools to answer a query, costs scale with complexity.

For more details on Tools pricing, please check out the pricing page.


How It Works

When you provide tools to a request, the xAI API can use them to gather information or perform actions:

  1. Analyzes the query and determines what information or actions are needed
  2. Decides what to do next: Make a tool call, or provide a final answer
  3. Executes the tool (for built-in tools) or returns a tool call request (for function calling)
  4. Processes results and continues until sufficient information is gathered
  5. Returns the final response with citations where applicable

Quick Start

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-1-fast-reasoning",
    tools=[
        web_search(),
        x_search(),
        code_execution(),
    ],
)

chat.append(user("What are the latest updates from xAI?"))

for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)

Citations

The API automatically returns source URLs for information gathered via tools. See Citations for details on accessing and using citation data.


Next Steps