Server Tools and Function Calling
On this page
Course updated November 2, 2026 based on xAI documentation: the batch discount is 20% on eligible text models (not 50%), examples follow the REST API and the official
xai-sdk(client.batch.*; xAI’s Batch API does not use OpenAI’s format, the OpenAI SDK does not apply), and the Responses API takes precedence over Chat Completions, a legacy endpoint at xAI. Sources: Batch API · REST Reference · Batch Pricing.
Enriching Batches with Tools
Batch requests aren’t limited to raw text. Server tools and function calling work in asynchronous mode too, with a key distinction: the former run during processing by xAI, while the latter returns control to you.
Server-Side Tools
Server tools run during batch processing, exactly like in real time: Grok calls the tool, receives the result, integrates it into its reasoning, and the batch returns the final answer. They work with the Responses API.
Web Search
{"custom_id": "news-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "What are the latest developments in quantum computing?"}], "tools": [{"type": "web_search"}]}}
In batch mode, web search serves data enrichment: verifying information, collecting recent facts, comparing profiles with online sources.
X Search
The x_search tool queries posts on X, useful for tracking sentiment on a topic or brand:
{"custom_id": "x-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "What is the dominant sentiment on X regarding this product launch?"}], "tools": [{"type": "x_search"}]}}
Code Execution
Grok can write and execute Python code in an isolated environment. In the Responses API, the tool is called code_interpreter (the xAI SDK names it code_execution):
{"custom_id": "calc-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "Calculate the 3-point moving average for this data: [12, 15, 18, 14, 22, 19, 25, 28, 30, 27]"}], "tools": [{"type": "code_interpreter"}]}}
Code execution is relevant for exact calculations, statistics, and data transformations that the model would handle less reliably from memory.
Remote MCP Servers
The mcp tool connects Grok to a remote MCP server, described in the request itself: no prior account configuration is required. server_url and server_label are required; allowed_tools restricts exposed tools, and authorization passes a token to the server. Only Streamable HTTP and SSE transports are accepted.
{"custom_id": "mcp-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "What does the xai-sdk-python repository do?"}], "tools": [{"type": "mcp", "server_label": "deepwiki", "server_url": "https://mcp.deepwiki.com/mcp"}]}}
Without allowed_tools, all tool definitions from the server are injected into the model’s context and billed as input tokens on every batch request: on 50,000 requests, restricting the list pays off quickly.
Client-Side Function Calling
Function calling is a client-side tool: Grok produces a structured function call, but your code executes it. In batch, the model returns these calls in its results, for offline processing.
Function Definitions
In the Responses API, declare the function flat, with name, description, and parameters at the top level. tool_choice: "required" forces the model to call at least one tool, which suits extraction:
{"custom_id": "extract-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "Extract the contact: Jean Dupont, 01 23 45 67 89, [email protected]"}], "tools": [{"type": "function", "name": "save_contact", "description": "Saves a contact extracted from text", "parameters": {"type": "object", "properties": {"name": {"type": "string"}, "phone": {"type": "string"}, "email": {"type": "string"}}, "required": ["name"]}}], "tool_choice": "required"}}
Processing Results
With the SDK, read function calls in tool_calls of each successful result:
import json
for r in page.succeeded:
for call in r.response.tool_calls:
if call.function.name == "save_contact":
contact = json.loads(call.function.arguments)
save_to_database(r.batch_request_id, contact)
Batch function calling is ideal for structured extraction: you define the data schema, Grok reads the documents and produces the calls, then you process them in bulk.
Combining Tools
You can combine multiple server tools in a single request:
{"custom_id": "multi-1", "method": "POST", "url": "/v1/responses", "body": {"model": "grok-4.3", "input": [{"role": "user", "content": "Find the Bitcoin closing price for the last seven days and calculate its change"}], "tools": [{"type": "web_search"}, {"type": "code_interpreter"}]}}
Grok uses the tools in the order it deems relevant: here, web search to get the data, then code execution for the calculation.
Batch Tool Limitations
- Server tools extend the processing time of each request, and thus the batch
- Requests with search depend on what sources publish at processing time, not submission time
- Function calling makes only one round trip per request: to continue, submit a new batch request containing the conversation and your function results
- Unfiltered MCP tool definitions bloat every request
Key Takeaways
- Server tools (web, X, code, MCP) run by xAI during processing, via the Responses API
- Describe an MCP server in the request (
server_url,server_label), with no prior configuration - Function calling returns structured calls that you execute client-side
- A multi-turn exchange requires a new batch request per turn
- Restrict exposed tools to avoid paying for their definitions on every request