SPB Git

Repository Public

overview.md 5.4 KB 175 lines markdown

1<!-- source: https://docs.x.ai/developers/tools/overview (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Tools34# Overview56The 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.78## Types of Tools910The xAI API offers two categories of tools:1112| Type | Description | Examples |13|------|-------------|----------|14| **Built-in Tools** | Server-side tools managed by xAI that execute automatically | Web Search, X Search, Code Interpreter, Image Generation, Collections Search |15| **Function Calling** | Custom functions you define that the model can invoke | Database queries, API calls, custom business logic |1617Built-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.1819## Pricing2021Tool 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.2223For more details on Tools pricing, please check out [the pricing page](/developers/pricing#tools-pricing).2425## How It Works2627When you provide tools to a request, the xAI API can use them to gather information or perform actions:28291. **Analyzes the query** and determines what information or actions are needed302. **Decides what to do next**: Make a tool call, or provide a final answer313. **Executes the tool** (for built-in tools) or returns a tool call request (for function calling)324. **Processes results** and continues until sufficient information is gathered335. **Returns the final response** with citations where applicable3435## Quick Start3637```bash customLanguage="bash"38curl https://api.x.ai/v1/responses \39  -H "Content-Type: application/json" \40  -H "Authorization: Bearer $XAI_API_KEY" \41  -d '{42  "model": "grok-4.6",43  "stream": true,44  "input": [45    {46      "role": "user",47      "content": "What are the latest updates from xAI?"48    }49  ],50  "tools": [51    { "type": "web_search" },52    { "type": "x_search" },53    { "type": "code_interpreter" }54  ]55}'56```5758```pythonXAI59import os6061from xai_sdk import Client62from xai_sdk.chat import user63from xai_sdk.tools import web_search, x_search, code_execution6465client = Client(api_key=os.getenv("XAI_API_KEY"))66chat = client.chat.create(67    model="grok-4.6",68    tools=[69        web_search(),70        x_search(),71        code_execution(),72    ],73)7475chat.append(user("What are the latest updates from xAI?"))7677for response, chunk in chat.stream():78    if chunk.content:79        print(chunk.content, end="", flush=True)8081print("\nCitations:", response.citations)82```8384```pythonOpenAISDK85import os86from openai import OpenAI8788client = OpenAI(89    api_key=os.getenv("XAI_API_KEY"),90    base_url="https://api.x.ai/v1",91)9293response = client.responses.create(94    model="grok-4.6",95    input=[96        {"role": "user", "content": "What are the latest updates from xAI?"}97    ],98    tools=[99        {"type": "web_search"},100        {"type": "x_search"},101        {"type": "code_interpreter"},102    ],103    stream=True,104)105106for event in response:107    if event.type == "response.output_text.delta":108        print(event.delta, end="", flush=True)109```110111```javascriptAISDK112import { xai } from '@ai-sdk/xai';113import { streamText } from 'ai';114115const { fullStream } = streamText({116  model: xai.responses('grok-4.6'),117  prompt: 'What are the latest updates from xAI?',118  tools: {119    web_search: xai.tools.webSearch(),120    x_search: xai.tools.xSearch(),121    code_execution: xai.tools.codeExecution(),122  },123});124125for await (const part of fullStream) {126  if (part.type === 'text-delta') {127    process.stdout.write(part.text);128  } else if (part.type === 'source' && part.sourceType === 'url') {129    console.log(`Citation: ${part.url}`);130  }131}132```133134```javascriptOpenAISDK135import OpenAI from "openai";136137const client = new OpenAI({138  apiKey: process.env.XAI_API_KEY,139  baseURL: "https://api.x.ai/v1",140});141142const stream = await client.responses.create({143  model: "grok-4.6",144  input: [145    { role: "user", content: "What are the latest updates from xAI?" }146  ],147  tools: [148    { type: "web_search" },149    { type: "x_search" },150    { type: "code_interpreter" },151  ],152  stream: true,153});154155for await (const event of stream) {156  if (event.type === "response.output_text.delta") {157    process.stdout.write(event.delta);158  }159}160```161162## Citations163164The API automatically returns source URLs for information gathered via tools. See [Citations](/developers/tools/citations) for details on accessing and using citation data.165166## Next Steps167168* **[Function Calling](/developers/tools/function-calling)** - Define custom tools the model can call169* **[Web Search](/developers/tools/web-search)** - Search the web and browse pages170* **[X Search](/developers/tools/x-search)** - Search X posts, users, and threads171* **[Code Execution](/developers/tools/code-execution)** - Execute Python code in a sandbox172* **[Image Generation](/developers/tools/image-generation)** - Generate and edit images in a conversation173* **[Collections Search](/developers/tools/collections-search)** - Query your uploaded documents174* **[Citations](/developers/tools/citations)** - Access source URLs and inline citations175
Files main
Menu

Theme