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:
| Type | Description | Examples |
|---|---|---|
| Built-in Tools | Server-side tools managed by xAI that execute automatically | Web Search, X Search, Code Interpreter, Collections Search |
| Function Calling | Custom functions you define that the model can invoke | Database 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:
- Analyzes the query and determines what information or actions are needed
- Decides what to do next: Make a tool call, or provide a final answer
- Executes the tool (for built-in tools) or returns a tool call request (for function calling)
- Processes results and continues until sufficient information is gathered
- 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
- Function Calling - Define custom tools the model can call
- Web Search - Search the web and browse pages
- X Search - Search X posts, users, and threads
- Code Execution - Execute Python code in a sandbox
- Collections Search - Query your uploaded documents
- Citations - Access source URLs and inline citations